Skip to content

Instantly share code, notes, and snippets.

@tudang
Created April 4, 2013 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tudang/5309687 to your computer and use it in GitHub Desktop.
Save tudang/5309687 to your computer and use it in GitHub Desktop.
@Test
public void testWeird3() throws SolverException {
ShareableResource resources = new ShareableResource("vcpu", 1);
resources.set(n1, 2);
resources.set(n2, 2);
Mapping map = new MappingBuilder().on(n1, n2).off(n3).run(n1, vm1, vm2).build();
Model model = new DefaultModel(map);
model.attach(resources);
ChocoLogging.setVerbosity(Verbosity.SEARCH);
ReconfigurationProblem rp = new DefaultReconfigurationProblemBuilder(model)
.labelVariables()
.build();
CPSolver solver = rp.getSolver();
IntDomainVar[] VMsOnAllNodes = rp.getNbRunningVMs();
int NUMBER_OF_NODE = map.getAllNodes().size();
// Each element is the number of VMs on each node
IntDomainVar[] vmsOnInvolvedNodes = new IntDomainVar[NUMBER_OF_NODE];
IntDomainVar[] busy = new IntDomainVar[NUMBER_OF_NODE];
int i = 0;
int maxVMs = rp.getSourceModel().getMapping().getAllVMs().size();
for (UUID n : map.getAllNodes()) {
vmsOnInvolvedNodes[i] = solver.createBoundIntVar("nVMs", 0, maxVMs);
IntDomainVar 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
IntDomainVar[] c = new IntDomainVar[]{solver.makeConstantIntVar(-1), VMsOnAllNodes[rp.getNode(n)],
state, vmsOnInvolvedNodes[i]};
solver.post(new ElementV(c, 0, solver.getEnvironment()));
// IF the node is online and hosting VMs -> busy = 1.
busy[i] = solver.createBooleanVar("busy" + n);
postIfOnlyIf(solver, busy[i], solver.geq(vmsOnInvolvedNodes[i], 1));
i++;
}
// idle is equals the number of vmsOnInvolvedNodes with value 0. (The node without VM)
IntDomainVar idle = solver.createBoundIntVar("Nidles", 0, NUMBER_OF_NODE);
solver.post(solver.occurence(vmsOnInvolvedNodes, idle, 0));
// idle should be less than Amount for MaxSN (0, in this case)
solver.post(solver.leq(idle, 0));
// Extract all the state of the involved nodes (all nodes in this case)
IntDomainVar[] states = new IntDomainVar[NUMBER_OF_NODE];
int j=0;
for (UUID 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.
IntExp sumStates = (solver.sum(states));
IntExp sumIB = solver.plus(solver.sum(busy), idle);
solver.post(solver.eq(sumStates, sumIB));
ReconfigurationPlan plan = rp.solve(0, false);
Assert.assertNotNull(plan);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment