-
Notifications
You must be signed in to change notification settings - Fork 18
Fix vector functions #345
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
base: master
Are you sure you want to change the base?
Fix vector functions #345
Changes from all commits
75d8c90
977da16
b6895fa
df3eb6c
5617acb
da6701c
90efc8c
68707b8
a070be2
9eb811b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,6 +104,98 @@ function MOI.set( | |
| return MOI.set(model, attr, bridge.constraint, mapped_func) | ||
| end | ||
|
|
||
| """ | ||
| _square_offset(s::MOI.AbstractSymmetricMatrixSetSquare) | ||
|
|
||
| Number of extra entries before the matrix in a square-form set. | ||
| Own implementation to avoid depending on the private | ||
| `MOI.Bridges.Constraint._square_offset`. | ||
| """ | ||
| _square_offset(::MOI.AbstractSymmetricMatrixSetSquare) = 0 | ||
| _square_offset(::MOI.RootDetConeSquare) = 1 | ||
| _square_offset(::MOI.LogDetConeSquare) = 2 | ||
|
|
||
| # Similar to `MOI.set` for `MOI.ConstraintPrimalStart` on `SquareBridge` in | ||
| # MathOptInterface/src/Bridges/Constraint/bridges/SquareBridge.jl | ||
| function MOI.set( | ||
| model::MOI.ModelLike, | ||
| attr::DiffOpt.ForwardConstraintFunction, | ||
| bridge::MOI.Bridges.Constraint.SquareBridge{T}, | ||
| func::MOI.VectorAffineFunction{T}, | ||
| ) where {T} | ||
| dim = MOI.side_dimension(bridge.square_set) | ||
| offset = _square_offset(bridge.square_set) | ||
| scalars = MOI.Utilities.eachscalar(func) | ||
| tri_scalars = | ||
| Vector{eltype(scalars)}(undef, offset + div(dim * (dim + 1), 2)) | ||
| for i in 1:offset | ||
| tri_scalars[i] = scalars[i] | ||
| end | ||
| k = offset | ||
| for j in 1:dim, i in 1:j | ||
| k += 1 | ||
| tri_scalars[k] = scalars[offset+j+(i-1)*dim] | ||
| end | ||
| MOI.set( | ||
| model, | ||
| attr, | ||
| bridge.triangle, | ||
| MOI.Utilities.operate(vcat, T, tri_scalars...), | ||
| ) | ||
| for ((i, j), ci) in bridge.sym | ||
| f_ij = scalars[offset+i+(j-1)*dim] | ||
| f_ji = scalars[offset+j+(i-1)*dim] | ||
| MOI.set(model, attr, ci, MOI.Utilities.operate(-, T, f_ij, f_ji)) | ||
| end | ||
| return | ||
| end | ||
|
|
||
| # Adjoint of `MOI.set` for `ForwardConstraintFunction` on `SquareBridge` above. | ||
| # The forward map extracts upper triangle and sym diffs; this is its transpose. | ||
| # Similar structure to `MOI.get` for `MOI.ConstraintPrimal` on `SquareBridge` in | ||
| # MathOptInterface/src/Bridges/Constraint/bridges/SquareBridge.jl | ||
| function MOI.get( | ||
| model::MOI.ModelLike, | ||
| attr::DiffOpt.ReverseConstraintFunction, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should imitate
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I followed very closely the MOI code |
||
| bridge::MOI.Bridges.Constraint.SquareBridge{T}, | ||
| ) where {T} | ||
| tri_func = DiffOpt.standard_form(MOI.get(model, attr, bridge.triangle)) | ||
| tri = MOI.Utilities.eachscalar(tri_func) | ||
| dim = MOI.side_dimension(bridge.square_set) | ||
| offset = _square_offset(bridge.square_set) | ||
| square = Vector{eltype(tri)}(undef, offset + dim^2) | ||
| for i in 1:offset | ||
| square[i] = tri[i] | ||
| end | ||
| k = offset | ||
| sym_index = 1 | ||
| for j in 1:dim, i in 1:j | ||
| k += 1 | ||
| upper_index = offset + i + (j - 1) * dim | ||
| lower_index = offset + j + (i - 1) * dim | ||
| if i == j | ||
| square[upper_index] = tri[k] | ||
| elseif sym_index <= length(bridge.sym) && | ||
| bridge.sym[sym_index].first == (i, j) | ||
| π = DiffOpt.standard_form( | ||
| MOI.get(model, attr, bridge.sym[sym_index].second), | ||
| ) | ||
| square[upper_index] = MOI.Utilities.operate( | ||
| +, | ||
| T, | ||
| MOI.Utilities.operate(+, T, tri[k], tri[k]), | ||
| π, | ||
| ) | ||
| square[lower_index] = MOI.Utilities.operate(-, T, π) | ||
| sym_index += 1 | ||
| else | ||
| square[upper_index] = tri[k] | ||
| square[lower_index] = tri[k] | ||
| end | ||
| end | ||
| return MOI.Utilities.operate(vcat, T, square...) | ||
| end | ||
|
|
||
| function _variable_to_index_map(bridge) | ||
| return Dict{MOI.VariableIndex,MOI.VariableIndex}( | ||
| v => MOI.VariableIndex(i) for | ||
|
|
||
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.
cc @blegat