1
$\begingroup$

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.

$\endgroup$

1 Answer 1

2
$\begingroup$

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]))
$\endgroup$
15
  • $\begingroup$ 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" $\endgroup$
    – PeterBe
    Feb 16, 2021 at 9:34
  • $\begingroup$ do you have gurobi installed in your system? PC or laptop? $\endgroup$ Feb 16, 2021 at 9:36
  • $\begingroup$ 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) $\endgroup$
    – PeterBe
    Feb 16, 2021 at 9:40
  • $\begingroup$ @PeterBe see the edited answer, install glpk and try again. $\endgroup$ Feb 16, 2021 at 10:18
  • $\begingroup$ 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. $\endgroup$
    – PeterBe
    Feb 16, 2021 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.