Skip to content

Commit

Permalink
<#56> Add MathOptInterface support
Browse files Browse the repository at this point in the history
- merge branch 'mg/MOptInterface' into devel
  • Loading branch information
migarstka committed Jan 7, 2019
2 parents 6245048 + b98d7db commit 21d1811
Show file tree
Hide file tree
Showing 14 changed files with 1,146 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ notifications:
git:
depth: 3


## uncomment the following lines to allow failures on nightly julia
## (tests will run but not make your overall status red)
matrix:
allow_failures:
- julia: nightly


## uncomment and modify the following lines to manually install system packages
#addons:
# apt: # apt-get for linux
Expand All @@ -29,6 +31,7 @@ matrix:
## uncomment the following lines to override the default test script
script:
- julia -e 'if VERSION >= v"0.7.0-" using Pkg; end; Pkg.clone(pwd()); Pkg.build("COSMO"); Pkg.test("COSMO"; coverage=true)';

after_success:
# push coverage results to Codecov
- julia -e 'if VERSION >= v"0.7.0-" using Pkg; end; cd(Pkg.dir("COSMO")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,40 @@ with decision variables `x ϵ R^n`, `s ϵ R^m` and data matrices `P=P'>=0`, `q
- Add the package via the package manager (type `]`): `add https://github.com/oxfordcontrol/COSMO.jl`
- Make the package available with `using COSMO`

### Example
### Example - JuMP
We consider the problem of finding the closest correlation matrix X, i.e. PSD and ones on the diagonal, to a random matrix C.
```julia
using COSMO, JuMP, LinearAlgebra, SparseArrays, Test, Random
rng = Random.MersenneTwister(12345);

# Original problem has the following format:
# min_X 1/2 ||X-C||^2
# s.t. Xii = 1
# X ⪴ 0

# create a random test matrix C
n = 8
C = -1 .+ rand(rng, n, n) .* 2;
c = vec(C);

# define problem in JuMP
q = -vec(C);
r = 0.5 * vec(C)' * vec(C);
m = Model(with_optimizer(COSMO.Optimizer, verbose=true, eps_abs = 1e-4));
@variable(m, X[1:n, 1:n], PSD);
x = vec(X);
@objective(m, Min, 0.5 * x' * x + q' * x + r)
for i = 1:n
@constraint(m, X[i, i] == 1.)
end

# solve and get results
status = JuMP.optimize!(m)
obj_val = JuMP.objective_value(m)
X_sol = JuMP.value.(X)
``

### Example - Direct solver interface
```julia
using COSMO, LinearAlgebra, SparseArrays, Test
Expand Down Expand Up @@ -82,7 +115,7 @@ check_termination | Check termination interval | 40
check_infeasibility | Check infeasibility interval | 40
scaling | Number of scaling iterations | 10
adaptive_rho | Automatic adaptation of step size parameter | true
time_limit | set solver time limit in s | 0
time_limit | set solver time limit in s | 0.0

For more low-level settings, see the Settings definition in `/src/types.jl`.

Expand Down
3 changes: 2 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
julia 0.7
julia 0.7
MathOptInterface 0.8 0.9
47 changes: 47 additions & 0 deletions examples/closest_correlation_matrix.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Test script to test solver with JuMP on a closest correlation matrix problem
using COSMO, JuMP, LinearAlgebra, SparseArrays, Test, Random
rng = Random.MersenneTwister(12345);

# Original problem has the following format:
# min_X 1/2 ||X-C||^2
# s.t. Xii = 1
# X ⪴ 0

# create a random test matrix C
n = 8
C = -1 .+ rand(rng, n, n) .* 2;
c = vec(C);

# define problem in JuMP
q = -vec(C);
r = 0.5 * vec(C)' * vec(C);
m = Model(with_optimizer(COSMO.Optimizer, verbose=true, eps_abs = 1e-4));
@variable(m, X[1:n, 1:n], PSD);
x = vec(X);
@objective(m, Min, 0.5 * x' * x + q' * x + r)
for i = 1:n
@constraint(m, X[i, i] == 1.)
end

# solve and get results
status = JuMP.optimize!(m)
obj_val = JuMP.objective_value(m)
X_sol = JuMP.value.(X)

known_opt_val = 12.5406
known_solution = [
1.0 0.732562 -0.319491 -0.359985 -0.287543 -0.15578 0.0264044 -0.271438;
0.732562 1.0 0.0913246 -0.0386357 0.299199 -0.122733 0.126612 -0.187489;
-0.319491 0.0913246 1.0 -0.0863377 0.432948 0.461783 -0.248641 -0.395299;
-0.359985 -0.0386357 -0.0863377 1.0 0.503379 0.250601 0.141151 0.286088;
-0.287543 0.299199 0.432948 0.503379 1.0 -0.0875199 0.137518 0.0262425;
-0.15578 -0.122733 0.461783 0.250601 -0.0875199 1.0 -0.731556 0.0841783;
0.0264044 0.126612 -0.248641 0.141151 0.137518 -0.731556 1.0 -0.436274;
-0.271438 -0.187489 -0.395299 0.286088 0.0262425 0.0841783 -0.436274 1.0 ];


@testset "Closest correlation matrix example" begin
@test isapprox(obj_val, known_opt_val , atol=1e-3)
@test norm(X_sol - known_solution, Inf) < 1e-3
end
nothing
1 change: 1 addition & 0 deletions src/COSMO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ include("./printing.jl") # TODO: unmodified - revisit
include("./setup.jl") # TODO: unmodified - revisit (short - consolidate?)
include("./solver.jl") # TODO: unmodified - revisit
include("./interface.jl") # TODO: unmodified - revisit
include("./MOIWrapper.jl")

end #end module

0 comments on commit 21d1811

Please sign in to comment.