I am very new to the world of programming and optimization. I would like to know if it is possible to use Pyomo and the Glpk solver on Spyder using the Anaconda GUI?
This is how I downloaded Pyomo and Glpk using the terminal window on a MacBook: conda install -c conda-forge pyomo conda install -c conda-forge glpk
I would like to use Spyder to recreate a simple optimization warehouse problem, but I am running into this error on the console on Spyder:
runfile('/Users/.../Spyder Tutorials and Examples /ch3_concrete_ex.py', wdir='/Users/.../Spyder Tutorials and Examples ')
runfile('/Users/.../Spyder Tutorials and Examples /ch3_concrete_ex.py', wdir='/Users/.../Spyder Tutorials and Examples ')
File "/Users/a.../Spyder Tutorials and Examples /ch3_concrete_ex.py", line 46
pyomo solve --solver=glpk ch3_concrete_ex.py
^
SyntaxError: invalid syntax
My guess is that I need to add Glpk and Pyomo to the environment in Spyder? Can someone please provide some guidance? Should I not being using Anaconda for this?
Below is a copy of my code:
from pyomo.environ import *
model=ConcreteModel(name="(WL)")
N=['Harlingen', 'Memphis']
M=['NYC','LA', "Chicago", 'Houston']
d={('Harlingen', 'NYC'):1956,\
('Harlingen', 'LA'):1606,\
('Harlingen', 'Chicago'): 1410,\
('Harlingen', 'Houston'):330,\
('Memphis', 'NYC'):1096,\
('Memphis', 'LA'):1792,\
('Memphis', 'Chicago'): 1410,\
('Memphis', 'Houston'): 330}
P=2
model.x = Var(N,M, bounds=(0,1))
model.y = Var(N, within=Binary)
def obj_rule(model):
return sum(d[n,m]*model.x[n,m] for n in N for m in M)
model.obj=Objective(rule=obj_rule)
def one_per_cust_rule (model,m):
return sum(model.x[n,m] for n in N)==1
model.one_per_cust_rule= Constraint(M,rule=one_per_cust_rule)
def warehouse_active_rule(model,n,m):
return model.x[n,m] <= model.y[n]
model.warehouse_active_rule= Constraint(N,M, rule=warehouse_active_rule)
def num_warehouses_rule(model):
return sum(model.y[n] for n in N) <=P
model.num_warehouses= Constraint(rule=num_warehouses_rule)
pyomo solve --solver=glpk ch3_concrete_ex.py
any tips would be much appreciated!
You are having problems because you are trying to execute a terminal command within the program. Let me explain... There are several ways to invoke pyomo to solve a model. Pyomo, if installed properly, has a command line interface that you can invoke with the command pyomo
. So this command:
pyomo solve --solver=glpk ch3_concrete_ex.py
Is something you would write from the terminal prompt, not inside of your program.
I find it much easier to invoke all of the solver stuff in the syntax of the program and just run the file like any other python program. Replace the line above with this syntax:
solver = SolverFactory('glpk')
solution = solver.solve(model)
display(model)
and then run your program like any other python program from within spyder or just from the terminal:
% : python ch_3_concrete_ex.py
I just did and it works/solves fine.
I am however surprised that glpk is conda-installable. I remember installing it and it was a big pain on mac. They must have cleaned up the install process. if you are in an academic program and will be doing a lot of optimizing, Gurobi is great and has a free/cheap license.