-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstraintSatisfactionProblemSolver.java
More file actions
276 lines (223 loc) · 8.98 KB
/
ConstraintSatisfactionProblemSolver.java
File metadata and controls
276 lines (223 loc) · 8.98 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
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
package model;
import java.util.ArrayList;
import java.util.List;
//A B C D E F G H
//0 1 2 3 4 5 6 7
/**
* @author Joel Hempel
* @since 10/21/2022
* Solves a constraint satisfaction problem with 8 variables and 17 constraints using DFS with pruning
*/
public class ConstraintSatisfactionProblemSolver {
char[] ordering = {'F', 'H', 'D', 'C', 'G', 'E', 'A', 'B'}; //CHANGE THIS TO HAVE A DIFFERENT ORDER OF CONSTRAINTS
ArrayList<Constraint> currentStateList;
ArrayList<ArrayList<Integer>> solutionList;
int numOfSolutionsFound = 0;
int numberOfFailedChecks = 0;
public ConstraintSatisfactionProblemSolver(){
currentStateList = new ArrayList<>();
solutionList = new ArrayList<ArrayList<Integer>>();
findSolutionDFS(0, currentStateList);
System.out.println("The number of failed branches is " + numberOfFailedChecks + " and the solutions (if any) are:\n");
for (int i = 0; i < solutionList.size(); i++){
printState(solutionList.get(i));
}
}
/**
* recursively prune paths and expand to further states if they increase the number of conditions met
* @param stateListIndex index of the list of parameters
* @return Boolean whether the next path makes the solution better
*/
private Boolean findSolutionDFS(int stateListIndex, ArrayList<Constraint> previousList){
ArrayList<Constraint> tmpState = new ArrayList<>(previousList);
if (previousList.size() == 8 && allConstraintsSatisfied(tmpState)){
ArrayList<Integer> solList = new ArrayList<>();
for (int i = 0; i < tmpState.size(); i++) {
solList.add(tmpState.get(i).getValue());
}
solutionList.add(solList);
this.numOfSolutionsFound++;
return true;
}
Constraint currentConstraint = new Constraint(ordering[stateListIndex]);
currentConstraint.setValue(1);
tmpState.add(currentConstraint);
if (allConstraintsSatisfied(tmpState)) {
findSolutionDFS(stateListIndex + 1, tmpState);
}
//test value of 2
currentConstraint.setValue(2);
if (allConstraintsSatisfied(tmpState)) {
findSolutionDFS(stateListIndex + 1, tmpState);
}
//test value of 3
currentConstraint.setValue(3);
if (allConstraintsSatisfied(tmpState)) {
findSolutionDFS(stateListIndex + 1, tmpState);
}
//test value of 4
currentConstraint.setValue(4);
if (allConstraintsSatisfied(tmpState)) {
findSolutionDFS(stateListIndex + 1, tmpState);
}
tmpState.remove(tmpState.size()-1);// remove the last element once it's been checked
return false;
}
/**
* Check constraints. Only check the constraints including variables that have been assigned already.
* @return Boolean whether all constraints have been satisfied
*/
public Boolean allConstraintsSatisfied(List<Constraint> list){
//check which constraints are already contained in the list
int containsA = -1;
int containsB = -1;
int containsC = -1;
int containsD = -1;
int containsE = -1;
int containsF = -1;
int containsG = -1;
int containsH = -1;
//make sure whichever constraints are in the list are satisfied
for (int i = 0; i < list.size(); i++) {
switch(list.get(i).getName()) {
case 'A':
containsA = i;
break;
case 'B':
containsB = i;
break;
case 'C':
containsC = i;
break;
case 'D':
containsD = i;
break;
case 'E':
containsE = i;
break;
case 'F':
containsF = i;
break;
case 'G':
containsG = i;
break;
case 'H':
containsH = i;
break;
}
if (containsA >= 0 && containsG >= 0) {
if (list.get(containsA).getValue() <= list.get(containsG).getValue()) {
numberOfFailedChecks++;
return false;
}
}
if (containsA >= 0 && containsH >= 0) {
if (list.get(containsA).getValue() > list.get(containsH).getValue()) {
numberOfFailedChecks++;
return false;
}
}
if (containsF >= 0 && containsB >= 0) {
if (Math.abs(list.get(containsF).getValue() - list.get(containsB).getValue()) != 1) {
numberOfFailedChecks++;
return false;
}
}
if (containsG >= 0 && containsH >= 0) {
if (list.get(containsG).getValue() >= list.get(containsH).getValue()) {
numberOfFailedChecks++;
return false;
}
}
if (containsG >= 0 && containsC >= 0) {
if (Math.abs(list.get(containsG).getValue() - list.get(containsC).getValue()) != 1) {
numberOfFailedChecks++;
return false;
}
}
if (containsH >= 0 && containsC >= 0) {
if (Math.abs(list.get(containsH).getValue() - list.get(containsC).getValue()) %2 != 0) {
numberOfFailedChecks++;
return false;
}
}
if (containsH >= 0 && containsD >= 0) {
if (list.get(containsH).getValue() == list.get(containsD).getValue()) {
numberOfFailedChecks++;
return false;
}
}
if (containsD >= 0 && containsG >= 0) {
if (list.get(containsD).getValue() < list.get(containsG).getValue()) {
numberOfFailedChecks++;
return false;
}
}
if (containsD >= 0 && containsC >= 0) {
if (list.get(containsD).getValue() == list.get(containsC).getValue()) {
numberOfFailedChecks++;
return false;
}
}
if (containsE >= 0 && containsC >= 0) {
if (list.get(containsE).getValue() == list.get(containsC).getValue()) {
numberOfFailedChecks++;
return false;
}
}
if (containsE >= 0 && containsD >= 0) {
if (list.get(containsE).getValue() >= list.get(containsD).getValue() -1) {
numberOfFailedChecks++;
return false;
}
}
if (containsE >= 0 && containsH >= 0) {
if (list.get(containsE).getValue() == list.get(containsH).getValue() -2) {
numberOfFailedChecks++;
return false;
}
}
if (containsG >= 0 && containsF >= 0) {
if (list.get(containsG).getValue() == list.get(containsF).getValue()) {
numberOfFailedChecks++;
return false;
}
}
if (containsH >= 0 && containsF >= 0) {
if (list.get(containsH).getValue() == list.get(containsF).getValue()) {
numberOfFailedChecks++;
return false;
}
}
if (containsC >= 0 && containsF >= 0) {
if (list.get(containsC).getValue() == list.get(containsF).getValue()) {
numberOfFailedChecks++;
return false;
}
}
if (containsD >= 0 && containsF >= 0) {
if (list.get(containsD).getValue() == list.get(containsF).getValue() -1) {
numberOfFailedChecks++;
return false;
}
}
if (containsE >= 0 && containsF >= 0) {
if (Math.abs(list.get(containsE).getValue() - list.get(containsF).getValue()) %2 != 1) {
numberOfFailedChecks++;
return false;
}
}
}
return true;
}
/**
* prints each variable and its value
* @param list the list of values
*/
void printState(List<Integer> list){
for (int i = 0; i < list.size(); i++) {
System.out.print(ordering[i] + "" +list.get(i) + " ");
}
System.out.println();
}
}