Skip to content

finalize function in templates not working with JuMP macros #76

@daschw

Description

@daschw

I suppose the idea of the finalize function in templates is to also allow adding additional constraints etc. (see point 3 here).
However, I think this does not really work at the moment.
If I try to replace the CHP addon in our CHP example with the following lines in the CHP template

...

  finalize: |
    model = this.model
    T = get_T(model)
    cm = this.get("power_ratio")
    cb = this.get("power_loss_ratio")
    p_max = this.get("p_max")
    @constraint(model, [t in T], cm * this.heat.exp.out_heat[t] <= this.power.exp.out_electricity[t])
    @constraint(model, [t in T], this.power.exp.out_electricity[t] <= p_max - cv * this.heat.exp.out_heat[t])

...

I get the following error

┌ Error: [generate] Error(s) during model generation
│   debug = "grid_electricity :: _construct_objective!"
│   number_of_errors = 1= = = = = = = = = [ Error #1 ] = = = = = = = = =
│    The function body AST defined by this @generated function is not pure. This likely means it contains a closure, a comprehension or a generator.
│    Stacktrace:
│     [1] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:__virtual__,), IESopt.var"#_RGF_ModTag", IESopt.var"#_RGF_ModTag", (0x21ea3912, 0x8ab7dfc6, 0xbf2cc269, 0xb6437d67, 0xd7a70dc1), Expr})(args::Virtual)
│       @ RuntimeGeneratedFunctions C:\Users\SchwabenederD\.julia\packages\RuntimeGeneratedFunctions\M9ZX8\src\RuntimeGeneratedFunctions.jl:150
│     [2] _build_model!(model::Model)
│       @ IESopt c:\Users\SchwabenederD\.julia\dev\IESopt\src\IESopt.jl:91
│     [3] build!(model::Model)
│       @ IESopt c:\Users\SchwabenederD\.julia\dev\IESopt\src\IESopt.jl:532
│     [4] (::IESopt.var"#439#441"{Bool, Model})()
│       @ IESopt c:\Users\SchwabenederD\.julia\dev\IESopt\src\IESopt.jl:284
│     [5] generate!(model::Model, filename::String; skip_validation::Bool, kwargs...)
│       @ IESopt c:\Users\SchwabenederD\.julia\dev\IESopt\src\IESopt.jl:276
│     [6] run(filename::String; kwargs...)
│       @ IESopt c:\Users\SchwabenederD\.julia\dev\IESopt\src\IESopt.jl:199
│     [7] run(filename::String)
│       @ IESopt c:\Users\SchwabenederD\.julia\dev\IESopt\src\IESopt.jl:196
└ @ IESopt c:\Users\SchwabenederD\.julia\dev\IESopt\src\IESopt.jl:330

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions