From 1ea12ac054733fbbc70e16e9736e870023365caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 1 Oct 2023 21:19:55 +0200 Subject: [PATCH] Fix format --- src/QCQP/MOI_wrapper.jl | 76 ++++++++++++++++++++++++----------------- src/functions.jl | 25 ++++++++++---- test/qcqp.jl | 15 +++++--- 3 files changed, 74 insertions(+), 42 deletions(-) diff --git a/src/QCQP/MOI_wrapper.jl b/src/QCQP/MOI_wrapper.jl index 30aefbd..9e6d25d 100644 --- a/src/QCQP/MOI_wrapper.jl +++ b/src/QCQP/MOI_wrapper.jl @@ -3,7 +3,10 @@ import MathOptInterface as MOI mutable struct Optimizer{T,O<:MOI.ModelLike} <: MOI.AbstractOptimizer model::O objective::Union{Nothing,PolyJuMP.ScalarPolynomialFunction{T}} - constraints::DataStructures.OrderedDict{Type,Tuple{Type,MOI.Utilities.VectorOfConstraints}} + constraints::DataStructures.OrderedDict{ + Type, + Tuple{Type,MOI.Utilities.VectorOfConstraints}, + } end function Optimizer{T}(model::MOI.ModelLike) where {T} @@ -37,13 +40,10 @@ end MOI.is_valid(model::Optimizer, i::MOI.Index) = MOI.is_valid(model.model, i) function MOI.is_valid( model::Optimizer{T}, - ::MOI.ConstraintIndex{ - PolyJuMP.ScalarPolynomialFunction{T}, - S - }, + ::MOI.ConstraintIndex{PolyJuMP.ScalarPolynomialFunction{T},S}, ) where {T,S<:MOI.AbstractScalarSet} return haskey(model.constraints, S) && - MOI.is_valid(model.constraints[S][2], ci) + MOI.is_valid(model.constraints[S][2], ci) end function MOI.get( @@ -98,7 +98,7 @@ function MOI.set( end function MOI.set(model::Optimizer, attr::MOI.AbstractModelAttribute, value) - MOI.set(model.model, attr, value) + return MOI.set(model.model, attr, value) end function MOI.get(model::Optimizer, attr::MOI.AbstractModelAttribute) @@ -140,31 +140,22 @@ function MOI.add_constraint( S = typeof(set) if !haskey(model.constraints, S) con = MOI.Utilities.VectorOfConstraints{F,S}() - model.constraints[S] = (P,con) + model.constraints[S] = (P, con) end return MOI.add_constraint(model.constraints[S][2], func, set) end function MOI.get( model::Optimizer{T}, - attr::Union{ - MOI.ConstraintFunction, - MOI.ConstraintSet, - }, - ci::MOI.ConstraintIndex{ - <:PolyJuMP.ScalarPolynomialFunction{T}, - S, - }, + attr::Union{MOI.ConstraintFunction,MOI.ConstraintSet}, + ci::MOI.ConstraintIndex{<:PolyJuMP.ScalarPolynomialFunction{T},S}, ) where {T,S} return MOI.get(model.constraints[S][2], attr, ci) end function MOI.get( model::Optimizer{T}, - attr::MOI.ListOfConstraintIndices{ - <:PolyJuMP.ScalarPolynomialFunction{T}, - S, - } + attr::MOI.ListOfConstraintIndices{<:PolyJuMP.ScalarPolynomialFunction{T},S}, ) where {T,S<:MOI.AbstractScalarSet} return MOI.get(model.constraints[S][2], attr) end @@ -208,13 +199,20 @@ function _add_monomials!(p::PolyJuMP.ScalarPolynomialFunction, monos1) end end -function _subs!(p::PolyJuMP.ScalarPolynomialFunction{T,P}, ::Nothing) where {T,P} - return p, Dict{MOI.VariableIndex,MP.variable_union_type(P)}( +function _subs!( + p::PolyJuMP.ScalarPolynomialFunction{T,P}, + ::Nothing, +) where {T,P} + return p, + Dict{MOI.VariableIndex,MP.variable_union_type(P)}( vi => var for (vi, var) in zip(p.variables, MP.variables(p.polynomial)) ) end -function _subs!(p::PolyJuMP.ScalarPolynomialFunction, index_to_var::Dict{K,V}) where {K,V} +function _subs!( + p::PolyJuMP.ScalarPolynomialFunction, + index_to_var::Dict{K,V}, +) where {K,V} old_var = V[] new_var = V[] for (vi, var) in zip(p.variables, MP.variables(p.polynomial)) @@ -234,7 +232,10 @@ function _subs!(p::PolyJuMP.ScalarPolynomialFunction, index_to_var::Dict{K,V}) w return p, index_to_var end -function _add_variables!(p::PolyJuMP.ScalarPolynomialFunction{T,P}, d) where {T,P} +function _add_variables!( + p::PolyJuMP.ScalarPolynomialFunction{T,P}, + d, +) where {T,P} if isnothing(d) d = Dict{MP.monomial_type(P),MOI.VariableIndex}() else @@ -249,7 +250,12 @@ function _add_variables!(p::PolyJuMP.ScalarPolynomialFunction{T,P}, d) where {T, return d end -function monomial_variable_index(model::Optimizer{T}, d::Dict, div, mono::MP.AbstractMonomialLike) where {T} +function monomial_variable_index( + model::Optimizer{T}, + d::Dict, + div, + mono::MP.AbstractMonomialLike, +) where {T} if !haskey(d, mono) x = div[mono] vx = monomial_variable_index(model, d, div, x) @@ -263,8 +269,10 @@ function monomial_variable_index(model::Optimizer{T}, d::Dict, div, mono::MP.Abs l = max(l, zero(T)) end u = max(bounds...) - d[mono], _ = MOI.add_constrained_variable(model.model, MOI.Interval(l, u)) - MOI.add_constraint(model, + d[mono], _ = + MOI.add_constrained_variable(model.model, MOI.Interval(l, u)) + MOI.add_constraint( + model, MA.@rewrite(one(T) * d[mono] - one(T) * vx * vy), MOI.EqualTo(zero(T)), ) @@ -293,10 +301,16 @@ function MOI.Utilities.final_touch(model::Optimizer{T}, _) where {T} end if !isempty(model.constraints) for S in keys(model.constraints) - for ci in MOI.get(model, MOI.ListOfConstraintIndices{ - PolyJuMP.ScalarPolynomialFunction{T,model.constraints[S][1]}, - S - }()) + for ci in MOI.get( + model, + MOI.ListOfConstraintIndices{ + PolyJuMP.ScalarPolynomialFunction{ + T, + model.constraints[S][1], + }, + S, + }(), + ) func = MOI.get(model, MOI.ConstraintFunction(), ci) func, index_to_var = _subs!(func, index_to_var) vars = _add_variables!(func, vars) diff --git a/src/functions.jl b/src/functions.jl index 975191a..f0d2a2a 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -75,7 +75,14 @@ function _to_polynomial!( end for t in func.quadratic_terms coef = t.variable_1 == t.variable_2 ? t.coefficient / 2 : t.coefficient - push!(terms, MP.term(coef, _to_polynomial!(d, T, t.variable_1) * _to_polynomial!(d, T, t.variable_2))) + push!( + terms, + MP.term( + coef, + _to_polynomial!(d, T, t.variable_1) * + _to_polynomial!(d, T, t.variable_2), + ), + ) end return MP.polynomial(terms) end @@ -134,10 +141,12 @@ function MOI.Utilities.is_coefficient_type( end # Placeholder for `promote_operation` -struct VectorPolynomialFunction{T,P<:MP.AbstractPolynomial{T}} <: MOI.AbstractVectorFunction -end +struct VectorPolynomialFunction{T,P<:MP.AbstractPolynomial{T}} <: + MOI.AbstractVectorFunction end -function MOI.Utilities.scalar_type(::Type{VectorPolynomialFunction{T,P}}) where {T,P} +function MOI.Utilities.scalar_type( + ::Type{VectorPolynomialFunction{T,P}}, +) where {T,P} return PolyJuMP.ScalarPolynomialFunction{T,P} end @@ -158,7 +167,9 @@ end function MOI.Utilities.promote_operation( ::typeof(-), ::Type{T}, - F::Type{<:Union{ScalarPolynomialFunction{T,P},VectorPolynomialFunction{T,P}}}, + F::Type{ + <:Union{ScalarPolynomialFunction{T,P},VectorPolynomialFunction{T,P}}, + }, ) where {T,P} return F end @@ -195,7 +206,9 @@ function MOI.Utilities.operate( p::ScalarPolynomialFunction{T,P}, f::Union{T,MOI.AbstractScalarFunction}, ) where {T,P} - d = Dict(vi => v for (vi, v) in zip(p.variables, MP.variables(p.polynomial))) + d = Dict( + vi => v for (vi, v) in zip(p.variables, MP.variables(p.polynomial)) + ) poly = _to_polynomial!(d, T, f) return _scalar_polynomial(d, T, op(p.polynomial, poly)) end diff --git a/test/qcqp.jl b/test/qcqp.jl index a3afbbd..427111c 100644 --- a/test/qcqp.jl +++ b/test/qcqp.jl @@ -49,14 +49,16 @@ end function _test_objective_or_constraint(x, y, T, obj::Bool) inner = Model{T}() optimizer = MOI.Utilities.MockOptimizer(inner) - model = PolyJuMP.JuMP.GenericModel{T}(() -> PolyJuMP.QCQP.Optimizer{T}(optimizer)) + model = PolyJuMP.JuMP.GenericModel{T}( + () -> PolyJuMP.QCQP.Optimizer{T}(optimizer), + ) PolyJuMP.@variable(model, 1 <= a <= 2) PolyJuMP.@variable(model, -5 <= b <= 3) PolyJuMP.@constraint(model, a + b >= 1) if obj - PolyJuMP.@objective(model, Min, a^3 - a^2 + 2a*b - b^2 + b^3) + PolyJuMP.@objective(model, Min, a^3 - a^2 + 2a * b - b^2 + b^3) else - PolyJuMP.@constraint(model, 0 <= a^3 - a^2 + 2a*b - b^2 + b^3 <= 1) + PolyJuMP.@constraint(model, 0 <= a^3 - a^2 + 2a * b - b^2 + b^3 <= 1) end PolyJuMP.optimize!(model) vis = MOI.get(inner, MOI.ListOfVariableIndices()) @@ -98,7 +100,9 @@ end function test_objective_and_constraint(x, y, T) inner = Model{T}() optimizer = MOI.Utilities.MockOptimizer(inner) - model = PolyJuMP.JuMP.GenericModel{T}(() -> PolyJuMP.QCQP.Optimizer{T}(optimizer)) + model = PolyJuMP.JuMP.GenericModel{T}( + () -> PolyJuMP.QCQP.Optimizer{T}(optimizer), + ) PolyJuMP.@variable(model, -2 <= a <= 3) PolyJuMP.@variable(model, 5 <= b <= 7) PolyJuMP.@constraint(model, 0 <= a^3 <= 1) @@ -136,7 +140,8 @@ function test_objective_and_constraint(x, y, T) @test length(cis) == 3 @test MOI.get(inner, MOI.ConstraintFunction(), cis[1]) ≈ o * a3 + z * a * b @test MOI.get(inner, MOI.ConstraintFunction(), cis[2]) ≈ o * b3 + z * a * b - @test MOI.get(inner, MOI.ConstraintFunction(), cis[3]) ≈ o * a3 * b3 + o * a6 + @test MOI.get(inner, MOI.ConstraintFunction(), cis[3]) ≈ + o * a3 * b3 + o * a6 end function runtests(x, y)