Skip to content
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

Implement constrained variables #167

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,13 @@ function MOI.get(model::Optimizer, tp::Type{MOI.VariableIndex}, attr::String)
return MOI.get(model.optimizer, tp, attr)
end

function MOI.add_variable(model::Optimizer)
function _add_variable(model::Optimizer, inner_vi)
_next_variable_index!(model)
return MOI.Utilities.CleverDicts.add_item(
model.variables,
MOI.add_variable(model.optimizer),
)
return MOI.Utilities.CleverDicts.add_item(model.variables, inner_vi)
end

function MOI.add_variable(model::Optimizer)
return _add_variable(model, MOI.add_variable(model.optimizer))
end

function MOI.supports_add_constrained_variable(
Expand All @@ -218,6 +219,20 @@ function MOI.supports_add_constrained_variables(
return MOI.supports_add_constrained_variables(model.optimizer, MOI.Reals)
end

function MOI.supports_add_constrained_variable(
model::Optimizer,
::Type{S},
) where {S<:MOI.AbstractScalarSet}
return MOI.supports_add_constrained_variable(model.optimizer, S)
end

function MOI.supports_add_constrained_variables(
model::Optimizer,
::Type{S},
) where {S<:MOI.AbstractVectorSet}
return MOI.supports_add_constrained_variables(model.optimizer, S)
end

function MOI.add_constrained_variable(
model::Optimizer{T},
set::MOI.Parameter{T},
Expand All @@ -234,6 +249,22 @@ function MOI.add_constrained_variable(
return p, cp
end

function MOI.add_constrained_variable(
model::Optimizer,
set::MOI.AbstractScalarSet,
)
inner_vi, inner_ci = MOI.add_constrained_variable(model.optimizer, set)
return _add_variable(model, inner_vi), inner_ci # FIXME map inner_ci
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs a fix, right? Otherwise, vi and ci wont actually match

end

function MOI.add_constrained_variables(
model::Optimizer,
set::MOI.AbstractVectorSet,
)
inner_vis, inner_ci = MOI.add_constrained_variables(model.optimizer, set)
return _add_variable.(Ref(model), inner_vis), inner_ci # FIXME map inner_ci
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

end

function _add_to_constraint_map!(model::Optimizer, ci)
model.constraint_outer_to_inner[ci] = ci
return
Expand Down
7 changes: 4 additions & 3 deletions src/ParametricOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,11 @@ mutable struct Optimizer{T,OT<:MOI.ModelLike} <: MOI.AbstractOptimizer

# extension data
ext::Dict{Symbol,Any}
function Optimizer(
function Optimizer{T}(
optimizer::OT;
evaluate_duals::Bool = true,
save_original_objective_and_constraints::Bool = true,
) where {OT}
T = Float64
) where {T,OT}
return new{T,OT}(
optimizer,
MOI.Utilities.CleverDicts.CleverDict{ParameterIndex,T}(
Expand Down Expand Up @@ -231,6 +230,8 @@ mutable struct Optimizer{T,OT<:MOI.ModelLike} <: MOI.AbstractOptimizer
end
end

Optimizer(args...; kws...) = Optimizer{Float64}(args...; kws...)

function _next_variable_index!(model::Optimizer)
return model.last_variable_index_added += 1
end
Expand Down
58 changes: 58 additions & 0 deletions test/moi_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1919,3 +1919,61 @@ function test_no_quadratic_terms()
@test MOI.get(optimizer, MOI.ConstraintDual(), c) ≈ -1 atol = ATOL
return
end

function test_constrained_variable()
optimizer = POI.Optimizer(GLPK.Optimizer())
MOI.set(optimizer, MOI.Silent(), true)
set = MOI.LessThan(1.0)
@test MOI.supports_add_constrained_variable(optmizer, typeof(set))
x, c = MOI.add_constrained_variable(optimizer, set)
@test c isa MOI.ConstraintIndex{typeof(x),typeof(set)}
@test MOI.get(optimizer, MOI.ConstraintFunction(), c) ≈ x
@test MOI.get(optimizer, MOI.ConstraintSet(), c) == set
@test MOI.supports(optimizer, MOI.VariableName(), typeof(x))
MOI.set(optimizer, MOI.VariableName(), x, "vname")
@test MOI.get(optimizer, MOI.VariableName(), x) == "vname"
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE)
obj_func = 1.0 * x
MOI.set(optimizer, MOI.ObjectiveFunction{typeof(obj_func)}(), obj_func)
MOI.optimize!(optimizer)
@test MOI.get(optimizer, MOI.ConstraintDual(), c) ≈ -1 atol = ATOL
@test MOI.get(optimizer, MOI.VariablePrimal(), x) ≈ 1 atol = ATOL
return
end

include("no_free_model.jl")

function test_constrained_variable()
optimizer = POI.Optimizer(NoFreeVariablesModel{Float64}())
set = MOI.LessThan(1.0)
@test MOI.supports_add_constrained_variable(optimizer, typeof(set))
x, c = MOI.add_constrained_variable(optimizer, set)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably add a test to check the match between ci and vi.

@test c isa MOI.ConstraintIndex{typeof(x),typeof(set)}
@test MOI.get(optimizer, MOI.ConstraintFunction(), c) ≈ x
@test MOI.get(optimizer, MOI.ConstraintSet(), c) == set
@test MOI.supports(optimizer, MOI.VariableName(), typeof(x))
MOI.set(optimizer, MOI.VariableName(), x, "vname")
@test MOI.get(optimizer, MOI.VariableName(), x) == "vname"
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE)
obj_func = 1.0 * x
MOI.set(optimizer, MOI.ObjectiveFunction{typeof(obj_func)}(), obj_func)
return
end

function test_constrained_variables()
optimizer = POI.Optimizer{Int}(NoFreeVariablesModel{Int}())
set = MOI.Nonnegatives(2)
@test MOI.supports_add_constrained_variables(optimizer, typeof(set))
x, c = MOI.add_constrained_variables(optimizer, set)
@test c isa MOI.ConstraintIndex{MOI.VectorOfVariables,typeof(set)}
@test MOI.get(optimizer, MOI.ConstraintFunction(), c) ≈
MOI.VectorOfVariables(x)
@test MOI.get(optimizer, MOI.ConstraintSet(), c) == set
@test MOI.supports(optimizer, MOI.VariableName(), eltype(x))
MOI.set(optimizer, MOI.VariableName(), x[1], "vname")
@test MOI.get(optimizer, MOI.VariableName(), x[1]) == "vname"
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MIN_SENSE)
obj_func = 1 * x[1] + 1 * x[2]
MOI.set(optimizer, MOI.ObjectiveFunction{typeof(obj_func)}(), obj_func)
return
end
40 changes: 40 additions & 0 deletions test/no_free_model.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
MOI.Utilities.@model(
NoFreeVariablesModel,
(),
(),
(MOI.Nonnegatives,),
(),
(),
(),
(MOI.VectorOfVariables,),
(),
)

function MOI.supports_constraint(
::NoFreeVariablesModel,
::Type{MOI.VectorOfVariables},
::Type{MOI.Reals},
)
return false
end

function MOI.supports_add_constrained_variable(
::NoFreeVariablesModel{T},
::Type{MOI.LessThan{T}},
) where {T}
return true
end

function MOI.supports_add_constrained_variables(
::NoFreeVariablesModel,
::Type{MOI.Nonnegatives},
)
return true
end

function MOI.supports_add_constrained_variables(
::NoFreeVariablesModel,
::Type{MOI.Reals},
)
return false
end
Loading