-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolver.java
More file actions
96 lines (89 loc) · 3.66 KB
/
Solver.java
File metadata and controls
96 lines (89 loc) · 3.66 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
import java.util.*;
import java.text.SimpleDateFormat;
/** A SOLVER that iterates through all the CONSTRAINTS and tries to satisfy all
* of them. It will repeat until it finds the ORDER that satisfies all CONSTRAINTS
* or until it reaches MAXREPETITIONS iterations without any improvement, whichever
* comes first.
* Returns a SOLVERRESULT object that carries the result of SOLVER.
*/
public class Solver {
/** ArrayList of CONSTRAINTS. */
private ArrayList<Constraint> constraints;
/** HashMap ORDER that will hold WIZARDS and their placement. */
private HashMap<String, Integer> order = new HashMap<String, Integer>();
/** Integer MAXSAT that holds the maximum number of CONSTRAINTS satisfied. */
private int maxSat = 0;
private int removed;
/** Integer MAXREPETITIONS that defines the max number of reptitions SOLVER
* executes without any improvement.
*/
private int maxRepetitions = 10;
/** SimpleDateFormat DATEFORMAT used as timestamps for each MAXSAT improvement. */
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
/** Constructor for SOLVER. Shuffles given order of WIZARDS and CONSTRAINTS. */
public Solver(ArrayList<String> wizards, ArrayList<Constraint> constraintArray, int removed) {
Collections.shuffle(wizards);
for (int i = 0; i < wizards.size(); i++) {
order.put(wizards.get(i), i);
}
this.constraints = constraintArray;
Collections.shuffle(constraints);
this.removed = removed;
}
/** Returns the number of CONSTRAINTS satisfied */
private int numSat() {
int numSatisfied = 0;
for (Constraint c: constraints) {
numSatisfied += c.satisfy(order);
}
return numSatisfied;
}
/** Iterates through each CONSTRAINT once and returns the number satisfied. */
private int solveOnce() {
for (Constraint constraint: constraints) {
int sat = constraint.satisfy(order);
if (sat == 0) {
constraint.swap(order, constraint.getA(), constraint.getC());
int swapA = numSat();
constraint.swap(order, constraint.getC(), constraint.getA());
constraint.swap(order, constraint.getB(), constraint.getC());
int swapB = numSat();
if (swapA > swapB) {
constraint.swap(order, constraint.getC(), constraint.getB());
constraint.swap(order, constraint.getA(), constraint.getC());
}
}
}
return numSat();
}
/** Iterates through each CONSTRAINT MAXREPETITIONS number of times and
* returns the optimal ORDER or the maximum ORDER that satisfies the most
* CONSTRAINTS satisfied if optimal solution was not found.
*/
public SolverResult solve() {
int numSolved = 0;
int repetitions = 0;
int iteration = 1;
boolean completed = false;
while (repetitions < maxRepetitions) {
System.out.println("ITERATION " + Integer.toString(iteration));
numSolved = solveOnce();
if (numSolved <= maxSat) {
repetitions++;
if (repetitions >= maxRepetitions) {
break;
}
} else {
maxSat = numSolved;
repetitions = 0;
System.out.println("SATISFIED " + Integer.toString(maxSat + removed));
}
if (numSolved == constraints.size()) {
completed = true;
break;
}
iteration++;
}
return new SolverResult(completed, numSat(), order);
}
}