setting custom matrix constraints
I've been trying to create a MIP that would optimize the trajectory of multiple agents. In order to come up with these trajectories, I needed to calculate velocities from actions and constrain those velocities by the variable max_v. So basically, I'm converting accelerations to velocities.
For each agent and axis(x,y) I created a variable of type MVar with shape T (timesteps)
I have a function called compute_v which takes in accelerations and spits out the velocity at every timestep using a cumulative sum.
def compute_v(actions):
return np.cumsum(actions)*delta_t
then I loop over each agent and each axis and try to execute the following code
m.AddConstrs(compute_v(axis_actions) <= np.ones(T)*max_v)
However, that gives me gurobipy.GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
If I do AddConstr and pass in compute_v(axis_actions)[0] for the left hand side, it doesn't work because I think it only adds the first constraint.
Finally I tried converting the cumsum into a matrix and tried addmconstrs
m.addMConstrs(np.tril(np.ones((T, T))), axis_actions, "<=",np.ones(T) * max_v)
And I think this worked but in the future I might be making more complicated functions and I don't want to come up with a matrix form every time. Is there a way to come up with a cleaner way to do this using AddConstr? Can someone explain the index error I get? If that doesn't work, is there a way to get the A matrix of a Gurobi linear expression?
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Can you post the code you use to define \( \texttt{delta_t} \), \( \texttt{axis_actions} \), and \(\texttt{max_v}\)?
0
Post is closed for comments.
Comments
2 comments