I am attempting to use Google's ORTools in Python to run an optimization. The catch is that the calculations I am trying to optimize have to be accessed through COM, as they are contained in a proprietary piece of external software. Is there any way to accomplish this?
The COM Object accepts integer or float values as inputs, but ORTools passes variables as variable objects, so the COM object is unable to use them. I attempted to get around this by using the solution_value() method, as this was the only method I could find to access variable values, but this issues the following error message and causes the solver to stop working:
"The model has been changed since the solution was last computed. MPSolverInterface:: sync_status_ = 0".
If I just pass the variable to the COM Object, I get the following error:
"Exception has occurred: TypeError. must be real number, not Variable."
See below for my example code:
from ortools.linear_solver import pywraplp
import win32com.client as win32
Program = win32.Dispatch("CalcProgram")
def Calculations(x,y):
Program.SetValue(x)
Program.SetValue(y.solution_value())
return Program.GetValue(Z)
solver = pywraplp.Solver.CreateSolver('SCIP')
x = solver.IntVar(1,25)
y = solver.IntVar(30,60)
solver.Minimize(Calculations(x,y))
status = solver.Solve()
You are missing the point of the linear solver and how to use it.
solver.Minimize() takes a linear expression, that is a python object built with overloaded +, -, * operators and variables and constants.
a valid call could be:
solver.Minimize(x + 2 * y)
Furthermore, solution_value() can only called after a successful Solve().
You should look at python samples to see how programs are structured. See https://github.com/google/or-tools/blob/stable/ortools/linear_solver/samples/simple_mip_program.py