James Watkins
James Watkins

Reputation: 4964

Java Choco CSP optimize using sum of variables

I am using Choco to solve a CSP. I want to do something like this:

BoolVar[] badRow = getBadRow();
solver.findOptimalSolution(ResolutionPolicy.MINIMIZE, sum(badRow));

In other words, i want the system to find a solution where badRow has the fewest true values in it. I don't want this to be a "constraint" because then the system will just fail, and I want to avoid failures. But I cant find any way to create an IntVar as a sum of other variables.

How do I accomplish this?

Upvotes: 2

Views: 1097

Answers (1)

James Watkins
James Watkins

Reputation: 4964

I was taking this from the wrong angle completely. Instead of trying to build a variable as a sum of other variables, I create a plain variable and then Constrain it to be the sum of other variables, like this:

IntVar overflows = VariableFactory.integer("overflows", 0, maxOverflows, solver);
Constraint tracker = IntConstraintFactory.sum(badRow, overflows);
solver.post(tracker);
solver.findAllOptimalSolutions(ResolutionPolicy.MINIMIZE, overflows, true);

And that solved it. I feel silly now for not seeing it.

Upvotes: 2

Related Questions