Search in sources :

Example 1 with Model

use of org.chocosolver.solver.Model in project scheduler by btrplace.

the class CSpread method inject.

@Override
public boolean inject(Parameters ps, ReconfigurationProblem rp) {
    if (cstr.isContinuous()) {
        Set<Node> usedNodes = new HashSet<>();
        for (VM vm : cstr.getInvolvedVMs()) {
            Node node = rp.getSourceModel().getMapping().getVMLocation(vm);
            if (node != null && !usedNodes.add(node)) {
                rp.getLogger().error("Constraint {} is not satisfied initially", cstr);
                // System.out.println(rp.getSourceModel().getMapping());
                return false;
            }
        }
    }
    List<IntVar> running = placementVariables(rp);
    Model csp = rp.getModel();
    if (running.isEmpty()) {
        return true;
    }
    // The lazy spread implementation for the placement
    csp.post(csp.allDifferent(running.toArray(new IntVar[running.size()]), "AC"));
    if (cstr.isContinuous()) {
        List<VM> vms = new ArrayList<>(cstr.getInvolvedVMs());
        for (int i = 0; i < vms.size(); i++) {
            VM vm = vms.get(i);
            VMTransition aI = rp.getVMAction(vm);
            for (int j = 0; j < i; j++) {
                VM vmJ = vms.get(j);
                VMTransition aJ = rp.getVMAction(vmJ);
                disallowOverlap(rp, aI, aJ);
            }
        }
    }
    return true;
}
Also used : Node(org.btrplace.model.Node) VM(org.btrplace.model.VM) Model(org.chocosolver.solver.Model) ArrayList(java.util.ArrayList) VMTransition(org.btrplace.scheduler.choco.transition.VMTransition) IntVar(org.chocosolver.solver.variables.IntVar) HashSet(java.util.HashSet)

Example 2 with Model

use of org.chocosolver.solver.Model in project scheduler by btrplace.

the class CSerialize method inject.

@Override
public boolean inject(Parameters ps, ReconfigurationProblem rp) throws SchedulerException {
    // Get the solver
    Model csp = rp.getModel();
    // Not enough VMs
    if (ser.getInvolvedVMs().size() < 2) {
        return true;
    }
    // Get all migrations involved
    for (VM vm : ser.getInvolvedVMs()) {
        VMTransition vt = rp.getVMAction(vm);
        if (vt instanceof RelocatableVM) {
            migrationList.add((RelocatableVM) vt);
        }
    }
    // Not enough migrations
    if (migrationList.size() < 2) {
        return true;
    }
    // Using a cumulative
    List<Task> tasks = new ArrayList<>();
    for (RelocatableVM mig : migrationList) {
        tasks.add(new Task(mig.getStart(), mig.getDuration(), mig.getEnd()));
    }
    IntVar[] heights = new IntVar[tasks.size()];
    Arrays.fill(heights, csp.intVar(1));
    csp.post(csp.cumulative(tasks.toArray(new Task[tasks.size()]), heights, csp.intVar(1), true));
    return true;
}
Also used : Task(org.chocosolver.solver.variables.Task) RelocatableVM(org.btrplace.scheduler.choco.transition.RelocatableVM) VM(org.btrplace.model.VM) Model(org.chocosolver.solver.Model) VMTransition(org.btrplace.scheduler.choco.transition.VMTransition) RelocatableVM(org.btrplace.scheduler.choco.transition.RelocatableVM) IntVar(org.chocosolver.solver.variables.IntVar)

Example 3 with Model

use of org.chocosolver.solver.Model in project scheduler by btrplace.

the class ChocoUtils method postImplies.

/**
 * Make and post an implies constraint where the first operand is a boolean: b1 -> 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 Model

use of org.chocosolver.solver.Model 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 5 with Model

use of org.chocosolver.solver.Model in project scheduler by btrplace.

the class DisjointTest method test.

@Test
public void test() {
    IntVar[] g1 = new IntVar[3];
    IntVar[] g2 = new IntVar[3];
    Model s = new Model();
    for (int i = 0; i < g1.length; i++) {
        g1[i] = s.intVar("G1-" + i, 0, (i + 1), false);
        g2[i] = s.intVar("G2-" + i, 0, (i + 1), false);
    }
    s.post(new Disjoint(g1, g2, 4));
    s.post(s.arithm(g2[g2.length - 1], "<=", g1[g1.length - 1]));
    s.getSolver().findAllSolutions();
}
Also used : Model(org.chocosolver.solver.Model) IntVar(org.chocosolver.solver.variables.IntVar) Test(org.testng.annotations.Test)

Aggregations

Model (org.chocosolver.solver.Model)38 IntVar (org.chocosolver.solver.variables.IntVar)32 Test (org.testng.annotations.Test)20 BoolVar (org.chocosolver.solver.variables.BoolVar)17 Node (org.btrplace.model.Node)7 VM (org.btrplace.model.VM)7 ArrayList (java.util.ArrayList)6 VMTransition (org.btrplace.scheduler.choco.transition.VMTransition)4 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 RelocatableVM (org.btrplace.scheduler.choco.transition.RelocatableVM)3 HashSet (java.util.HashSet)2 SchedulerModelingException (org.btrplace.scheduler.SchedulerModelingException)2 Task (org.chocosolver.solver.variables.Task)2 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Among (org.btrplace.model.constraint.Among)1 SplitAmong (org.btrplace.model.constraint.SplitAmong)1 Parameters (org.btrplace.scheduler.choco.Parameters)1