Constraint that calls for the previous set member

时间秒杀一切 提交于 2019-12-08 11:33:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!