JuliaStochOpt / ParameterJuMP.jl

A JuMP extension to use parameter in constraints RHS
MIT License
41 stars 5 forks source link

Macro constructor #4

Closed blegat closed 3 years ago

blegat commented 6 years ago

What would you think of a constructor to create the variables ? The easiest way is to implement

struct Parameter{T} <: JuMP.AbstractVariable
    value::T
end
function JuMP.buildvariable(_error::Function, info::VariableInfo, ::Param)
    info.haslb && error(...)
    ....
    !info.hasfix && errror(...)
    Parameter(info.fixedvalue)
end

and

struct ParameterRef <: JuMP.AbstractVariableRef
    ...
end
function JuMP.addvariable(m::Model, p::Parameter, name::String="")
    ...
    ParameterRef(...)
end

The user will then be able to create parameters using the @variable macro, e.g.

@variable m x[2:3, 1:5] == 1 Param()
joaquimg commented 6 years ago

This would be great!

joaquimg commented 5 years ago

We could have a @parameter macro that simply calls the @variable macro with the extra argument.

joaquimg commented 5 years ago

Would this allow to create multiple parameters at once?

blegat commented 5 years ago

Yes, with the container syntax, it creates a container of parameters

joaquimg commented 5 years ago

I tried for a long to create a @parameter macro that correctly calls the @variable marco with appropriate arguments. Do you think that is feasible?

joaquimg commented 5 years ago

In #47 I managed to get @variable m x[2:3, 1:5] == 1 ParameterJuMP.Param() working.

Still have to figure out how to get this: @variable m x[2:3, 1:5] == 1 Param() working... Param is being exported, but there might be some scope/escape problem

It would also be nice to have anonimous: @variable m Param()

and of course it would be even nicer to have:

@parameter

But figuring out what to esc might be not trivial...

I asked a bit on discourse, but their solutions won´t work: https://discourse.julialang.org/t/args-in-macros/22256

blegat commented 5 years ago

It would also be nice to have anonimous: @variable m Param()

This won't you need to do @variable(m, variable_type=Param())

blegat commented 5 years ago

there might be some scope/escape problem

That would be surprising, it works with PolyJuMP, SumOfSquares and SetProg

odow commented 3 years ago

At some point, this got fixed.