use of org.chocosolver.solver.variables.IntVar in project scheduler by btrplace.
the class DefaultReconfigurationProblemTest method testTimeout.
/**
* Test the report of a timeout.
*/
@Test(expectedExceptions = { UnstatableProblemException.class })
public void testTimeout() throws UnstatableProblemException {
Model mo = new DefaultModel();
Mapping map = mo.getMapping();
for (int i = 0; i < 10; i++) {
Node n = mo.newNode();
VM vm = mo.newVM();
map.addOnlineNode(n);
map.addRunningVM(vm, n);
}
Parameters ps = new DefaultParameters();
ReconfigurationProblem rp = new DefaultReconfigurationProblemBuilder(mo).setParams(ps).build();
Solver s = rp.getSolver();
IntVar nbNodes = rp.getModel().intVar("nbNodes", 1, map.getAllNodes().size(), true);
Stream<Slice> dSlices = rp.getVMActions().stream().filter(t -> t.getDSlice() != null).map(VMTransition::getDSlice);
IntVar[] hosters = dSlices.map(Slice::getHoster).toArray(IntVar[]::new);
rp.getModel().post(rp.getModel().atMostNValues(hosters, nbNodes, true));
rp.setObjective(true, nbNodes);
// 1 ms
rp.getSolver().limitTime(1);
// -1 will be ignored as it is a negative value (assumed no timeout)
ReconfigurationPlan plan = rp.solve(-1, true);
Assert.assertNotNull(plan);
Assert.assertEquals(s.getMeasures().getSolutionCount(), 1);
Mapping dst = plan.getResult().getMapping();
Assert.assertEquals(usedNodes(dst), 1);
}
use of org.chocosolver.solver.variables.IntVar in project scheduler by btrplace.
the class IssuesTest method testIssue5b.
/**
* Test a suspicious bug in issue #5
*/
@Test
public void testIssue5b() throws SchedulerException {
Model model = new DefaultModel();
Node n1 = model.newNode();
Node n2 = model.newNode();
Node n3 = model.newNode();
VM vm1 = model.newVM();
VM vm2 = model.newVM();
VM vm3 = model.newVM();
VM vm4 = model.newVM();
Mapping map = model.getMapping().on(n1, n2, n3).run(n2, vm1, vm2, vm3, vm4);
ReconfigurationProblem rp = new DefaultReconfigurationProblemBuilder(model).build();
List<IntVar> nodes_state = rp.getNbRunningVMs();
IntVar[] nodeVM = new IntVar[map.getAllNodes().size()];
int i = 0;
for (Node n : map.getAllNodes()) {
nodeVM[i++] = nodes_state.get(rp.getNode(n));
}
IntVar idle = rp.getModel().intVar("Nidles", 0, map.getAllNodes().size(), true);
rp.getModel().post(rp.getModel().count(0, nodeVM, idle));
// Amount of maxSpareNode = 1
rp.getModel().post(rp.getModel().arithm(idle, "<=", 1));
ReconfigurationPlan plan = rp.solve(0, false);
Assert.assertNotNull(plan);
}
use of org.chocosolver.solver.variables.IntVar 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);
}
use of org.chocosolver.solver.variables.IntVar in project scheduler by btrplace.
the class SliceTest method testInstantiation.
/**
* It's a container so we only tests the instantiation and the getters.
*/
@Test
public void testInstantiation() {
Model mo = new DefaultModel();
VM vm1 = mo.newVM();
org.chocosolver.solver.Model m = new org.chocosolver.solver.Model("");
IntVar st = m.intVar("start", 1);
IntVar ed = m.intVar("end", 3);
IntVar duration = m.intVar("duration", 2);
IntVar hoster = m.intVar("hoster", 4);
Slice sl = new Slice(vm1, st, ed, duration, hoster);
Assert.assertEquals(vm1, sl.getSubject());
Assert.assertEquals(st, sl.getStart());
Assert.assertEquals(ed, sl.getEnd());
Assert.assertEquals(hoster, sl.getHoster());
Assert.assertEquals(duration, sl.getDuration());
Assert.assertFalse(sl.toString().contains("null"));
duration = m.intVar("duration", 3, 5, true);
sl = new Slice(vm1, st, ed, duration, hoster);
Assert.assertFalse(sl.toString().contains("null"));
}
use of org.chocosolver.solver.variables.IntVar in project scheduler by btrplace.
the class DisjointTest method disjointMultipleTest.
@Test
public void disjointMultipleTest() {
IntVar[][] groups = new IntVar[3][3];
Model s = new Model();
for (int g = 0; g < groups.length; g++) {
for (int i = 0; i < groups[g].length; i++) {
groups[g][i] = s.intVar("G" + g + "-" + i, 0, 2, false);
}
}
s.post(new DisjointMultiple(groups, 3));
for (int g = 1; g < groups.length; g++) {
s.post(s.arithm(groups[g - 1][2], "<=", groups[g][2]));
}
// SMF.log(s, true, true);
// SMF.logContradiction(s);
s.getSolver().findAllSolutions();
}
Aggregations