Danny Kaufman
Danny Kaufman

Reputation: 159

Pyomo/IPOPT: retrieving computed variables

I'm wondering what is the Pyomo-ish way to retrieve solution values (specifically for only the variables calculated by a solver).

In my case, the variable 'x' is indexed by three sets, thus its keys are 3-tuples, e.g. x[CC,N5R_0,ntg]. Before solving, the variable 'x' has 7626 elements. The solver (IPOPT) reports that it is solving a problem with 1339 elements:

Total number of variables............................:     1339

After solving, the instance.x component still has 7626 elements. But when looking at results, I only want to pull out the values for the 1339 computed variables.

I tried just getting the nonzero values, but that does not work if the initial values weren't all zero.

Is there a quick and programmatic way to extract the values for just those 1339 variable components? (besides separate post-parsing of the IPOPT output file?)

For instance, is this information embedded somehow in the model instance object after .solve()?

Thanks

Upvotes: 1

Views: 421

Answers (1)

Bethany Nicholson
Bethany Nicholson

Reputation: 2828

The short answer is yes, every Var component has a stale flag that indicates whether the value of a variable was updated the last time the model was solved. If you call pprint on a Var you will see a 'Stale' column with this value for every index in the variable. You can loop through only non-stale variables using something like:

for v in m.component_data_objects(Var):
    if v.stale:
        continue
    print(str(v), ' = ', value(v))

Upvotes: 1

Related Questions