# Can I define constraints in Pyomo as a list?

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))
print(m.c1[k])
## First constraint END ##


The output is as follows:

Does this mean the constraints are created correctly?

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:

model.cons1[i].pprint()

• Thanks @OguzToragay for your reply. I will try this and let you know. :) – PcumP_Ravenclaw Mar 29 at 2:20