Skip to content

Commit

Permalink
Document how to use MOI.VectorAffineFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
oyamad authored May 15, 2019
1 parent 3b289dd commit aadada4
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions docs/src/optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,65 @@ m = Model()

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

## Using a Polyhedra Optimizer with MathOptInterface

Polyhedra Optimizers by dafault support only a few constraint types in MathOptInterface (MOI).
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 exmaple, consider the linear program:

```math
\[
\max\ c x \quad \text{s.t.}\ A x \leq b
\]
```

where

```julia
A = [1 1; -1 0; 0 -1]
b = [1, 0, 0]
c = [1, 0]
```

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

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

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

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

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)

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))
```

To solve:

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

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

0 comments on commit aadada4

Please sign in to comment.