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

Question:

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?

Asked By: Steven01123581321

||

Answers:

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.

Answered By: mcsoini
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.