Search code examples
pythonpyomo

is it possible to share constraint cross-blocks?


I have a parent block (Block0) containing 3 sub-blocks:

  1. Block1 and Block2 formulate the same problem A, but with slightly different set of indexes
  2. Block3 formulates another problem B, which uses quadratic constraints
  3. Block1 and Block2 make use of decision variables within Block3 (which are constrained by such quadratic constraint) to determine the solution of problem A

The implementation logic is something along these lines:

block0 = pyo.ConcreteModel("Block0")

block3 = prepare_problemB_model(data3)
block0.add_component("Block3", block3)


block1 = prepare_problemA_model(data1, block3.component_objects(pyo.Var))
block2 = prepare_problemA_model(data2, block3.component_objects(pyo.Var))

block0.add_component("Block1", block1)
block0.add_component("Block2", block2)

block0.obj = pyo.Objective(expr=block1.obj + block2.obj + block3.obj)

I can solve block0 if the decision variables within Block3 (which are shared with Block1 and Block2) are fixed to a constant value. Otherwise I get the following error:

ERROR: Model contains an expression (*CONSTRAINT-NAME*)
    that contains a variable (*VAR-NAME*) that is not attached to an active
    block on the submodel being written

Do you have any ideas? is a straightforward way to copy Block3's model into Block1 and Block2?

I'm using pyomo 6.5.0 with gurobi solver

Thanks


Solution

  • The short answer is to upgrade to Pyomo 6.6.1.

    The slightly longer explanation is that the original LP writer had a step where it “pre-collected” model variables by walking the model (block tree that the writer was handed) before collecting the active constraints. This was done to make certain operations more efficient. The error you are seeing was raised when a constraint referenced an unfixed variable that was not found by that initial collection. Pyomo removed this restriction in 6.5 for the NL writer and in 6.6 for the LP writer (the Baron and GAMS interfaces still have it, and will hopefully be relaxed in 6.7).