• Community Moderator

Hi Anne,

the code you posted, or at least its screenshot, won't work since you did not define $$t$$ properly. (For future reference, it is much easier to help if you post the actual code and not a screenshot).

If you wish to consider all $$t \in T$$, you'd need to iterate over $$T$$ (usually this is done by a for loop). I have too little information to help you with this matter further - you'd need to tell us more about the model you're trying to implement.

As a side note, you could easily model your graph using NetworkX. Then, you can iterate over arcs in your graph to create a tupledict of variables in your Gurobi model.

Hope this helps.

Best regards
Jonasz

Hi Jonas,

Thanks for your reply. I'm trying to model an optimzation of the optimal pallet mix for a company, in order to find the trade-off between costs and sustainability. Find below some of the code. Hope this helps you understand the problem, and look forward to your suggestions on modelling the time in the correct way. I also included the objective function, where i do refer to t in model T. CP is a parameter i did not define in below code, but which is defined in the actual code, including all other parameters.

#Import packages
import pyomo.environ as pe
import pyomo.opt as po

from collections import defaultdict

#Dictionary - Sets

#Pallet Management System
#1 = buy/sell, 2 = rental/lease

P={1,2}

#Materials
#3 = wood, 4 = plastic

N={3,4}

#EOL Scenarios
#5 = repair, 6 = recycle, 7 = downcycle, 8 = landfill, 9 = incineration

D={5,6,7,8,9}

#Time Periods in weeks

T=5

#Nodes - Node (s,t) s = life cycle or use phase, t = period
V = {'G','RM', 'MP', 'CPM', 'DC', 'RET', 'RC', 'DEOL', 'LEOL', 'IEOL' }

#Arcs - Arc ((s,t), (s',t')) - t' >= t

Arcs = {(('G', t-1), ('RM', t)),
(('RM', t), ('MP', t)),
(('MP', t), ('CPM', t)),
(('CPM', t), ('DC', t+1)),
(('DC', t+1), ('RET', t+2)),
(('RET', t+2), ('RC', t+2)),
(('RC', t+2), ('CPM', t+3)),
(('RC', t+2), ('MP', t+3)),
(('RC', t+2), ('RM', t+3)),
(('RC', t+2), ('DEOL', t+3)),
(('RC', t+2), ('LEOL', t+3)),
(('RC', t+2), ('IEOL', t+3)),
(('DC', t+2), ('CPM', t+3)),
(('DC', t+2), ('MP', t+3)),
(('DC', t+2), ('RM', t+3)),
(('DC', t+2), ('DEOL', t+3)),
(('DC', t+2), ('LEOL', t+3)),
(('DC', t+2), ('IEOL', t+3))
}

#Making sets:
model = pe.ConcreteModel()

#Pallet types
model.P = pe.Set(initialize = P, ordered = False)

#Materials
model.N = pe.Set(initialize = N, ordered = False)

#EOL scenarios
model.D = pe.Set(initialize = D, ordered = False)

#Time Periods
model.T = pe.RangeSet(1, T)

#Nodes
model.V = pe.Set(initialize = V, ordered = False)

#Arcs
model.A = pe.Set(initialize = A, ordered = False)

#Decision Variables
model.Y = pe.Var(model.P, model.N, model.A, model.T, domain = pe.NonNegativeReals)
model.Inv = pe.Var(model.P, model.N, model.V, model.T, domain = pe.NonNegativeReals)
model.X = pe.Var(model.P, model.N, model.V, domain = pe.Binary)

#1 - Objective function - minimize costs

obj_expr = sum(model.Y[p,n,i,j,t,t]*CP[p,n] for p in model.P for n in model.N for (i,j) in model.A for t in model.T) \
+ sum(model.Y[p,n,i,j,t,t+1]*CP[p,n]for p in model.P for n in model.N for (i,j) in model.A for t in model.T)
model.obj=pe.Objective(expr=obj_expr, sense=pe.minimize)

• Community Moderator

You need to define your arcs somewhat like this:

Arcs = []for t in range(1, T+1):    if t-1 >= 1 and t+3 <= T:        Arcs += [(('G', t-1), ('RM', t)),                (('RM', t), ('MP', t)),                (('MP', t), ('CPM', t)),                (('CPM', t), ('DC', t+1)),                (('DC', t+1), ('RET', t+2)),                (('RET', t+2), ('RC', t+2)),                (('RC', t+2), ('CPM', t+3)),                (('RC', t+2), ('MP', t+3)),                (('RC', t+2), ('RM', t+3)),                (('RC', t+2), ('DEOL', t+3)),                (('RC', t+2), ('LEOL', t+3)),                (('RC', t+2), ('IEOL', t+3)),                (('DC', t+2), ('CPM', t+3)),                (('DC', t+2), ('MP', t+3)),                (('DC', t+2), ('RM', t+3)),                (('DC', t+2), ('DEOL', t+3)),                (('DC', t+2), ('LEOL', t+3)),                (('DC', t+2), ('IEOL', t+3))        ]

Is this the snippet you were looking for?

Best regards
Jonasz

Hi Jonasz,

thanks a lot . i implemented it like this and it seems to work. unfortunately i ran into the next problem while trying to define the objective function. i used the code described above and added your part on modelling the arcs. when i try to code the objective function, it keeps giving me a key error for the CT[p,n,a] * L[a] part. furthermore, i need to use a to call an element from model A and am not able to use (i,j). would you know how to fix this? at some point in my code i will need to call only node i from the pair (i,j) in model.A, but at the moment i am only able to call the full arc using a in model A

#CO(p,n) - Purchase/leasing cost of pallet type p of material m - €/PAL
CO = {(1, 3): 5, (1, 4): 5, \
(2, 3): 5, (2, 4): 5}

#CT(p,n) - Transportation cost per pallet type p per km - €/km-PAL
#CT = {}
#for a in model.A:
#CT = {(1, 3, a): 0.00015, (1, 4, a): 0.00015, (2, 3, a): 0.00015, (2, 4, a): 0.00015}

CT = {}
for t in range(1, T+1):
if t-1 >= 1 and t+3 <= T:
CT = {((1, 3), (('CPM', t), ('DC', t+1))): 0.00015,\
((1, 4), (('DC', t+1), ('RET', t+2))): 0.00015,\
((2, 3), (('RET', t+2), ('RC', t+2))): 0.00015, \
((2, 4), (('RC', t+2), ('CPM', t+3))): 0.00015,\
((1, 3), (('DC', t+2), ('CPM', t+3))): 0.00015
}

#S(p,n) - Credit for selling or returning pallet type p - €/PAL
S = {(1, 3): 5, (1, 4): 15, \
(2, 3): 8, (2, 4): 20}

#L(i,j) - Distance travelled between nodes i and j - km

#L = {('CPM', 'DC'): 100, ('DC', 'RET'): 150, ('DC', 'CPM'): 50, ('RET', 'RC'): 75, ('RC', 'CPM'): 125}

L = {}
for t in range(1, T+1):
if t-1 >= 1 and t+3 <= T:
L = {(('CPM', t), ('DC', t+1)):100, (('DC', t+1), ('RET', t+2)):150, (('DC', t+2), ('CPM', t+3)):50,\
(('RET', t+2), ('RC', t+2)):75, (('RC', t+2), ('CPM', t+3)):125}

#OBjective funciton

objexpr = sum(model.Y[p,n,a] * (CO[p,n] + CT[p,n,a] * L[a]- S[p,n])  for p in model.P for n in model.N for a in model.A)
model.obj=pe.Objective(expr=objexpr,sense=pe.minimize)