/
consolidation.jl
237 lines (206 loc) · 9.03 KB
/
consolidation.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# MIT License
#
# Copyright (c) 2018 Martin Biel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
abstract type AbstractConsolidation end
abstract type AbstractConsolidator end
"""
RawConsolidationParameter
An optimizer attribute used for raw parameters of the consolidator. Defers to `RawOptimizerAttribute`.
"""
struct RawConsolidationParameter <: ConsolidationParameter
name::Any
end
function MOI.get(consolidator::AbstractConsolidator, param::RawConsolidationParameter)
name = Symbol(param.name)
if !(name in fieldnames(typeof(consolidator)))
error("Unrecognized parameter name: $(name) for consolidator $(typeof(consolidator)).")
end
return getfield(consolidator, name)
end
function MOI.set(consolidator::AbstractConsolidator, param::RawConsolidationParameter, value)
name = Symbol(param.name)
if !(name in fieldnames(typeof(consolidator)))
error("Unrecognized parameter name: $(name) for consolidator $(typeof(consolidator)).")
end
setfield!(consolidator, name, value)
return nothing
end
# No consolidation
# ------------------------------------------------------------
"""
NoConsolidation
Empty functor object for running the L-shaped algorithm without consolidation.
"""
struct NoConsolidation <: AbstractConsolidation end
function consolidate!(::AbstractLShaped, ::NoConsolidation)
return nothing
end
function add_cut!(::AbstractLShaped, ::NoConsolidation, ::AbstractHyperPlane)
return nothing
end
function add_cut!(::AbstractLShaped, ::NoConsolidation, ::Integer, ::AbstractHyperPlane)
return nothing
end
# Consolidation
# ------------------------------------------------------------
"""
Consolidation
Functor object for using consolidation in an L-shaped algorithm. Create by supplying a [`Consolidate`](@ref) object through `consolidate` in `LShaped.Optimizer` or by setting the [`Consolidator`](@ref) attribute.
...
# Algorithm parameters
- `tresh::T` = 0.95: Relative amount of redundant cuts in a former iteration required to consider the iteration redundant
- `at::Int = 5.0`: Number of times an iteration can be redundant before consolidation is triggered
- `rebuild::Function = at_tolerance()`: Function deciding when the master model should be rebuilt according to performed consolidations
...
"""
struct Consolidation{T <: AbstractFloat} <: AbstractConsolidation
cuts::Vector{Vector{AnySparseOptimalityCut{T}}}
feasibility_cuts::Vector{Vector{SparseFeasibilityCut{T}}}
consolidated::Vector{Bool}
redundance_count::Vector{Int}
redundance_treshold::T
consolidation_trigger::Int
rebuild::Function
function Consolidation(::Type{T}, tresh::T, at::Int, rebuild::Function) where T <: AbstractFloat
cuts = Vector{Vector{AnySparseOptimalityCut{T}}}()
push!(cuts, Vector{AnySparseOptimalityCut{T}}())
feasibility_cuts = Vector{Vector{SparseFeasibilityCut{T}}}()
push!(feasibility_cuts, Vector{SparseFeasibilityCut{T}}())
return new{T}(cuts, feasibility_cuts, [false], [0], tresh, at, rebuild)
end
end
function consolidate!(lshaped::AbstractLShaped, consolidation::Consolidation{T}) where T <: AbstractFloat
for i in findall(map(!,consolidation.consolidated))
nc = sum(num_subproblems.(consolidation.cuts[i]))
nc == 0 && continue
count = 0
for cut in consolidation.cuts[i]
# Check for redundance
if !active(lshaped, cut)
count += num_subproblems(cut)
end
end
if count/nc >= consolidation.redundance_treshold
consolidation.redundance_count[i] += 1
end
if consolidation.redundance_count[i] == consolidation.consolidation_trigger
consolidated_cut = aggregate(consolidation.cuts[i])
empty!(consolidation.cuts[i])
push!(consolidation.cuts[i], consolidated_cut)
consolidation.consolidated[i] = true
end
end
for i in findall(c->length(c)>0, consolidation.feasibility_cuts)
inactive = Vector{Int}()
for (j,cut) in enumerate(consolidation.feasibility_cuts[i])
# Check for redundance
if !active(lshaped, cut)
push!(inactive, j)
end
end
deleteat!(consolidation.feasibility_cuts[i], inactive)
end
if minimum_requirements(lshaped,consolidation) && consolidation.rebuild(lshaped, consolidation)
# Rebuild sparingly
rebuild_master!(lshaped, consolidation)
lshaped.data.consolidations += 1
end
# Prepare memory for next iteration
allocate!(consolidation)
return nothing
end
function allocate!(consolidation::Consolidation{T}) where T <: AbstractFloat
push!(consolidation.cuts, Vector{AnySparseOptimalityCut{T}}())
push!(consolidation.feasibility_cuts, Vector{SparseFeasibilityCut{T}}())
push!(consolidation.consolidated, false)
push!(consolidation.redundance_count, 0)
return nothing
end
function add_cut!(::AbstractLShaped, ::Consolidation, ::AbstractHyperPlane)
return nothing
end
function add_cut!(lshaped::AbstractLShaped, consolidation::Consolidation{T}, cut::HyperPlane{FeasibilityCut}) where T <: AbstractFloat
push!(consolidation.feasibility_cuts[timestamp(lshaped)], cut)
return nothing
end
function add_cut!(lshaped::AbstractLShaped, consolidation::Consolidation{T}, cut::AnySparseOptimalityCut) where T <: AbstractFloat
push!(consolidation.cuts[timestamp(lshaped)], cut)
return nothing
end
function rebuild_master!(lshaped::AbstractLShaped, consolidation::Consolidation)
# Remove all cutsa
remove_cut_constraints!(lshaped)
# Readd consolidated cuts
readd_cuts!(lshaped, consolidation)
return nothing
end
num_optimalitycuts(consolidation::Consolidation) = sum(length.(consolidation.cuts))
num_feasibilitycuts(consolidation::Consolidation) = sum(length.(consolidation.feasibility_cuts))
function num_cutconstraints(consolidation::Consolidation)
return num_optimalitycuts(consolidation) + num_feasibilitycuts(consolidation)
end
function num_cutconstraints(lshaped::AbstractLShaped)
return length(lshaped.cut_constraints)
end
# Rebuild functions
# ------------------------------------------------------------
function minimum_requirements(lshaped::AbstractLShaped, consolidation::Consolidation)
return (num_optimalitycuts(consolidation) > 0 &&
count(consolidation.consolidated) > 0) ||
(num_feasibilitycuts(consolidation) > 0 &&
num_feasibilitycuts(consolidation)/num_cutconstraints(lshaped) <= 0.1)
end
"""
at_tolerance(τ = 0.4, miniter = 0)
Rebuild master when at least nconsolidations*`miniter` iterations has passed and the ratio of number of cuts in the consolidated collection and the number of cuts in the master model has decreased below `τ`.
"""
at_tolerance() = at_tolerance(0.4, 0)
function at_tolerance(τ, miniter)
return (lshaped, consolidation) -> begin
return lshaped.data.iterations >= (lshaped.data.consolidations+1)*miniter && num_cutconstraints(consolidation)/num_cutconstraints(lshaped) <= τ
end
end
# API
# ------------------------------------------------------------
"""
DontConsolidate
Factory object for [`NoConsolidation`](@ref). Passed by default to `consolidate` in `LShaped.Optimizer`.
"""
struct DontConsolidate <: AbstractConsolidator end
function (::DontConsolidate)(::Type{T} where T <: AbstractFloat)
return NoConsolidation()
end
"""
Consolidate
Factory object for [`Consolidation`](@ref). Pass to `consolidate` in `LShaped.Optimizer` or set the [`Consolidator`](@ref) attribute. See ?Consolidation for parameter descriptions.
"""
mutable struct Consolidate <: AbstractConsolidator
redundance_treshold::Float64
consolidation_trigger::Int
rebuild::Function
end
Consolidate(; tresh::Float64 = 0.95, at::Int = 5, rebuild = at_tolerance()) = Consolidate(tresh, at, rebuild)
function (consolidator::Consolidate)(::Type{T}) where T <: AbstractFloat
return Consolidation(T,
consolidator.redundance_treshold,
consolidator.consolidation_trigger,
consolidator.rebuild)
end