iriniapid
iriniapid

Reputation: 75

Constraint that calls for the previous set member

I have the following type of constraint:

def C_rule(model,t-1):
    return x[t]<=y[t-1]

model.C=Constraint(model.t,rule=C_rule)

But set model.t elements are string type so i cannot access the previous element this way. Is there a way to do that ?

Upvotes: 0

Views: 325

Answers (1)

Bethany Nicholson
Bethany Nicholson

Reputation: 2828

If you declare your Set to be ordered then you can do something like this:

m.s = Set(initialize=['A','B','C'], ordered=True)
m.v = Var(m.s)

def _c_rule(m, i):
    if i == 'A':
        return Constraint.Skip
    return m.v[i] <= m.v[m.s.prev(i)]
m.c = Constraint(m.s, rule=_c_rule)

# Or the opposite way
def _c2_rule(m, i):
    if i == 'C':
        return Constraint.Skip
    return m.v[m.s.next(i)] <= m.v[i]
m.c2 = Constraint(m.s, rule=_c2_rule)

Upvotes: 1

Related Questions