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.