bright612
bright612

Reputation: 55

How to put multiple constraints at once in pyomo?

I want to input many constraints at once. My constraints are as follows.

def ss1_rule(model):
    return model.ss[1,1] - model.sss[1,1] <= 0.0
model.ss1 = Constraint(rule=ss1_rule)

def ss2_rule(model):
    return model.ss[1,1] - model.sss[2,1] <= 0.0
model.ss2 = Constraint(rule=ss2_rule)

def ss3_rule(model):
    return model.ss[1,1] - model.sss[3,1] <= 0.0
model.ss3 = Constraint(rule=ss3_rule)

Is there any way to enter the regular constraint at once?

Thank you.

Upvotes: 3

Views: 2675

Answers (1)

Qi Chen
Qi Chen

Reputation: 1718

There's various ways of accomplishing this. You want to use a Set or RangeSet.

model.my_set = Set(initialize=[1, 2, 3])
def ss_rule(model, s):
    return model.ss[1, 1] = model.sss[s, 1] <= 0.0
model.ss = Constraint(model.my_set, rule=ss_rule)

or the equivalent using a shortcut notation:

model.my_set = Set(initialize=[1, 2, 3])
@model.Constraint(model.my_set)
def ss_rule(model, s):
    return model.ss[1, 1] = model.sss[s, 1] <= 0.0

Upvotes: 3

Related Questions