Skip to content

Commit a5373c5

Browse files
authored
Merge pull request #2 from jump-dev/bl/clean_up
Implement MOI interface for LP forms
2 parents de6863d + e17450a commit a5373c5

File tree

8 files changed

+629
-651
lines changed

8 files changed

+629
-651
lines changed

.travis.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
## Documentation: http://docs.travis-ci.com/user/languages/julia/
22
language: julia
3+
codecov: true
34
os:
45
- linux
56
julia:
6-
- 0.6
7-
- 0.7
87
- 1.0
8+
- 1
99
- nightly
1010
matrix:
1111
allow_failures:
@@ -23,7 +23,3 @@ notifications:
2323
on_success: change # options: [always|never|change] default: always
2424
on_failure: always # options: [always|never|change] default: always
2525
on_start: never # options: [always|never|change] default: always
26-
27-
after_success:
28-
- julia -e 'if VERSION >= v"0.7-"; using Pkg; else; cd(Pkg.dir("MatrixOptInterface")); end; Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
29-

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.1.0"
66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
88
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
9+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
910
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1011

1112
[compat]

README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,89 @@
11
# MatrixOptInterface.jl
2-
An interface for matrix form optimization problems
2+
3+
**This package is this in early development, feedback is welcome!**
4+
5+
An interface for optimization problems in matrix forms.
6+
Several matrix forms are represented in subtypes of
7+
[`MathOptInterface (MOI)`](https://github.com/jump-dev/MathOptInterface.jl)
8+
models.
9+
As they implement the MOI API, they can be copied directly to an MOI or JuMP
10+
model.
11+
12+
Here is a simple example copying a linear program represented in its standard
13+
form into JuMP:
14+
```julia
15+
import MatrixOptInterface
16+
const MatOI = MatrixOptInterface
17+
lp = MatOI.LPStandardForm{Float64, Matrix{Float64}}(MOI.MAX_SENSE, [1.0, 0.0], [1.0 1.0], [1.0])
18+
19+
using JuMP
20+
model = Model()
21+
MOI.copy_to(model, lp)
22+
```
23+
24+
Giving some arbitrary names to the variables for pretty printing:
25+
```julia
26+
set_name.(all_variables(model), "x" .* string.(1:2))
27+
print(model)
28+
```
29+
we get the following output:
30+
```
31+
Max x1
32+
Subject to
33+
x1 + x2 = 1.0
34+
[x1, x2] ∈ MathOptInterface.Nonnegatives(2)
35+
```
36+
37+
The LP standard form is the the only one implemented in this package.
38+
The 4 different available forms of linear programs are given below.
39+
In each form, `sense` is a `MOI.OptimizationSense` so its values
40+
are either `MOI.FEASIBILITY_SENSE`, `MOI.MAX_SENSE` or `MOI.MIN_SENSE`.
41+
42+
An LP standard form with `LPStandardForm(sense, c, A, b)`:
43+
```
44+
sense ⟨c, x⟩
45+
s.t.
46+
Ax == b
47+
x >= 0
48+
```
49+
50+
An LP geometric form with `LPGeometricForm(sense, c, A, b)`:
51+
```
52+
sense ⟨c, x⟩
53+
s.t.
54+
Ax <= b
55+
```
56+
57+
A generic LP form with `LPForm(sense, c, A, c_lb, c_ub, v_lb, v_ub)`:
58+
```
59+
sense ⟨c, x⟩
60+
s.t.
61+
c_lb <= Ax <= c_ub
62+
v_lb <= x <= v_ub
63+
```
64+
65+
An LP Solver form with `LPSolverForm(sense, c, A, b, senses, v_lb, v_ub)`:
66+
```
67+
sense ⟨c, x⟩
68+
s.t.
69+
Ax senses b
70+
v_lb <= x <= v_ub
71+
```
72+
where `senses` is a vector whose `i`th entry is either
73+
* `MatOI.EQUAL_TO` if `(Ax)_i = b_i`,
74+
* `MatOI.LESS_THAN` if `(Ax)_i <= b_i`,
75+
* `MatOI.GREATER_THAN` if `(Ax)_i >= b_i`.
76+
77+
## Transition from linprog
78+
79+
If you are used to [`MathProgBase`](https://github.com/JuliaOpt/MathProgBase.jl)'s or MATLAB's `linprog` function, this package provides an easy transition.
80+
The package does not provide a one-shot function that takes the form as input are returns the solution and status as
81+
in general, solvers may have multiple solutions to report and the status cannot be summarized in a single value.
82+
`MathOptInterface` embraces this complexity and allows to retrieve multiple solutions,
83+
has 4 different solver-independent statuses (`MOI.TerminationStatus`, `MOI.PrimalStatus`, `MOI.DualStatus` and `MOI.RawStatusString`) and also
84+
allows solvers to provide solver independent statuses.
85+
For these reasons, there is no `linprog` functions covering all use cases but using this packages one covering your use cases can be implemented in just a few lines:
86+
1) Create the appropriate structure from the matrix data, e.g. `LPForm`, `LPSolverForm`, ...
87+
2) Copy it to a JuMP model or MOI optimizer.
88+
3) Call `JuMP.optimize!` or `MOI.optimize!`.
89+
4) Retrieve and return solutions and statuses.

src/MatrixOptInterface.jl

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,63 @@
11
module MatrixOptInterface
22

3-
using LinearAlgebra
3+
using LinearAlgebra, SparseArrays
44
using MathOptInterface
55
const MOI = MathOptInterface
66
const MOIU = MathOptInterface.Utilities
77

88
# export MatrixOptimizer
99

10-
@enum ConstraintSense EQUAL_TO GREATER_THAN LESS_THAN
10+
@enum ConstraintSense EQUAL_TO GREATER_THAN LESS_THAN INTERVAL
11+
_sense(::Type{<:MOI.EqualTo}) = EQUAL_TO
12+
_sense(::Type{<:MOI.LessThan}) = LESS_THAN
13+
_sense(::Type{<:MOI.GreaterThan}) = GREATER_THAN
14+
_sense(::Type{<:MOI.Interval}) = INTERVAL
15+
16+
_no_upper(bound) = bound != typemax(bound)
17+
_no_lower(bound) = bound != typemin(bound)
18+
19+
function _bound_sense(lb::T, ub::T) where T
20+
if _no_upper(ub)
21+
if _no_lower(lb)
22+
if lb == ub
23+
return EQUAL_TO
24+
else
25+
return INTERVAL
26+
end
27+
else
28+
return LESS_THAN
29+
end
30+
else
31+
if _no_lower(lb)
32+
return GREATER_THAN
33+
else
34+
return
35+
end
36+
end
37+
end
38+
function _bound_set(lb::T, ub::T) where T
39+
if _no_upper(ub)
40+
if _no_lower(lb)
41+
if ub == lb
42+
return MOI.EqualTo(lb)
43+
else
44+
return MOI.Interval(lb, ub)
45+
end
46+
else
47+
return MOI.LessThan(ub)
48+
end
49+
else
50+
if _no_lower(lb)
51+
return MOI.GreaterThan(lb)
52+
else
53+
return
54+
end
55+
end
56+
end
1157

1258
@enum VariableType CONTINUOUS INTEGER BINARY
1359

1460
include("matrix_input.jl")
15-
include("allocate_load.jl")
61+
include("change_form.jl")
1662

17-
end
63+
end

0 commit comments

Comments
 (0)