Skip to content
Advertisement

Formulating the constraint x[i+1]<= x[i] in Pyomo

I have my decision variable x which is indexed on the list N.

I want the constraint that x[i+1] <= x[i] or x[i] <= x[i-1]. But how do I do this in Pyomo without going out of range with my index?

model.x = Var(N, within=NonNegativeReals)

def constraint1(model, n):
    return model.x[n+1] <= model.x[n] 
model.constraint1 = Constraint(N, rule=constraint1)

This thus doesn’t work. Anyone an idea how to do this?

Advertisement

Answer

You could use Constraint.Skip to avoid accessing an invalid index (see e.g. here). For example:

def constraint1(model, n):
    if n == 0:
        return Constraint.Skip
    else:
        return model.x[n] <= model.x[n - 1] 

I don’t know how your N is defined, but this should give you an idea.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement