3

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

1
  • I am glad to see people working with promo. I have similar interests and I work on similar problems. Do you mind sending me a private message? You can find my email in my profile page Commented Jul 25, 2017 at 21:30

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.