1

I am new to Pyomo and I have the following optimization problem (original transport problem by Dantzig):

# -*- coding: utf-8 -*-
"""
Transport problem in Pyomo

Created on Mon Feb 15 09:55:06 2021

"""

import pyomo.environ as pyo

#Define the model

model = pyo.ConcreteModel()

#Define the sets model.set_plants = pyo.Set(initialize=['Seattle', 'San_Diego']) model.set_markets = pyo.Set(initialize=['New_York', 'Chicago', 'Topeka'])

Parameters

valuesForCapacity = {'Seattle':350, 'San_Diego':600} valuesForDemand = {'New_York': 325, 'Chicago': 300, 'Topeka': 275}

model.param_capacityOfPlants = pyo.Param(model.set_plants, initialize=valuesForCapacity) model.param_demandAtMarkets = pyo.Param(model.set_markets, initialize=valuesForDemand)

model.param_capacityOfPlants.pprint() model.param_demandAtMarkets.pprint()

#Parameter entry as table

valuesForDistance = {('Seattle', 'New_York'): 2.5, ('Seattle', 'Chicago'): 1.7, ('Seattle', 'Topeka'): 1.8, ('San_Diego', 'New_York'): 2.5, ('San_Diego', 'Chicago'): 1.8, ('San_Diego', 'Topeka'): 1.4}

model.param_distances = pyo.Param(model.set_plants, model.set_markets, initialize=valuesForDistance)

model.param_distances.pprint()

#Scalar freightCostsPerUnitPerThousandMiles = 90

#Variables

model.variable_x = pyo.Var(model.set_plants,model.set_markets, within=pyo.NonNegativeReals) model.variable_totalCosts = pyo.Var()

model.variable_x.pprint()

#Constraints

def supplyConstraintRule(model,i): return sum(model.variable_x[i,j] for j in model.set_markets)<=model.param_capacityOfPlants[i]

model.constraint_supply = pyo.Constraint (model.set_plants, rule=supplyConstraintRule)

def demandConstraintRule (model, j): return sum(model.variable_x [i,j] for i in model.set_plants)>=model.param_demandAtMarkets[j]

model.constraint_demand = pyo.Constraint (model.set_markets, rule=demandConstraintRule) model.constraint_demand.pprint()

#Objective

def ObjectiveRule (model): return sum( sum(model.variable_x[i,j]* model.param_distances[i,j]*freightCostsPerUnitPerThousandMiles for i in model.set_plants) for j in model.set_markets)

model.objective = pyo.Objective(rule=ObjectiveRule, sense =pyo.minimize)

model.objective.pprint()

opt = pyo.SolverFactory('glpk') #opt = SolverFactory("gurobi", solver_io="python") opt.solve(model)

I tried to solve it with the statement opt = pyo.SolverFactory('glpk') as recommended here https://pyomo.readthedocs.io/en/stable/solving_pyomo_models.html but I get the error message "ApplicationError: No executable found for solver 'glpk'". I also tried to use the statement opt = SolverFactory("gurobi", solver_io="python")but here I get the error "NameError: name 'SolverFactory' is not defined"

Do you know what I have to do in order to solve the model? I'd appreciate every comment.

PeterBe
  • 1,632
  • 2
  • 15
  • 30

1 Answers1

2

If you add pyo. to the beginning of your solver definition line, it works and gives the solution as follow:

param_capacityOfPlants : Size=2, Index=set_plants, Domain=Any, Default=None, Mutable=False
    Key       : Value
    San_Diego :   600
      Seattle :   350
param_demandAtMarkets : Size=3, Index=set_markets, Domain=Any, Default=None, Mutable=False
    Key      : Value
     Chicago :   300
    New_York :   325
      Topeka :   275
param_distances : Size=6, Index=param_distances_index, Domain=Any, Default=None, Mutable=False
    Key                       : Value
     ('San_Diego', 'Chicago') :   1.8
    ('San_Diego', 'New_York') :   2.5
      ('San_Diego', 'Topeka') :   1.4
       ('Seattle', 'Chicago') :   1.7
      ('Seattle', 'New_York') :   2.5
        ('Seattle', 'Topeka') :   1.8
variable_x : Size=6, Index=variable_x_index
    Key                       : Lower : Value : Upper : Fixed : Stale : Domain
     ('San_Diego', 'Chicago') :     0 :  None :  None : False :  True : NonNegativeReals
    ('San_Diego', 'New_York') :     0 :  None :  None : False :  True : NonNegativeReals
      ('San_Diego', 'Topeka') :     0 :  None :  None : False :  True : NonNegativeReals
       ('Seattle', 'Chicago') :     0 :  None :  None : False :  True : NonNegativeReals
      ('Seattle', 'New_York') :     0 :  None :  None : False :  True : NonNegativeReals
        ('Seattle', 'Topeka') :     0 :  None :  None : False :  True : NonNegativeReals
constraint_demand : Size=3, Index=set_markets, Active=True
    Key      : Lower : Body                                                          : Upper : Active
     Chicago : 300.0 :   variable_x[Seattle,Chicago] + variable_x[San_Diego,Chicago] :  +Inf :   True
    New_York : 325.0 : variable_x[Seattle,New_York] + variable_x[San_Diego,New_York] :  +Inf :   True
      Topeka : 275.0 :     variable_x[Seattle,Topeka] + variable_x[San_Diego,Topeka] :  +Inf :   True
objective : Size=1, Index=None, Active=True
    Key  : Active : Sense    : Expression
    None :   True : minimize : 225.0*variable_x[Seattle,New_York] + 225.0*variable_x[San_Diego,New_York] + 153.0*variable_x[Seattle,Chicago] + 162.0*variable_x[San_Diego,Chicago] + 162.0*variable_x[Seattle,Topeka] + 125.99999999999999*variable_x[San_Diego,Topeka]
153675.0

for that you need to add the following lines to your code:

opt = pyo.SolverFactory('gurobi', solver_io="python")
results = opt.solve(model)
print(pyo.value(model.objective))

As you import Pyomo.environ as pyo, for all the defined words in pyomo you need to add pyo. to the beginning. For instance, to get the value of the objective function:

my_obj = pyo.value(model.objective)

To be able to use glpk you need to install it by using:

conda install glpk

See also this if you cannot find the exe path. The following lines worked for me:

opt = pyo.SolverFactory('glpk')
results = opt.solve(model)
print(pyo.value(model.objective))

And I got the same solution: 153675.0

Edit2: To see all the variables' value:

for v in model.component_objects(pyo.Var,active=True):
    for index in v:
        print(index, ': ', pyo.value(v[index]))
Oguz Toragay
  • 8,652
  • 2
  • 13
  • 41
  • Thanks for your answer Oguz. When I use your suggested code (copy & paste) I get the following error message: "ApplicationError: No Python bindings available for <class 'pyomo.solvers.plugins.solvers.gurobi_direct.GurobiDirect'> solver plugin" – PeterBe Feb 16 '21 at 09:34
  • do you have gurobi installed in your system? PC or laptop? – Oguz Toragay Feb 16 '21 at 09:36
  • Thanks Ogiz for your comment. I do not have installed anything other than Pyomo. So maybe I should not use Gurobi but the other solver (glpk) – PeterBe Feb 16 '21 at 09:40
  • @PeterBe see the edited answer, install glpk and try again. – Oguz Toragay Feb 16 '21 at 10:18
  • Thanks a lot Oguz for your tremendous help. It works with GLPK solver (I will later try it with Gurobi). How can I print the solution with all the variables that you posted above? Currently I only get the value of the objective function. – PeterBe Feb 16 '21 at 10:58
  • @PeterBe see my answer here: https://or.stackexchange.com/a/2720/39 you have a variable without any initialization value called: model.variable_totalCosts , you should comment it out. – Oguz Toragay Feb 16 '21 at 11:38
  • Thanks Oguz once again for your great help and your effort. I really appreciate it. I upvoted and accepted your answer. – PeterBe Feb 16 '21 at 12:50
  • Hi Oguz, when I use your code from the link I get the following error message: " for v in model.component_objects(pyo.Var,active=True): ^ IndentationError: unexpected indent – PeterBe Feb 17 '21 at 17:14
  • Check the indentation. Python is sensitive to that. – Oguz Toragay Feb 17 '21 at 17:52
  • I checked it 10 times and could not find anything – PeterBe Feb 17 '21 at 17:54
  • It says unexpected indent and points at a specific position in the line of the code. But at this position there is no indent – PeterBe Feb 17 '21 at 17:56
  • Thanks for your comments in the chat. I still have questions (see the chat). Would you mind replying to them? I'd appreciate every further comment from you – PeterBe Feb 19 '21 at 09:01
  • Hi Oguz, would you mind looking into the chat and answering my follow up questions as the suggested code by you (from the link) yields an error message. I'd appreciate any further comments from you. – PeterBe Feb 22 '21 at 08:22
  • Thanks Oguz for your great help :-) – PeterBe Mar 16 '21 at 08:25