I am trying to model the transportation problem of Dantzig in Pyomo (see the GAMS code here https://www.gams.com/latest/gamslib_ml/libhtml/gamslib_trnsport.html or the description here https://www.math.uh.edu/~rohop/fall_06/Chapter1.pdf).
I have problem defining the constraints for the supply using sets and variables. So basically what I want to do is to add the constraints: For all set_plants Sum over all set_markets x(set_plants, set_markets) <= param_capacityOfPlant (set_plants)
in GAMS it is this line
supply(i).. sum(j, x(i,j)) =l= a(i);
and in the description (https://www.math.uh.edu/~rohop/fall_06/Chapter1.pdf) it is equation 1.2
I am struggeling to implement this in Pyomo. I tried different things and I always get the error "TypeError: Cannot apply a Set operator to an indexed Var component (variable_x)"
Here you can see my code in which the lines at the very bottom are problematic (after #Constraints):
# -*- 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):
#First try
# return pyo.summation(variable_x)<=model.param_capacityOfPlants
#Second try
#return sum(variable_x for j in model.set_markets)<=model.param_capacityOfPlants
# Third try
return sum(variable_x(i,j) for j in model.set_markets)<=model.param_capacityOfPlants(i)
model.constraint_supply = pyo.Constraint (model.variable_x, rule=supplyConstraintRule)
Do you know how I have to implement this? I'd appreciate every comment from you.