简体   繁体   中英

How i can get sum of variables in choco solver

I have

IntVar[] orArea = new IntVar[N]; 
IntVar[] orCount = new IntVar[N]; 
IntVar[] orRows = new IntVar[N]; 
IntVar total_trim = model.intVar("trim", 0, 1000);
for (int i = 0; i < N; i++) {
    orRows[i] = model.intVar("or_" + i + "_rows",0, 5);
    orCount[i] = model.intVar("or_" + i + "_count", 0, O[i][2]);
    orArea[i] = model.intScaleView(orCount[i],O[i][0] * O[i][1]);
}

I want get sum of area to IntVar

Something like that:

IntVar totalAreas = orArea.sum();

and continue to use it

IntVar trimToOrder = total_trim.mul(1000000).div(totalAreas).intVar();
model.setObjective(Model.MINIMIZE, trimToOrder);

You can declare a sum constraint like this:

IntVar totalAreas = model.intVar("totalAreas", 0, Stream.of(orArea).mapToInt(IntVar::getUB).sum());
model.sum(orArea, "=", totalAreas).post();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM