MVars and MConstrs in feasRelax
AnsweredUsing the Python API, I would like to use feasRelax on a model using MVars and MConstrs. Following the examples I've seen for Vars and Constrs, here's the type of thing I've been trying.
import gurobipy as gp
from gurobipy import GRB
import numpy as np
m = gp.Model()
x = m.addMVar( 2 )
m.setObjective( x.sum() )
# Set infeasible constraints
A = np.array([
[ 1 ,-1],
[-1 ,-1],
[-0.5, 1]
])
b = np.array([-1,-2,1.5])
c = m.addMConstr( A , x , '>=' , b )
m.feasRelax(
0,
False,
None,
None,
None,
[c],
[1]
)
m.optimize()
print('Optimization status:',m.Status)
print(x.X)
The result in this case is
AttributeError: 'gurobipy.MConstr' object has no attribute '__cindex__'
I've tried quite a few variations, but I'm stumped. I'd rather not have to surrender and set up the slack variables myself ;-)
Thanks for any help!
-
Hi Jonathan,
You can get your code to work using
m.feasRelax(
0,
False,
None,
None,
None,
c.tolist(),
[1]*c.size,
)It may alternatively be worth using feasRelaxS too, which avoids the requirement of specifying which constraints to relax.
- Riley
1 -
Ah, thanks so much! I had tried "list(c)" but I guess tolist() does something fancier.
My actual application is more complicated that this example, and I need to specify the relaxable constraints. feasRelax will be much better than feasRelaxS for me.
0
Please sign in to leave a comment.
Comments
2 comments