0

[I'm using Choco 3.3.3]

I have an IntVar a and an int b. I want to save the difference into an IntVar[] array d. I've done this at another point in the code in exactly the same way and it worked without a problem, but here I just don't get it to work.

d = VF.boundedArray("d", num_ts, -20, 20, solver);
for(int t=0; t < num_ts; t++){
    IntVar a = VF.bounded("a", 0, 10, solver);
    solver.post(ICF.sum(aa[t], a)); //values are [2,2,2,1,2,2]
    int b = bb[t][j]; //values are [2,3,2,2,2,2]
    IntVar c = VF.offset(a, -b);
    ...//see below
}

When I just use c (d[t] = c;), the result when printing out the values of d is [0, -1, 0, -1, 0, 0], but I need the absolut of that, so [0,1,0,1,0,0]

These are the different things that I've tried and the results of d:

d[t] = VF.abs(c); //result [0,0,0,0,0,0]
solver.post(ICF.times(c, -1, d[t])); //result [-7,-6,-7,-7,-7,-7]
solver.post(ICF.sum(new IntVar[]{VF.minus(a), VF.fixed(b, solver)}, d[t])); //result [-7,-6,-7,-7,-7,-7]
solver.post(ICF.arithm(a, "+", d[t], "=", b)); //result [-7,-6,-7,-7,-7,-7]
solver.post(ICF.distance(VF.fixed(b, solver), a, "=", d[t])); //result [-20, -20, -20, -20, -20, -20]

Can anyone tell me what I'm doing wrong? I am especially perplexed as to where [-7,-6,-7,-7,-7,-7] are coming from...

Dan
  • 7,286
  • 6
  • 49
  • 114
Dora
  • 47
  • 1
  • 8

1 Answers1

0

I am not sure I understood what you want to do and what you really tried but my guess is that you did not "solve" the model (or at least you did not propagate the constraints). As shown in the javadoc, variable.getValue() returns the LOWER BOUND in case the variable is not instantiated (it throws an exception when passing -ea to the JVM arguments). So add -ea, make sure you call solver.findSolution() before asking for the variable value and see if its working. If not, please send an executable code so that we can reproduce the problem. Did it solved your problem?

Jean-Guillaume, https://www.cosling.com/

  • When adding -ea I do get an AssertionError with a lot of "FAILURE >> ...." but I don't know what the problem is. I definetely call solver.findOptimalSolution before asking for the variable values. My suspicion is that, because aa in my example is a IntVar[] where only a few entries have constraints. I thought that if an entry in an array doesn't have a constraint the lower bound is just used. I don't see how I can change this. Unfortunately I can't show my complete code publicly and I have not figured out an example that produces the same problem. – Dora Mar 23 '16 at 16:38
  • At another point in my code I have the same problem. I have an IntVar[] and only the first few entries have a constraint, depending on the assignment of other variables. When I make the sum of this array I also get completely random solutions and with -ea I get an AssertionError – Dora Mar 23 '16 at 16:46
  • Can you replace findOptimalSolution with findSolution just to see if it works? – Jean-Guillaume Fages Mar 25 '16 at 08:12
  • Do you specify a search heuristic? If so, you need to pass it all your variables. – Jean-Guillaume Fages Mar 25 '16 at 16:37
  • There is no bug.The problem is that your search heuristic did not cover all variables, so some where not instantiated when the solving process ended. – Jean-Guillaume Fages Apr 08 '16 at 11:29