Theodorska
Theodorska

Reputation: 49

is pyomo constraint with multiple indexes of one variable possible?

Is there a way to set multiple indexes to a set value within one constraint without having to type out the same variable for each indexed time. I have provided an example, imagine that you want to optimize when to charge your electric vehicle but you don't want it to charge at certain hours in a day. The example below works to avoid charging at the 4th and 5th hour. However, what if I want it to not charge for 15 hours of the day but don't feel like writing m.EVcharge[0]+m.EVcharge[1]+... putting m.EVcharge[:15] == 0 will not work because the constraints don't handle slices that well in pyomo.

def time_rule(m):
return  m.EVcharge[4]+m.EVcharge[5] == 0
m.time_rule = Constraint(time, rule=time_rule)

Upvotes: 0

Views: 896

Answers (1)

AirSquid
AirSquid

Reputation: 11883

Yes. There are a variety of ways to do this. You could make a subset of m.time and only pass that subset in to the constraint rule, which would constrain them to zero, or sum across the subset and make constrain it to zero (both assume negative charging is not possible.)

Or, you could do it more cleanly with data or a parameter that holds the limit for any arbitrary time block and use that, which keeps the data separate from the model, which is generally a good idea...

import pyomo.environ as pyo

# some background data on limits...

use_limit = {   0:3,    # limited juice avial
                1:3,
                2:0,    # no juice avail.  :)
                3:0}

m = pyo.ConcreteModel('EV Charge')

m.T = pyo.Set(initialize=range(6))

m.EV_charge = pyo.Var(m.T, domain=pyo.NonNegativeReals)

# Constraints
def charge_limit(m, time):
    return m.EV_charge[time] <= use_limit[time]
m.C1 = pyo.Constraint(use_limit.keys(), rule=charge_limit)

m.pprint()

Yields:

2 Set Declarations
    C1_index : Size=1, Index=None, Ordered=False
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    4 : {0, 1, 2, 3}
    T : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    6 : {0, 1, 2, 3, 4, 5}

1 Var Declarations
    EV_charge : Size=6, Index=T
        Key : Lower : Value : Upper : Fixed : Stale : Domain
          0 :     0 :  None :  None : False :  True : NonNegativeReals
          1 :     0 :  None :  None : False :  True : NonNegativeReals
          2 :     0 :  None :  None : False :  True : NonNegativeReals
          3 :     0 :  None :  None : False :  True : NonNegativeReals
          4 :     0 :  None :  None : False :  True : NonNegativeReals
          5 :     0 :  None :  None : False :  True : NonNegativeReals

1 Constraint Declarations
    C1 : Size=4, Index=C1_index, Active=True
        Key : Lower : Body         : Upper : Active
          0 :  -Inf : EV_charge[0] :   3.0 :   True
          1 :  -Inf : EV_charge[1] :   3.0 :   True
          2 :  -Inf : EV_charge[2] :   0.0 :   True
          3 :  -Inf : EV_charge[3] :   0.0 :   True

Upvotes: 1

Related Questions