Extract gurobi 3-dimensional variable value to a ndarray?
AnsweredI have gruobio 3d-variables defined as:
self.r = self.model.addVars(self.noCells, self.noHours, self.noM, vtype=GRB.BINARY, name='Renew_Charging')
need to retrieve the optimal values after optimization.
Tried code as below, the dimension is not correct.
```
Var = np.ndarray((self.noCells, self.noHours, self.noM))
for m in range(self.noM):
for i in range(self.noCells):
for j in range(self.noHours):
names_to_retrieve = f"Renew_Charging[{i},{j},{m}]"
Var[i, j, m] = self.model.getVarByName(names_to_retrieve).X
```
-
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 Scott,
The easiest way to get the solution values and access them in the same way as the variables is via:
values = model.getAttr("X", self.model.getVars())The method returns a dictionary with the same keys as the variables. This should also be much faster than using getVarByName() for each variable.
Best regards,
Mario0
Post is closed for comments.
Comments
2 comments