Skip to content
Permalink
Browse files

[Perfs] Drastic improvements of performance due to a bug in the inter…

…action with Choco. The factor is only 1.5 now.
  • Loading branch information...
ptal committed May 7, 2019
1 parent 928a998 commit a79ed0a1e41ddc9a16106ce0b0b46c7745b3c7b2
@@ -27,11 +27,10 @@
public class Benchmark
{
private boolean human = true;
private int trials = 10;
private int timeLimitSeconds = 1080;

private List<Integer> paramsNQueens = Arrays.asList(7,8,9,10,11,12,13);//,11,12,13,14);
private List<Integer> paramsGolombRuler = Arrays.asList(10,11);//,10,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);

public static void main(String[] args) {
@@ -57,20 +56,26 @@ private void benchNQueens() {

private void benchGolombRuler() {
for (Integer n : paramsGolombRuler) {
runBonsaiGolombRulerIOLB(n);
runBonsaiInlinedGolombRulerIOLB(n);
}
for (Integer n : paramsGolombRuler) {
runBonsaiGolombRulerFFM(n);
runBonsaiInlinedGolombRulerIOLB(n);
}
for (Integer n : paramsGolombRuler) {
runBonsaiGolombRulerMDLB(n);
runBonsaiGolombRulerIOLB(n);
}
// for (Integer n : paramsGolombRuler) {
// runBonsaiGolombRulerFFM(n);
// }
// for (Integer n : paramsGolombRuler) {
// runBonsaiGolombRulerMDLB(n);
// }
for (Integer n : paramsGolombRuler) {
runChocoGolombRulerIOLB(n);
}
for (Integer n : paramsGolombRuler) {
runChocoGolombRulerFFM(n);
}
// for (Integer n : paramsGolombRuler) {
// runChocoGolombRulerFFM(n);
// }
}

private void benchLatinSquare() {
@@ -90,6 +95,10 @@ private void runBonsaiGolombRulerIOLB(int n) {
runBonsai("GolombRulerIOLB", n, (p) -> p.golombRulerIOLBWithStats(), (p) -> p.golombRulerIOLB());
}

private void runBonsaiInlinedGolombRulerIOLB(int n) {
runBonsai("InlinedGolombRulerIOLB", n, (p) -> p.inlined_golomb_iolb(), (p) -> p.inlined_golomb_iolb());
}

private void runBonsaiGolombRulerFFM(int n) {
runBonsai("GolombRulerFFM", n, (p) -> p.golombRulerFFMWithStats(), (p) -> p.golombRulerFFM());
}
@@ -116,15 +125,6 @@ private void runBonsai(String name, int n,
System.out.println(Config.current.toCSV(human));
return;
}
ArrayList<Long> cumulatedTime = new ArrayList();
for (int i = 0; i < trials; ++i) {
Config.current.time = 0;
Config.current.obj = -1;
machine = new SpaceMachine<>(new Problems(), process, false);
machine.execute();
cumulatedTime.add(Config.current.time);
}
Config.current.time = cumulatedTime.get(cumulatedTime.size()/2);
System.out.println(Config.current.toCSV(human));
}

@@ -137,13 +137,6 @@ private void runChoco(String name, int n, Supplier<Boolean> model) {
System.out.println(Config.current.toCSV(human));
return;
}
ArrayList<Long> cumulatedTime = new ArrayList();
for (int i = 0; i < trials; ++i) {
Config.current.time = 0;
model.get();
cumulatedTime.add(Config.current.time);
}
Config.current.time = cumulatedTime.get(cumulatedTime.size()/2);
System.out.println(Config.current.toCSV(human));
}

@@ -39,7 +39,7 @@ public proc solveIOLB() =
modelChoco(write domains, write constraints);
module Solver solver = new Solver(write domains, write constraints, write consistent);
single_space IntVar x = rulerLengthVar(write domains);
module MinimizeBAB bab = new MinimizeBAB(write constraints, write consistent, write x);
module MinimizeBAB bab = new MinimizeBAB(write domains, write consistent, write x);
par
<> run solver.inputOrderLB()
<> run solver.propagation()
@@ -54,7 +54,7 @@ public proc solveFFM() =
modelChoco(write domains, write constraints);
module Solver solver = new Solver(write domains, write constraints, write consistent);
single_space IntVar x = rulerLengthVar(write domains);
module MinimizeBAB bab = new MinimizeBAB(write constraints, write consistent, write x);
module MinimizeBAB bab = new MinimizeBAB(write domains, write consistent, write x);
par
<> run solver.failFirstMiddle()
<> run solver.propagation()
@@ -69,7 +69,7 @@ public proc solveMDLB() =
modelChoco(write domains, write constraints);
module Solver solver = new Solver(write domains, write constraints, write consistent);
single_space IntVar x = rulerLengthVar(write domains);
module MinimizeBAB bab = new MinimizeBAB(write constraints, write consistent, write x);
module MinimizeBAB bab = new MinimizeBAB(write domains, write consistent, write x);
par
<> run solver.minDomLB()
<> run solver.propagation()
@@ -29,6 +29,11 @@
import org.chocosolver.solver.variables.*;
import org.chocosolver.solver.constraints.nary.alldifferent.*;

import org.chocosolver.solver.search.strategy.selectors.variables.*;
import org.chocosolver.solver.search.strategy.selectors.values.*;
import org.chocosolver.solver.*;
import org.chocosolver.solver.exception.ContradictionException;

public class Problems
{
public proc nqueens() =
@@ -151,7 +156,6 @@ public proc golombRulerMDLBWithStats() =
end
end


private proc abortWhenTimeout() =
single_space long start = currentTime();
flow checkTime(start) end
@@ -166,4 +170,112 @@ public static void checkTime(long start) {
throw new TimeLimitException();
}
}

public proc inlined_golomb_iolb() =
single_space StackLR stack = new StackLR();
universe with stack in
world_line VarStore domains = new VarStore();
world_line ConstraintStore constraints = new ConstraintStore();
single_time ES consistent = unknown;
modelChoco(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
<>
loop
when consistent |= true then
when true |= consistent then
single_space LMin pre_obj = new LMin(x.getLB());
pause;
obj <- pre_obj;
else pause end
else pause end
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
end
end
end
end
end
public static VariableSelector<IntVar> inputOrder(VarStore domains) {
return new InputOrder(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);
return new ES(Kleene.UNKNOWN);
}
catch (ContradictionException c) {
return new ES(Kleene.FALSE);
}
}
private static void modelChoco(VarStore domains, ConstraintStore constraints)
{
int m = Config.current.n;
IntVar[] ticks = new IntVar[m];
IntVar[] diffs = new IntVar[(m*m -m)/2];
Model model = domains.model();
int ub = (m < 31) ? (1 << (m + 1)) - 1 : 9999;
for(int i=0; i < ticks.length; i++) {
ticks[i] = (IntVar) domains.alloc(new VarStore.IntDomain(0, ub, true));
}
for(int i=0; i < diffs.length; i++) {
diffs[i] = (IntVar) domains.alloc(new VarStore.IntDomain(0, ub, true));
}
constraints.join_in_place(model.arithm(ticks[0], "=", 0));
for (int i = 0; i < m - 1; i++) {
constraints.join_in_place(model.arithm(ticks[i + 1], ">", ticks[i]));
}

IntVar[][] m_diffs = new IntVar[m][m];
for (int k = 0, i = 0; i < m - 1; i++) {
for (int j = i + 1; j < m; j++, k++) {
// d[k] is m[j]-m[i] and must be at least sum of first j-i integers
// <cpru 04/03/12> it is worth adding a constraint instead of a view
constraints.join_in_place(model.scalar(new IntVar[]{ticks[j], ticks[i]}, new int[]{1, -1}, "=", diffs[k]));
constraints.join_in_place(model.arithm(diffs[k], ">=", (j - i) * (j - i + 1) / 2));
constraints.join_in_place(model.arithm(diffs[k], "-", ticks[m - 1], "<=", -((m - 1 - j + i) * (m - j + i)) / 2));
constraints.join_in_place(model.arithm(diffs[k], "<=", ticks[m - 1], "-", ((m - 1 - j + i) * (m - j + i)) / 2));
m_diffs[i][j] = diffs[k];
}
}
constraints.join_in_place(model.allDifferent(diffs, "BC"));

// break symmetries
if (m > 2) {
constraints.join_in_place(model.arithm(diffs[0], "<", diffs[diffs.length - 1]));
}
}

private static IntVar rulerLengthVar(VarStore domains) {
int m = Config.current.n;
return (IntVar)domains.model().getVars()[m - 1];
}
}
@@ -86,7 +86,7 @@ public void solve() {
}
}
MeasuresRecorder m = model.getSolver().getMeasures();
Config.current.nodes = m.getBackTrackCount();
Config.current.nodes = m.getDecisionCount() * 2 + 1;
Config.current.solutions = m.getSolutionCount();
Config.current.fails = m.getFailCount();
Config.current.time = m.getTimeCountInNanoSeconds();
@@ -118,6 +118,7 @@ public void buildModel() {
if (m > 2) {
model.arithm(diffs[0], "<", diffs[diffs.length - 1]).post();
}
// model.getSolver().showDecisions();
}

public void configureSearch(boolean isFFM) {
@@ -27,7 +27,7 @@
#[run(WhenT.whenAndTell, "94")]
#[run(WhenT.nonMonotonicTell, "4")]
#[run(WhenT.nonMonotonicTell2, "344")]
// #[run(WhenT.example4_4_thesis, "33")]
#[debug(WhenT.example4_4_thesis, "33")]

package test;

@@ -27,16 +27,14 @@

public class MaximizeBAB
{
ref world_line ConstraintStore constraints;
ref world_line VarStore domains;
ref single_time ES consistent;
ref single_space IntVar x;

single_space LMax obj = bot;
single_space LMax objV = new LMax(0);
world_line LMax conV = new LMax(1);

public MaximizeBAB(ConstraintStore constraints, ES consistent, IntVar x) {
this.constraints = constraints;
public MaximizeBAB(VarStore domains, ES consistent, IntVar x) {
this.domains = domains;
this.consistent = consistent;
this.x = x;
}
@@ -56,9 +54,9 @@ proc maximize() =
end

flow yield_objective() =
consistent <- updateBound(write x, read obj)
consistent <- updateBound(write domains, write x, read obj)

public static ES updateBound(IntVar x, LMax obj) {
public static ES updateBound(VarStore _domains, IntVar x, LMax obj) {
try {
x.updateLowerBound(obj.unwrap() + 1, Cause.Null);
return new ES(Kleene.UNKNOWN);
@@ -27,14 +27,14 @@

public class MinimizeBAB
{
ref world_line ConstraintStore constraints;
ref world_line VarStore domains;
ref single_time ES consistent;
ref single_space IntVar x;

public single_space LMin obj = bot;

public MinimizeBAB(ConstraintStore constraints, ES consistent, IntVar x) {
this.constraints = constraints;
public MinimizeBAB(VarStore domains, ES consistent, IntVar x) {
this.domains = domains;
this.consistent = consistent;
this.x = x;
}
@@ -54,9 +54,9 @@ proc minimize() =
end

flow yield_objective() =
consistent <- updateBound(write x, read obj)
consistent <- updateBound(write domains, write x, read obj)

public static ES updateBound(IntVar x, LMin obj) {
public static ES updateBound(VarStore _domains, IntVar x, LMin obj) {
try {
x.updateUpperBound(obj.unwrap() - 1, Cause.Null);
return new ES(Kleene.UNKNOWN);
@@ -105,6 +105,7 @@ public Kleene propagate(VarStore vstore) {
}
catch (ContradictionException e) {
solver.getEngine().flush();
return Kleene.FALSE;
}
switch (solver.isSatisfied()) {
case TRUE: return Kleene.TRUE;
@@ -116,6 +116,8 @@ public VarStore join(Object value) {

public void join_eq(IntVar x, Integer v) {
try {
// for(int i=0; i < depth; i++) { System.out.print("."); }
// System.out.println(x + "=" + v);
x.instantiateTo(v, Cause.Null);
}
catch (ContradictionException c) {
@@ -125,6 +127,8 @@ public void join_eq(IntVar x, Integer v) {

public void join_neq(IntVar x, Integer v) {
try {
// for(int i=0; i < depth; i++) { System.out.print("."); }
// System.out.println(x + "!=" + v);
x.removeValue(v, Cause.Null);
}
catch (ContradictionException c) {
@@ -263,7 +263,7 @@ impl<'a> ExpressionCompiler<'a>
fn entailment(&mut self, rel: EntailmentRel, vars: &Vec<Variable>) {
match rel.op {
EntailmentKind::StrictEntailment => { panic!("|< is not yet implemented."); }
EntailmentKind::Equality => { panic!("== is not yet implemented."); }
// EntailmentKind::Equality => { panic!("== is not yet implemented."); }
_ => ()
};
self.fmt.push("Cast.toLattice(\"<expr in entailment relation>\",");
@@ -39,8 +39,8 @@ impl Maven {
pub fn delete_source_files(&self) {
let source_path = self.source_path();
if source_path.exists() {
fs::remove_dir_all(self.source_path()).unwrap();
fs::remove_dir_all(self.sandbox.join("target/")).unwrap();
let _ = fs::remove_dir_all(self.source_path());
let _ = fs::remove_dir_all(self.sandbox.join("target/"));
}
}

0 comments on commit a79ed0a

Please sign in to comment.
You can’t perform that action at this time.