Skip to content
Permalink
Browse files

[compiler][examples] Fix a bug in the initialization of class fields.…

… Add all the examples presented in the paper submitted to PPDP19.
  • Loading branch information...
ptal committed May 9, 2019
1 parent b29f645 commit 1647951bed141b1856580ae9f16d6bd339f5872c
@@ -40,7 +40,7 @@ public static void main(String[] args) {
private void start() {
Config.timeout = timeLimitSeconds;
System.out.println(Config.headerCSV());
// benchNQueens();
benchNQueens();
// benchLatinSquare();
benchGolombRuler();
}
@@ -0,0 +1,34 @@
// Copyright 2019 Pierre Talbot

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bonsai.examples;

import bonsai.runtime.lattices.*;
import java.lang.System;

public class BoundedDiscrepancy {
world_line LMax dis = new LMax(0);
ref single_space LMax limit;
public BoundedDepth(LMax limit) {
this.limit = limit;
}
public flow bound_dis =
space nothing end;
when dis |= limit then
prune
else
space readwrite dis.inc() end
end
end
}
@@ -32,6 +32,31 @@
end
end

public proc bounded_dis =
module Tree tree = new Tree();
single_space LMax max_depth = new LMax(2);
module BoundedDepth bd = new BoundedDepth(max_depth);
module BoundedDiscrepancy ld = new BoundedDiscrepancy(limit);
par
<> run tree.binary()
<> run ld.bound_dis()
<> run bd.bound_depth()
<> run vizualize()
end
end

public proc bounded_depth_dis =
module Tree tree = new Tree();
module BoundedDepth bd = new BoundedDepth(limit);
module BoundedDiscrepancy ld = new BoundedDiscrepancy(limit);
par
<> run tree.binary()
<> run ld.bound_dis()
<> run bd.bound_depth()
<> run vizualize()
end
end

public proc vizualize =
module Depth viz_depth = new Depth();
par
@@ -20,18 +20,28 @@
import bonsai.runtime.core.*;
import bonsai.runtime.synchronous.*;
import bonsai.runtime.lattices.*;
import bonsai.runtime.lattices.choco.*;

import org.chocosolver.solver.variables.*;
import org.chocosolver.solver.constraints.nary.alldifferent.*;

public class PPDP19
{
public static void main(String[] args) {
section3BoundedDepth();
PPDP19 demo = new PPDP19();
demo.beginMessage();
demo.section3BoundedDepth();
demo.section5CSP();
demo.section5LDS();
demo.section5LDS_IDS();
demo.endMessage();
}

// The code is slightly different than in the paper (which is simplified for clarity).
// We must initialize the field "limit" of the class "BoundedTree" outside of the constructor.
// The interface with "SpaceMachine" takes the module creating the program, and the process constructor separately.
static void section3BoundedDepth() {
System.out.println("\n Section 3. Demo of iterative deepening search as appearing in Section 3.");
void section3BoundedDepth() {
System.out.println("\n Section 3. Demo of iterative deepening search (IDS).");
System.out.println(" =========\n");

for(int limit = 0; limit < 3; limit++) {
@@ -43,4 +53,54 @@ static void section3BoundedDepth() {
machine.execute();
}
}

// We illustrate the section 5.1 with the NQueen CSP.
void section5CSP() {
System.out.println("\n Section 5.1. Demo of CSP state space exploration (3 first solutions of the 8-Queens problem).");
System.out.println(" ===========\n");
Solver solver = new Solver();
StackLR queue = new StackLR();
SpaceMachine<Solver> machine = new SpaceMachine<>(solver, (p) -> p.nqueens(), queue);
for(int i = 0; i < 3; i++) {
machine.execute();
}
}

void section5LDS() {
System.out.println("\n Section 5.2. Demo of limited-discrepancy search (LDS).");
System.out.println(" ===========\n");

for(int limit = 0; limit < 3; limit++) {
System.out.println("Limit = " + limit);
BoundedTree program = new BoundedTree();
program.limit = new LMax(limit);
StackLR queue = new StackLR();
SpaceMachine<BoundedTree> machine = new SpaceMachine<>(program, (p) -> p.bounded_dis(), queue);
machine.execute();
}
}

void section5LDS_IDS() {
System.out.println("\n Section 5.3. Demo of LDS+IDS.");
System.out.println(" ===========\n");

for(int limit = 0; limit < 3; limit++) {
System.out.println("Limit = " + limit);
BoundedTree program = new BoundedTree();
program.limit = new LMax(limit);
StackLR queue = new StackLR();
SpaceMachine<BoundedTree> machine = new SpaceMachine<>(program, (p) -> p.bounded_depth_dis(), queue);
machine.execute();
}
}

void beginMessage() {
System.out.println("\n >>>> Welcome to the demo of the code presented in the paper submitted to PPDP19. <<<<\n");
System.out.println("Note: The tree are drawn as follows: a '*' represents one node, and the spaces before '*' represent the depth of this node.");
System.out.println("For additional strategies including DDS and ILDS, please see libstd/ in the package `bonsai.strategies.*`.\n");
}

void endMessage() {
System.out.println("\n Thanks for watching!");
}
}
@@ -30,13 +30,28 @@
public class Solver
{
single_time ES consistent = unknown;
ref world_line VarStore domains;
ref world_line ConstraintStore constraints;
public world_line VarStore domains = new VarStore();
public world_line ConstraintStore constraints = new ConstraintStore();
single_space ArrayList<IntVar> queens = new ArrayList();

public Solver(VarStore domains, ConstraintStore constraints) {
this.domains = domains;
this.constraints = constraints;
}
public proc nqueens =
modelNQueens(8, write queens, write domains, write constraints);
run search_one_sol()
end

public proc search_one_sol =
par
<> run search()
<> loop
when consistent |= true then
when true |= consistent then
printSolution(queens);
stop;
else pause end
else pause end
end
end
end

public proc search = par run propagation() <> run branch() end

@@ -45,22 +60,59 @@ public Solver(VarStore domains, ConstraintStore constraints) {
when consistent |= true then prune end
end

flow branch =
when unknown |= consistent then
single_time IntVar x = failFirstVar(domains);
single_time Integer v = middleValue(x);
space constraints <- x.le(v) end;
space constraints <- x.gt(v) end
proc branch =
single_space VariableSelector<IntVar> var = firstFail(domains);
single_space IntValueSelector val = middle();
flow
when unknown |= consistent then
single_time IntVar x = failFirstVar(var, domains);
single_time Integer v = middleValue(val, x);
space constraints <- x.le(v) end;
space constraints <- x.gt(v) end
end
end
end
private VariableSelector<IntVar> firstFail(VarStore domains) {
return new FirstFail(domains.model());
}
private IntValueSelector middle() {
return new IntDomainMiddle(true);
}
// Interface to the Choco solver.
private IntVar failFirstVar(VarStore domains) {
VariableSelector<IntVar> var = new FirstFail(domains.model());
private IntVar failFirstVar(VariableSelector<IntVar> var, VarStore domains) {
return var.getVariable(domains.vars());
}
private Integer middleValue(IntVar x) {
IntValueSelector val = new IntDomainMiddle(true);
private Integer middleValue(IntValueSelector val, IntVar x) {
return val.selectValue(x);
}
private void modelNQueens(int n, ArrayList<IntVar> queens, VarStore domains,
ConstraintStore constraints)
{
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"));
for(IntVar v : vars) { queens.add(v); }
}
private void printSolution(ArrayList<IntVar> queens) {
System.out.print("solution: [");
for (IntVar v : queens) {
System.out.print(v.getValue() + ",");
}
System.out.println("]");
}
}
@@ -99,4 +99,8 @@ private ES castToES(String from, Object o) {
}
return (ES) o;
}

public String toString() {
return value.toString();
}
}
@@ -94,14 +94,15 @@ impl<'a> StatementCompiler<'a>
}

fn local_decl_init_expr(&mut self, binding: Binding, is_field: bool) {
let is_single_time = binding.is_single_time();
let ty = Some(binding.ty);
match binding.expr {
Some(expr) =>
if is_field {
if is_field && !is_single_time {
self.fmt.push(&format!("new FunctionCall(Arrays.asList(), (__args) -> {{ return {}; }})", binding.name));
}
else {
compile_functional_expr(self.session, self.context, self.fmt, expr, ty)
compile_functional_expr(self.session, self.context, self.fmt, expr, ty);
},
None => compile_functional_expr(self.session, self.context, self.fmt, Expr::new(DUMMY_SP, ExprKind::Bottom), ty)
}

0 comments on commit 1647951

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