# Decision Optimization

Â View Only

## If then CPO Java

• #### 1.  If then CPO Java

Posted Mon August 21, 2023 04:17 PM

Hello everyone.

I would like to know how to express the sentence if x = 10 then p + 5 = q. However, I am struggling to accomplish this, specifically, I do not know how to express p - q in Java. Below follows my code:

``````import ilog.cp.*;
import ilog.concert.*;

public class Color {
public static void main(String[] args) {
try {
IloCP cp = new IloCP();

IloIntVar x = cp.intVar(10, 10);
IloIntVar p = cp.intVar(0, 10);
IloIntervalVar q = cp.intervalVar(0, 10);

IloConstraint eq = cp.eq(x, 10);

IloNumExpr ex = p - q; ???????????????????

IloConstraint eq1 = cp.eq(ex, -5);
IloConstraint ifThen = cp.ifThen(eq, eq1);

if (cp.solve()) {
System.out.println("Solved");
} else
System.out.println("Error");

} catch (IloException e) {
System.err.println("Error " + e);
}
}
}
``````

Thanks and regards.

------------------------------
------------------------------

• #### 2.  RE: If then CPO Java

IBM Champion
Posted Mon August 21, 2023 10:42 PM

If p is an integer and q is an interval, I don't think the statement p + 5 = q makes sense. Do you mean that p + 5 should be in the interval given by q (q-start <= p + 5 < q-end)?

------------------------------
Paul Rubin
Professor Emeritus
Michigan State University
------------------------------

• #### 3.  RE: If then CPO Java

Posted Tue August 22, 2023 05:29 AM

Thanks for saving my day once more!

So, if we impose the constraint (q-start <= p + 5 < q-end), the q would have multiple values as solutions, and based on the 4th comment given in this thread:

CP Optimizer can solve problems on integer decision variables only. Floatting points variables are admitted but they must behave as an expression. That is they must be fixed to a value once all the integer variables are fixed. They cannot be decision variables.

That is why I am forcing the fixing over q. For a more detailed description of my intentions, I am trying to model an MTZ constraint, so my end goal would be

if next[i] = j then mtz[j] = mtz[i] + distance[i, j]

Of course we could simply create a variable x[i, j] telling whether the arc between i and j exists, but I am trying to scape from this modeling in order to have a model with less variables.

Case you have any other insight in this regard, please, let me know.

Thanks and BR.

------------------------------
------------------------------

• #### 4.  RE: If then CPO Java

Posted Tue August 22, 2023 06:50 AM

Hello,
it seems that you expect that in CP Optimizer Interval Variables are continuous variables, but they are a different kind of variables, typically used to modelize tasks in scheduling.

In CP, instead of using MTZ constraints as in MIP, one can use a subCircuit constraint: it has been introduced in the last release of CP Optimizer (see https://www.ibm.com/docs/en/icos/22.1.1?topic=2211-new-circuit-subcircuit-constraint) and an example of use is given in the release (for Java, see cpoptimizer/examples/src/java/Cvrptw.java)

------------------------------
Olivier Lhomme
------------------------------

• #### 5.  RE: If then CPO Java

Posted Tue August 22, 2023 07:09 AM

Thank you very much.

However, in my case the MTZ has also another purpose, it is being also used for delimiting the maximum spent time, specifically I am working with a max-profit TSP, So, when I state that if next[i] = j then mtz[j] = mtz[i] + distance[i, j], I am aiming to also impose the constraint

mtz[i] <= salesman_distance_limit, forall i

Let me know case any additional clarificaion is required.

Thanks and regards.

------------------------------
------------------------------

• #### 6.  RE: If then CPO Java

Posted Tue August 22, 2023 10:51 AM

Have a look at the Cvrptw.java example:

the total distance is simply a sum of terms like "distance[i][next[i]]", where "next" is the intExprArray passed to the subCircuit constraint.

Note the useful CP construct: the array distance[i] is indexed by the intExp, distance[i][next[i]]

If your question is about how to force each partial sum to be less than salesman_distance_limit:

just add a constraint in the loop that builds the sum to constrain the current partial sum.

(not needed if you have non-negative distances only)

------------------------------
Olivier Lhomme
------------------------------

• #### 7.  RE: If then CPO Java

Posted Tue August 22, 2023 12:03 PM
Edited by Matheus Andrade Tue August 22, 2023 03:28 PM

Thanks for the attention.

Yes, I already took a look at the mentioned file, however, the script discretizes the floating magnitudes, cf. the lines 217 to 221 of the mentioned file.

``````private IloIntVar[] veh;
private IloIntVar[] startTime;
private IloIntVar used;
private IloIntVar[] prev;``````

Obviously, I could do the same for my case, however I would like to know if it is possible to model a distance limit constraint (salesman_distance_limit) without the discretization, by using CPO. And, without creating variables for the arcs, i.e., keeping the variables number linear wrt the number of nodes.

Precisely, I am trying to express the following reasoning through CPO.

``````...
IloIntVar[] next;
IloIntervalVar[] incurred_distance;
...
IloConstraint null_distance_depot = cp.eq(incurred_distance[depot], 0.0); // Don't think it is semantically right,
// but you get the meaning
...
for (int i : nodes)
for (int j : nodes) {
if (j == depot) continue;

IloConstraint arc_ij = cp.eq(next[i], j);
IloConstraint increment_distance = cp.eq(incurred_distance[j], incurred_distance[i] + DISTANCE_MATRIX[i][j]);

IloConstraint ifThen = cp.ifThen(arc_ij, increment_distance);
}
...
``````

Note that, in the above code, whenever all the integer variables assume some integer value, there will be only one possible assignment of values for the floating variables (the incremental incurred distance at each node), and thus, the statement give at the 4th comment given in this thread, is respected.

Thanks for the attention, and BR.

------------------------------