-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add POI.Parameters #40
Conversation
Codecov Report
@@ Coverage Diff @@
## master #40 +/- ##
==========================================
+ Coverage 90.71% 90.94% +0.22%
==========================================
Files 4 4
Lines 528 541 +13
==========================================
+ Hits 479 492 +13
Misses 49 49
Continue to review full report at Codecov.
|
Not sure we should create another set for this. Maybe @odow has a suggestion for this... If JuMP is doing something really different we should re-think the way forward and not add much complexity here. |
@joaquimg indeed there is this version. It works on MOI but it does not work in JuMP, at least the way I tried. using JuMP, ParametricOptInterface, MathOptInterface, GLPK
const POI = ParametricOptInterface
const MOI = MathOptInterface
n = 100
model = Model(() -> POI.ParametricOptimizer(GLPK.Optimizer()))
@variable(model, x[i=1:2] >= 0)
@variable(model, y[i=1:3] in POI.Parameter.(zeros(3))) leads to ERROR: MethodError: no method matching build_variable(::JuMP.var"#_error#108"{LineNumberNode}, ::Vector{ScalarVariable{Float64, Float64, Float64, Float64}}, ::Vector{ParametricOptInterface.Parameter})
Closest candidates are:
build_variable(::Function, ::Vector{var"#s142"} where var"#s142"<:ScalarVariable, ::AbstractVectorSet) at C:\Users\guilhermebodin\.julia\packages\JuMP\Xrr7O\src\sets.jl:14
build_variable(::Function, ::VariableInfo, ::Any...; kwargs...) at C:\Users\guilhermebodin\.julia\packages\JuMP\Xrr7O\src\macros.jl:1340
build_variable(::Function, ::Vector{var"#s142"} where var"#s142"<:ScalarVariable, ::MathOptInterface.AbstractVectorSet) at C:\Users\guilhermebodin\.julia\packages\JuMP\Xrr7O\src\macros.jl:1356
...
Stacktrace:
[1] macro expansion
@ C:\Users\guilhermebodin\.julia\packages\JuMP\Xrr7O\src\macros.jl:142 [inlined]
[2] top-level scope
@ REPL[25]:1 |
@joaquimg Alternatively we can also make this work by adding these two methods using JuMP, ParametricOptInterface, MathOptInterface, GLPK
const POI = ParametricOptInterface
const MOI = MathOptInterface
n = 100
function JuMP.build_variable(
_error::Function,
variables::Vector{<:ScalarVariable},
sets::Vector{<:MOI.AbstractScalarSet},
)
v = Vector{VariableConstrainedOnCreation}(undef, length(sets))
for i in eachindex(variables)
v[i] = VariableConstrainedOnCreation(variables[i], sets[i])
end
return v
end
function JuMP.add_variable(
model::Model,
variables::Vector{VariableConstrainedOnCreation},
names,
)
var_indices = Vector{MOI.VariableIndex}(undef, length(variables))
for (i, variable) in enumerate(variables)
var_indices[i] = JuMP._moi_add_constrained_variable(
JuMP.backend(model),
variable.scalar_variable,
variable.set,
names[i],
)
end
return [JuMP.VariableRef(model, var_index) for var_index in var_indices]
end
model = Model(() -> POI.ParametricOptimizer(GLPK.Optimizer()))
@variable(model, x[i=1:2] >= 0)
@variable(model, y[i=1:3] in POI.Parameter.(zeros(3)))
@constraint(model, 2*x[1] + x[2] + y[1] <= 4)
@constraint(model, 1*x[1] + 2*x[2] + y[3] <= 4)
@objective(model, Max, 4*x[1] + 3*x[2] + y[2])
optimize!(model) And this would also be possible @variable(model, z[i=1:3] in MOI.LessThan.(zeros(3))) |
It might be that JuMP is missing those functions. I just don't know if they are not there on purpose. |
strongly related discussion: jump-dev/JuMP.jl#2148 |
@joaquimg it works on your branch of JuMP (ParametricOptInterface) pkg> st
Project ParametricOptInterface v0.1.1
Status `C:\Users\guilhermebodin\Documents\Github\ParametricOptInterface.jl\Project.toml`
[4076af6c] JuMP v0.21.9 `https://github.com/jump-dev/JuMP.jl.git#jg/at_var`
[b8f27783] MathOptInterface v0.9.22
julia>
julia> model = Model(() -> POI.ParametricOptimizer(GLPK.Optimizer()));
julia>
julia> @variable(model, x[i=1:2] >= 0)
2-element Vector{VariableRef}:
x[1]
x[2]
julia> @variable(model, y[i=1:3] in POI.Parameter.([1.0, 2.0, 3.0]))
3-element Vector{VariableRef}:
y[1]
y[2]
y[3]
julia>
julia> @constraint(model, 2*x[1] + x[2] + y[1] <= 4)
2 x[1] + x[2] + y[1] <= 4.0
julia> @constraint(model, 1*x[1] + 2*x[2] + y[3] <= 4)
x[1] + 2 x[2] + y[3] <= 4.0
julia>
julia> @objective(model, Max, 4*x[1] + 3*x[2] + y[2])
4 x[1] + 3 x[2] + y[2]
julia>
julia> optimize!(model) |
Closing this because either we go on with jump-dev/JuMP.jl#2657 or define it outside the package for now. |
This is a step towards solving #20
Now this works
but this does not
The model gives the following error: