Search in sources :

Example 1 with BoolVar

use of org.chocosolver.solver.variables.BoolVar in project narchy by automenta.

the class AirPlaneLanding method buildModel.

@Override
public void buildModel() {
    data = parse(mData.source());
    n = data.length;
    planes = new IntVar[n];
    tardiness = new IntVar[n];
    earliness = new IntVar[n];
    LLTs = new int[n];
    int obj_ub = 0;
    IntVar ZERO = VariableFactory.fixed(0, solver);
    for (int i = 0; i < n; i++) {
        planes[i] = VariableFactory.bounded("p_" + i, data[i][ELT], data[i][LLT], solver);
        // earliness[i] = VariableFactory.bounded("a_" + i, 0, data[i][TT] - data[i][ELT], solver);
        // tardiness[i] = VariableFactory.bounded("t_" + i, 0, data[i][LLT] - data[i][TT], solver);
        obj_ub += Math.max((data[i][TT] - data[i][ELT]) * data[i][PCBT], (data[i][LLT] - data[i][TT]) * data[i][PCAT]);
        earliness[i] = Max.var(ZERO, VariableFactory.offset(VariableFactory.minus(planes[i]), data[i][TT]));
        tardiness[i] = Max.var(ZERO, VariableFactory.offset(planes[i], -data[i][TT]));
        LLTs[i] = data[i][LLT];
    }
    List<BoolVar> booleans = new ArrayList<BoolVar>();
    // disjunctive
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            BoolVar boolVar = VariableFactory.bool("b_" + i + "_" + j, solver);
            booleans.add(boolVar);
            Constraint c1 = precedence(planes[i], data[i][ST + j], planes[j]);
            Constraint c2 = precedence(planes[j], data[j][ST + i], planes[i]);
            LogicalConstraintFactory.ifThenElse(boolVar, c1, c2);
        }
    }
    bVars = booleans.toArray(new BoolVar[booleans.size()]);
    objective = VariableFactory.bounded("obj", 0, obj_ub, solver);
    // builder cost array
    costLAT = new int[2 * n];
    maxCost = new TObjectIntHashMap<IntVar>();
    for (int i = 0; i < n; i++) {
        costLAT[i] = data[i][PCBT];
        costLAT[n + i] = data[i][PCAT];
        maxCost.put(planes[i], Math.max(data[i][PCBT], data[i][PCAT]));
    }
    // solver.post(Sum.eq(ArrayUtils.append(earliness, tardiness), costLAT, objective, 1, solver));
    IntVar obj_e = VariableFactory.bounded("obj_e", 0, obj_ub, solver);
    solver.post(IntConstraintFactory.scalar(earliness, Arrays.copyOfRange(costLAT, 0, n), obj_e));
    IntVar obj_t = VariableFactory.bounded("obj_t", 0, obj_ub, solver);
    solver.post(IntConstraintFactory.scalar(tardiness, Arrays.copyOfRange(costLAT, n, 2 * n), obj_t));
    solver.post(IntConstraintFactory.sum(new IntVar[] { obj_e, obj_t }, objective));
    solver.post(IntConstraintFactory.alldifferent(planes, "BC"));
}
Also used : Constraint(org.chocosolver.solver.constraints.Constraint) IntVar(org.chocosolver.solver.variables.IntVar) Constraint(org.chocosolver.solver.constraints.Constraint) BoolVar(org.chocosolver.solver.variables.BoolVar)

Example 2 with BoolVar

use of org.chocosolver.solver.variables.BoolVar in project scheduler by btrplace.

the class ChocoUtils method postIfOnlyIf.

/**
 * Make and post a constraint that states and(or(b1, non c2), or(non b1, c2))
 *
 * @param rp the problem to solve
 * @param b1 the first constraint
 * @param c2 the second constraint
 */
public static void postIfOnlyIf(ReconfigurationProblem rp, BoolVar b1, Constraint c2) {
    Model csp = rp.getModel();
    BoolVar notBC1 = b1.not();
    BoolVar bC2 = csp.boolVar(rp.makeVarLabel(c2.toString(), " satisfied"));
    c2.reifyWith(bC2);
    BoolVar notBC2 = bC2.not();
    csp.post(rp.getModel().or(rp.getModel().or(b1, bC2), rp.getModel().or(notBC1, notBC2)));
}
Also used : Model(org.chocosolver.solver.Model) BoolVar(org.chocosolver.solver.variables.BoolVar)

Example 3 with BoolVar

use of org.chocosolver.solver.variables.BoolVar in project scheduler by btrplace.

the class ChocoUtils method postImplies.

/**
 * Make and post an implies constraint where the first operand is a boolean: b1 -&gt; c2.
 * The constraint is translated into (or(not(b1,c2))
 *
 * @param rp the problem to solve
 * @param b1 the first constraint as boolean
 * @param c2 the second constraint
 */
public static void postImplies(ReconfigurationProblem rp, BoolVar b1, Constraint c2) {
    Model s = rp.getModel();
    BoolVar bC2 = s.boolVar(rp.makeVarLabel(c2.toString(), " satisfied"));
    c2.reifyWith(bC2);
    BoolVar notB1 = b1.not();
    s.post(rp.getModel().arithm(b1, "!=", notB1));
    s.post(rp.getModel().or(notB1, bC2));
}
Also used : Model(org.chocosolver.solver.Model) BoolVar(org.chocosolver.solver.variables.BoolVar)

Example 4 with BoolVar

use of org.chocosolver.solver.variables.BoolVar in project scheduler by btrplace.

the class FastIFFEqTest method test4.

@Test
public void test4() {
    Model csp = new Model();
    BoolVar b = csp.boolVar(true);
    IntVar x = csp.intVar("x", 0, 2);
    int c = 3;
    csp.post(new FastIFFEq(b, x, c));
    Assert.assertEquals(0, csp.getSolver().findAllSolutions().size());
}
Also used : Model(org.chocosolver.solver.Model) IntVar(org.chocosolver.solver.variables.IntVar) BoolVar(org.chocosolver.solver.variables.BoolVar) Test(org.testng.annotations.Test)

Example 5 with BoolVar

use of org.chocosolver.solver.variables.BoolVar in project scheduler by btrplace.

the class FastIFFEqTest method test5.

@Test
public void test5() {
    Model csp = new Model();
    BoolVar b = csp.boolVar(true);
    IntVar x = csp.intVar("x", 0, 3);
    int c = 2;
    csp.post(new FastIFFEq(b, x, c));
    Assert.assertEquals(1, csp.getSolver().findAllSolutions().size());
}
Also used : Model(org.chocosolver.solver.Model) IntVar(org.chocosolver.solver.variables.IntVar) BoolVar(org.chocosolver.solver.variables.BoolVar) Test(org.testng.annotations.Test)

Aggregations

BoolVar (org.chocosolver.solver.variables.BoolVar)22 Model (org.chocosolver.solver.Model)17 IntVar (org.chocosolver.solver.variables.IntVar)17 Test (org.testng.annotations.Test)16 VM (org.btrplace.model.VM)4 Constraint (org.chocosolver.solver.constraints.Constraint)4 DefaultModel (org.btrplace.model.DefaultModel)2 Mapping (org.btrplace.model.Mapping)2 Model (org.btrplace.model.Model)2 Node (org.btrplace.model.Node)2 OptConstraint (org.btrplace.model.constraint.OptConstraint)2 SatConstraint (org.btrplace.model.constraint.SatConstraint)2 ReconfigurationPlan (org.btrplace.plan.ReconfigurationPlan)2 Slice (org.btrplace.scheduler.choco.Slice)2 VMTransition (org.btrplace.scheduler.choco.transition.VMTransition)2 ShareableResource (org.btrplace.model.view.ShareableResource)1 CMinMTTR (org.btrplace.scheduler.choco.constraint.mttr.CMinMTTR)1 KeepRunningVM (org.btrplace.scheduler.choco.transition.KeepRunningVM)1 RelocatableVM (org.btrplace.scheduler.choco.transition.RelocatableVM)1 ContradictionException (org.chocosolver.solver.exception.ContradictionException)1