Generate a nice output file

1,557 views
Skip to first unread message

Johannes Bethcke

unread,
Nov 2, 2015, 6:48:42 AM11/2/15
to Pyomo Forum
Hello:)

I'm using pyomo and Couenne to solve MINLP. How would you

 - specify a location and name for the solver output file (.log)?
 - generate human-readable file containing the actual variable values of a solution? (As a "instance.display() to file")?

Greetings from Darmstadt,

Johannes

Gabriel Hackebeil

unread,
Nov 2, 2015, 7:01:16 AM11/2/15
to pyomo...@googlegroups.com
 - specify a location and name for the solver output file (.log)?

opt.solve(instance, logfile=“logfile_name.log”)

Though if you just want to stream the solver output you can do

opt.solve(instance, tee=True)

 - generate human-readable file containing the actual variable values of a solution? (As a "instance.display() to file”)?

The easiest thing to do would be to create this file manually. This would look something like

with open(‘results.txt’, ‘w’) as f:
   for var in instance.component_data_objects(Var):
      f.write(‘%s: %s\n’ % (var, var.value))

Gabe

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Siirola, John D

unread,
Nov 2, 2015, 7:13:23 AM11/2/15
to pyomo...@googlegroups.com
One addition to Gabe's solution: if you have large index sets, "blindly" converting a component data to a string can be very slow. If you find that it takes a long time to output the model results, you can manage the conversion from data to name explicitly:

buffer = {}

with open(‘results.txt’, ‘w’) as f:
   for var in instance.component_data_objects(Var):
      f.write(‘%s: %s\n’ % (var.came(fully_qualified=True, name_buffer=buffer), var.value))

John

Johannes Bethcke

unread,
Nov 2, 2015, 8:45:12 AM11/2/15
to Pyomo Forum
Dear Gabe and John,

Thank you for your anwers. That did the trick! Do you know an easy way to format the printed varibles values nicely? Just some alignment and indention. Similar to let's say:

    pumpOperating : Size=2, Index=pumpOperating_index, Domain=Binary
        Key                            : Lower : Value : Upper : Fixed : Stale
        (1, 'storageTank', 'junction') :     0 :   1.0 :     1 : False : False
        (2, 'storageTank', 'junction') :     0 :   1.0 :     1 : False : False
    rotationalSpeed : Size=2, Index=rotationalSpeed_index, Domain=Reals
        Key                            : Lower : Value             : Upper : Fixed : Stale
        (1, 'storageTank', 'junction') :     0 : 60.30087959861426 :   100 : False : False
        (2, 'storageTank', 'junction') :     0 : 43.07654652028928 :   100 : False : False
    pressureIncrease : Size=2, Index=pressureIncrease_index, Domain=NonNegativeReals
        Key                            : Lower : Value : Upper : Fixed : Stale
        (1, 'storageTank', 'junction') :     0 :  10.0 :  None : False : False
        (2, 'storageTank', 'junction') :     0 :  50.0 :  None : False : False

The copy & paste didn't work perfectly here.

Johannes

Gabriel Hackebeil

unread,
Nov 2, 2015, 4:25:11 PM11/2/15
to pyomo...@googlegroups.com
If you want to send the exact output from pprint to a file (for StringIO object), you can specify that using the ‘ostream’ keyword. E.g.,

with open(‘something.txt’, ‘w’) as f:
    model.y.pprint(ostream=f)

Python also provides you with a number of modules for serialization of data structures (e.g., yaml, json). There are many options here, but as an example, you could easily populate your own dictionary and serialize this to a file using yaml. Not only would this file be human readable, but you could load it back into memory without writing another parser.

Gabe

Johannes Bethcke

unread,
Nov 10, 2015, 5:25:56 AM11/10/15
to Pyomo Forum
Thanks for your answer. The following code produces the output I was looking for:


with open(‘something.txt’, ‘w’) as f:
    f.write("My description of the instance!\n")
    instance.display(ostream=f)

Johannes
Reply all
Reply to author
Forward
0 new messages