use of org.chocosolver.solver.constraints.Constraint in project scheduler by btrplace.
the class IssuesTest method testIssue10.
@Test
public void testIssue10() 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();
Mapping map = model.getMapping().on(n1, n2).off(n3).run(n1, vm1, vm2);
// model.attach(resources);
ReconfigurationProblem rp = new DefaultReconfigurationProblemBuilder(model).build();
// n3 goes online
rp.getNodeAction(n3).getState().instantiateTo(1, Cause.Null);
rp.getModel().post(rp.getModel().arithm(rp.getEnd(), "<=", 10));
int NUMBER_OF_NODE = map.getAllNodes().size();
// Extract all the state of the involved nodes (all nodes in this case)
List<IntVar> VMsOnAllNodes = rp.getNbRunningVMs();
// Each element is the number of VMs on each node
IntVar[] vmsOnInvolvedNodes = new IntVar[NUMBER_OF_NODE];
BoolVar[] idles = new BoolVar[NUMBER_OF_NODE];
int i = 0;
int maxVMs = rp.getSourceModel().getMapping().getAllVMs().size();
for (Node n : map.getAllNodes()) {
vmsOnInvolvedNodes[i] = rp.getModel().intVar("nVMs" + n, -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 number of VMs on a node is 0 -> Idle
idles[i] = rp.getModel().boolVar("idle" + n);
ChocoUtils.postIfOnlyIf(rp, idles[i], rp.getModel().arithm(vmsOnInvolvedNodes[i], "=", 0));
i++;
}
IntVar sum = rp.getModel().intVar("sum", 0, 1000, true);
rp.getModel().post(rp.getModel().sum(idles, "=", sum));
// idle should be less than Amount for MaxSN (0, in this case)
rp.getModel().post(rp.getModel().arithm(sum, "=", 0));
System.err.flush();
CMinMTTR obj = new CMinMTTR();
obj.inject(new DefaultParameters(), rp);
ReconfigurationPlan plan = rp.solve(0, false);
Assert.assertNotNull(plan);
}
use of org.chocosolver.solver.constraints.Constraint 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);
}
use of org.chocosolver.solver.constraints.Constraint in project narchy by automenta.
the class AllIntervalSeries method buildModel.
@Override
public void buildModel() {
vars = VariableFactory.enumeratedArray("v", m, 0, m - 1, solver);
dist = new IntVar[m - 1];
if (!use_views) {
dist = VariableFactory.enumeratedArray("dist", m - 1, 1, m - 1, solver);
for (int i = 0; i < m - 1; i++) {
solver.post(IntConstraintFactory.distance(vars[i + 1], vars[i], "=", dist[i]));
}
} else {
for (int i = 0; i < m - 1; i++) {
IntVar k = VariableFactory.bounded(StringUtils.randomName(), -20000, 20000, solver);
solver.post(IntConstraintFactory.sum(new IntVar[] { vars[i], k }, vars[i + 1]));
dist[i] = VariableFactory.abs(k);
solver.post(IntConstraintFactory.member(dist[i], 1, m - 1));
}
}
ALLDIFF = new Constraint[2];
ALLDIFF[0] = (IntConstraintFactory.alldifferent(vars, "BC"));
ALLDIFF[1] = (IntConstraintFactory.alldifferent(dist, "BC"));
solver.post(ALLDIFF);
// break symetries
OTHERS = new Constraint[2];
OTHERS[0] = (IntConstraintFactory.arithm(vars[1], ">", vars[0]));
OTHERS[1] = (IntConstraintFactory.arithm(dist[0], ">", dist[m - 2]));
solver.post(OTHERS);
}
use of org.chocosolver.solver.constraints.Constraint 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"));
}
use of org.chocosolver.solver.constraints.Constraint 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);
}
Aggregations