I have OR-Tools running in a Python on Linux Azure Function.
I'd like to make this an API end point where I can post a JSON representation of an Integer Programming problem and receive a solution back.
# Grab the POST data as an object
req_json = req.get_json()
# Create the MIP solver BOP/SAT/CBC
solver = pywraplp.Solver.CreateSolver('BOP')
# Create the variables
x = {}
for i in range(len(req_json['Variables'])):
x[i] = solver.IntVar(0, req_json['Variables'][i]['UpperBound'], req_json['Variables'][i]['Name'])
print('Number of variables =', solver.NumVariables())
# Create the constraints
for j in range(len(req_json['Constraints'])):
solver.Add(req_json['Constraints'][j]['Formula'])
print('Number of constraints =', solver.NumConstraints())
However it seems like solver.Add()
does not support a string representation of the formula, it's expecting an object like solver.Add(x + y < 5)
, so is there another way to do this?
My variables and constraint formulas are dynamic and will be provided by another system. I've had a look at using Arrays to define the model instead, but my formulas are quite complex and I'm not sure I'd be able to reduce them to an array of constraint coefficients.