Skip to content

Commit b19fcf2

Browse files
authored
Add example (#23)
* Add example * Fix format * Add JuliaFormatter v2
1 parent 780b3fc commit b19fcf2

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

.github/workflows/format_check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
shell: julia --color=yes {0}
1919
run: |
2020
using Pkg
21-
Pkg.add(PackageSpec(name="JuliaFormatter", version="1"))
21+
Pkg.add(PackageSpec(name="JuliaFormatter", version="2"))
2222
using JuliaFormatter
2323
format(".", verbose=true)
2424
out = String(read(Cmd(`git diff`)))

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,58 @@ Extends MathOptInterface (MOI) to low-rank constraints.
1414
See "Why you should stop using the monomial basis" at [JuMP-dev 2024](https://jump.dev/meetings/jumpdev2024/) : [slides](https://jump.dev/assets/jump-dev-workshops/2024/legat.html) [video](https://youtu.be/CGPHaHxCG2w)
1515

1616
Started as an [MOI issue](https://github.com/jump-dev/MathOptInterface.jl/issues/2197) and [MOI PR](https://github.com/jump-dev/MathOptInterface.jl/pull/2198).
17+
18+
## Use with JuMP
19+
20+
To use LowRankOpt with [JuMP](https://github.com/jump-dev/JuMP.jl), use
21+
add its bridge to your model with:
22+
```julia
23+
model = Model()
24+
LRO.add_all_bridges(model, Float64)
25+
```
26+
Then, either use the `LRO.SetDotProducts` or `LRO.LinearCombinationInSet`.
27+
[Check with `print_active_bridges(model)`](https://jump.dev/JuMP.jl/stable/tutorials/conic/ellipse_approx/)
28+
to see if the solver receives the low-rank constraint or if it is transformed to classical constraints.
29+
The solvers that support `LRO.SetDotProducts` are:
30+
* [DSDP.jl](https://github.com/jump-dev/DSDP.jl/pull/37), [Hypatia.jl](https://github.com/jump-dev/Hypatia.jl/pull/844), [SDPLR.jl](https://github.com/jump-dev/SDPLR.jl/pull/26)
31+
The solvers that support `LRO.LinearCombinationInSet` are:
32+
* [Hypatia.jl](https://github.com/jump-dev/Hypatia.jl/pull/844)
33+
If you use `LRO.LinearCombinationInSet` while the solvers supports `LRO.SetDotProducts` or vice versa, simply [use a `Dualization.jl` layer](https://jump.dev/JuMP.jl/stable/tutorials/conic/dualization/).
34+
35+
Note that `Hypatia.jl` only supports `LRO.SetDotProducts{LRO.WITHOUT_SET}` or `LRO.LinearCombinationInSet{LRO.WITHOUT_SET}` and not the `LRO.WITH_SET` version.
36+
37+
## Example
38+
39+
Below is [this example](https://github.com/jump-dev/SDPLR.jl?tab=readme-ov-file#example-modifying-the-rank-and-checking-optimality)
40+
adapted to exploit the low-rank constraints.
41+
42+
```julia-repl
43+
julia> include(joinpath(dirname(dirname(pathof(LowRankOpt))), "examples", "maxcut.jl"))
44+
maxcut (generic function with 2 methods)
45+
46+
julia> model = maxcut(weights, SDPLR.Optimizer);
47+
48+
julia> optimize!(model)
49+
50+
*** SDPLR 1.03-beta ***
51+
52+
===================================================
53+
major minor val infeas time
54+
---------------------------------------------------
55+
1 9 1.77916821e+02 2.0e+01 0
56+
2 12 -1.78345610e+01 4.7e-01 0
57+
3 14 -1.80546137e+01 2.5e-01 0
58+
4 16 -1.80170234e+01 9.7e-02 0
59+
5 18 -1.79967453e+01 4.0e-02 0
60+
6 19 -1.79987069e+01 1.1e-02 0
61+
7 20 -1.79999405e+01 1.7e-03 0
62+
8 21 -1.79999841e+01 3.3e-04 0
63+
9 22 -1.79999912e+01 5.9e-06 0
64+
===================================================
65+
66+
DIMACS error measures: 5.86e-06 0.00e+00 0.00e+00 0.00e+00 2.25e-05 9.84e-06
67+
68+
69+
julia> objective_value(model)
70+
17.99998881724702
71+
```

examples/maxcut.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using LinearAlgebra, JuMP, LowRankOpt
2+
3+
function maxcut(weights, solver)
4+
N = LinearAlgebra.checksquare(weights)
5+
L = Diagonal(weights * ones(N)) - weights
6+
model = Model(solver)
7+
LRO.Bridges.add_all_bridges(backend(model).optimizer, Float64)
8+
cone = MOI.PositiveSemidefiniteConeTriangle(N)
9+
factors = LRO.TriangleVectorization.(
10+
LRO.positive_semidefinite_factorization.(e_i.(1:N, N)),
11+
)
12+
set = LRO.SetDotProducts{LRO.WITH_SET}(cone, factors)
13+
@variable(
14+
model,
15+
dot_prod_set[1:(length(factors)+MOI.dimension(cone))] in set
16+
)
17+
dot_prod = dot_prod_set[1:length(factors)]
18+
X = reshape_vector(
19+
dot_prod_set[length(factors) .+ (1:MOI.dimension(cone))],
20+
SymmetricMatrixShape(N),
21+
)
22+
@objective(model, Max, dot(L, X) / 4)
23+
@constraint(model, dot_prod .== 1)
24+
return model
25+
end

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
33
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
44
LowRankOpt = "607ca3ad-272e-43c8-bcbe-fc71b56c935c"
55
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
6+
SDPLR = "56161740-ea4e-4253-9d15-43c62ff94d95"
67
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

0 commit comments

Comments
 (0)