-
Notifications
You must be signed in to change notification settings - Fork 94
[docs] Add tutorial on transitioning from MPB #1505
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
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Transitioning from MathProgBase | ||
|
||
MathOptInterface is a replacement for [MathProgBase.jl](https://github.com/JuliaOpt/MathProgBase.jl). | ||
However, it is not a direct replacement. | ||
|
||
## Transitioning a solver interface | ||
|
||
Writing a solver interface in MathOptInterface is more work than the equivalent | ||
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. I wouldn't say this. It may potentially be much closer to the solver. MPB was tailored to 3 specific format. If you didn't fit then it was way more work. CSDP MPB wrapper was so complicated I had to create SemidefiniteModels.jl. Now it's quite simple. 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. For conic solver, it will be simpler than with MPB with MatrixOfConstraints. 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. Make a PR with the suggested wording. I didn't think it was controversial that it was more complicated! We now have lots of constraint types and attributes and copy-to and caching and layers. It might be simpler for you, but it certainly isn't for most people. 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. It depends what you mean by simpler I guess :) |
||
interface in MathProgBase. | ||
|
||
For more information, read [Implementing a solver interface](@ref). | ||
|
||
## Transitioning the high-level functions | ||
|
||
MathOptInterface doesn't provide replacements for the high-level interfaces in | ||
MathProgBase. We recommend you use [JuMP](https://github.com/jump-dev/JuMP.jl) | ||
as a modeling interface instead. | ||
|
||
!!! tip | ||
If you haven't used JuMP before, start with the tutorial | ||
[Getting started with JuMP](https://jump.dev/JuMP.jl/stable/tutorials/Getting%20started/getting_started_with_JuMP/) | ||
|
||
### linprog | ||
|
||
Here is one way of transitioning from `linprog`: | ||
|
||
```julia | ||
using JuMP | ||
|
||
function linprog(c, A, sense, b, l, u, solver) | ||
N = length(c) | ||
model = Model(solver) | ||
@variable(model, l[i] <= x[i=1:N] <= u[i]) | ||
@objective(model, Min, c' * x) | ||
eq_rows, ge_rows, le_rows = sense .== '=', sense .== '>', sense .== '<' | ||
@constraint(model, A[eq_rows, :] * x .== b[eq_rows]) | ||
@constraint(model, A[ge_rows, :] * x .>= b[ge_rows]) | ||
@constraint(model, A[le_rows, :] * x .<= b[le_rows]) | ||
optimize!(model) | ||
return ( | ||
status = termination_status(model), | ||
objval = objective_value(model), | ||
sol = value.(x) | ||
) | ||
end | ||
``` | ||
|
||
### mixintprog | ||
|
||
Here is one way of transitioning from `mixintprog`: | ||
|
||
```julia | ||
using JuMP | ||
|
||
function mixintprog(c, A, rowlb, rowub, vartypes, lb, ub, solver) | ||
N = length(c) | ||
model = Model(solver) | ||
@variable(model, lb[i] <= x[i=1:N] <= ub[i]) | ||
for i in 1:N | ||
if vartypes[i] == :Bin | ||
set_binary(x[i]) | ||
elseif vartypes[i] == :Int | ||
set_integer(x[i]) | ||
end | ||
end | ||
@objective(model, Min, c' * x) | ||
@constraint(model, rowlb .<= A * x .<= rowub) | ||
optimize!(model) | ||
return ( | ||
status = termination_status(model), | ||
objval = objective_value(model), | ||
sol = value.(x) | ||
) | ||
end | ||
``` | ||
|
||
### quadprog | ||
|
||
Here is one way of transitioning from `quadprog`: | ||
|
||
```julia | ||
using JuMP | ||
|
||
function quadprog(c, Q, A, rowlb, rowub, lb, ub, solver) | ||
N = length(c) | ||
model = Model(solver) | ||
@variable(model, lb[i] <= x[i=1:N] <= ub[i]) | ||
@objective(model, Min, c' * x + 0.5 * x' * Q * x) | ||
@constraint(model, rowlb .<= A * x .<= rowub) | ||
optimize!(model) | ||
return ( | ||
status = termination_status(model), | ||
objval = objective_value(model), | ||
sol = value.(x) | ||
) | ||
end | ||
``` |
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.
Isn't https://github.com/jump-dev/MatrixOptInterface.jl providing a closer interface to what MPB looked like?
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.
For the modeling side, yes. But it still needs some development to integrate properly, so I held off mentioning it.