-
Notifications
You must be signed in to change notification settings - Fork 15
/
simplex_oracles.jl
135 lines (120 loc) · 3.56 KB
/
simplex_oracles.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
UnitSimplexOracle(right_side)
Represents the scaled unit simplex:
```
C = {x ∈ R^n_+, ∑x ≤ right_side}
```
"""
struct UnitSimplexOracle{T} <: LinearMinimizationOracle
right_side::T
end
UnitSimplexOracle{T}() where {T} = UnitSimplexOracle{T}(one(T))
UnitSimplexOracle(rhs::Integer) = UnitSimplexOracle{Rational{BigInt}}(rhs)
"""
LMO for scaled unit simplex:
`∑ x_i = τ`
Returns either vector of zeros or vector with one active value equal to RHS if
there exists an improving direction.
"""
function compute_extreme_point(lmo::UnitSimplexOracle{T}, direction; v=nothing, kwargs...) where {T}
idx = argmin_(direction)
if direction[idx] < 0
return ScaledHotVector(lmo.right_side, idx, length(direction))
end
return ScaledHotVector(zero(T), idx, length(direction))
end
function convert_mathopt(
lmo::UnitSimplexOracle{T},
optimizer::OT;
dimension::Integer,
use_modify::Bool=true,
kwargs...,
) where {T,OT}
MOI.empty!(optimizer)
τ = lmo.right_side
n = dimension
(x, _) = MOI.add_constrained_variables(optimizer, [MOI.Interval(0.0, 1.0) for _ in 1:n])
MOI.add_constraint(
optimizer,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(ones(n), x), 0.0),
MOI.LessThan(τ),
)
return MathOptLMO(optimizer, use_modify)
end
"""
Dual costs for a given primal solution to form a primal dual pair
for scaled unit simplex.
Returns two vectors. The first one is the dual costs associated with the constraints
and the second is the reduced costs for the variables.
"""
function compute_dual_solution(::UnitSimplexOracle{T}, direction, primalSolution) where {T}
idx = argmax(primalSolution)
critical = min(direction[idx], 0)
lambda = [critical]
mu = direction .- lambda
return lambda, mu
end
"""
ProbabilitySimplexOracle(right_side)
Represents the scaled probability simplex:
```
C = {x ∈ R^n_+, ∑x = right_side}
```
"""
struct ProbabilitySimplexOracle{T} <: LinearMinimizationOracle
right_side::T
end
ProbabilitySimplexOracle{T}() where {T} = ProbabilitySimplexOracle{T}(one(T))
ProbabilitySimplexOracle(rhs::Integer) = ProbabilitySimplexOracle{Float64}(rhs)
"""
LMO for scaled probability simplex.
Returns a vector with one active value equal to RHS in the
most improving (or least degrading) direction.
"""
function compute_extreme_point(
lmo::ProbabilitySimplexOracle{T},
direction;
v=nothing,
kwargs...,
) where {T}
idx = argmin_(direction)
if idx === nothing
@show direction
end
return ScaledHotVector(lmo.right_side, idx, length(direction))
end
function convert_mathopt(
lmo::ProbabilitySimplexOracle{T},
optimizer::OT;
dimension::Integer,
use_modify=true::Bool,
kwargs...,
) where {T,OT}
MOI.empty!(optimizer)
τ = lmo.right_side
n = dimension
(x, _) = MOI.add_constrained_variables(optimizer, [MOI.Interval(0.0, 1.0) for _ in 1:n])
MOI.add_constraint(
optimizer,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(ones(n), x), 0.0),
MOI.EqualTo(τ),
)
return MathOptLMO(optimizer, use_modify)
end
"""
Dual costs for a given primal solution to form a primal dual pair
for scaled probability simplex.
Returns two vectors. The first one is the dual costs associated with the constraints
and the second is the reduced costs for the variables.
"""
function compute_dual_solution(
::ProbabilitySimplexOracle{T},
direction,
primal_solution;
kwargs...,
) where {T}
idx = argmax(primal_solution)
lambda = [direction[idx]]
mu = direction .- lambda
return lambda, mu
end