Skip to content
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

Document how to use MOI.VectorAffineFunction #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/src/optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,66 @@ m = Model()

poly = polyhedron(m, CDDLib.Library(:exact))
```

## Using a Polyhedra Optimizer with MathOptInterface

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain why one would use MOI directly instead of with JuMP (which is recommended) which is simply Model(with_optimizer(CDDLib.Optimizer)). The reason could be that JuMP only does Float64 arithmetic while CDD allows rational arithmetic.

Polyhedra Optimizers by dafault support only a few constraint types in MathOptInterface (MOI).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dafault -> default

Apply `MOI.Bridges.full_bridge_optimizer` to a Polyhedra Optimizer to enable a broader set of constraint types, such as `VectorAffineFunction`:
see the [list](http://www.juliaopt.org/MathOptInterface.jl/dev/apimanual/#Constraints-by-function-set-pairs-1) from MOI.

As an example, consider the linear program:
```math
\[
\max\ c x \quad \text{s.t.}\ A x \leq b,\ x \geq 0
\]
```
where
```julia
A = [1 2; 3 1]
b = [1, 2]
c = [1, 1]
```

Let us solve this program with `CDDLib.Optimizer` in exact arithmetic.
To set up:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

```julia
using CDDLib
using MathOptInterface
const MOI = MathOptInterface

m, n = size(A)
T = Rational{BigInt}

# Enable `VectorAffineTerm`, `VectorOfVariables`, `Nonnegatives`, `Nonpositives`
optimizer = MOI.Bridges.full_bridge_optimizer(CDDLib.Optimizer{T}(), T)

# max cx
x = MOI.add_variables(optimizer, n)
MOI.set(optimizer, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}(),
MOI.ScalarAffineFunction{T}(MOI.ScalarAffineTerm{T}.(c, x), 0))
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE)

# Ax - b <= 0
terms = MOI.VectorAffineTerm{T}.(
1:m, MOI.ScalarAffineTerm{T}.(A, reshape(x, 1, n))
)
f = MOI.VectorAffineFunction{T}(vec(terms), -b)
MOI.add_constraint(optimizer, f, MOI.Nonpositives(m))

# x >= 0
MOI.add_constraint(optimizer, MOI.VectorOfVariables(x), MOI.Nonnegatives(n))
```

To solve:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty line

```julia
MOI.optimize!(optimizer)
MOI.get(optimizer, MOI.VariablePrimal(), x)
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The standard way is to add the output in the same code block after an #output comment like https://juliadocs.github.io/Documenter.jl/stable/man/doctests/#%22Script%22-Examples-1
We could turn it into a doctest later by using a MOIU.MockOptimizer so that we don't require CDDLib (that's what we do for JuMP's doctests).


```
2-element Array{Rational{BigInt},1}:
3//5
1//5
```