-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForwardCheckingMRV.java
More file actions
156 lines (130 loc) · 5.17 KB
/
Copy pathForwardCheckingMRV.java
File metadata and controls
156 lines (130 loc) · 5.17 KB
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
import java.util.*;
public class ForwardCheckingMRV {
private long assignmentCounter = 0L;
private long consistencyCounter = 0L;
private Cell[][] grid;
public void resetCounters() {
assignmentCounter = 0L;
consistencyCounter = 0L;
}
public long getAssignmentCount() {
return assignmentCounter;
}
public long getConsistencyCount() {
return consistencyCounter;
}
public boolean solve(Cell[][] grid) {
this.grid = grid;
resetCounters();
for (int i = 0; i < 2; i++) {
for(int j = 0; j < 10; j++) {
grid[i][j].restartDomain();
}
}
return forwardCheckingMRVRecursive();
}
private boolean forwardCheckingMRVRecursive() {
if (isAssignmentComplete()) {
return true;
}
int[] rowAndColumn = selectMRVVariable();
if (rowAndColumn == null) return isAssignmentComplete();
int r = rowAndColumn[0];
int c = rowAndColumn[1];
for (int value = 0; value < 10; value++) {
if (grid[r][c].isValueInDomain(value)) {
grid[r][c].setValue(value);
assignmentCounter++;
List<int[]> prunedDomains = pruneDomains(r, c, value);
if (prunedDomains != null) {
if (forwardCheckingMRVRecursive()) {
return true;
}
}
restoreDomains(prunedDomains);
grid[r][c].setValue(-1);
}
}
return false;
}
private int[] selectMRVVariable() {
int minDomainSize = 11;
int[] mrvCell = null;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 10; j++) {
if (grid[i][j].getValue() == -1) {
int currentDomainSize = grid[i][j].getSizeOfAvailableDomain();
if (currentDomainSize < minDomainSize) {
minDomainSize = currentDomainSize;
mrvCell = new int[]{i, j};
if (minDomainSize <= 1) {
return mrvCell;
}
}
}
}
}
return mrvCell;
}
private List<int[]> pruneDomains(int r, int c, int value) {
List<int[]> prunedList = new ArrayList<>();
// 1- Row Constraint
for (int col = 0; col < 10; col++) {
if (col != c && grid[r][col].getValue() == -1) {
if (grid[r][col].isValueInDomain(value)) {
grid[r][col].updateDomain(value, false); prunedList.add(new int[]{r, col, value});
consistencyCounter++; if (grid[r][col].isDomainEmpty()) return null;
}
}
}
// 2- Diagonal Constraint
int[] dRow = {-1, -1, 1, 1}; int[] dCol = {-1, 1, -1, 1};
for (int i = 0; i < dRow.length; i++) {
int nr = r + dRow[i]; int nc = c + dCol[i];
if (nr >= 0 && nr < 2 && nc >= 0 && nc < 10 && grid[nr][nc].getValue() == -1) {
if (grid[nr][nc].isValueInDomain(value)) {
grid[nr][nc].updateDomain(value, false); prunedList.add(new int[]{nr, nc, value});
consistencyCounter++; if (grid[nr][nc].isDomainEmpty()) return null;
}
}
}
// 3- Sum Constraint
int otherRow = 1 - r;
if (grid[otherRow][c].getValue() == -1) {
int requiredSum = grid[2][c].getValue(); int neededValue = requiredSum - value;
if (neededValue >= 0 && neededValue <= 9) {
for (int v = 0; v < 10; v++) {
if (v != neededValue && grid[otherRow][c].isValueInDomain(v)) {
grid[otherRow][c].updateDomain(v, false); prunedList.add(new int[]{otherRow, c, v});
consistencyCounter++;
}
}
if (grid[otherRow][c].isDomainEmpty()) return null;
} else {
return null;
}
if (grid[otherRow][c].isValueInDomain(value)) {
grid[otherRow][c].updateDomain(value, false); prunedList.add(new int[]{otherRow, c, value});
consistencyCounter++; if (grid[otherRow][c].isDomainEmpty()) return null;
}
}
return prunedList;
}
private void restoreDomains(List<int[]> prunedList) {
if (prunedList == null) return;
for (int[] prune : prunedList) {
int r = prune[0]; int c = prune[1]; int restoredValue = prune[2];
if (grid[r][c].getValue() == -1) {
grid[r][c].updateDomain(restoredValue, true);
}
}
}
private boolean isAssignmentComplete() {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 10; j++) {
if (grid[i][j].getValue() == -1) return false;
}
}
return true;
}
}