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

Add support for MOI.Parameter #139

Merged
merged 1 commit into from
Nov 22, 2024
Merged
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
9 changes: 9 additions & 0 deletions src/eago_optimizer/moi_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ end

MOI.supports_constraint(::Optimizer, ::Type{VI}, ::Type{S}) where S <: VAR_SETS = true
MOI.supports_constraint(::Optimizer,::Type{T},::Type{S}) where {T<:Union{SAF,SQF,MOI.ScalarNonlinearFunction},S<:INEQ_SETS} = true
MOI.supports_add_constrained_variable(::Optimizer, ::Type{MOI.Parameter{Float64}}) = true

MOI.is_valid(m::Optimizer, v::VI) = (1 <= v.value <= m._input_problem._variable_count)
MOI.is_valid(m::Optimizer, c::CI{VI,S}) where S <: VAR_SETS = (1 <= c.value <= m._input_problem._variable_count)
Expand All @@ -62,6 +63,14 @@ function MOI.add_variable(m::Optimizer, name::String)
return vi
end

function MOI.add_constrained_variable(m::Optimizer, p::MOI.Parameter{Float64})
vi = VI(m._input_problem._variable_count += 1)
m._input_problem._variable_names[vi] = ""
ci = CI{VI, MOI.Parameter{Float64}}(m._input_problem._constraint_count += 1)
_constraints(m, VI, MOI.Parameter{Float64})[ci] = (vi, p)
return vi, ci
end

function MOI.add_constraint(m::Optimizer, f::F, s::S) where {F<:Union{SAF,SQF,MOI.ScalarNonlinearFunction},S<:INEQ_SETS}
check_inbounds!(m, f)
ci = CI{F, S}(m._input_problem._constraint_count += 1)
Expand Down
2 changes: 2 additions & 0 deletions src/eago_optimizer/optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ _constraints(m::Optimizer, ::Type{VI}, ::Type{IT}) = m._input_problem._vi_it_con
_constraints(m::Optimizer, ::Type{VI}, ::Type{ZO}) = m._input_problem._vi_zo_constraints
_constraints(m::Optimizer, ::Type{VI}, ::Type{INT}) = m._input_problem._vi_int_constraints

_constraints(m::Optimizer, ::Type{VI}, ::Type{MOI.Parameter{Float64}}) = m._input_problem._parameter_constraints

_constraints(m::Optimizer, ::Type{SAF}, ::Type{LT}) = _constraints(m._input_problem, SAF, LT)
_constraints(m::Optimizer, ::Type{SAF}, ::Type{GT}) = _constraints(m._input_problem, SAF, GT)
_constraints(m::Optimizer, ::Type{SAF}, ::Type{ET}) = _constraints(m._input_problem, SAF, ET)
Expand Down
1 change: 1 addition & 0 deletions src/eago_optimizer/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ function initial_parse!(m::Optimizer{R,S,T}) where {R,S,T}
variable_load_parse!(m, VI, ET)
variable_load_parse!(m, VI, ZO)
variable_load_parse!(m, VI, INT)
variable_load_parse!(m, VI, MOI.Parameter{Float64})
wp._variable_count = ip._variable_count

# Copy all constraint dictionaries from the input problem to the working problem
Expand Down
4 changes: 4 additions & 0 deletions src/eago_optimizer/types/global_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ Base.@kwdef mutable struct InputProblem
_vi_zo_constraints::Dict{CI{VI,ZO}, Tuple{VI,ZO}} = Dict{CI{VI,ZO}, Tuple{VI,ZO}}()
_vi_int_constraints::Dict{CI{VI,INT}, Tuple{VI,INT}} = Dict{CI{VI,INT}, Tuple{VI,INT}}()

_parameter_constraints::Dict{CI{VI,MOI.Parameter{Float64}}, Tuple{VI,MOI.Parameter{Float64}}} = Dict{CI{VI,MOI.Parameter{Float64}}, Tuple{VI,MOI.Parameter{Float64}}}()

_linear_leq_constraints::Dict{CI{SAF,LT}, Tuple{SAF,LT}} = Dict{CI{SAF,LT}, Tuple{SAF,LT}}()
_linear_geq_constraints::Dict{CI{SAF,GT}, Tuple{SAF,GT}} = Dict{CI{SAF,GT}, Tuple{SAF,GT}}()
_linear_eq_constraints::Dict{CI{SAF,ET}, Tuple{SAF,ET}} = Dict{CI{SAF,ET}, Tuple{SAF,ET}}()
Expand Down Expand Up @@ -368,6 +370,8 @@ _constraints(m::InputProblem, ::Type{VI}, ::Type{IT}) = m._vi_it_constraints
_constraints(m::InputProblem, ::Type{VI}, ::Type{ZO}) = m._vi_zo_constraints
_constraints(m::InputProblem, ::Type{VI}, ::Type{INT}) = m._vi_int_constraints

_constraints(m::InputProblem, ::Type{VI}, ::Type{MOI.Parameter{Float64}}) = m._parameter_constraints

_constraints(m::InputProblem, ::Type{SAF}, ::Type{LT}) = m._linear_leq_constraints
_constraints(m::InputProblem, ::Type{SAF}, ::Type{GT}) = m._linear_geq_constraints
_constraints(m::InputProblem, ::Type{SAF}, ::Type{ET}) = m._linear_eq_constraints
Expand Down
22 changes: 22 additions & 0 deletions src/eago_optimizer/types/variable_info.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,31 @@ function VariableInfo(v::VariableInfo{T}, s::INT) where {T <: AbstractFloat}
upper_bound = u)
end

function VariableInfo(v::VariableInfo{T}, p::MOI.Parameter{T}) where {T <: AbstractFloat}
isempty(v) && return v
l = max(p.value, lower_bound(v))
u = min(p.value, upper_bound(v))
check_isempty(l, u, is_integer(v)) && return empty_variable_info(T)
out = VariableInfo(is_integer = is_integer(v),
has_lower_bound = !isinf(l),
has_upper_bound = !isinf(u),
has_constraints = !isinf(l) | !isinf(u),
is_fixed = true,
lower_bound = l,
upper_bound = u)
return VariableInfo(is_integer = is_integer(v),
has_lower_bound = !isinf(l),
has_upper_bound = !isinf(u),
has_constraints = !isinf(l) | !isinf(u),
is_fixed = true,
lower_bound = l,
upper_bound = u)
end

ZO(v::VariableInfo) = MOI.ZeroOne()
ET(v::VariableInfo{T}) where {T <: AbstractFloat} = MOI.EqualTo{T}(v.lower_bound)
IT(v::VariableInfo{T}) where {T <: AbstractFloat} = MOI.Interval{T}(v.lower_bound,v.upper_bound)
GT(v::VariableInfo{T}) where {T <: AbstractFloat} = MOI.GreaterThan{T}(v.lower_bound)
LT(v::VariableInfo{T}) where {T <: AbstractFloat} = MOI.LessThan{T}(v.upper_bound)
INT(v::VariableInfo{T}) where {T <: AbstractFloat} = MOI.Semiinteger{T}(v.lower_bound, v.upper_bound)
MOI.Parameter{Float64}(v::VariableInfo{T}) where {T <: AbstractFloat} = MOI.Parameter{T}(v.lower_bound)
Loading