python - Pyomo constraints for specific range -


following part optimization code i'm trying 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 year old bus in use @ end of year t') >model.r = var(model.k, model.i, model.t, bounds=(0,20), doc='number of k type year old bus salvaged @ year t') 

i'm trying write constraint says, age of bus i<=4, salvaged number of buses r[k,i,t] = 0 tried following. doesn't seem work.

>def constraint_5(model,k,t): >    if (i<=4): >        return model.r[k,i,t] == 0 

i've tried defining subset. doesn't work well.

>model.sal = set(initialize=[0,1,2,3,4], doc='minimum age in usage') >def constraint_5(model,k,t): >    in model.w: >        return model.r[k,i,t] == 0 

can me? thanks

you can indexing constraints on of sets , using constraint.skip skip adding constraint 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 index constraint on subset 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) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -