Search in sources :

Example 11 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 12 with BoolVar

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

the class CSpread method precedenceIfOverlap.

/**
 * Establish the precedence constraint {@code c.getEnd() <= d.getStart()} if the two slices may overlap.
 */
private static void precedenceIfOverlap(ReconfigurationProblem rp, Slice d, Slice c) {
    Model csp = rp.getModel();
    // No need to place the constraints if the slices do not have a chance to overlap
    if (!(c.getHoster().isInstantiated() && !d.getHoster().contains(c.getHoster().getValue())) && !(d.getHoster().isInstantiated() && !c.getHoster().contains(d.getHoster().getValue()))) {
        BoolVar eq = csp.boolVar(rp.makeVarLabel(d.getHoster(), "", c.getHoster(), "?"));
        rp.getModel().arithm(d.getHoster(), "=", c.getHoster()).reifyWith(eq);
        org.chocosolver.solver.constraints.Constraint leqCstr = rp.getModel().arithm(c.getEnd(), "<=", d.getStart());
        ChocoUtils.postImplies(rp, eq, leqCstr);
    }
}
Also used : Model(org.chocosolver.solver.Model) BoolVar(org.chocosolver.solver.variables.BoolVar)

Example 13 with BoolVar

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

the class DefaultCumulatives method symmetryBreakingForStayingVMs.

/**
 * Symmetry breaking for VMs that stay running, on the same node.
 *
 * @return {@code true} iff the symmetry breaking does not lead to a problem without solutions
 */
private boolean symmetryBreakingForStayingVMs(ReconfigurationProblem rp) {
    for (VM vm : rp.getFutureRunningVMs()) {
        VMTransition a = rp.getVMAction(vm);
        Slice dSlice = a.getDSlice();
        Slice cSlice = a.getCSlice();
        if (dSlice != null && cSlice != null) {
            BoolVar stay = ((KeepRunningVM) a).isStaying();
            Boolean ret = strictlyDecreasingOrUnchanged(vm);
            if (Boolean.TRUE.equals(ret) && !zeroDuration(rp, stay, cSlice)) {
                return false;
            // Else, the resource usage is decreasing, so
            // we set the cSlice duration to 0 to directly reduces the resource allocation
            } else if (Boolean.FALSE.equals(ret) && !zeroDuration(rp, stay, dSlice)) {
                // (the allocation will be performed at the end of the reconfiguration process)
                return false;
            }
        }
    }
    return true;
}
Also used : Slice(org.btrplace.scheduler.choco.Slice) KeepRunningVM(org.btrplace.scheduler.choco.transition.KeepRunningVM) VM(org.btrplace.model.VM) KeepRunningVM(org.btrplace.scheduler.choco.transition.KeepRunningVM) VMTransition(org.btrplace.scheduler.choco.transition.VMTransition) BoolVar(org.chocosolver.solver.variables.BoolVar)

Example 14 with BoolVar

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

the class IssuesTest method testIssue5a.

/**
 * Another test related to issue #5.
 *
 * @throws org.btrplace.scheduler.SchedulerException
 */
@Test
public void testIssue5a() throws SchedulerException, ContradictionException {
    Model model = new DefaultModel();
    Node n1 = model.newNode();
    Node n2 = model.newNode();
    Node n3 = model.newNode();
    VM vm1 = model.newVM();
    VM vm2 = model.newVM();
    ShareableResource resources = new ShareableResource("vcpu", 1, 1);
    resources.setCapacity(n1, 2);
    resources.setCapacity(n2, 2);
    Mapping map = model.getMapping().on(n1, n2).off(n3).run(n1, vm1, vm2);
    model.attach(resources);
    ReconfigurationProblem rp = new DefaultReconfigurationProblemBuilder(model).build();
    List<IntVar> VMsOnAllNodes = rp.getNbRunningVMs();
    int NUMBER_OF_NODE = map.getAllNodes().size();
    // Each element is the number of VMs on each node
    IntVar[] vmsOnInvolvedNodes = new IntVar[NUMBER_OF_NODE];
    BoolVar[] busy = new BoolVar[NUMBER_OF_NODE];
    rp.getEnd().updateUpperBound(10, Cause.Null);
    int i = 0;
    int maxVMs = rp.getSourceModel().getMapping().getAllVMs().size();
    for (Node n : map.getAllNodes()) {
        vmsOnInvolvedNodes[i] = rp.getModel().intVar("nVMs", -1, maxVMs, true);
        IntVar state = rp.getNodeAction(n).getState();
        // If the node is offline -> the temporary variable is -1, otherwise, it equals the number of VMs on that node
        Constraint elem = rp.getModel().element(vmsOnInvolvedNodes[i], new IntVar[] { rp.getModel().intVar(-1), VMsOnAllNodes.get(rp.getNode(n)) }, state, 0);
        rp.getModel().post(elem);
        // IF the node is online and hosting VMs -> busy = 1.
        busy[i] = rp.getModel().boolVar("busy" + n);
        ChocoUtils.postIfOnlyIf(rp, busy[i], rp.getModel().arithm(vmsOnInvolvedNodes[i], ">=", 1));
        i++;
    }
    // idle is equals the number of vmsOnInvolvedNodes with value 0. (The node without VM)
    IntVar idle = rp.getModel().intVar("Nidles", 0, NUMBER_OF_NODE, true);
    rp.getModel().post(rp.getModel().count(0, vmsOnInvolvedNodes, idle));
    // idle should be less than Amount for MaxSN (0, in this case)
    rp.getModel().post(rp.getModel().arithm(idle, "<=", 0));
    // Extract all the state of the involved nodes (all nodes in this case)
    IntVar[] states = new IntVar[NUMBER_OF_NODE];
    int j = 0;
    for (Node n : map.getAllNodes()) {
        states[j++] = rp.getNodeAction(n).getState();
    }
    // In case the number of VMs is inferior to the number of online nodes, some nodes have to shutdown
    // to satisfy the constraint. This could be express as:
    // The addition of the idle nodes and busy nodes should be equals the number of online nodes.
    IntVar sumStates = rp.getModel().intVar("sumStates", 0, 1000, true);
    rp.getModel().post(rp.getModel().sum(states, "=", sumStates));
    IntVar sumBusy = rp.getModel().intVar("sumBusy", 0, 1000, true);
    rp.getModel().post(rp.getModel().sum(states, "=", sumBusy));
    IntVar sumIB = rp.getModel().intVar("ib", 0, 1000, true);
    @SuppressWarnings("unused") Task task = new Task(sumBusy, idle, sumIB);
    // solver.eq(sumStates, sumIB));
    rp.getModel().post(rp.getModel().arithm(sumStates, "=", sumIB));
    ReconfigurationPlan plan = rp.solve(0, false);
    Assert.assertNotNull(plan);
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) Task(org.chocosolver.solver.variables.Task) OptConstraint(org.btrplace.model.constraint.OptConstraint) Constraint(org.chocosolver.solver.constraints.Constraint) SatConstraint(org.btrplace.model.constraint.SatConstraint) Node(org.btrplace.model.Node) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) Mapping(org.btrplace.model.Mapping) ShareableResource(org.btrplace.model.view.ShareableResource) IntVar(org.chocosolver.solver.variables.IntVar) OptConstraint(org.btrplace.model.constraint.OptConstraint) Constraint(org.chocosolver.solver.constraints.Constraint) SatConstraint(org.btrplace.model.constraint.SatConstraint) BoolVar(org.chocosolver.solver.variables.BoolVar) VM(org.btrplace.model.VM) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) Test(org.testng.annotations.Test)

Example 15 with BoolVar

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

the class FastIFFEqTest method test2.

@Test
public void test2() {
    Model csp = new Model();
    BoolVar b = csp.boolVar("b");
    IntVar x = csp.intVar("x", 0, 3, false);
    int c = 2;
    csp.post(new FastIFFEq(b, x, c));
    Assert.assertEquals(4, 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