-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllIntervals.java
More file actions
71 lines (51 loc) · 1.89 KB
/
AllIntervals.java
File metadata and controls
71 lines (51 loc) · 1.89 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
import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solver;
import org.chocosolver.solver.variables.IntVar;
/**
* This is a solution for the All Intervals problem from CSPLib
* http://csplib.org/Problems/prob007/
*
* For a given n, there is a set with n integers from 0 to n-1.
* For the set, there is an intervals set with n-1 elements, in which any element contains
* the subtraction of the element in the set and the following number in the set, such that
* it is a permutation of the integers from 1 to n-1.
*/
public class AllIntervals {
private Model model;
private IntVar[] set;
private IntVar[] intervals;
private AllIntervals(int n) {
model = new Model(n + " - All Intervals");
set = model.intVarArray(n, 0, n-1);
intervals = model.intVarArray(n-1, 1, n-1);
for(int i = 0; i < intervals.length-1; i++) {
intervals[i].eq(set[i+1].sub(set[i]).abs()).post();
}
model.allDifferent(set).post();
model.allDifferent(intervals).post();
}
public void run(boolean printAll) {
Solver sv = model.getSolver();
sv.solve();
do {
solveAndPrint(sv);
} while((sv.solve() && printAll));
System.out.print("\n No (more) solutions found after " + sv.getTimeCount() + " secs");
}
private void solveAndPrint(Solver sv) {
System.out.println("\n -- solution " + sv.getSolutionCount() + " :");
for (IntVar aSet : set) {
// Print the set
System.out.print(String.format("%d ", aSet.getValue()));
}
System.out.println();
for (IntVar interval : intervals) {
// Print the intervals
System.out.print(interval.getValue());
}
}
public static void main(String[] args) {
AllIntervals intervals = new AllIntervals(10);
intervals.run(true);
}
}