AttributeError: 'gurobipy.Var' object has no attribute 'X' when model is optimized
AnsweredI am creating a Gurobi model to solve a minimization problem across a series of graphs, minimizing the distance from a node across a sample of graphs.
I have confirmed that the model is optimized, outputting model.Status of 2 and model.SolCount of 1. However, when trying to access the individual variable attributes using the var.X attribute, I get the title attribute error. Within prior debugging, I have seen that the .X attribute does not exist if the model has not yet been optimized, but mine has. Is there another way that I can get these values or fix this error? Thank you!
-
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 Jack,
It will be hard to help without seeing some code. Are you able to share your code via a share drive, or github, or something like Google Colab?
- Riley
0 -
Hi Riley,
The relevant code and files can all be found in the below Github repository:
https://github.com/Jack-Heavey-12/pool_testing/tree/main
The files 'pool.py' and 'lambda_pool.py' are both having the same errors.
Thank you for your help!
0 -
Hi Jack,
I'd guess the issue is that you're creating the model within a function, and after the function is complete Python's garbage collector is removing the model, and consequently solution values.
A couple of solutions:
1) return the model object from the function, and assign it, so that it is not removed, or
2 you can assign the X values to an attribute on the variables, eg
def LinearProgram(graph, set_list, cascades, B=3, overlapping=True):
...
variables = m.getVars()
for v in variables:
v._X = v.X
return x, y, m.objVal, variables
...
def rounding(x_dict):
...
if limit <= x[S]._X:
...- Riley
0 -
Thank you Riley, this did resolve the issue.
0
Post is closed for comments.
Comments
5 comments