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
```
-
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
Please sign in to leave a comment.
Comments
1 comment