问题
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 ?
回答1:
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)
来源:https://stackoverflow.com/questions/43047684/constraint-that-calls-for-the-previous-set-member