-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solve.java
694 lines (590 loc) · 22.5 KB
/
Solve.java
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
//Jackson Mishuk
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
/*
**NOTE: The code seems to be fully consistent with your examples, except for the xword2 with large dictionary examples.
I am assuming this has to do with the arbitrary nature of mrv and mrv+deg when they are both the same in ordering
The file Search.java contains all of the necessary code.
This program allows you to give an input file with a puzzle layout and word bank The program will find a solution for the puzzle using the words given if there is one.
Command line arguments include: -d(File location of dictionary)*, -p(File location of puzzle)*, -v(Verbosity Level), -vs or --variable-selection(Variable selection, -vo or --value-order(Value order), -lfc or --limited-forward-check(Limited forward check)
**NOTE: All command line arguments marked with star are required**
EXAMPLE OF CMD ARGUEMENT: java Solve.java -d ../a02-data/dictionary-large.txt -p ../a02-data/xword01.txt -v 2 -vs mrv --value-order lcv
-d* :String reference for dictionary file
-p* :String reference for puzzle file
-v :The amount of information about the search that you want to display(Higher the number the more information that will display)
{0(Default), 1, 2}
-vs or --variable-selection :Allows use of different heuristics for ordering of finding different words
{static(Default), mrv(minimum remaining values), deg(degree/most constraining variable), mrv+deg(mrv but ties are broken by degree)}
-vo or --value-order :Allows for the use of different heuristics for ordering of values tried from the domain of a variable
{static(Default), lcv(Least constraining value)}
-lfc or --limited-forward-check :Indicates limited forward checking when checking for value consistency
{Disabled by default}
*/
public class Solve {
public static void main(String[] args) {
String[] arg = {"-d", "a02-data/dictionary-medium.txt", "-p", "a02-data/xword02.txt", "-v", "1", "-vs", "mrv"};
Problem P = new Problem(arg);
if(P.dictionaryFile==null || P.puzzleFile==null) {
System.out.println("Either the dictionary file or the puzzle file can not be found\n");
return;
}
if(!loadDictionary(P) || !loadPuzzle(P)) {
return;
}
if(!cspLoad(P)) {
return;
}
if(P.verbosityLvl >= 1) {
System.out.println("* Attempting to solve crossword puzzle...\n");
}
if(!cspSolve(P)) {
return;
}
}
/*
* Loads the dictionary file
*
* Returns boolean(true if the file is valid, false if not)
*/
private static boolean loadDictionary(Problem P) {
if(P.verbosityLvl >= 1) {
System.out.printf("* Reading dictionary from [%s]\n" , P.dictionaryFile);
}
try (BufferedReader br = new BufferedReader(new FileReader(P.dictionaryFile))){
String line = br.readLine();
int i = 0;
while(line != null) {
i++;
P.WordList.add(line);
line = br.readLine();
}
if(P.verbosityLvl >= 2) {
System.out.printf("** Dictionary has %d words\n\n" , i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return true;
}
/*
* Loads the puzzle file
*
* Returns boolean(true if the file is valid, false if not)
*/
private static boolean loadPuzzle(Problem P) {
if(P.verbosityLvl >= 1) {
System.out.printf("* Reading puzzle from [%s]\n" , P.puzzleFile);
}
File file = new File(P.puzzleFile);
try {
Scanner scan = new Scanner(file);
String row = scan.next();
String col = scan.next();
P.PuzzleInfo = new String[Integer.valueOf(row)][Integer.valueOf(col)];
StringBuilder puzzleString = new StringBuilder();
if(P.verbosityLvl >= 2) {
puzzleString.append("** Puzzle\n");
}
for(int i=0; i<P.PuzzleInfo.length; i++) {
String[] info = new String[Integer.valueOf(col)];
for(int j = 0; j<info.length; j++) {
P.PuzzleInfo[i][j] = scan.next();
if(P.verbosityLvl >= 2)
puzzleString.append(String.format("%-2s ", P.PuzzleInfo[i][j]));
}
puzzleString.append("\n");
}
scan.close();
if(P.verbosityLvl >= 2) {
System.out.println(puzzleString.toString());
}
}
catch(Exception e) {
e.printStackTrace();
}
return true;
}
/*
* Loads the Constraint Satisfaction Problem
*
* Returns boolean(true)
*/
private static boolean cspLoad(Problem P) {
int varsCreated = 0;
int constraintsCreated = 0;
//Increment a row of the puzzle
for(int i = 0; i<P.PuzzleInfo.length; i++) {
//Increment a column of the puzzle(backwards to guarentee that a down has already been seen when a constraint is being added)
for(int j = P.PuzzleInfo[i].length - 1; j >= 0; j--) {
if(!(P.PuzzleInfo[i][j].equals("#") || P.PuzzleInfo[i][j].equals("_"))){
//Checking for down words
if(i == 0 || P.PuzzleInfo[i-1][j].equals("#")) {
int length = 0;
for(int k = i; k<P.PuzzleInfo.length; k++) {
if(P.PuzzleInfo[k][j].equals("#")) {
break;
}else {
length++;
}
}
P.VarList.put(-Integer.valueOf(P.PuzzleInfo[i][j]), new Problem.Var(Integer.valueOf(P.PuzzleInfo[i][j]),
/*isAcross value*/false, length, P, null));
varsCreated++;
}
//Checking for across words
if(j == 0 || P.PuzzleInfo[i][j-1].equals("#")) {
HashMap<Integer, int[]> Collisions = new HashMap<Integer, int[]>();
int length = 0;
for(int k = j; k<P.PuzzleInfo[i].length; k++) {
if(P.PuzzleInfo[i][k].equals("#")) {
break;
}else {
//Across words check for collisions with Down words
for(int h = i; h>=0 && !P.PuzzleInfo[h][k].equals("#"); h--) {
if(!P.PuzzleInfo[h][k].equals("_") && (h == 0 || P.PuzzleInfo[h-1][k].equals("#"))) {
//Goal here is to create an entire constraint map or add a constraint within
constraintsCreated++;
/*System.out.printf("%3s) Constraint: %2d Across Letter %2d; %2d Down Letter %2d\n",
constraintsCreated, Integer.valueOf(P.PuzzleInfo[i][j]), k, Integer.valueOf(P.PuzzleInfo[h][k]), i-h);*/
int[] downColis = {-Integer.valueOf(P.PuzzleInfo[h][k]), i-h};
Collisions.put(k-j, downColis);
}
}
length++;
}
}
P.VarList.put(Integer.valueOf(P.PuzzleInfo[i][j]), new Problem.Var(Integer.valueOf(P.PuzzleInfo[i][j]),
/*isAcross value*/true, length, P, Collisions));
varsCreated++;
}
}
}
}
if(P.verbosityLvl >= 1) {
System.out.printf("* CSP has %d variables\n"
+ "* CSP has %d constraints\n", varsCreated, constraintsCreated);
//Change to true to make sure variables are being created correctly
debugVals(false, P);
}
return true;
}
//Used for debugging csp only!
static private void debugVals(boolean bool, Problem P) {
if(bool) {
System.out.println();
Object[] keySet = P.VarList.keySet().toArray();
for(int i = 0; i < keySet.length; i++) {
Problem.Var element = P.VarList.get(keySet[i]);
String direction = element.name > 0 ? "Across": " Down ";
String notDirection = element.name < 0 ? "Across": "Down";
String DomainStr = "";
for(String DomainElem:element.Domain)
DomainStr += DomainElem + ", ";
String ConstraintsStr = "";
Object[] constraintKeySet = element.Collisions.keySet().toArray();
for(int j = 0; j<constraintKeySet.length; j++) {
int[] constraintArr = element.Collisions.get(constraintKeySet[j]);
if(j==0)
ConstraintsStr += String.format(" The character %d colides with character %d of %d %S"
, constraintKeySet[j], constraintArr[1], Math.abs(constraintArr[0]), notDirection);
else
ConstraintsStr += String.format(", \n \t\t\t The character %d colides with character %d of %d %S "
, constraintKeySet[j], constraintArr[1], Math.abs(constraintArr[0]), notDirection);
}
System.out.printf("\n##\t\t Value[%d %-6S]; Length[%d]; \n \t\t Domain[%s]\n\n \t\t Constraints[%s]\n",
Math.abs(element.name), direction, element.length, DomainStr, ConstraintsStr);
}
}
}
private static int recursiveCalls = 0;//This is called within the backtracking search
/*
* Used to solve the csp
*
* Returns boolean(true with success and false with failure)
*/
private static boolean cspSolve(Problem P) {
HashMap<Problem.Var, String> Assignment = new HashMap<Problem.Var, String>();
if(P.verbosityLvl >= 2)
System.out.printf("** Running backtracking search...\n"
+ "Backtrack Call:\n");
long startTimeSearch = System.currentTimeMillis();
//lastSystemTime = startTimeSearch;
Assignment = backtrack(P, Assignment, 1);
if(Assignment == null) {
System.out.printf("FAILED; Solving took %dms (%d recursive calls)\n\n",
System.currentTimeMillis() - startTimeSearch, recursiveCalls);
return false;
}else {
System.out.printf("SUCCESS! Solving took %dms (%d recursive calls)\n\n",
System.currentTimeMillis() - startTimeSearch, recursiveCalls);
System.out.println(puzzleString(Assignment, P));
}
return true;
}
/*
* Backtrack method with recursive calls to method
*
* Returns HashMap<Problem.Var, String>(The Assignment of a solved puzzle, or null indicating failure)
*/
private static HashMap<Problem.Var, String> backtrack(Problem P, HashMap<Problem.Var, String> Assignment, int tabAmt) {
recursiveCalls++;
if(Assignment.size() >= P.VarList.size()) {
if(P.verbosityLvl >= 2) {
for(int i = 0; i<tabAmt; i++)
System.out.print(" ");
System.out.println("Assignment is complete!\n");
}
return Assignment;
}
Problem.Var Variable = setUnassignedVariable(P, Assignment);
char dir = Variable.name>0 ? 'a' : 'd';
if(P.verbosityLvl >= 2) {
for(int i = 0; i<tabAmt; i++)
System.out.print(" ");
System.out.printf("Trying values for X%d%c\n", Math.abs(Variable.name), dir);
}
if(Variable == null)
return null;
for(Object Value:OrderDomainValues(P, Variable, Assignment)) {
if(Variable.isConsistent((String)Value, Assignment, P)) {
if(P.verbosityLvl >= 2) {
for(int i = 0; i<tabAmt; i++)
System.out.print(" ");
System.out.printf("Assignment { X%d%c = %S } is consistent\n", Math.abs(Variable.name), dir, Value);
}
Assignment.put(Variable, (String)Value);
if(P.verbosityLvl >= 2) {
for(int i = 0; i<tabAmt; i++)
System.out.print(" ");
System.out.printf("Backtrack Call:\n");
}
if(backtrack(P, Assignment, tabAmt+1)/*Recursive Call*/ != null)
return Assignment;
Assignment.remove(Variable);
}else {
if(P.verbosityLvl >= 2) {
for(int i = 0; i<tabAmt; i++)
System.out.print(" ");
System.out.printf("Assignment { X%d%c = %S } is inconsistent\n", Math.abs(Variable.name), dir, Value);
}
}
}
if(P.verbosityLvl >= 2) {
for(int i = 0; i<tabAmt; i++)
System.out.print(" ");
System.out.printf("Failed call; backtracking...\n");
}
return null;
}
/*
* Uses selected heuristic to order the variables
*
* Returns Var(The Var to be solved next)
*/
private static Problem.Var setUnassignedVariable(Problem P, HashMap<Problem.Var, String> Assignment) {
Integer[] keySet = P.VarList.keySet().toArray(new Integer[0]);
if(P.variableSelection.equals("static")) {
Arrays.sort(keySet, new Comparator<Integer>() {
public int compare(Integer i1,Integer i2){
if(Assignment.get(P.VarList.get(i1)) != null) return -1;
if(Assignment.get(P.VarList.get(i2)) != null) return 1;
int absO1 = Math.abs(i1); int absO2 = Math.abs(i2);
if(absO1>absO2) return 1;
if(absO1<absO2) return -1;
if(i1<i2) return 1;
return -1;
}
});
}else if(P.variableSelection.equals("mrv")) {
Arrays.sort(keySet, new Comparator<Integer>() {
public int compare(Integer i1,Integer i2){
Problem.Var V1 = P.VarList.get(i1);
Problem.Var V2 = P.VarList.get(i2);
if(Assignment.get(V1) != null && Assignment.get(V2) == null) return -1;
if(Assignment.get(V2) != null && Assignment.get(V1) == null) return 1;
int D1const = 0;
for(String element:V1.Domain) {
if(V1.isConsistent(element, Assignment, P)) D1const++;
}
int D2const = 0;
for(String element:V2.Domain) {
if(V2.isConsistent(element, Assignment, P)) D2const++;
}
if(D1const>D2const) return 1;
if(D1const<D2const) return -1;
return 0;
}
});
}else if(P.variableSelection.equals("deg")) {
Arrays.sort(keySet, new Comparator<Integer>() {
public int compare(Integer i1,Integer i2){
HashMap<Integer, int[]> C1 = P.VarList.get(i1).Collisions;
int c1Unassigned = 0;
Integer[] keySet1 = C1.keySet().toArray(new Integer[0]);
for(int i = 0; i<C1.size(); i++) {
if(Assignment.get(P.VarList.get(C1.get(keySet1[i])[0]))==null) c1Unassigned++;
}
if(c1Unassigned == 0) return 1;
HashMap<Integer, int[]> C2 = P.VarList.get(i2).Collisions;
if(c1Unassigned>C2.size()) return -1;
int c2Unassigned = 0;
Integer[] keySet2 = C2.keySet().toArray(new Integer[0]);
for(int i = 0; i<C2.size(); i++) {
if(Assignment.get(P.VarList.get(C2.get(keySet2[i])[0]))==null) c2Unassigned++;
}
if(c1Unassigned<c2Unassigned) return 1;
if(c1Unassigned>c2Unassigned) return -1;
return 0;
}
});
}else if(P.variableSelection.equals("mrv+deg")){
Arrays.sort(keySet, new Comparator<Integer>() {
public int compare(Integer i1,Integer i2){
Problem.Var V1 = P.VarList.get(i1);
Problem.Var V2 = P.VarList.get(i2);
int D1const = 0; int D2const = 0;
if(Assignment.get(V1) != null && Assignment.get(V2) == null) return -1;
if(Assignment.get(V2) != null && Assignment.get(V1) == null) return 1;
if(Assignment.get(V2) != null && Assignment.get(V1) != null) {}
else {
for(String element:V1.Domain) {
if(V1.isConsistent(element, Assignment, P)) D1const++;
}
for(String element:V2.Domain) {
if(V2.isConsistent(element, Assignment, P)) D2const++;
}
if(D1const>D2const) return 1;
if(D1const<D2const) return -1;
}
HashMap<Integer, int[]> C1 = P.VarList.get(i1).Collisions;
HashMap<Integer, int[]> C2 = P.VarList.get(i2).Collisions;
int c1Unassigned = 0;
Integer[] keySet1 = C1.keySet().toArray(new Integer[0]);
for(int i = 0; i<C1.size(); i++) {
if(Assignment.get(P.VarList.get(C1.get(keySet1[i])[0]))==null) c1Unassigned++;
}
int c2Unassigned = 0;
Integer[] keySet2 = C2.keySet().toArray(new Integer[0]);
for(int i = 0; i<C2.size(); i++) {
if(Assignment.get(P.VarList.get(C2.get(keySet2[i])[0]))==null) c2Unassigned++;
}
if(c1Unassigned>c2Unassigned) return 1;
if(c1Unassigned<c2Unassigned) return -1;
return 0;
}
});
}
for(int i = 0; i < keySet.length; i++) {
if(Assignment.get(P.VarList.get(keySet[i])) == null) {
return P.VarList.get(keySet[i]);
}
}
return null;
}
/*
* Uses selected heuristic to order the values
*
* Returns Object[](The order of values to be checked)
*/
private static Object[] OrderDomainValues(Problem P, Problem.Var Variable, HashMap<Problem.Var, String> Assignment) {
if(P.valueOrder.equals("static"))
Arrays.sort(Variable.Domain);
else if(P.valueOrder.equals("lcv"))
Arrays.sort(Variable.Domain, new Comparator<String>() {
public int compare(String i1,String i2){
int count1 = 0;
int count2 = 0;
HashMap<Integer, int[]> collis = Variable.Collisions;
Integer[] keyArr = collis.keySet().toArray(new Integer[0]);
for(int i = 0; i<keyArr.length; i++) {
int tempKey = keyArr[i];
int[] tempColis = collis.get(tempKey);
Problem.Var tempVar = P.VarList.get(tempColis[0]);
if(Assignment.get(tempVar) != null) continue;
char int1Char = i1.charAt(tempKey);
char int2Char = i2.charAt(tempKey);
for(int j = 0; j < tempVar.Domain.length; j++) {
char checkingChar = tempVar.Domain[j].charAt(tempColis[1]);
if(int1Char == checkingChar) {
count1++;
}
if(int2Char == checkingChar) {
count2++;
}
}
}
if(count1<count2) return 1;
if(count1>count2) return -1;
return 0;
}
});
return Variable.Domain;
}
/*
* Calculates final puzzle after search
*
* Returns String(Final puzzle after solve)
*/
private static String puzzleString(HashMap<Problem.Var, String> cspRet, Problem P) {
StringBuilder stringRet = new StringBuilder("");
for(int i = 0; i<P.PuzzleInfo.length; i++) {
for(int j = 0; j<P.PuzzleInfo[i].length; j++) {
if (Character.isDigit(P.PuzzleInfo[i][j].charAt(0))){
addWordsRecurse(i, j, cspRet, P);
}
}
}
for(int i = 0; i<P.PuzzleInfo.length; i++) {
for(int j = 0; j<P.PuzzleInfo[i].length; j++) {
if(P.PuzzleInfo[i][j].equals("#")) stringRet.append(" ");
else stringRet.append(String.format("%s", P.PuzzleInfo[i][j]));
}
stringRet.append("\n");
}
return stringRet.toString();
}
//Used the build puzzle string only!
private static void addWordsRecurse(int i, int j, HashMap<Problem.Var, String> cspRet, Problem P){
int currNum = Integer.valueOf(P.PuzzleInfo[i][j]);
//across
if(j == 0 || P.PuzzleInfo[i][j-1].equals("#")) {
for(int k = j; k < P.VarList.get(currNum).length + j; k++) {
if(Character.isDigit(P.PuzzleInfo[i][k].charAt(0)) && Integer.valueOf(P.PuzzleInfo[i][k]) != currNum){
addWordsRecurse(i, k, cspRet, P);
}
P.PuzzleInfo[i][k] = String.valueOf(cspRet.get(P.VarList.get(currNum)).charAt(k-j));
}
}
//down
if(i == 0 || P.PuzzleInfo[i-1][j].equals("#")) {
for(int k = i; k < P.VarList.get(-currNum).length + i; k++) {
if(Character.isDigit(P.PuzzleInfo[k][j].charAt(0)) && Integer.valueOf(P.PuzzleInfo[k][j]) != currNum) {
addWordsRecurse(k, j, cspRet, P);
}
P.PuzzleInfo[k][j] = String.valueOf(cspRet.get(P.VarList.get(-currNum)).charAt(k-i));
}
}
}
static private class Problem{
//All defined by user command line argument
private String dictionaryFile = null;
private String puzzleFile = null;
private int verbosityLvl = 0;
private String variableSelection = "static";
private String valueOrder = "static";
private boolean limitedForwardCheck = false;
private String[][] PuzzleInfo;
private LinkedList<String> WordList = new LinkedList<String>();
private HashMap<Integer, Var> VarList = new HashMap<Integer, Var>();
Problem(String[] args){
for(int j = 0; j<args.length; j++){
switch (args[j]) {
case "-d":
j++;
dictionaryFile = args[j];
break;
case "-p":
j++;
puzzleFile = args[j];
break;
case "-v":
j++;
verbosityLvl = Integer.parseInt(args[j]);
break;
case "-vs":
case "--variable-selection":
j++;
variableSelection = args[j];
break;
case "-vo":
case "--value-order":
j++;
valueOrder = args[j];
break;
case "-lfc":
case "--limited-forward-check":
limitedForwardCheck = true;
break;
}
}
}
private static class Var{
private int name;
private int length;
private String[] Domain;
//<Int of spot collision in current var, {Var it intersects with, spot in the other word}>
private HashMap<Integer, int[]> Collisions = new HashMap<Integer, int[]>();
Var(int n, boolean a/*Is Across*/, int l, Problem P, HashMap<Integer, int[]> C){
if(a) {
name = n;
//Deals with collisions here
createCollisions(C, P);
}else
name = -n;
length = l;
createDoman(P);
}
private void createDoman(Problem P) {
int j = 0;
Domain = new String[P.WordList.size()];
for(String Word:P.WordList) {
if(Word.length() == this.length) {
Domain[j] = Word;
j++;
}
}
Domain = Arrays.copyOf(Domain, j);
}
//Will only be used in across variables
private void createCollisions(HashMap<Integer, int[]> C, Problem P) {
Collisions = C;
Object[] keySet = C.keySet().toArray();
for(int i = 0; i < keySet.length; i++) {
int[] collisionInfo = C.get(keySet[i]);
P.VarList.get(collisionInfo[0]).addDownCollision(collisionInfo[1], this.name, (int)keySet[i]);
}
}
//Will only be used in down variables
private void addDownCollision(int downSpot, int acrossValue, int acrossSpot) {
int intArr[] = {acrossValue, acrossSpot};
this.Collisions.put(downSpot, intArr);
}
//Used to see if a value is consistent in the given assignment in this Var
public boolean isConsistent(String Value, HashMap<Problem.Var, String> Assignment, Problem P) {
Integer[] collisionsKeySet = Collisions.keySet().toArray(new Integer[0]);
for(int i = 0; i<collisionsKeySet.length; i++) {
int[] tempCollisionArr = Collisions.get(collisionsKeySet[i]);
Var tempVar = P.VarList.get(tempCollisionArr[0]);
if(Assignment.get(tempVar) != null &&
Value.charAt((int)collisionsKeySet[i]) != Assignment.get(tempVar).charAt(tempCollisionArr[1])) {
return false;
}
//Limited forward Checking
else if(P.limitedForwardCheck) {
for(int j = 0; j<tempVar.Domain.length; j++){
String element = tempVar.Domain[j];
if(Value.charAt((int)collisionsKeySet[i]) == element.charAt(tempCollisionArr[1])) {
j=tempVar.Domain.length;
continue;
}
if(j + 1 == tempVar.Domain.length) {
return false;
}
}
}
}
return true;
}
}
}
}