Skip to content

Commit aadada4

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

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

docs/src/optimization.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,65 @@ 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 exmaple, consider the linear program:
137+
138+
```math
139+
\[
140+
\max\ c x \quad \text{s.t.}\ A x \leq b
141+
\]
142+
```
143+
144+
where
145+
146+
```julia
147+
A = [1 1; -1 0; 0 -1]
148+
b = [1, 0, 0]
149+
c = [1, 0]
150+
```
151+
152+
Let us solve this program with `CDDLib.Optimizer` in exact arithmetic.
153+
To set up:
154+
155+
```julia
156+
using CDDLib
157+
using MathOptInterface
158+
const MOI = MathOptInterface
159+
160+
m, n = size(A)
161+
T = Rational{BigInt}
162+
163+
# Enable `VectorAffineTerm` and `Nonpositives`
164+
optimizer = MOI.Bridges.full_bridge_optimizer(CDDLib.Optimizer{T}(), T)
165+
166+
x = MOI.add_variables(optimizer, n)
167+
MOI.set(optimizer, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}(),
168+
MOI.ScalarAffineFunction{T}(MOI.ScalarAffineTerm{T}.(c, x), 0))
169+
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE)
170+
171+
terms = MOI.VectorAffineTerm{T}.(
172+
1:m, MOI.ScalarAffineTerm{T}.(A, reshape(x, 1, n))
173+
)
174+
f = MOI.VectorAffineFunction{T}(vec(terms), -b)
175+
MOI.add_constraint(optimizer, f, MOI.Nonpositives(m))
176+
```
177+
178+
To solve:
179+
180+
```julia
181+
MOI.optimize!(optimizer)
182+
MOI.get(optimizer, MOI.VariablePrimal(), x)
183+
```
184+
185+
```
186+
2-element Array{Rational{BigInt},1}:
187+
1//1
188+
0//1
189+
```
190+

0 commit comments

Comments
 (0)