Round all the elements in a gurobi MVar
AnsweredI'm trying to set a gurobi MVar to a list of integers. In particular, in the code below
model.addConstr(d1_dtheta == (d1 @ dtheta))
d1_dtheta is an MVar, d1 is a numpy matrix and dtheta is a gurobi MVar. Later, I try to constraint d1_dtheta to a predefined list of integers but I can't seem to figure out a way to do this. The result of d1_dtheta is an MVar of floating point numbers that I wish to round to the nearest integer. It was my understanding that MVars are just numpy ndarray so I thought np.around but it keeps giving me syntax issues. Is there an appropriate way to round elements in an MVar?
0
-
Official comment
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Rahul,
Please see the following example
import numpy as np
m = gp.Model()
x = m.addMVar((5,5))
x_up = m.addMVar((5,5), vtype="I")
x_nearest = m.addMVar((5,5), vtype="I")
x_down = m.addMVar((5,5), vtype="I")
m.addConstr(x == np.random.randint(0,1000, (5,5))/100)
epsilon = 1e-5
# round up
m.addConstr(x <= x_up)
m.addConstr(x_up - 1 + epsilon <= x)
# round nearest
m.addConstr(x - 0.5 + epsilon <= x_nearest)
m.addConstr(x_nearest - 0.5 <= x)
# round down
m.addConstr(x <= x_down + 1 - epsilon)
m.addConstr(x_down <= x)
m.optimize()
print("x\n", x.X)
print("x rounded up\n", x_up.X)
print("x rounded nearest\n", x_nearest.X)
print("x rounded down\n", x_down.X)Does this make sense?
- Riley
0
Post is closed for comments.
Comments
2 comments