'Is it possible to formulate constraints with pyomo in a matrix way?

I'm a fresh with pyomo.There is a problem that has me very confused recently.

Here is the code:

def unit_commitment():
    model = pyo.ConcreteModel()
    model.GN = pyo.Set(initialize = GN) # GN=280
    model.TN = pyo.Set(initialize = TN) # TN=24
    model.LN = pyo.Set(initialize = LN) # LN=6060
    model.Pc = pyo.Var(model.GN, model.TN, domain = pyo.NonNegativeReals)
    def branch_Cap1(model, l, t):
        return sum(Tc[l, n] * model.Pc[n, t] for n in range(GenCount)) <= Fmax_re[l, t] #Tc is a ndarray:(6060,280), GenCount = 280, Fmax_re is a ndarray:(6060,24)
    model.branch_Cap1 = pyo.Constraint(model.LN, model.TN, rule=branch_Cap1)
    return model

As you can see, for each constraint branch_Cap1[l, t], it has to "for" n=280 times to formulate one single constraint.

And if I want to formulate all of the constraints, it has to take about 280 * 6060 * 24 = 40,723,200 times of calculation.

It takes a very long time.(about 1 hour, which is unacceptable for me)

And I have noticed that pyomo has a formulation called: pyomo.core.kernel.matrix_constraint.matrix_constraint,matrix_constraint which I think may be helpful to me. matrix_constraint So I gave it a try:

model.branhc_Cap = pyomo.core.kernel.matrix_constraint.matrix_constraint(A=Tc, x=model.Pc, ub=Fmax_re)

then it came to an error:

self.x = x
raise ValueError(
ValueError: Argument length must be 280 not 6720

It seems that my model.Pc's length is 6720, which equals to 280 *24, not 280.

So how can I fix the problem? Or if there is some other way for me to take to formulate the constraints more efficiently?

Your help is very important to me.

Thanks a lot.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source