Reputation: 31
I have some code for supply chain management on based on cost where supply tries meets the demand but this only works when the supply is greater than the demand. Is there any way I can optimize it to work in both ways (i.e. when supply > demand
and supply < demand
)?
from pyomo.core import *
model = AbstractModel()
model.warehouses = Set()
model.stores = Set()
model.supply = Param(model.warehouses)
model.demand = Param(model.stores)
model.costs = Param(model.warehouses, model.stores)
model.amounts = Var(model.warehouses, model.stores,
within=NonNegativeReals)
def costRule(model):
return sum(
model.costs[n,i]
for n in model.warehouses
for i in model.stores
)
model.cost=Objective(rule=costRule)
def minDemandRule(model, store):
return sum(model.amounts[i, store] for i in model.warehouses) >=
model.demand[store]
model.demandConstraint = Constraint(model.stores, rule=minDemandRule)
def maxSupplyRule(model, warehouse):
return sum(model.amounts[warehouse, j] for j in model.stores) <=
model.supply[warehouse]
model.supplyConstraint = Constraint(model.warehouses, rule=maxSupplyRule)
My input:
set warehouses := hyd ban coh;
set stores := cbe mdu cnr whc whe;
param: supply :=
hyd 10
ban 10
coh 10;
param: demand :=
cbe 5
mdu 5
cnr 5
whc 5
whe 5;
param costs:
cbe mdu cnr whc whe:=
hyd 1 3 2 2 1
ban 4 5 1 1 3
coh 2 3 3 2 1;
Here supply = 30
and demand = 25
. It works and the output I get is
Variable:
amounts[ban,cbe]:
Value: 5
amounts[ban,whc]:
Value: 5
amounts[coh,whe]:
Value: 5
amounts[hyd,cnr]:
Value: 5
amounts[hyd,mdu]:
Value: 5
But it doesn't work when the supply is less than the demand.
Here supply = 30
and demand = 40
:
param: supply :=
hyd 10
ban 10
coh 10;
param: demand :=
cbe 5
mdu 5
cnr 5
whc 5
whe 20;
param costs:
cbe mdu cnr whc whe:=
hyd 1 3 2 2 1
ban 4 5 1 1 3
coh 2 3 3 2 1;
I require the desired output as below: Variable:
amounts[ban,cbe]:
Value: 5
amounts[ban,whc]:
Value: 5
amounts[coh,whe]:
Value: 10
amounts[hyd,cnr]:
Value: 5
amounts[hyd,mdu]:
Value: 5
whe_demand =10
Upvotes: 1
Views: 822
Reputation: 1718
It looks like your model is infeasible when demand is greater than supply, because of the constraint model.demandConstraint
. The shipment quantities cannot fulfill demand because otherwise the other constraint model.supplyConstraint
would be violated.
If you want to penalize lack of demand satisfaction, then you could define slack variables for the relevant constraint and add those terms to the objective.
This book could be a good resource for you: https://www.wiley.com/en-us/Model+Building+in+Mathematical+Programming%2C+5th+Edition-p-9781118443330
Upvotes: 3