Skip to main content

AttributeError: 'gurobipy.Var' object has no attribute 'X' when model is optimized

Answered

Comments

5 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Riley Clement
    • Gurobi Staff

    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
  • Jack Heavey
    • Gurobi-versary
    • First Comment
    • First Question

    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
  • Riley Clement
    • Gurobi Staff

    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
  • Jack Heavey
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you Riley, this did resolve the issue.

    0

Post is closed for comments.