You cannot use Java's method "compareTo" directly but rather you should post the implies-constraints as below:
public static void main(String[] args) {
Problem p = ProblemFactory.newProblem("TestCompareTo");
Var x = p.variable("X", 0, 3);
Var y = p.variable("Y", 0, 3);
Var cost = p.variable("Cost", 0, 10);
Constraint more = p.linear(x, ">", y);
Constraint lessOrEqual = p.linear(x, "<=", y);
Constraint minus = p.linear(cost, "=", x.minus(y));
Constraint plus = p.linear(cost, "=", x.plus(y));
more.implies(minus).post();
lessOrEqual.implies(plus).post();
p.log("=== Find solution");
Solver solver = p.getSolver();
Solution solution = solver.findOptimalSolution(Objective.MAXIMIZE, cost);
solution.log();
}
This code produces:
=== Find solution
Solution #1:
X[3] Y[3] Cost[6]
Of course, you may use all possible comparisons of constrained variables x and y.
On Monday, April 29, 2013 6:25:04 AM UTC-4,
wee...@gmail.com wrote:
Dear all,
I am trying to write a simple example as follows:
-----------------------------------------------------------------------------------------------------
Problem p = ProblemFactory.newProblem("SimpleTest");
Var x = p.variable("X", 0, 3);
Var y = p.variable("Y", 0, 3);
Var cost = p.variable("C", 0, 10);
if (x.compareTo(y) == 1) {
cost = x.minus(y);
} else {
cost = x.plus(y);
}
p.log("=== Find solution");
Solver solver = p.getSolver();
Solution solution = solver.findOptimalSolution(Objective.MAXIMIZE, cost);
------------------------------------------------------------------------------------------------------------------------
As you can see, the value of the variable "cost" is dependent on the other two variables "x" and "y".
The objective is to find a solution that maximizes the value of "cost".
The results from running this example is:
=== Find solution
Solution #1:
X[3] Y[0] C[0] [3]
Can anyone tell me whether I am using the function compareTo() properly?
I thought the optimal solution would either be X[2] Y[3], or X[3] Y[2].
In addition, why is the value of "C" shown as 0 in the results, while the value of "cost" is shown as 3?
Thank you in advance for your help.
Wee Lum