/
FastExactSolvers.jl
221 lines (178 loc) · 6.28 KB
/
FastExactSolvers.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
College(j, f, t)
Contains a college's index `j`, admissions probability `f` and utility value `t`.
Used only by `applicationorder()`.
"""
struct College
j::Int
f::Float64
t::Float64
end
# Overload isless() so that the heap is ordered by expected utility.
isless(c1::College, c2::College) = isless(c1.f * c1.t, c2.f * c2.t)
"""
applicationorder_list(mkt::SameCostsMarket) -> X, V
Produce the optimal application order `X` and associated valuations `V`
for the [`SameCostsMarket`](@ref) defined by `mkt`. Uses a list data structure;
typically faster than the equivalent [`applicationorder_heap`](@ref).
All `SameCostsMarket`s satisfy a nestedness property, meaning that the optimal
portfolio when `mkt.h = h` is given by the first `h` entries of the optimal portfolio
when `mkt.h = mkt.m`. For example:
```julia-repl
julia> mkt = SameCostsMarket([0.2, 0.5, 0.1, 0.6, 0.1], [1, 4, 9, 1, 8], 5);
julia> x, v = applicationorder_list(mkt)
([2, 3, 5, 4, 1], [2.0, 2.7, 3.24, 3.483, 3.5154])
julia> x[1:4], v[4] # optimal portfolio and valuation for h = 4
([2, 3, 5, 4], 3.483)
```
"""
function applicationorder_list(
mkt::SameCostsMarket;
verbose::Bool=false
)::Tuple{Vector{Int},Vector{Float64}}
apporder = zeros(Int, mkt.h)
v = zeros(mkt.h)
mkt_list = College[College(j, mkt.f[j], mkt.t[j]) for j in 1:mkt.m]
c_best::College, idx_best::Int = findmax(mkt_list)
@inbounds for j in 1:mkt.h
if verbose
println("Iteration $j")
println(" ft: ", [mkt_list[i].f * mkt_list[i].t for i in 1:length(mkt_list)])
println(" Add school $(c_best.j)")
end
v[j] = get(v, j - 1, 0) + c_best.f * c_best.t
apporder[j] = c_best.j
next_c_best::College = College(0, 1.0, -1.0)
next_idx_best::Int = 0
for i in 1:idx_best-1
mkt_list[i] =
College(
mkt_list[i].j,
mkt_list[i].f,
mkt_list[i].t * (1 - c_best.f),
)
if isless(next_c_best, mkt_list[i])
next_c_best = mkt_list[i]
next_idx_best = i
end
end
for i in idx_best+1:length(mkt_list)
mkt_list[i-1] =
College(
mkt_list[i].j,
mkt_list[i].f,
mkt_list[i].t - c_best.f * c_best.t,
)
if isless(next_c_best, mkt_list[i-1])
next_c_best = mkt_list[i-1]
next_idx_best = i - 1
end
end
c_best, idx_best = next_c_best, next_idx_best
pop!(mkt_list)
end
return mkt.perm[apporder], v
end
"""
applicationorder_list(mkt::SameCostsMarket) -> X, V
Produce the optimal application order `X` and associated valuations `V`
for the [`SameCostsMarket`](@ref) defined by `mkt`. Uses a heap data structure;
typically the equivalent [`applicationorder_list`](@ref) is faster.
All `SameCostsMarket`s satisfy a nestedness property, meaning that the optimal
portfolio when `mkt.h = h` is given by the first `h` entries of the optimal portfolio
when `mkt.h = mkt.m`. For example:
```julia-repl
julia> mkt = SameCostsMarket([0.2, 0.5, 0.1, 0.6, 0.1], [1, 4, 9, 1, 8], 5);
julia> x, v = applicationorder_heap(mkt)
([2, 3, 5, 4, 1], [2.0, 2.7, 3.24, 3.483, 3.5154])
julia> x[1:4], v[4] # optimal portfolio and valuation for h = 4
([2, 3, 5, 4], 3.483)
```
"""
function applicationorder_heap(mkt::SameCostsMarket)::Tuple{Vector{Int},Vector{Float64}}
apporder = zeros(Int, mkt.h)
v = zeros(mkt.h)
mkt_heap = BinaryMaxHeap{College}(collect(College(j, mkt.f[j], mkt.t[j]) for j in 1:mkt.m))
@inbounds for j in 1:mkt.h
c_k = first(mkt_heap)
v[j] = get(v, j - 1, 0) + c_k.f * c_k.t
apporder[j] = c_k.j
# These two implementations perform almost identically
mkt_heap = BinaryMaxHeap{College}(
# College{T}[
# College(
# c.j,
# c.f,
# c.j < c_k.j ? c.t * (1 - c_k.f) : c.t - c_k.f * c_k.t,
# )
# for c in mkt_heap.valtree if c.j != c_k.j
# ]
map(filter(c -> c.j != c_k.j, mkt_heap.valtree)) do c
College(
c.j,
c.f,
c.j < c_k.j ? c.t * (1 - c_k.f) : c.t - c_k.f * c_k.t,
)
end
)
end
return mkt.perm[apporder], v
end
# Used by dynamic program below
@inbounds function V_recursor!(
V_dict::Dict{Tuple{Int,Int},Float64},
j::Int,
h::Int,
mkt::VariedCostsMarket
)::Float64
return get(V_dict, (j, h)) do
if j == 0 || h == 0
return 0.0
elseif h < mkt.g[j]
push!(V_dict, (j, h) => V_recursor!(V_dict, j - 1, h, mkt))
return V_dict[(j, h)]
else
push!(V_dict, (j, h) => max(
V_recursor!(V_dict, j - 1, h, mkt),
(1 - mkt.f[j]) * V_recursor!(V_dict, j - 1, h - mkt.g[j], mkt) + mkt.f[j] * mkt.t[j]
))
return V_dict[(j, h)]
end
end
end
"""
optimalportfolio_dynamicprogram(mkt::VariedCostsMarket) -> X, v
Use the dynamic program on application costs to produce the optimal portfolio `X` and associated
value `v` for the [`VariedCostsMarket`](@ref) defined by `mkt`.
```julia-repl
julia> mkt = VariedCostsMarket([0.2, 0.5, 0.1, 0.6, 0.1], [1, 4, 9, 1, 8], [2, 4, 2, 5, 1], 8);
julia> optimalportfolio_dynamicprogram(mkt)
([3, 5, 2], 3.24)
```
"""
function optimalportfolio_dynamicprogram(
mkt::VariedCostsMarket;
verbose::Bool=false
)::Tuple{Vector{Int},Float64}
V_dict = Dict{Tuple{Int,Int},Float64}()
sizehint!(V_dict, mkt.m * mkt.m ÷ 2)
v = V_recursor!(V_dict, mkt.m, mkt.H, mkt)
h = mkt.H
X = Int[]
for j in reverse(1:mkt.m)
if V_recursor!(V_dict, j - 1, h, mkt) < V_recursor!(V_dict, j, h, mkt)
push!(X, j)
h -= mkt.g[j]
end
end
if verbose
V_table = Array{Union{Missing,Float64}}(missing, mkt.m, mkt.H)
for (j, h) in keys(V_dict)
if 0 < j ≤ mkt.m && 0 < h ≤ mkt.H
V_table[j, h] = V_dict[(j, h)]
end
end
display(V_table)
end
return mkt.perm[X], v
end