I am trying to define an objective function which uses the current value of a variable, as an integer, for computation (as shown below):
def foo(arg1, arg2, arg3): return arg1 - arg2 + arg3 def objective_that_does_not_work(x0, x1, x2): y = foo(x0.solution_value(), x1.solution_value(), x2.solution_value()) return y def objective_that_works(x0, x1, x2): y = foo(x0, x1, x2) return y
where x0, x1, x2 are IntVars.
In my use case, the function foo is defined in a way that it requires int arguments.
When I try and maximize objective_that_does_not_work
, it randomly gives me either the upper bound or the lower bound for each of x0, x1, x2 (different values each time I run the solver).
My questions:
Is it possible to define the objective in terms of the value of the IntVar, instead of the IntVar itself?
If yes, is
solution_value()
the right attribute to be using? From the documentation, it seemed the most appropriate, but I may be misinterpreting its use.
Thank you
Advertisement
Answer
You can only express the objective as a linear combination of variables.