Skip to content

Commit

Permalink
Fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Oct 1, 2023
1 parent 6798140 commit 1ea12ac
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 42 deletions.
76 changes: 45 additions & 31 deletions src/QCQP/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -37,13 +40,10 @@ end
MOI.is_valid(model::Optimizer, i::MOI.Index) = MOI.is_valid(model.model, i)
function MOI.is_valid(

Check warning on line 41 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L40-L41

Added lines #L40 - L41 were not covered by tests
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) &&

Check warning on line 45 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L45

Added line #L45 was not covered by tests
MOI.is_valid(model.constraints[S][2], ci)
MOI.is_valid(model.constraints[S][2], ci)
end

function MOI.get(

Check warning on line 49 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L49

Added line #L49 was not covered by tests
Expand Down Expand Up @@ -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)

Check warning on line 101 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L100-L101

Added lines #L100 - L101 were not covered by tests
end

function MOI.get(model::Optimizer, attr::MOI.AbstractModelAttribute)
Expand Down Expand Up @@ -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)

Check warning on line 143 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L139-L143

Added lines #L139 - L143 were not covered by tests
end
return MOI.add_constraint(model.constraints[S][2], func, set)

Check warning on line 145 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L145

Added line #L145 was not covered by tests
end

function MOI.get(

Check warning on line 148 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L148

Added line #L148 was not covered by tests
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)

Check warning on line 153 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L153

Added line #L153 was not covered by tests
end

function MOI.get(

Check warning on line 156 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L156

Added line #L156 was not covered by tests
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)

Check warning on line 160 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L160

Added line #L160 was not covered by tests
end
Expand Down Expand Up @@ -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!(

Check warning on line 202 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L202

Added line #L202 was not covered by tests
p::PolyJuMP.ScalarPolynomialFunction{T,P},
::Nothing,
) where {T,P}
return p,

Check warning on line 206 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L206

Added line #L206 was not covered by tests
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!(

Check warning on line 212 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L212

Added line #L212 was not covered by tests
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))
Expand All @@ -234,7 +232,10 @@ function _subs!(p::PolyJuMP.ScalarPolynomialFunction, index_to_var::Dict{K,V}) w
return p, index_to_var

Check warning on line 232 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L232

Added line #L232 was not covered by tests
end

function _add_variables!(p::PolyJuMP.ScalarPolynomialFunction{T,P}, d) where {T,P}
function _add_variables!(

Check warning on line 235 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L235

Added line #L235 was not covered by tests
p::PolyJuMP.ScalarPolynomialFunction{T,P},
d,
) where {T,P}
if isnothing(d)
d = Dict{MP.monomial_type(P),MOI.VariableIndex}()

Check warning on line 240 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L239-L240

Added lines #L239 - L240 were not covered by tests
else
Expand All @@ -249,7 +250,12 @@ function _add_variables!(p::PolyJuMP.ScalarPolynomialFunction{T,P}, d) where {T,
return d

Check warning on line 250 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L247-L250

Added lines #L247 - L250 were not covered by tests
end

function monomial_variable_index(model::Optimizer{T}, d::Dict, div, mono::MP.AbstractMonomialLike) where {T}
function monomial_variable_index(

Check warning on line 253 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L253

Added line #L253 was not covered by tests
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)
Expand All @@ -263,8 +269,10 @@ function monomial_variable_index(model::Optimizer{T}, d::Dict, div, mono::MP.Abs
l = max(l, zero(T))

Check warning on line 269 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L259-L269

Added lines #L259 - L269 were not covered by tests
end
u = max(bounds...)
d[mono], _ = MOI.add_constrained_variable(model.model, MOI.Interval(l, u))
MOI.add_constraint(model,
d[mono], _ =

Check warning on line 272 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L271-L272

Added lines #L271 - L272 were not covered by tests
MOI.add_constrained_variable(model.model, MOI.Interval(l, u))
MOI.add_constraint(

Check warning on line 274 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L274

Added line #L274 was not covered by tests
model,
MA.@rewrite(one(T) * d[mono] - one(T) * vx * vy),

Check warning on line 276 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L276

Added line #L276 was not covered by tests
MOI.EqualTo(zero(T)),
)
Expand Down Expand Up @@ -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(

Check warning on line 304 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L302-L304

Added lines #L302 - L304 were not covered by tests
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)
Expand Down
25 changes: 19 additions & 6 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(

Check warning on line 147 in src/functions.jl

View check run for this annotation

Codecov / codecov/patch

src/functions.jl#L147

Added line #L147 was not covered by tests
::Type{VectorPolynomialFunction{T,P}},
) where {T,P}
return PolyJuMP.ScalarPolynomialFunction{T,P}

Check warning on line 150 in src/functions.jl

View check run for this annotation

Codecov / codecov/patch

src/functions.jl#L150

Added line #L150 was not covered by tests
end

Expand All @@ -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
Expand Down Expand Up @@ -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(

Check warning on line 209 in src/functions.jl

View check run for this annotation

Codecov / codecov/patch

src/functions.jl#L209

Added line #L209 was not covered by tests
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))

Check warning on line 213 in src/functions.jl

View check run for this annotation

Codecov / codecov/patch

src/functions.jl#L212-L213

Added lines #L212 - L213 were not covered by tests
end
15 changes: 10 additions & 5 deletions test/qcqp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1ea12ac

Please sign in to comment.