Reputation: 31
Following is a part from an optimization code I'm trying to run.
from pyomo.environ import *
model = ConcreteModel()
## Define sets
model.k = Set(initialize=['Diesel','Diesel_hybrid', 'Battery_electric'], doc='Vehicle Type')
model.i = Set(initialize=[0,1,2,3,4,5], doc='Age')
model.t = Set(initialize=[2018,2019,2020,2021,2022,2023], doc='Years')
## Define variables
model.P = Var(model.k, model.t, bounds=(0,None), doc='number of k type vehicle purchased in year t')
model.A = Var(model.k, model.i, model.t, bounds=(0,None), doc='number of k type i year old bus in use at the end of year t')
model.R = Var(model.k, model.i, model.t, bounds=(0,20), doc='number of k type i year old bus salvaged at year t')
I'm trying to write a constraint that says, for age of bus i<=4, salvaged number of buses R[k,i,t] = 0 I tried the following. It doesn't seem to work.
def constraint_5(model,k,t):
if (i<=4):
return model.R[k,i,t] == 0
I've also tried defining a subset. That doesn't work as well.
model.sal = Set(initialize=[0,1,2,3,4], doc='Minimum age in usage')
def constraint_5(model,k,t):
for i in model.w:
return model.R[k,i,t] == 0
Can anyone help me? Thanks
Upvotes: 3
Views: 3937
Reputation: 2818
You can do this by indexing your constraints over all of the sets and using Constraint.Skip
to skip adding the constraint for undesired indices
def constraint_5(model,k,i,t):
if i<=4:
return model.R[k,i,t] == 0
return Constraint.Skip
model.con5 = Constraint(model.k,model.i,model.t,rule=constraint_5)
Or you could index the constraint over the subset that you created
def constraint_5(model,k,i,t):
return model.R[k,i,t] == 0
model.con5 = Constraint(model.k,model.sal,model.t,rule=constraint_5)
Upvotes: 4