El_que_no_duda
El_que_no_duda

Reputation: 417

Can't understand how to run pyomo scripts

Hello I am trying to use Pyomo to run optimization problems. I have found a fair number of tutorials and videos about how to build models, etc. However I can't understand how to actually run the scripts.

Here is a simple piece of code from this tutorial -> video

This is the piece of code from minute 7

from pyomo.environ import *

# creating a model object
model = ConcreteModel()
# declaring the decision variables
model.x = Var(within=NonNegativeReals)
# declaring the objective function
model.maximizeZ = Objective(expr=model.x, sense=maximize)
# declaring the constraints
model.Constraint1=Constraint (expr=model.x <=100) #cant be more than 100
print("helloworld")

Then, if I run that from Spyder, I only get "helloworld" as output due to the print statement. Then the guy on the video opens a command prompt and runs the following command in order to solve the optimization using pyomo:

pyomo solve --solver=glpk hello.py

The name of my file is hello.py and my prompts are in the same directory as the hello.py file. But this does nothing on my pc. I tried using the regular windows prompt and the Anaconda prompt. Windows says that pyomo doesnt exists, and the Anaconda one does not say or do anything. This is the line I run on the command prompts.

Anybody has any idea about what should I do differently?

Upvotes: 0

Views: 717

Answers (1)

AirSquid
AirSquid

Reputation: 11938

There are 2 main ways of working with pyomo models. You can use the command line (as shown above) and the pyomo executable command as you are trying to do. The other way (which I think is preferable) is to embed all of the solving commands into the script and then run it just like any old python program. If you look at some of the answers that I (or others) have posted with the pyomo tag, you will see many examples of the latter method.

The first question (for either method): Do you have a solver installed? There are several freebies out there. Some are easier to install than others.

After that, if you solve within a script, you will have lines like:

solver=SoverFactory('glpk')  # or other legal solver name that you have installed
results = solver.solve(model)
print(results)
model.display()

and then you can just run that as any python file from command line or IDE.

Upvotes: 1

Related Questions