-
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 9 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,88 @@ 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 | ||
|
|
||
| function _square_to_triangle_indices( | ||
| bridge::MOI.Bridges.Constraint.SquareBridge, | ||
| ) | ||
| s = bridge.square_set | ||
| dim = MOI.side_dimension(s) | ||
| offset = _square_offset(s) | ||
| upper_triangle_indices = collect(1:offset) | ||
| sizehint!(upper_triangle_indices, offset + div(dim * (dim + 1), 2)) | ||
| k = offset | ||
| for j in 1:dim | ||
| for i in 1:j | ||
| k += 1 | ||
| push!(upper_triangle_indices, k) | ||
| end | ||
| k += dim - j | ||
| end | ||
| return upper_triangle_indices | ||
| end | ||
|
|
||
| """ | ||
| _triangle_to_square_scalars(tri_scalars, s) | ||
|
|
||
| Expand triangle-vectorized scalars to square column-major form, mirroring | ||
| off-diagonal entries. `s` is the square set (e.g. `PositiveSemidefiniteConeSquare`). | ||
| """ | ||
| function _triangle_to_square_scalars(tri_scalars, s) | ||
| dim = MOI.side_dimension(s) | ||
| offset = _square_offset(s) | ||
| square_dim = offset + dim * dim | ||
| square = Vector{eltype(tri_scalars)}(undef, square_dim) | ||
| for i in 1:offset | ||
| square[i] = tri_scalars[i] | ||
| end | ||
| tri_k = offset | ||
| for j in 1:dim | ||
| for i in 1:j | ||
| tri_k += 1 | ||
| ij = offset + i + (j - 1) * dim | ||
| square[ij] = tri_scalars[tri_k] | ||
| if i != j | ||
| ji = offset + j + (i - 1) * dim | ||
| square[ji] = tri_scalars[tri_k] | ||
| end | ||
| end | ||
| end | ||
| return square | ||
| end | ||
|
|
||
| function MOI.set( | ||
| model::MOI.ModelLike, | ||
| attr::DiffOpt.ForwardConstraintFunction, | ||
| bridge::MOI.Bridges.Constraint.SquareBridge{T}, | ||
| func::MOI.VectorAffineFunction{T}, | ||
| ) where {T} | ||
| indices = _square_to_triangle_indices(bridge) | ||
| tri_func = MOI.Utilities.eachscalar(func)[indices] | ||
| return MOI.set(model, attr, bridge.triangle, tri_func) | ||
|
||
| end | ||
|
|
||
| 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_raw = MOI.get(model, attr, bridge.triangle) | ||
| tri_func = DiffOpt.standard_form(tri_func_raw) | ||
| tri_scalars = MOI.Utilities.eachscalar(tri_func) | ||
| square_scalars = _triangle_to_square_scalars(tri_scalars, bridge.square_set) | ||
| return MOI.Utilities.operate(vcat, T, square_scalars...) | ||
| 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