-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from blegat/bl/invariant_polytope_inplace
Modify optimizer in-place in invariant_polytope
- Loading branch information
Showing
10 changed files
with
246 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,94 @@ | ||
using JuMP | ||
using ParameterJuMP | ||
using Polyhedra | ||
mutable struct BalancedComplexPolytope{T, VT <: AbstractVector{T}, D<:Polyhedra.FullDim} | ||
mutable struct BalancedComplexPolytope{T, CT, VT <: AbstractVector{CT}, D<:Polyhedra.FullDim} | ||
d::D | ||
points::Vector{VT} | ||
optimizer_constructor | ||
model::Union{Nothing, JuMP.Model} | ||
real_z::Union{Nothing, Vector{ParameterJuMP.ParameterRef}} | ||
imag_z::Union{Nothing, Vector{ParameterJuMP.ParameterRef}} | ||
t_0::Union{Nothing, JuMP.VariableRef} | ||
function BalancedComplexPolytope{T, VT, D}( | ||
model::Union{Nothing, MOI.ModelLike} | ||
sum_con::Union{Nothing, MOI.ConstraintIndex{MOI.ScalarAffineFunction{T}, MOI.LessThan{T}}} | ||
real_z::Union{Nothing, Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{T}, MOI.EqualTo{T}}}} | ||
imag_z::Union{Nothing, Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{T}, MOI.EqualTo{T}}}} | ||
t_0::Union{Nothing, MOI.VariableIndex} | ||
function BalancedComplexPolytope{T, CT, VT, D}( | ||
d::Polyhedra.FullDim, points::Polyhedra.PointIt, | ||
optimizer_constructor=nothing) where {T, VT, D} | ||
new{T, VT, D}(Polyhedra.FullDim_convert(D, d), | ||
Polyhedra.lazy_collect(points), optimizer_constructor, nothing, nothing, nothing, nothing) | ||
optimizer_constructor=nothing) where {T, CT, VT, D} | ||
new{T, CT, VT, D}(Polyhedra.FullDim_convert(D, d), | ||
Polyhedra.lazy_collect(points), optimizer_constructor, nothing, nothing, nothing, nothing) | ||
end | ||
end | ||
function BalancedComplexPolytope(d::Polyhedra.FullDim, points::Polyhedra.PointIt, args...) | ||
return BalancedComplexPolytope{Polyhedra.coefficient_type(eltype(points)), eltype(points), | ||
typeof(d)}(d, points, args...) | ||
VT = eltype(points) | ||
CT = Polyhedra.coefficient_type(VT) | ||
return BalancedComplexPolytope{real(CT), CT, VT, typeof(d)}(d, points, args...) | ||
end | ||
|
||
Polyhedra.FullDim(v::BalancedComplexPolytope) = v.d | ||
|
||
function _reset_model(p::BalancedComplexPolytope) | ||
p.model = nothing | ||
p.real_z = nothing | ||
p.imag_z = nothing | ||
p.t_0 = nothing | ||
end | ||
|
||
function _build_model(brp::BalancedComplexPolytope) | ||
function _build_model(brp::BalancedComplexPolytope{T}) where T | ||
@assert brp.model === nothing | ||
n = length(brp.points) | ||
brp.model = ParameterJuMP.ModelWithParams(brp.optimizer_constructor) | ||
brp.model = MOI.instantiate(brp.optimizer_constructor, with_bridge_type = T) | ||
# Real part of `t` | ||
α = @variable(brp.model, [1:n]) | ||
α = Vector{MOI.VariableIndex}(undef, n) | ||
# Imaginary part of `t` | ||
β = @variable(brp.model, [1:n]) | ||
q = @variable(brp.model, [1:n]) | ||
β = Vector{MOI.VariableIndex}(undef, n) | ||
q = Vector{MOI.VariableIndex}(undef, n) | ||
# |t| ≤ q | ||
for i in 1:n | ||
@constraint(brp.model, [q[i], α[i], β[i]] in SecondOrderCone()) | ||
vars, con = MOI.add_constrained_variables(brp.model, MOI.SecondOrderCone(3)) | ||
q[i], α[i], β[i] = vars | ||
end | ||
# sum |t| ≤ sum q ≤ 1 | ||
@constraint(brp.model, sum(q) ≤ 1) | ||
return sum(α[i] .* real.(brp.points[i]) for i in 1:n) - sum(β[i] .* imag.(brp.points[i]) for i in 1:n), | ||
sum(α[i] .* imag.(brp.points[i]) for i in 1:n) + sum(β[i] .* real.(brp.points[i]) for i in 1:n) | ||
end | ||
|
||
function _fix(p::BalancedComplexPolytope, v) | ||
JuMP.fix.(brp.real_z, real.(v)) | ||
JuMP.fix.(brp.imag_z, imag.(v)) | ||
brp.sum_con = MOI.add_constraint(brp.model, _sum(q, T), MOI.LessThan(one(T))) | ||
rp = [real.(p) for p in brp.points] | ||
ip = [imag.(p) for p in brp.points] | ||
real_hull = MOI.Utilities.operate!.( | ||
-, T, | ||
_convex_combination(α, rp, brp.d), | ||
_convex_combination(β, ip, brp.d)) | ||
imag_hull = MOI.Utilities.operate!.( | ||
+, T, | ||
_convex_combination(α, ip, brp.d), | ||
_convex_combination(β, rp, brp.d)) | ||
return real_hull, imag_hull | ||
end | ||
|
||
function _parametrized_model(brp::BalancedComplexPolytope, v) | ||
real_hull, imag_hull = _build_model(brp) | ||
brp.real_z = ParameterJuMP.add_parameters(brp.model, collect(real.(v))) | ||
brp.imag_z = ParameterJuMP.add_parameters(brp.model, collect(imag.(v))) | ||
@constraint(brp.model, brp.real_z .== real_hull) | ||
@constraint(brp.model, brp.imag_z .== imag_hull) | ||
brp.real_z = [MOI.add_constraint(brp.model, real_hull[i], MOI.EqualTo(real(v[i]))) for i in 1:brp.d] | ||
brp.imag_z = [MOI.add_constraint(brp.model, imag_hull[i], MOI.EqualTo(imag(v[i]))) for i in 1:brp.d] | ||
end | ||
|
||
function _ratio_model(brp::BalancedComplexPolytope, v) | ||
function _ratio_model(brp::BalancedComplexPolytope{T}, v) where T | ||
real_hull, imag_hull = _build_model(brp) | ||
brp.t_0 = @variable(brp.model) | ||
@constraint(brp.model, brp.t_0 .* real.(v) .== real_hull) | ||
@constraint(brp.model, brp.t_0 .* imag.(v) .== imag_hull) | ||
brp.t_0 = MOI.add_variable(brp.model) | ||
for i in 1:brp.d | ||
push!(real_hull[i].terms, MOI.ScalarAffineTerm(-real(v[i]), brp.t_0)) | ||
push!(imag_hull[i].terms, MOI.ScalarAffineTerm(-imag(v[i]), brp.t_0)) | ||
end | ||
brp.real_z = [MOI.add_constraint(brp.model, real_hull[i], MOI.EqualTo(zero(T))) for i in 1:brp.d] | ||
brp.imag_z = [MOI.add_constraint(brp.model, imag_hull[i], MOI.EqualTo(zero(T))) for i in 1:brp.d] | ||
end | ||
|
||
function _update_model(p::BalancedComplexPolytope{T}, v) where T | ||
vars, con = MOI.add_constrained_variables(p.model, MOI.SecondOrderCone(3)) | ||
q, α, β = vars | ||
MOI.modify(p.model, p.sum_con, MOI.ScalarCoefficientChange(q, one(T))) | ||
for i in 1:p.d | ||
MOI.modify(p.model, p.real_z[i], MOI.ScalarCoefficientChange(α, real(v[i]))) | ||
MOI.modify(p.model, p.imag_z[i], MOI.ScalarCoefficientChange(β, imag(v[i]))) | ||
end | ||
end | ||
|
||
function _fix(bcp::BalancedComplexPolytope, v) | ||
for i in 1:bcp.d | ||
if bcp.t_0 === nothing | ||
MOI.set(bcp.model, MOI.ConstraintSet(), bcp.real_z[i], MOI.EqualTo(real(v[i]))) | ||
MOI.set(bcp.model, MOI.ConstraintSet(), bcp.imag_z[i], MOI.EqualTo(imag(v[i]))) | ||
else | ||
MOI.modify(bcp.model, bcp.real_z[i], MOI.ScalarCoefficientChange(bcp.t_0, -real(v[i]))) | ||
MOI.modify(bcp.model, bcp.imag_z[i], MOI.ScalarCoefficientChange(bcp.t_0, -imag(v[i]))) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
b767d51
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register()
b767d51
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error while trying to register: "Tag with name
0.1.0
already exists and points to a different commit"