-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
1117 lines (1057 loc) · 32.1 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// к работе:
// [x] добавить комбинированные решения
// [x] добавить методы реального времени (при редактировании прямо тут же применять комбинированные решения)
// [+] добавить ручное редактирование stowage
// [+] убрать процедуру копирования copy3dArray, применив указатель sip(быстрый поиск в глубину)
// [+] запоминаем все действия в подзадаче, и откатываем их назад при возвращении к исходной задаче
// [ ] undo history + отход назад если противоречие
// [ ] избавиться от границ и попытаться от прочих догм
// [ ] обобщить до произвольного графа
// [?] добавить алгоритм решающий булевы и рекурсивные формулы например понятие суммы
// [+] добавить понятие суммы
// [ ] добавить булевы формулы
// [ ] добавить рекурсивные формулы
// [ ] максимально плотное объединение(union) недезинтегрированных решений (т.е. все остальные кроме дубликатов)
// (позволяет убрать симметричные решения, чтобы в множестве решений не было дубликатов и union был более информативным для человека)
// решение: unionA отображается в минимальный unionB, состоящий из решений не приводимых друг к другу симметриями, которые переводят в себя unionA
// изучить монотонные фунции и найти остальные миры кроме линейных
// изучить построение электронных схем
// обдумать модель "игру" социального взаимодействия нейронов
let logic = [];
let globalUnionArray = []
let stowage = [];
let defaultStowage = []
let errors = [];
let solves = [];
let BG_COLOR = [125, 125, 125] // [100, 100, 100]
let GRID_COLOR = [ 60, 60, 60] // [0, 0, 0]
let POINTS_COLOR = [255, 0, 255]
let ZERO_COLOR = [ 0, 0, 0] // [255, 255, 255]
let ONE_COLOR = [225, 225, 0] // [0, 125, 125]
let ERROR_COLOR = [255, 0, 0]
let TEXT_COLOR = [ 0, 0, 0] // [0, 0, 0]
let STABLE_COLOR = [ 0, 255, 0] // [0, 0, 0]
let logicSize = {w: 12, h: 12, z: 2};
let rectSize = 30;
let offset = {x: 50, y: 10};
offset.dy = (logicSize.h + 1) * rectSize;
let textOffset = {x: 10, y: 21};
// counts {
let n = 0, maxPreSolveIterations = 0,
maxSetOfChangesLength = 0,
solutionsCount = 0;
// counts }
let listOfSymbols = [' ', 'o', '*', 'E']
let lengthOfLogicSet = 2;
let defaultPavement = 0; // lengthOfLogicSet <=> *
let maxPrint = 10
let randomFilterDefaultValue = 1
let randomDampingCoefficient = 1
let maxStowage = 0
let maxBranches = 150000
let maxSolutions = Infinity
let debugStep = 50000
let preSolveEnable = true
let errorUpgrade = true
let unionAutoEnable = false
let autoPlace = false
let functionalSymmetryEnable = false
let zeroBorders = true
let zLoopBack = true
function put(i, j) {
for(let kk = 0; kk < logicSize.z; kk++) {
if(stowage.length >= maxStowage + defaultStowage.length)
return
else
putToStowage(i, j, kk)
}
}
function makeDiagonalStowage() {
let wh = logicSize.w + logicSize.h
for(let i = 0; i < wh; i++) {
for(let j = 0; j <= i; j++) {
let x = i - j
let y = j
if(x < logicSize.w && y < logicSize.h) {
put(x, y)
}
}
}
}
function makeLinearStowage() {
for(let i = 0; i < logicSize.w; i++) {
for(let j = 0; j < logicSize.h; j++) {
for(let k = 0; k < logicSize.z; k++) {
if(stowage.length >= maxStowage) {
break
} else {
putToStowage(i, j, k)
}
}
}
}
}
const tests = {
sumTest: {
enabled: false,
target: 0,
dt: 0,
},
kernelSumTest: {
enabled: false,
target: 1,
dt: Infinity,
},
stableTest: {
enabled: true,
listOfPoints: [],
},
callbackList: [sumTestCallback, kernelTestCallback, stableTestCallback],
}
tests.stableTest.listOfPoints.add = function(x, y) {
if(x === undefined || y === undefined) throw "input must be two arguments (x, y)"
let list = tests.stableTest.listOfPoints
for(let i = list.length - 1; i >= 0; i--) {
if(list[i].x === x && list[i].y === y) {
list.splice(i, 1)
return false
}
}
// otherwise
list.push({x, y})
return true
}
tests.stableTest.listOfPoints.remove = function(x, y) {
if(x === undefined || y === undefined) throw "input must be two arguments (x, y)"
let list = tests.stableTest.listOfPoints
for(let i = list.length - 1; i >= 0; i--) {
if(list[i].x === x && list[i].y === y) {
list.splice(i, 1)
}
}
return tests.stableTest.listOfPoints
}
tests.stableTest.listOfPoints.clear = function() {
let list = tests.stableTest.listOfPoints
list.splice(0, list.length)
return tests.stableTest.listOfPoints
}
function stableTestCallback(arr) {
// use .bind to reduce args length to one when test launched
// or make global testOptions object
const testOptions = tests.stableTest
//-------------------------------------------------------------------------------------
if(testOptions.enabled === false) {
return true
}
if(logicSize.z < 2) {
// throw "logicSize.z < 2 => stableTest should not be used"
return true
}
//-------------------------------------------------------------------------------------
let listOfPoints = testOptions.listOfPoints
for(let i = 0; i < listOfPoints.length; i++) {
let point = listOfPoints[i]
let firstCell = arr[point.x][point.y][0]
if(firstCell === 2) {
continue
}
// firstCell < 2
for(let k = 1; k < logicSize.z; k++) {
let otherCell = arr[point.x][point.y][k]
if(otherCell === 2) {
continue
}
// otherCell < 2 AND firstCell < 2
if(otherCell !== firstCell) {
// not stable column of cells
return false
}
}
}
return true
}
function kernelTestCallback(arr) {
const testOptions = tests.kernelSumTest
//-------------------------------------------------------------------------------------
if(testOptions.enabled === false) {
return true
}
if(logicSize.z < 2) {
// throw "logicSize.z < 2 => kernelTest should not be used"
return true
}
const targetsum = {min: testOptions.target, max: testOptions.target + testOptions.dt}
//-------------------------------------------------------------------------------------
let total = {min: 0, max: 0}
for(let j = 0; j < logicSize.h; j++) {
for(let i = 0; i < logicSize.w; i++) {
let firstCell = arr[i][j][0]
let secondCell = arr[i][j][1]
if(firstCell === 2 || secondCell === 2) {
total.max += 1
} else if(firstCell !== secondCell) {
total.min += 1
total.max += 1
}
}
}
// [targetsum.min targetsum.max] < [total.min total.max]
// [total.min total.max] < [targetsum.min targetsum.max]
if( total.min > targetsum.max || total.max < targetsum.min ) {
return false
}
return true
}
function sumTestCallback(arr) {
const testOptions = tests.sumTest
//-------------------------------------------------------------------------------------
if(testOptions.enabled === false) {
return true
}
const targetsum = {min: testOptions.target, max: testOptions.target + testOptions.dt}
//-------------------------------------------------------------------------------------
let total = {min: 0, max: 0}
for(let j = 0; j < logicSize.h; j++) {
for(let i = 0; i < logicSize.w; i++) {
for(let k = 0; k < logicSize.z; k++) {
let cell = arr[i][j][k]
if(cell === 2) {
total.max += 1
} else {
total.min += cell
total.max += cell
}
}
}
}
if(!(total.min <= targetsum.max && total.max >= targetsum.min)) {
return false
}
return true
}
function globalSolve() {
// initialize union array with empty sets "E"
globalUnionArray = init3DArray(logicSize.w, logicSize.h, logicSize.z, 3)
let arr = copy3dArray(logic);
stowage = []
for(let u = 0; u < defaultStowage.length; u++) {
let point = defaultStowage[u]
stowage.push({
i: point[0],
j: point[1],
k: point[2],
})
}
makeDiagonalStowage()
// global variables
solves = []
n = 0; maxPreSolveIterations = 0;
maxSetOfChangesLength = 0;
solutionsCount = 0
randomFilterCurrentValue = randomFilterDefaultValue
// global variables
console.log("solutions: ")
let sip = 0, changedPos = null
console.time('solve time')
solve(arr, sip, changedPos)
console.timeEnd('solve time')
const foundedAllSolutions = isFoundedAllSolutions()
if(unionAutoEnable) {
applyUnion()
} else {
console.log('union: ')
print3dSolve(globalUnionArray, undefined, true)
}
// no recursion
if(foundedAllSolutions)
console.log(`all ${solutionsCount} solutions found.`)
else
console.log(`NOT ALL ${solutionsCount} solutions found.`)
console.log(`maxSolutions = ${maxSolutions}`)
console.log(`branches = ${n}`)
if(preSolveEnable) {
console.log(`maxPreSolveIterations = ${maxPreSolveIterations}`)
console.log(`maxSetOfChangesLength = ${maxSetOfChangesLength}`)
}
}
function isFoundedAllSolutions() {
return n < maxBranches && solutionsCount < maxSolutions
}
function numberOfCellsToString(numberOfCells) {
if(numberOfCells === undefined) {
return "";
} else if(numberOfCells.min === numberOfCells.max) {
return `[${numberOfCells.min}]`;
} else {
return `[${numberOfCells.min}..${numberOfCells.max}]`;
}
}
function getNumberOfCells(arr) {
let min = 0;
let max = 0;
for(let i = 0; i < arr.length; i++)
for(let j = 0; j < arr[0].length; j++)
for(let k = 0; k < arr[0][0].length; k++) {
let cell = arr[i][j][k];
if(cell < 2) {
min += cell;
max += cell;
} else if (cell === 2) {
max += 1;
} else {
return undefined;
}
}
return {min, max};
}
//**********************************************************************************
function unionTo(solution, unionArray) {
for(let i = 0; i < logicSize.w; i++)
for(let j = 0; j < logicSize.h; j++)
for(let k = 0; k < logicSize.z; k++) {
let cell = solution[i][j][k]
let unionCell = unionArray[i][j][k]
unionArray[i][j][k] = logicUnion(unionCell, cell)
}
}
function applyUnion() {
const foundedAllSolutions = isFoundedAllSolutions()
// print3dSolve(unionArray)
if(solutionsCount > 0 && foundedAllSolutions) {
logic = copy3dArray(globalUnionArray)
return true
}
return false
}
function union(solutions) {
let unionArray = init3DArray(logicSize.w, logicSize.h, logicSize.z, 3);
for(let index = 0; index < solutions.length; index++) {
let solution = solutions[index]
for(let i = 0; i < logicSize.w; i++)
for(let j = 0; j < logicSize.h; j++)
for(let k = 0; k < logicSize.z; k++) {
let cell = solution[i][j][k]
let unionCell = unionArray[i][j][k]
unionArray[i][j][k] = logicUnion(unionCell, cell)
}
}
return unionArray
}
function setZeroBorders() {
for(let k = 0; k < logicSize.z; k++) {
for(let i = 0; i < logicSize.w; i++) {
logic[i][0][k] = 0;
logic[i][logicSize.h - 1][k] = 0;
}
for(let i = 0; i < logicSize.h; i++) {
logic[0][i][k] = 0;
logic[logicSize.w - 1][i][k] = 0;
}
}
}
function shiftLogicInZ(dz) {
if(dz > 0) {
for(let i = 0; i < logicSize.w; i++) {
for(let j = 0; j < logicSize.h; j++) {
for(let k = logicSize.z - 1 - dz; k >= 0; k--) {
logic[i][j][k + dz] = logic[i][j][k]
}
}
}
} else if(dz < 0) {
for(let i = 0; i < logicSize.w; i++) {
for(let j = 0; j < logicSize.h; j++) {
for(let k = 0; k <= logicSize.z - 1 - (-dz); k++) {
logic[i][j][k] = logic[i][j][k + (-dz)]
}
}
}
}
}
function changeLogicSizeBy(dw, dh, dz, _zeroBorders = false) {
resizeLogicTo(logicSize.w + dw, logicSize.h + dh, logicSize.z + dz, _zeroBorders)
}
function resizeLogicTo(w, h, z, _zeroBorders = false) {
let minSize = {
w: Math.min(logicSize.w, w),
h: Math.min(logicSize.h, h),
z: Math.min(logicSize.z, z),
}
let temp = copy3dArray(logic)
logicSize = {w, h, z}
initialization(_zeroBorders)
// fill current logic array with values
for(let i = 0; i < minSize.w; i++)
for(let j = 0; j < minSize.h; j++)
for(let k = 0; k < minSize.z; k++)
logic[i][j][k] = temp[i][j][k]
draw() // what if call draw() 1000000 times? you can replace it by setting draw flag (needRedrawInThisTick) and add draw function in timer
}
function initialization(_zeroBorders = false) {
offset.dy = (logicSize.h + 1) * rectSize
let size = {
w: (logicSize.w+1) * rectSize + offset.x - 10,
h: logicSize.z * offset.dy + offset.y - 10
}
resizeCanvas(size.w, size.h)
logic = init3DArray(logicSize.w, logicSize.h, logicSize.z, 2);
if(_zeroBorders) {
setZeroBorders()
}
draw()
}
function setup() {
createCanvas(0, 0)
offset.x = windowWidth / 2 + 200
noLoop();
textFont("consolas", 20);
initialization(zeroBorders)
// logic[1][1][0] = 0; logic[2][1][0] = 0; logic[3][1][0] = 0; logic[4][1][0] = 0; logic[5][1][0] = 0;
// logic[1][2][0] = 0; logic[2][2][0] = 0; logic[3][2][0] = 0; logic[4][2][0] = 0; logic[5][2][0] = 0;
// logic[1][3][0] = 0; logic[2][3][0] = 1; logic[3][3][0] = 1; logic[4][3][0] = 1; logic[5][3][0] = 0;
// logic[1][4][0] = 0; logic[2][4][0] = 0; logic[3][4][0] = 0; logic[4][4][0] = 0; logic[5][4][0] = 0;
// logic[1][5][0] = 0; logic[2][5][0] = 0; logic[3][5][0] = 0; logic[4][5][0] = 0; logic[5][5][0] = 0;
}
function functionalSymmetry(i, j, k) {
//return {i: logicSize.w - i - 1, j: j};
let di = 2, dj = 2;
if(i < di || i >= logicSize.w - di || j < dj || j >= logicSize.h - dj)
return {i: i, j: j, k: (k + 1) % 2};
return {i: i, j: j, k: k};
}
let shiftPressed = false
let controlPressed = false
function keyReleased() {
if(keyCode == SHIFT) {
shiftPressed = false
}
if(keyCode == CONTROL) {
controlPressed = false
}
}
function keyPressed() {
if(keyCode == SHIFT) {
shiftPressed = true
}
if(keyCode == CONTROL) {
controlPressed = true
}
if(keyCode == 85) { // u
if(applyUnion()) {
console.log('union applied')
} else {
console.warn('not all solutions found or solutionsCount = 0 or maxSolutions')
}
}
if(keyCode == 187) { // "="
if(autoPlace) {
autoPlace = false;
console.log("autoPlace -disabled.");
} else {
autoPlace = true;
console.log("autoPlace +enabled.");
}
}
if(keyCode == ENTER) {
let detect = detectErrors(logic);
if(!detect) {
console.clear();
globalSolve();
} else {
console.log("detectErrors = ", detect);
}
}
//********************************************************************************
draw();
}
function mousePressed() {
let i = floor((mouseX - offset.x) / rectSize);
let j = floor((mouseY - offset.y) / rectSize) % (logicSize.h + 1);
let k = floor((mouseY - offset.y) / offset.dy);
if(i >= 0 && i < logicSize.w && j >= 0 && j < logicSize.h && k >= 0 && k < logicSize.z) {
if(shiftPressed) {
if(mouseButton == LEFT) {
if(addToStowage(defaultStowage, [i, j, k])) {
console.log('added point to defaultStowage',[i, j, k])
} else {
console.log('removed point from defaultStowage',[i, j, k])
}
} else if(mouseButton == RIGHT) {
defaultStowage = []
console.log('defaultStowage cleared')
}
} else if(controlPressed) {
if(mouseButton == LEFT) {
if( tests.stableTest.listOfPoints.add(i, j) ) {
console.log('added point to StableTest', [i, j])
} else {
console.log('removed point from StableTest', [i, j])
}
} else if(mouseButton == RIGHT) {
tests.stableTest.listOfPoints.clear()
console.log('StableTest list cleared')
}
} else {
let value;
if(mouseButton == LEFT) {
if(logic[i][j][k] != 1) value = 1; else value = 2;
} else
if(mouseButton == RIGHT) {
if(logic[i][j][k] != 0) value = 0; else value = 2;
}
if(mouseButton != CENTER) {
if(autoPlace) {
for(let z = 0; z < logicSize.z; z++)
logic[i][j][z] = value;
} else {
logic[i][j][k] = value;
}
}
}
}
if(shiftPressed == false) {
let detect = detectErrors(logic);
if(detect) {
console.clear();
}
console.log("detectErrors = ", detect);
}
//********************************************************************************
draw();
}
function addToStowage(stow, point) {
for(let i = 0; i < stow.length; i++) {
let pointInStow = stow[i]
let equal = true
for(let j = 0; j < 3; j++) {
equal = equal && (pointInStow[j] === point[j])
}
if(equal == true) { // если такая уже есть
stow.splice(i, 1) // удаляем
return false // скажем что удалили
}
}
stow.push(point) // добавляем точку
return true
}
function putToStowage(i, j, k) {
if(logic[i][j][k] < 2)
return false;
stowage.push({i, j, k});
return true;
}
// исправлено
function draw() {
background(BG_COLOR)
stroke(GRID_COLOR)
drawBackground(offset.x, offset.y, offset.dy, logic)
for(let z = 0; z < logicSize.z; z++) {
drawGrid(offset.x, offset.y + offset.dy * z, logicSize.w, logicSize.h, rectSize)
}
drawText(offset.x + textOffset.x, offset.y + textOffset.y, logic)
for(let i = 0; i < defaultStowage.length; i++) {
let point = defaultStowage[i]
drawPoint(point[0], point[1], point[2], POINTS_COLOR)
}
for(let i = 0; i < tests.stableTest.listOfPoints.length; i++) {
let point = tests.stableTest.listOfPoints[i]
for(let z = 0; z < logicSize.z; z++) {
drawPoint(point.x, point.y, z, STABLE_COLOR, 'S')
}
}
}
function drawPoint(x, y, z, color, character) {
if(character === undefined) {
noFill()
strokeWeight(3)
stroke(color)
rect(
offset.x + rectSize * x,
offset.y + rectSize * y + offset.dy * z,
rectSize, rectSize
)
strokeWeight(1)
} else {
noStroke()
fill(color)
text(
character,
textOffset.x + offset.x + rectSize * x,
textOffset.y + offset.y + rectSize * (y+0.0) + offset.dy * z
)
}
}
//**********************************************************************************
// исправлено
function preSolveError(arr, setOfChanges) {
let changes = false;
// let mainError = isError(arr, changedPos);
// if(mainError) {
// return 1;
// }
//changedPos = {i: -1, j: -1}; // changedPos is empty => absolutely no Errors in all cells
// это не пригодится
for(let i = 0; i < logicSize.w; i++)
for(let j = 0; j < logicSize.h; j++)
for(let k = 0; k < logicSize.z; k++) {
let cell = arr[i][j][k]
if(cell == 2) {
arr[i][j][k] = 0
changedPos = {i, j, k}
let zeroErr = isError(arr, changedPos);
arr[i][j][k] = 1
let oneErr = isError(arr, changedPos)
if(!zeroErr && !oneErr) {
// 0 0 => x = ?
// массив не меняется
arr[i][j][k] = 2;
} else {
// запоминаем изменение массива
setOfChanges.push(changedPos)
if(zeroErr && oneErr) { // 1 1 => x = []
return 1; // error
} else
if(zeroErr) { // 1 0 => x = 1
arr[i][j][k] = 1;
changes = true;
} else
if(oneErr) { // 0 1 => x = 0
arr[i][j][k] = 0;
changes = true;
}
}
}
}
if(changes) {
return 2; // no error but some changes detected
} else {
return 0; // no changes no error
}
}
// работает
function isError(arr, changedPos) {
// launch test callbacks
for(let i = 0; i < tests.callbackList.length; i++) {
let testCallback = tests.callbackList[i]
if(testCallback(arr) == false) {
return true
}
}
if(changedPos != null) {
let x = changedPos.i;
let y = changedPos.j;
let z = changedPos.k;
for(let i = x - 1; i <= x + 1; i++)
for(let j = y - 1; j <= y + 1; j++)
for(let k = z - 1; k <= z + 1; k++) { // действительно ли это так или можно это упростить
let actualK
if(zLoopBack) {
actualK = (k + logicSize.z) % logicSize.z
} else {
actualK = k
if(k === 0) continue
}
if( notInRange(i, j, actualK, logicSize.w, logicSize.h, logicSize.z) )
continue;
if(valueError(arr, i, j, actualK))
return true;
}
return false;
}
let startK = zLoopBack ? 0 : 1
for(let i = 0; i < logicSize.w; i++)
for(let j = 0; j < logicSize.h; j++)
for(let k = startK; k < logicSize.z; k++)
if(valueError(arr, i, j, k))
return true;
return false;
}
// работает
function valueError(arr, i, j, k, detect) {
let cell = arr[i][j][k];
if(cell == 2) return false;
// cell != 2
if(functionalSymmetryEnable) {
let fSym = functionalSymmetry(i, j, k);
let symCell = arr[fSym.i][fSym.j][fSym.k];
if(symCell != 2) { // if cell != 2 and symCell != 2
if(symCell != cell) {
if(detect != undefined) {
errors.push({i, j, k});
}
return true;
}
}
}
if(errorInCell(arr, i, j, k)) {
if(detect != undefined) {
console.log("error at ", i, j, k);
errors.push({i, j, k});
}
return true;
}
return false;
}
// исправлено
function isRandomFiltering(solutionsCount) {
randomFilterCurrentValue *= randomDampingCoefficient
if(randomFilterCurrentValue < 1) {
// console.log('randomFilterCurrentValue: ', randomFilterCurrentValue)
return Math.random() < randomFilterCurrentValue
} else {
return true
}
}
function solve(arr, sip, changedPos, setOfChanges = []) {
if(n >= maxBranches || solutionsCount >= maxSolutions) {
return;
}
n++;
if(n % debugStep === 0) {
console.log(`{${n}}`)
}
if(setOfChanges.length > 0) {
throw new Error('такого не бывает')
}
if(errorUpgrade === false) {
changedPos = null;
}
// detect errors for cut one branch
if( isError(arr, changedPos) ) {
return;
}
if(preSolveEnable) {
// для того чтобы обратить все изменения в presolve
// их все нужно запомнить
let iterations = 1;
// копия ссылки, массив не копируется
let temp = arr
// let temp = copy3dArray(arr)
// some manipulations for presolve
let pre = preSolveError(temp, setOfChanges)
while(pre === 2) { // some changes detected
pre = preSolveError(temp, setOfChanges)
iterations++
}
if(iterations > maxPreSolveIterations) {
maxPreSolveIterations = iterations
}
if(setOfChanges.length > maxSetOfChangesLength) {
maxSetOfChangesLength = setOfChanges.length
}
if(pre === 1) { // error detected
return;
}
// in this line pre always equal zero
}
// some stuff
if(sip >= stowage.length) {
// add successful branch to solves
// solves.push( copy3dArray(arr) ) // THIS CODE LINE TAKES UP A LOT OF MEMORY
unionTo(arr, globalUnionArray)
solutionsCount++
if(solutionsCount <= maxPrint && isRandomFiltering(solutionsCount)) {
print3dSolve(arr, solutionsCount, true)
}
return;
}
let stow = stowage[sip];
let si = stow.i, sj = stow.j, sk = stow.k;
sip++;
let cell = arr[si][sj][sk];
while (cell < 2 && sip < stowage.length) { // if cell is constant
stow = stowage[sip];
si = stow.i; sj = stow.j; sk = stow.k;
sip++;
cell = arr[si][sj][sk];
//console.log("CONSTANT USING", "sip = " + sip);
}
let previousValue = cell // не всегда = 2
// recursive call solve()
// if(previousValue != 2) {
// console.log('previousValue != 2: stow', stow)
// }
for(let value = 0; value < 2; value++) {
let pos = {i: si, j: sj, k: sk};
arr[si][sj][sk] = value
// выделяем массив для изменений на уровне ниже
let levelDownSetOfChanges = []
solve(arr, sip, pos, levelDownSetOfChanges)
for(let i = 0; i < levelDownSetOfChanges.length; i++) {
let curPos = levelDownSetOfChanges[i]
arr[curPos.i][curPos.j][curPos.k] = 2
// console.log('curPos: ', curPos)
}
arr[si][sj][sk] = previousValue
}
// for(let value = 0; value < 2; value++) {
// let pos = {i: si, j: sj, k: sk};
// let temp = copy3dArray(arr);
// temp[si][sj][sk] = value;
// solve(temp, sip, pos);
// }
return;
}
// исправлено
function printSolve(arr, n) {
let str = getStatsString(arr, n, showNumberOfCells);
for(let i = 0; i < logicSize.h; i++) {
for(let j = 0; j < logicSize.w; j++) {
str += getSymbol(arr[j][i]) + " ";
}
str += "\n";
}
console.log(str);
}
function getStatsString(arr, n, showNumberOfCells = false) {
let str = "";
if(n !== undefined) {
str += `${n}) `;
}
let stringNumberCells = numberOfCellsToString(getNumberOfCells(arr))
if(showNumberOfCells) {
str += `${stringNumberCells}`;
}
if(n !== undefined || stringNumberCells.length > 0) {
str += '\n';
}
return str;
}
function print3dSolve(arr, n, showNumberOfCells = false) {
let str = getStatsString(arr, n, showNumberOfCells);
for(let k = 0; k < logicSize.z; k++) {
str += " *** t = " + (k+1) + " *** \n";
for(let i = 0; i < logicSize.h; i++) {
for(let j = 0; j < logicSize.w; j++) {
str += getSymbol(arr[j][i][k]) + " ";
}
str += "\n";
}
}
console.log(str);
}
// осталось исправить
function loadToArray(arr, load, offsetX, offsetY) {
for(let i = 0; i < load.length; i++)
for(let j = 0; j < load[0].length; j++) {
let i_ = j + offsetX, j_ = i + offsetY;
if(i_ < 0 || i_ >= logicSize.w || j_ < 0 || j_ >= logicSize.h)
continue;
arr[i_][j_] = load[i][j];
}
}
// исправлено
function notInRange(i, j, k, w, h, z) {
return (i < 0 || i >= w || j < 0 || j >= h || k < 0 || k >= z);
}
// исправлено
function getCell(arr, i, j, k, w, h, z) {
if(notInRange(i, j, k, w, h, z))
return defaultPavement;
return arr[i][j][k];
}
// наверное исправлено
// cell = 0 => sum != 3
function errorInCell(arr, x, y, z) {
let minSum = 0, maxSum = 0;
// потом нужно исправить эту строку
let mainCell = arr[x][y][z];
let k = (z - 1 + logicSize.z) % logicSize.z; // предполагая положительный обход вдось оси z
let prevCell = arr[x][y][k];
if(mainCell == 2)
return false;
// TEST STUFF
if(zLoopBack === false) {
if(z === 0) {
throw `z === ${z} AND zLoopBack === false`
}
}
for(let i = x - 1; i <= x + 1; i++)
for(let j = y - 1; j <= y + 1; j++) {
if(i == x && j == y) continue;
let cell = getCell(arr, i, j, k, logicSize.w, logicSize.h, logicSize.z);
if(cell == 2) {
maxSum++;
} else {
maxSum += cell;
minSum += cell;
}
}
if(prevCell == 2) {
if(mainCell == 0)
if(minSum == 3 && maxSum == 3) // sum = 3
return true;
if(mainCell == 1)
if(minSum > 3 || maxSum < 2) // sum not in {2, 3}
return true;
}
if(mainCell == 0) {
if(prevCell == 0)
if(minSum == 3 && maxSum == 3) // sum = 3
return true;
if(prevCell == 1)
if(minSum >= 2 && maxSum <= 3) // сумма должна быть только либо 2 либо 3
return true;
/* возможны три варианта:
1. только 2
2. только 3
3. внутри отрезка [2, 3]
*/
}
if(mainCell == 1) {
if(prevCell == 0)
if(minSum > 3 || maxSum < 3) // sum != 3
return true;
if(prevCell == 1)
if(minSum > 3 || maxSum < 2) // sum not in {2, 3}
return true;
}
return false;
}
// работает
function initArray(xSize, ySize, value) {
let arr = [];
for(let i = 0; i < xSize; i++) {
arr[i] = new Array();
for(let j = 0; j < ySize; j++) {
arr[i][j] = value;
}
}
return arr;
}
// работает
function init3DArray(xSize, ySize, zSize, value) {
let arr = [];
for(let i = 0; i < xSize; i++) {
arr[i] = new Array();
for(let j = 0; j < ySize; j++) {
arr[i][j] = new Array();
for(let k = 0; k < zSize; k++) {
arr[i][j][k] = value;
}
}
}
return arr;
}
// исправлено
function detectErrors(arr) {
errors = [];
let result = false;
let startK = zLoopBack ? 0 : 1
for(let i = 0; i < logicSize.w; i++)
for(let j = 0; j < logicSize.h; j++)
for(let k = startK; k < logicSize.z; k++) {
result = valueError(arr, i, j, k, true) || result;
}