Skip to content
Advertisement

OR-Tools MIP Solver – Defining an objective in terms of int, instead of IntVar

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:

  1. Is it possible to define the objective in terms of the value of the IntVar, instead of the IntVar itself?

  2. 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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement