Skip to content

Commit

Permalink
[benchmark] Add an inlined version of NQueens.
Browse files Browse the repository at this point in the history
  • Loading branch information
ptal committed May 9, 2019
1 parent 1647951 commit 4df3e18
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 22 deletions.
31 changes: 19 additions & 12 deletions benchmark/src/main/java/benchmark/Benchmark.java
Expand Up @@ -29,9 +29,9 @@ public class Benchmark
private boolean human = true;
private int timeLimitSeconds = 1080;

private List<Integer> paramsNQueens = Arrays.asList(7,8,9,10,11,12,13);//,11,12,13,14);
private List<Integer> paramsNQueens = Arrays.asList(7,8,9,10,11);//,11,12,13,14);
private List<Integer> paramsGolombRuler = Arrays.asList(7,8,9,10,11);//,10,11,12,13,14);
private List<Integer> paramsLatinSquare = Arrays.asList(30,35,40,45,50,55,60,65,70,75,80);
private List<Integer> paramsLatinSquare = Arrays.asList(30,35,40,45,50,55);

public static void main(String[] args) {
new Benchmark().start();
Expand All @@ -40,7 +40,10 @@ public static void main(String[] args) {
private void start() {
Config.timeout = timeLimitSeconds;
System.out.println(Config.headerCSV());
benchNQueens();
// WARM-UP
// runChocoNQueens(14);
// Benches
// benchNQueens();
// benchLatinSquare();
benchGolombRuler();
}
Expand All @@ -49,18 +52,18 @@ private void benchNQueens() {
for (Integer n : paramsNQueens) {
runBonsaiNQueens(n);
}
for (Integer n : paramsNQueens) {
runBonsaiInlinedNQueens(n);
}
for (Integer n : paramsNQueens) {
runChocoNQueens(n);
}
}

private void benchGolombRuler() {
for (Integer n : paramsGolombRuler) {
runBonsaiInlinedGolombRulerIOLB(n);
}
for (Integer n : paramsGolombRuler) {
runBonsaiInlinedGolombRulerIOLB(n);
}
// for (Integer n : paramsGolombRuler) {
// runBonsaiInlinedGolombRulerIOLB(n);
// }
for (Integer n : paramsGolombRuler) {
runBonsaiGolombRulerIOLB(n);
}
Expand All @@ -70,9 +73,9 @@ private void benchGolombRuler() {
// for (Integer n : paramsGolombRuler) {
// runBonsaiGolombRulerMDLB(n);
// }
for (Integer n : paramsGolombRuler) {
runChocoGolombRulerIOLB(n);
}
// for (Integer n : paramsGolombRuler) {
// runChocoGolombRulerIOLB(n);
// }
// for (Integer n : paramsGolombRuler) {
// runChocoGolombRulerFFM(n);
// }
Expand All @@ -91,6 +94,10 @@ private void runBonsaiNQueens(int n) {
runBonsai("NQueens", n, (p) -> p.nqueensWithStats(), (p) -> p.nqueens());
}

private void runBonsaiInlinedNQueens(int n) {
runBonsai("InlinedNQueens", n, (p) -> p.inlined_nqueens(), (p) -> p.inlined_nqueens());
}

private void runBonsaiGolombRulerIOLB(int n) {
runBonsai("GolombRulerIOLB", n, (p) -> p.golombRulerIOLBWithStats(), (p) -> p.golombRulerIOLB());
}
Expand Down
Expand Up @@ -44,7 +44,7 @@ public proc solveIOLB() =
<> run solver.inputOrderLB()
<> run solver.propagation()
<> flow when consistent |= true then
when true |= consistent then updateBound(domains) end end
when true |= consistent then updateBound(domains); end end
end
<> run bab.solve();
end
Expand Down
93 changes: 84 additions & 9 deletions benchmark/src/main/java/benchmark/bonsai/Problems.bonsai.java
Expand Up @@ -174,26 +174,31 @@ public static void checkTime(long start) {
public proc inlined_golomb_iolb() =
single_space StackLR stack = new StackLR();
universe with stack in
single_space LMax nodes = new LMax(0);
single_space LMax fails = new LMax(0);
single_space LMax solutions = new LMax(0);
single_space long start = currentTime();
world_line VarStore domains = new VarStore();
world_line ConstraintStore constraints = new ConstraintStore();
single_time ES consistent = unknown;
modelChoco(write domains, write constraints);
modelGolombRuler(write domains, write constraints);
single_space IntVar x = rulerLengthVar(write domains);
single_space LMin obj = bot;
single_space VariableSelector<IntVar> var = inputOrder(write domains);
single_space IntValueSelector val = min();
par
single_space long start = currentTime();
flow checkTime(start) end
<>
flow consistent <- updateBound(write domains, write x, read obj) end
<>
flow consistent <- constraints.propagate(readwrite domains) end
flow
readwrite nodes.inc();
consistent <- constraints.propagate(readwrite domains)
end
<>
loop
when consistent |= true then
when true |= consistent then
single_space LMin pre_obj = new LMin(x.getLB());
readwrite solutions.inc();
pause;
obj <- pre_obj;
else pause end
Expand All @@ -208,22 +213,37 @@ flow checkTime(start) end
space readwrite domains.join_neq(y, v) end
end
end
<>
flow
when consistent |= false then
readwrite fails.inc();
updateStats(start, nodes, fails, solutions)
end
end
end
end
end

private static void updateStats(long start, LMax nodes, LMax fails, LMax solutions) {
Config.current.nodes = nodes.unwrap();
Config.current.fails = fails.unwrap();
Config.current.solutions = solutions.unwrap();
checkTime(start);
}

public static VariableSelector<IntVar> inputOrder(VarStore domains) {
return new InputOrder(domains.model());
}

public static VariableSelector<IntVar> firstFail(VarStore domains) {
return new FirstFail(domains.model());
}

public static IntValueSelector min() {
return new IntDomainMin();
}

public static ES updateBound(VarStore _domains, IntVar x, LMin obj) {
Config.current.nodes++;
Config.current.fails++;
Config.current.solutions++;
Config.current.obj = obj.unwrap();
try {
x.updateUpperBound(obj.unwrap() - 1, Cause.Null);
Expand All @@ -234,7 +254,7 @@ public static ES updateBound(VarStore _domains, IntVar x, LMin obj) {
}
}

private static void modelChoco(VarStore domains, ConstraintStore constraints)
private static void modelGolombRuler(VarStore domains, ConstraintStore constraints)
{
int m = Config.current.n;
IntVar[] ticks = new IntVar[m];
Expand Down Expand Up @@ -278,4 +298,59 @@ private static IntVar rulerLengthVar(VarStore domains) {
int m = Config.current.n;
return (IntVar)domains.model().getVars()[m - 1];
}

public proc inlined_nqueens() =
single_space StackLR stack = new StackLR();
universe with stack in
single_space LMax nodes = new LMax(0);
single_space LMax fails = new LMax(0);
single_space LMax solutions = new LMax(0);
single_space long start = currentTime();
world_line VarStore domains = new VarStore();
world_line ConstraintStore constraints = new ConstraintStore();
single_time ES consistent = unknown;
modelNQueens(write domains, write constraints);
single_space VariableSelector<IntVar> var = firstFail(write domains);
single_space IntValueSelector val = min();
par
flow
readwrite nodes.inc();
consistent <- constraints.propagate(readwrite domains)
end
<>
flow
when unknown |= consistent then
single_time IntVar y = readwrite var.getVariable(domains.vars());
single_time Integer v = readwrite val.selectValue(y);
space readwrite domains.join_eq(y, v) end;
space readwrite domains.join_neq(y, v) end
else
when consistent |= false then
readwrite fails.inc();
else
readwrite solutions.inc();
end;
updateStats(start, nodes, fails, solutions);
end
end
end
end
end

private static void modelNQueens(VarStore domains,
ConstraintStore constraints)
{
int n = Config.current.n;
IntVar[] vars = new IntVar[n];
IntVar[] diag1 = new IntVar[n];
IntVar[] diag2 = new IntVar[n];
for(int i = 0; i < n; i++) {
vars[i] = (IntVar) domains.alloc(new VarStore.IntDomain(1, n, false));
diag1[i] = domains.model().intOffsetView(vars[i], i);
diag2[i] = domains.model().intOffsetView(vars[i], -i);
}
constraints.join_in_place(new AllDifferent(vars, "BC"));
constraints.join_in_place(new AllDifferent(diag1, "BC"));
constraints.join_in_place(new AllDifferent(diag2, "BC"));
}
}

0 comments on commit 4df3e18

Please sign in to comment.