Skip to content

Commit fef7019

Browse files
committed
Document how to use MOI.VectorAffineFunction
Close #170
1 parent 3b289dd commit fef7019

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

docs/src/optimization.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,66 @@ m = Model()
126126

127127
poly = polyhedron(m, CDDLib.Library(:exact))
128128
```
129+
130+
## Using a Polyhedra Optimizer with MathOptInterface
131+
132+
Polyhedra Optimizers by dafault support only a few constraint types in MathOptInterface (MOI).
133+
Apply `MOI.Bridges.full_bridge_optimizer` to a Polyhedra Optimizer to enable a broader set of constraint types, such as `VectorAffineFunction`:
134+
see the [list](http://www.juliaopt.org/MathOptInterface.jl/dev/apimanual/#Constraints-by-function-set-pairs-1) from MOI.
135+
136+
As an example, consider the linear program:
137+
```math
138+
\[
139+
\max\ c x \quad \text{s.t.}\ A x \leq b,\ x \geq 0
140+
\]
141+
```
142+
where
143+
```julia
144+
A = [1 2; 3 1]
145+
b = [1, 2]
146+
c = [1, 1]
147+
```
148+
149+
Let us solve this program with `CDDLib.Optimizer` in exact arithmetic.
150+
To set up:
151+
152+
```julia
153+
using CDDLib
154+
using MathOptInterface
155+
const MOI = MathOptInterface
156+
157+
m, n = size(A)
158+
T = Rational{BigInt}
159+
160+
# Enable `VectorAffineTerm`, `VectorOfVariables`, `Nonnegatives`, `Nonpositives`
161+
optimizer = MOI.Bridges.full_bridge_optimizer(CDDLib.Optimizer{T}(), T)
162+
163+
# max cx
164+
x = MOI.add_variables(optimizer, n)
165+
MOI.set(optimizer, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}(),
166+
MOI.ScalarAffineFunction{T}(MOI.ScalarAffineTerm{T}.(c, x), 0))
167+
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE)
168+
169+
# Ax - b <= 0
170+
terms = MOI.VectorAffineTerm{T}.(
171+
1:m, MOI.ScalarAffineTerm{T}.(A, reshape(x, 1, n))
172+
)
173+
f = MOI.VectorAffineFunction{T}(vec(terms), -b)
174+
MOI.add_constraint(optimizer, f, MOI.Nonpositives(m))
175+
176+
# x >= 0
177+
MOI.add_constraint(optimizer, MOI.VectorOfVariables(x), MOI.Nonnegatives(n))
178+
```
179+
180+
To solve:
181+
182+
```julia
183+
MOI.optimize!(optimizer)
184+
MOI.get(optimizer, MOI.VariablePrimal(), x)
185+
```
186+
187+
```
188+
2-element Array{Rational{BigInt},1}:
189+
3//5
190+
1//5
191+
```

0 commit comments

Comments
 (0)