Search in sources :

Example 26 with IntVar

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

the class CAmong method inject.

@Override
public boolean inject(Parameters ps, ReconfigurationProblem rp) throws SchedulerException {
    int nextGrp = -1;
    int curGrp = -1;
    List<Collection<Node>> groups = new ArrayList<>();
    groups.addAll(cstr.getGroupsOfNodes());
    Set<VM> running = new HashSet<>();
    Mapping src = rp.getSourceModel().getMapping();
    for (VM vm : cstr.getInvolvedVMs()) {
        if (rp.getFutureRunningVMs().contains(vm)) {
            // The VM will be running
            running.add(vm);
            IntVar vAssign = rp.getVMAction(vm).getDSlice().getHoster();
            // If one of the VM is already placed, no need for the constraint, the group will be known
            if (vAssign.isInstantiated()) {
                // Get the group of nodes that match the selected node
                int g = getGroup(rp.getNode(vAssign.getValue()), groups);
                if (errorReported(rp, vm, nextGrp, g)) {
                    return false;
                }
                nextGrp = g;
            }
        }
        if (cstr.isContinuous() && src.isRunning(vm)) {
            // The VM is already running, so we get its current group
            Node curNode = src.getVMLocation(vm);
            int g = getGroup(curNode, groups);
            if (errorReported(rp, vm, curGrp, g)) {
                return false;
            }
            curGrp = g;
        }
    }
    if (cstr.isContinuous() && curGrp != -1) {
        return restrictGroup(ps, rp, running, groups, curGrp);
    } else if (groups.size() == 1) {
        return restrictGroup(ps, rp, running, groups, 0);
    }
    return restrictGroup(ps, rp, running, groups, nextGrp);
}
Also used : VM(org.btrplace.model.VM) Node(org.btrplace.model.Node) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Mapping(org.btrplace.model.Mapping) IntVar(org.chocosolver.solver.variables.IntVar) Constraint(org.chocosolver.solver.constraints.Constraint) HashSet(java.util.HashSet)

Example 27 with IntVar

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

the class CLonely method continuousRestriction.

private static void continuousRestriction(ReconfigurationProblem rp, Collection<VM> vms, Set<VM> otherVMs) {
    // Get the position of all the others c-slices and their associated end moment
    TIntArrayList otherPos = new TIntArrayList();
    TIntArrayList minePos = new TIntArrayList();
    List<IntVar> otherEnds = new ArrayList<>();
    List<IntVar> mineEnds = new ArrayList<>();
    Mapping map = rp.getSourceModel().getMapping();
    for (Node n : map.getOnlineNodes()) {
        for (VM vm : map.getRunningVMs(n)) {
            if (!vms.contains(vm)) {
                otherPos.add(rp.getNode(map.getVMLocation(vm)));
                VMTransition a = rp.getVMAction(vm);
                otherEnds.add(a.getCSlice().getEnd());
            } else {
                minePos.add(rp.getNode(map.getVMLocation(vm)));
                VMTransition a = rp.getVMAction(vm);
                mineEnds.add(a.getCSlice().getEnd());
            }
        }
    }
    for (VM vm : vms) {
        VMTransition a = rp.getVMAction(vm);
        Precedences p = new Precedences(a.getDSlice().getHoster(), a.getDSlice().getStart(), otherPos.toArray(), otherEnds.toArray(new IntVar[otherEnds.size()]));
        rp.getModel().post(p);
    }
    // TODO: The following reveals a model problem. Too many constraints!!
    for (VM vm : otherVMs) {
        VMTransition a = rp.getVMAction(vm);
        Precedences p = new Precedences(a.getDSlice().getHoster(), a.getDSlice().getStart(), minePos.toArray(), mineEnds.toArray(new IntVar[mineEnds.size()]));
        rp.getModel().post(p);
    }
}
Also used : Precedences(org.btrplace.scheduler.choco.extensions.Precedences) Node(org.btrplace.model.Node) VM(org.btrplace.model.VM) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) VMTransition(org.btrplace.scheduler.choco.transition.VMTransition) Mapping(org.btrplace.model.Mapping) IntVar(org.chocosolver.solver.variables.IntVar) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Example 28 with IntVar

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

the class CMaxOnline method inject.

@Override
public boolean inject(Parameters ps, ReconfigurationProblem rp) throws SchedulerException {
    Model csp = rp.getModel();
    if (constraint.getInvolvedNodes().isEmpty()) {
        // The constraint is entailed as it contains no node.
        return true;
    }
    if (constraint.isContinuous()) {
        CPowerView view = (CPowerView) rp.getView(CPowerView.VIEW_ID);
        if (view == null) {
            view = new CPowerView();
            if (!rp.addView(view)) {
                throw new SchedulerModelingException(rp.getSourceModel(), "Unable to attach view '" + CPowerView.VIEW_ID + "'");
            }
            if (!view.inject(ps, rp)) {
                throw new SchedulerModelingException(rp.getSourceModel(), "Unable to inject view '" + CPowerView.VIEW_ID + "'");
            }
        }
        int numberOfTasks = constraint.getInvolvedNodes().size();
        int i = 0;
        int[] nodeIdx = new int[numberOfTasks];
        for (Node n : constraint.getInvolvedNodes()) {
            nodeIdx[i++] = rp.getNode(n);
        }
        IntVar capacity = rp.fixed(constraint.getAmount(), "capacity");
        // The state of the node:
        IntVar[] heights = new IntVar[numberOfTasks];
        IntVar[] starts = new IntVar[numberOfTasks];
        IntVar[] ends = new IntVar[numberOfTasks];
        // Online duration:
        IntVar[] durations = new IntVar[numberOfTasks];
        // Online duration is modeled as a task
        Task[] taskVars = new Task[numberOfTasks];
        for (int idx = 0; idx < nodeIdx.length; idx++) {
            Node n = rp.getNode(nodeIdx[idx]);
            // ---------------GET PowerStart and PowerEnd of the node------------------
            starts[idx] = view.getPowerStart(rp.getNode(n));
            ends[idx] = view.getPowerEnd(rp.getNode(n));
            // ------------------------------------------------------------------------
            durations[idx] = rp.makeUnboundedDuration(rp.makeVarLabel("Dur(", n, ")"));
            csp.post(csp.arithm(durations[idx], "<=", rp.getEnd()));
            // All tasks have to be scheduled
            heights[idx] = csp.intVar(1);
            taskVars[idx] = new Task(starts[idx], durations[idx], ends[idx]);
        }
        csp.post(csp.cumulative(taskVars, heights, capacity, true));
    }
    // Constraint for discrete model
    List<IntVar> nodesState = new ArrayList<>(constraint.getInvolvedNodes().size());
    for (Node ni : constraint.getInvolvedNodes()) {
        nodesState.add(rp.getNodeAction(ni).getState());
    }
    IntVar mySum = csp.intVar(rp.makeVarLabel("nbOnline"), 0, constraint.getAmount(), true);
    csp.post(csp.sum(nodesState.toArray(new IntVar[nodesState.size()]), "=", mySum));
    csp.post(csp.arithm(mySum, "<=", constraint.getAmount()));
    return true;
}
Also used : Task(org.chocosolver.solver.variables.Task) Node(org.btrplace.model.Node) Model(org.chocosolver.solver.Model) ArrayList(java.util.ArrayList) CPowerView(org.btrplace.scheduler.choco.view.CPowerView) IntVar(org.chocosolver.solver.variables.IntVar) SchedulerModelingException(org.btrplace.scheduler.SchedulerModelingException)

Example 29 with IntVar

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

the class CResourceCapacity method injectWithSingleNode.

private boolean injectWithSingleNode(CShareableResource rcm, ReconfigurationProblem rp) {
    int amount = cstr.getAmount();
    Model csp = rp.getModel();
    Node n = cstr.getInvolvedNodes().iterator().next();
    int nIdx = rp.getNode(n);
    IntVar v = rcm.getVirtualUsage().get(nIdx);
    csp.post(csp.arithm(v, "<=", amount));
    // Continuous in practice ?
    if (cstr.isContinuous()) {
        if (cstr.isSatisfied(rp.getSourceModel())) {
            try {
                v.updateUpperBound(cstr.getAmount(), Cause.Null);
            } catch (ContradictionException e) {
                rp.getLogger().error("Unable to restrict to up to " + cstr.getAmount() + ", the maximum '" + rcm.getResourceIdentifier() + "' usage on " + n, e);
                return false;
            }
        } else {
            rp.getLogger().error("The constraint '{}' must be already satisfied to provide a continuous restriction", cstr);
            return false;
        }
    }
    return true;
}
Also used : ContradictionException(org.chocosolver.solver.exception.ContradictionException) Node(org.btrplace.model.Node) Model(org.chocosolver.solver.Model) IntVar(org.chocosolver.solver.variables.IntVar)

Example 30 with IntVar

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

the class CRunningCapacity method filterWithSingleNode.

private boolean filterWithSingleNode(ReconfigurationProblem rp) {
    Node n = cstr.getInvolvedNodes().iterator().next();
    IntVar v = rp.getNbRunningVMs().get(rp.getNode(n));
    Model csp = rp.getModel();
    csp.post(csp.arithm(v, "<=", cstr.getAmount()));
    return !cstr.isContinuous() || injectContinuous(rp);
}
Also used : Node(org.btrplace.model.Node) Model(org.chocosolver.solver.Model) IntVar(org.chocosolver.solver.variables.IntVar)

Aggregations

IntVar (org.chocosolver.solver.variables.IntVar)78 VM (org.btrplace.model.VM)35 Model (org.chocosolver.solver.Model)32 Test (org.testng.annotations.Test)30 Node (org.btrplace.model.Node)29 ArrayList (java.util.ArrayList)23 VMTransition (org.btrplace.scheduler.choco.transition.VMTransition)22 BoolVar (org.chocosolver.solver.variables.BoolVar)17 Mapping (org.btrplace.model.Mapping)16 Model (org.btrplace.model.Model)15 RelocatableVM (org.btrplace.scheduler.choco.transition.RelocatableVM)13 HashSet (java.util.HashSet)11 Slice (org.btrplace.scheduler.choco.Slice)10 DefaultModel (org.btrplace.model.DefaultModel)9 TIntArrayList (gnu.trove.list.array.TIntArrayList)8 ShareableResource (org.btrplace.model.view.ShareableResource)8 ReconfigurationPlan (org.btrplace.plan.ReconfigurationPlan)8 Constraint (org.chocosolver.solver.constraints.Constraint)8 List (java.util.List)7 Set (java.util.Set)7