# Pyomo style "blocks" in JuMP

**URL:** <https://discourse.julialang.org/t/pyomo-style-blocks-in-jump/31849>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [December 4, 2019, 11:53am UTC](https://discourse.julialang.org/t/pyomo-style-blocks-in-jump/31849 "2019-12-04T11:53:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonmat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonmat/32/11690_2.png) [@jonmat](https://discourse.julialang.org/u/jonmat)\
**Post date:** [December 4, 2019, 11:53am UTC](https://discourse.julialang.org/t/pyomo-style-blocks-in-jump/31849/1 "2019-12-04T11:53:58Z")

</div>

I’m new to Julia/JuMP and I’m wondering if Julia has any capability that replicates “blocks” in Pyomo? In other words the possibility to maintain separate namespaces within your model, and build hierarchical/compositional models from blocks. I’ve come across StructJuMP, but I don’t think this is quite what I’m looking for. Any help would be much appreciated!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [December 4, 2019, 2:23pm UTC](https://discourse.julialang.org/t/pyomo-style-blocks-in-jump/31849/2 "2019-12-04T14:23:19Z")

</div>

The short answer is _no_.

The longer answer is that you are much freer to structure your code in different ways so that separate namespaces aren’t needed.

In particular, you can use [_anonymous variables_](https://www.juliaopt.org/JuMP.jl/v0.20.0/variables/#Anonymous-JuMP-variables-1), which aren’t “named” in the Pyomo sense.

Then you can do things like:

```nohighlight
model = Model()
x = @variable(model, [1:5])
function foo_constraints(model, y)
    @constraint(model, 2 * y <= 1)
end
foo_constraints(model, x)

```

You’re also free to wrap variables/constraints/etc inside structs:

```nohighlight
struct Block
    x::VariableRef
end
function Block(model::Model)
    x = @variable(model)
    return Block(x)
end

function block_constraint(block::Block)
    model = owner_model(block.x)
    @constraint(model, 2 * block.x <= 1)
end

model = Model()
block = Block(model)
block_constraint(block)

```

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [December 4, 2019, 2:36pm UTC](https://discourse.julialang.org/t/pyomo-style-blocks-in-jump/31849/3 "2019-12-04T14:36:28Z")

</div>

Do you have an example of what you’d like to do with blocks? As @odow described, we encourage users to rely on Julia’s existing constructs for composition, i.e., functions and structs.

---

<div class="post-metadata">

**Author:** ![jonmat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonmat/32/11690_2.png) [@jonmat](https://discourse.julialang.org/u/jonmat)\
**Post date:** [December 4, 2019, 4:57pm UTC](https://discourse.julialang.org/t/pyomo-style-blocks-in-jump/31849/4 "2019-12-04T16:57:37Z")

</div>

Thanks for the responses, this is helpful. So I’m probably more worried about compositional models than namespaces, although that’s helpful to know about anonymous variables. Here’s a simple working example of the kind of thing I’d like to do - excuse the pyomo syntax!

```julia
import pyomo.environ as pyo

block_1 = pyo.Block()
block_1.x = pyo.Var(bounds=(0,3))
block_1.p = pyo.Param(initialize=5)

block_2 = pyo.Block()
block_2.x = pyo.Var(bounds=(2,5))
block_2.p = pyo.Param(initialize=-2)

block_3 = pyo.Block()
block_3.block_1 = block_1
block_3.block_2 = block_2

def expr(b):
    return b.block_1.x * b.block_1.p + b.block_2.x * b.block_2.p
block_3.expr = pyo.Expression(rule=expr)

model = pyo.ConcreteModel()
model.block_3 = block_3

def obj_expr(m):
    return m.block_3.expr
model.obj = pyo.Objective(rule=obj_expr)

opt = pyo.SolverFactory("glpk")
opt.solve(model)

print(pyo.value(model.block_3.block_1.x))
print(pyo.value(model.block_3.block_2.x))

```

A desired feature I haven’t included here would be the ability to reference components from “higher level” blocks on lower level blocks. In general this isn’t a great design pattern, but an example could be defining multiple variables on low level blocks over a single index that is defined at the model (highest) level.

In general, I’d be doing this kind of block construction in a much more programmatic way - main goal is programmatic construction of a model from the bottom up. Also each block could have an arbitrary number of variables, parameters, constraints, expressions etc. Some of these components would be referenced at higher levels of the model.

Is there also anywhere you could point me to that has documentation on `struct`, can’t seem to find it in the regular JuMP docs. Thanks!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [December 4, 2019, 5:44pm UTC](https://discourse.julialang.org/t/pyomo-style-blocks-in-jump/31849/5 "2019-12-04T17:44:57Z")

</div>

`struct` is a standard Julia struct.

See [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#Composite-Types-1).

The “JuMP” version of your Pyomo model could look like:

```nohighlight
using JuMP
using GLPK

struct Block
    x::VariableRef
    p::Float64
    function Block(model, lb, ub, p0)
        x = @variable(model, lower_bound = lb, upper_bound = ub)
        return new(x, p0)
    end
end

struct Block3
    block_1::Block
    block_2::Block
end

function expr(b::Block3)
    return b.block_1.x * b.block_1.p + b.block_2.x * b.block_2.p
end

model = Model(with_optimizer(GLPK.Optimizer))
block_1 = Block(model, 0, 3, 5)
block_2 = Block(model, 2, 5, -2)
block_3 = Block3(block_1, block_2)
@objective(model, Min, expr(block_3))
optimize!(model)
value(block_3.block_1.x)
value(block_3.block_2.x)

```

You could also use `Dict`s:

```nohighlight
using JuMP
using GLPK

function Block(model, lb, ub, p0)
    x = @variable(model, lower_bound = lb, upper_bound = ub)
    return Dict(
        :x => x, 
        :p => p0
    )
end

function block_expr(b)
    return b[:block_1][:x] * b[:block_1][:p] + b[:block_2][:x] * b[:block_2][:p]
end

model = Model(with_optimizer(GLPK.Optimizer))
block_1 = Block(model, 0, 3, 5)
block_2 = Block(model, 2, 5, -2)
block_3 = Dict(
    :block_1 => block_1, 
    :block_2 => block_2
)
@objective(model, Min, block_expr(block_3))
optimize!(model)
value(block_3[:block_1][:x])
value(block_3[:block_2][:x])

```

---

<div class="post-metadata">

**Author:** ![jonmat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonmat/32/11690_2.png) [@jonmat](https://discourse.julialang.org/u/jonmat)\
**Post date:** [December 5, 2019, 11:42am UTC](https://discourse.julialang.org/t/pyomo-style-blocks-in-jump/31849/6 "2019-12-05T11:42:57Z")

</div>

Thanks, this is super helpful! Final question - I’m likely going to be building up blocks and expressions iteratively. For example, Block3 could contain an arbitrary number of blocks, which may or may not all be added at once, and the expression attached to Block3 could be a sum over all relevant variables on blocks within Block3. Would there be a performance reason to use structs over dicts or vice versa in this context? More generally is there a significant performance loss from using mutable rather than immutable containers in Julia?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [December 5, 2019, 2:23pm UTC](https://discourse.julialang.org/t/pyomo-style-blocks-in-jump/31849/7 "2019-12-05T14:23:38Z")

</div>

> For example, Block3 could contain an arbitrary number of blocks, which may or may not all be added at once, and the expression attached to Block3 could be a sum over all relevant variables on blocks within Block3.

Use a vector:

```Julia
struct Block3
    model::Model
    blocks::Vector{Block}
    Block3(model) = new(model, Block[])
end
b = Block3(model)
push!(b.blocks, block_1)
push!(b.blocks, block_2)

function block_expr(b::Block3)
    return @expression(b.model, sum(block.x * block.p for block in b.blocks))
end

```

In general, JuMP doesn’t provide a lot of “helper” type functions. We expect users to use Julia constructs. If you’re coming from a math. programming background without much programming, there is a learning curve. But in the long run, it lets you structure your code in a much more effective way than if JuMP provided Promo-type functionality.

> Would there be a performance reason to use structs over dicts or vice versa in this context? More generally is there a significant performance loss from using mutable rather than immutable containers in Julia?

Short answer is “yes”. But you should try the easiest thing first (e.g., use dictionaries or mutable structs). Then benchmark your code, and only try to optimize the run time if it’s too slow.

---

<div class="post-metadata">

**Author:** ![jonmat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonmat/32/11690_2.png) [@jonmat](https://discourse.julialang.org/u/jonmat)\
**Post date:** [December 5, 2019, 3:06pm UTC](https://discourse.julialang.org/t/pyomo-style-blocks-in-jump/31849/8 "2019-12-05T15:06:17Z")

</div>

For sure, I’ll try diving a bit deeper into the Julia language itself which should illuminate some of the more “helper” type things I want to do. Thanks for the examples!
