Original Message:
Sent: Mon March 18, 2024 10:46 AM
From: Christiane Bracchi
Subject: CP Optimizer: alwaysConstant with OPL and Python
Hello Rustam,
Unfortunately, you've found a bug in the OPL.
We are working to fix it.
We are very sorry for the inconvenience.
In the meantime you can use the following workaround.
range steps1 = 0..4;int k[i in steps1]= i*period_size;... alwaysConstant(my_state_function, k[h], k[h+1]);
I hope this helps.
------------------------------
Christiane Bracchi
Original Message:
Sent: Tue March 12, 2024 09:35 AM
From: Rustam Salikhov
Subject: CP Optimizer: alwaysConstant with OPL and Python
Hi everyone,
I have found the situation when function alwaysConstant is not working with OPL. The problem occurs when constraints using alwaysConstant are enumerated in a loop. However, such problem doesn't occur when modeling with Python.
1 case (OPL with loop)
using CP;int period_size = 50;range steps = 0..3;stateFunction my_state_function;subject to { forall( h in steps ) { alwaysConstant(my_state_function, h * period_size, (h + 1) * period_size); }}execute{ writeln(my_state_function);}
gives
Computation error: an overflow occurred.
2 case (OPL without loop)
using CP;int period_size = 50;range steps = 0..3;stateFunction my_state_function;subject to { alwaysConstant (my_state_function, 0, 50); alwaysConstant (my_state_function, 50, 100); alwaysConstant (my_state_function, 100, 150); alwaysConstant (my_state_function, 150, 200);}execute{ writeln(my_state_function);}
gives
stepwise{ -1 -> 0; 0 -> 200; -1 }
3 case (Python with loop)
import docplex.cp.model as cp_modelif __name__ == "__main__": model = cp_model.CpoModel("alwaysConstant with cycle") period_size = 50 step_max = 3 my_state_function = cp_model.state_function(name="my state function") for h in range(step_max + 1): model.add(cp_model.always_constant(my_state_function, (h * period_size, (h + 1) * period_size))) solution = model.solve(TimeLimit=10, trace_log=False) print(solution.get_var_solution(my_state_function))
gives
my state function: ((-4503599627370494, 0, -1), (0, 50, 0), (50, 100, 0), (100, 150, 0), (150, 200, 0), (200, 4503599627370494, -1))
4 case (Python without loop)
import docplex.cp.model as cp_modelif __name__ == "__main__": model = cp_model.CpoModel("alwaysConstant without cycle") period_size = 50 step_max = 3 my_state_function = cp_model.state_function(name="my state function") model.add(cp_model.always_constant(my_state_function, (0, 50))) model.add(cp_model.always_constant(my_state_function, (50, 100))) model.add(cp_model.always_constant(my_state_function, (100, 150))) model.add(cp_model.always_constant(my_state_function, (150, 200))) solution = model.solve(TimeLimit=10, trace_log=False) print(solution.get_var_solution(my_state_function))
gives
my state function: ((-4503599627370494, 0, -1), (0, 50, 0), (50, 100, 0), (100, 150, 0), (150, 200, 0), (200, 4503599627370494, -1))
------------------------------
Rustam Salikhov
------------------------------