I would like to define the following constraint in Pyomo $$W^\top{\bf 1}\le\hat w=\begin{bmatrix}\hat{w}_1&\hat{w}_2&\ldots&\hat{w}_N\end{bmatrix}^\top$$ where $W$ is a $2\times4$ matrix. Can I define the scalar constraints using the entries of $\hat w$ and put these constraints as a list in Pyomo?

## First constraint START ##
m.c1 = []
for k in range(N):
    c1_exp = np.matmul((W_var).T,ones_W)[k][0] <= w_hat[k][0]
    m.c1.append(Constraint(expr= c1_exp))
## First constraint END ##

The output is as follows:

console output

Does this mean the constraints are created correctly?


1 Answer 1


Although the code that you shared shows that simple constraints have been generated, it does not show the constraints. A better way to generate these constraints in Pyomo is to use constraint lists as follow:

model.cons1 = ConstraintList()

and then in a for loop you will generate the expression for each constraint and add them to the list:

model.cons1.add(expr = expression)

you can also in each iteration $i$ of for loop observe the generated constraint by:

  • 1
    $\begingroup$ Thanks @OguzToragay for your reply. I will try this and let you know. :) $\endgroup$ Commented Mar 29, 2021 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.