use of org.chocosolver.solver.variables.IntVar in project scheduler by btrplace.
the class CNetwork method addLinkConstraints.
/**
* Add the cumulative constraints for each link.
*
* Full-duplex links are considered, two cumulative constraints are defined per link by looking at
* the migration direction for each link on the migration path.
*
* @param rp the reconfiguration problem
*/
private void addLinkConstraints(ReconfigurationProblem rp) {
// Links limitation
List<Task> tasksListUp = new ArrayList<>();
List<Task> tasksListDown = new ArrayList<>();
List<IntVar> heightsListUp = new ArrayList<>();
List<IntVar> heightsListDown = new ArrayList<>();
for (Link l : net.getLinks()) {
for (VM vm : rp.getVMs()) {
VMTransition a = rp.getVMAction(vm);
if (a instanceof RelocatableVM && !a.getDSlice().getHoster().isInstantiatedTo(a.getCSlice().getHoster().getValue())) {
Node src = source.getMapping().getVMLocation(vm);
Node dst = rp.getNode(a.getDSlice().getHoster().getValue());
List<Link> path = net.getRouting().getPath(src, dst);
// Check first if the link is on migration path
if (path.contains(l)) {
// Get link direction
LinkDirection linkDirection = net.getRouting().getLinkDirection(src, dst, l);
// UpLink
if (linkDirection == LinkDirection.UPLINK) {
tasksListUp.add(((RelocatableVM) a).getMigrationTask());
heightsListUp.add(((RelocatableVM) a).getBandwidth());
} else // DownLink
{
tasksListDown.add(((RelocatableVM) a).getMigrationTask());
heightsListDown.add(((RelocatableVM) a).getBandwidth());
}
}
}
}
if (!tasksListUp.isEmpty()) {
// Post the cumulative constraint for the current UpLink
csp.post(csp.cumulative(tasksListUp.toArray(new Task[tasksListUp.size()]), heightsListUp.toArray(new IntVar[heightsListUp.size()]), csp.intVar(l.getCapacity()), true));
tasksListUp.clear();
heightsListUp.clear();
}
if (!tasksListDown.isEmpty()) {
// Post the cumulative constraint for the current DownLink
csp.post(csp.cumulative(tasksListDown.toArray(new Task[tasksListDown.size()]), heightsListDown.toArray(new IntVar[heightsListDown.size()]), csp.intVar(l.getCapacity()), true));
tasksListDown.clear();
heightsListDown.clear();
}
}
}
use of org.chocosolver.solver.variables.IntVar in project scheduler by btrplace.
the class CShareableResource method linkVirtualToPhysicalUsage.
private boolean linkVirtualToPhysicalUsage() throws SchedulerException {
int min = Integer.MAX_VALUE;
// Number of VMs with a 0 usage
int nbZeroes = 0;
for (int vId = 0; vId < vmAllocation.size(); vId++) {
int alloc = vmAllocation.get(vId);
if (alloc > 0) {
min = Math.min(alloc, min);
} else {
nbZeroes++;
}
}
for (int nIdx = 0; nIdx < ratios.size(); nIdx++) {
if (!linkVirtualToPhysicalUsage(nIdx)) {
return false;
}
if (!capHosting(nIdx, min, nbZeroes)) {
return false;
}
}
// The slice scheduling constraint that is necessary
TIntArrayList cUse = new TIntArrayList();
List<IntVar> dUse = new ArrayList<>();
for (VMTransition a : rp.getVMActions()) {
VM vm = a.getVM();
Slice c = a.getCSlice();
Slice d = a.getDSlice();
if (c != null) {
cUse.add(getSourceResource().getConsumption(vm));
}
if (d != null) {
int m = getVMAllocation(rp.getVM(vm));
dUse.add(rp.fixed(m, "vmAllocation('", getResourceIdentifier(), "', '", vm, "'"));
}
}
Cumulatives v = (Cumulatives) rp.getView(Cumulatives.VIEW_ID);
v.addDim(virtRcUsage, cUse.toArray(), dUse.toArray(new IntVar[dUse.size()]));
checkInitialSatisfaction();
return true;
}
use of org.chocosolver.solver.variables.IntVar in project scheduler by btrplace.
the class DefaultAliasedCumulatives method beforeSolve.
/**
* Get the generated constraints.
*
* @return a list of constraint that may be empty.
*/
@Override
public boolean beforeSolve(ReconfigurationProblem r) {
super.beforeSolve(r);
for (int i = 0; i < aliases.size(); i++) {
int capa = capacities.get(i);
int[] alias = aliases.get(i);
int[] cUse = cUsages.get(i);
int[] dUses = new int[dUsages.get(i).length];
for (IntVar dUseDim : dUsages.get(i)) {
dUses[i++] = dUseDim.getLB();
}
r.getModel().post(new AliasedCumulatives(alias, new int[] { capa }, cHosts, new int[][] { cUse }, cEnds, dHosts, new int[][] { dUses }, dStarts, associations));
}
return true;
}
use of org.chocosolver.solver.variables.IntVar in project scheduler by btrplace.
the class DefaultCumulatives method beforeSolve.
/**
* Build the constraint.
*
* @return the resulting constraint
*/
@Override
@SuppressWarnings("squid:S3346")
public boolean beforeSolve(ReconfigurationProblem rp) {
super.beforeSolve(rp);
if (rp.getSourceModel().getMapping().getNbNodes() == 0 || capacities.isEmpty()) {
return true;
}
int nbDims = capacities.size();
int nbRes = capacities.get(0).size();
// We get the UB of the node capacity and the LB for the VM usage.
int[][] capas = new int[nbRes][nbDims];
int d = 0;
for (List<IntVar> capaDim : capacities) {
assert capaDim.size() == nbRes;
for (int j = 0; j < capaDim.size(); j++) {
capas[j][d] = capaDim.get(j).getUB();
}
d++;
}
assert cUsages.size() == nbDims;
int nbCHosts = cUsages.get(0).length;
int[][] cUses = new int[nbCHosts][nbDims];
d = 0;
for (int[] cUseDim : cUsages) {
assert cUseDim.length == nbCHosts;
for (int i = 0; i < nbCHosts; i++) {
cUses[i][d] = cUseDim[i];
}
d++;
}
assert dUsages.size() == nbDims;
int nbDHosts = dUsages.get(0).length;
int[][] dUses = new int[nbDHosts][nbDims];
d = 0;
for (IntVar[] dUseDim : dUsages) {
assert dUseDim.length == nbDHosts;
for (int j = 0; j < nbDHosts; j++) {
dUses[j][d] = dUseDim[j].getLB();
}
d++;
}
symmetryBreakingForStayingVMs(rp);
IntVar[] earlyStarts = rp.getNodeActions().stream().map(NodeTransition::getHostingStart).toArray(IntVar[]::new);
IntVar[] lastEnd = rp.getNodeActions().stream().map(NodeTransition::getHostingEnd).toArray(IntVar[]::new);
rp.getModel().post(new TaskScheduler(earlyStarts, lastEnd, capas, cHosts, cUses, cEnds, dHosts, dUses, dStarts, associations));
return true;
}
use of org.chocosolver.solver.variables.IntVar in project scheduler by btrplace.
the class DefaultReconfigurationProblemTest method testMinimize.
/**
* Test a minimization problem: use the minimum number of nodes.
*
* @throws org.btrplace.scheduler.SchedulerException
*/
@Test
public void testMinimize() throws SchedulerException {
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);
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);
}
Aggregations