/
print.jl
425 lines (385 loc) · 12.5 KB
/
print.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# Copyright (c) 2017-23, Oscar Dowson and SDDP.jl contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
const _RULE = "-------------------------------------------------------------------"
function print_helper(f, io, args...)
f(stdout, args...)
return f(io, args...)
end
function print_banner(io)
println(io, _RULE)
println(io, " SDDP.jl (c) Oscar Dowson and contributors, 2017-23")
println(io, _RULE)
return
end
function _unique_paths(model::PolicyGraph{T}) where {T}
if is_cyclic(model)
return Inf
end
parents = Dict{T,Set{T}}(t => Set{T}() for t in keys(model.nodes))
children = Dict{T,Set{T}}(t => Set{T}() for t in keys(model.nodes))
for (t, node) in model.nodes
for child in node.children
if child.probability > 0
push!(parents[child.term], t)
push!(children[t], child.term)
end
end
end
ordered = T[]
in_order = Dict{T,Bool}(t => false for t in keys(model.nodes))
stack = Tuple{T,Bool}[]
for root_child in model.root_children
if iszero(root_child.probability) || in_order[root_child.term]
continue
end
push!(stack, (root_child.term, true))
while !isempty(stack)
node, needs_checking = pop!(stack)
if !needs_checking
push!(ordered, node)
in_order[node] = true
continue
elseif in_order[node]
continue
end
push!(stack, (node, false))
for child in children[node]
if !in_order[child]
push!(stack, (child, true))
end
end
end
end
total_scenarios = 0.0
incoming_scenarios = Dict{T,Float64}(t => 0.0 for t in keys(model.nodes))
for node in reverse!(ordered)
N = length(model[node].noise_terms)
if length(parents[node]) == 0 # Must come from the root node.
incoming_scenarios[node] = N
else
incoming_scenarios[node] =
N * sum(incoming_scenarios[p] for p in parents[node])
end
if length(children[node]) == 0 # It's a leaf!
total_scenarios += incoming_scenarios[node]
end
end
return total_scenarios
end
function _merge_tuple(x, y)
if x == (-1, -1)
return (y, y)
elseif y < x[1]
return (y, x[2])
elseif y > x[2]
return (x[1], y)
else
return x
end
end
_constraint_key(F, S) = replace("$(F) in $(S)", "MathOptInterface" => "MOI")
function print_problem_statistics(
io::IO,
model::PolicyGraph,
existing_cuts::Bool,
parallel_scheme,
risk_measure,
sampling_scheme,
)
constraint_types = Dict{String,Tuple{Int,Int}}()
variables = (-1, -1)
for (_, node) in model.nodes
variables = _merge_tuple(variables, JuMP.num_variables(node.subproblem))
for (F, S) in JuMP.list_of_constraint_types(node.subproblem)
key = _constraint_key(F, S)
num_con = get(constraint_types, key, (-1, -1))
constraint_types[key] = _merge_tuple(
num_con,
JuMP.num_constraints(node.subproblem, F, S),
)
end
end
pad = maximum(length(k) for k in keys(constraint_types))
println(io, "problem")
println(io, " nodes : ", length(model.nodes))
println(io, " state variables : ", length(model.initial_root_state))
paths = Printf.@sprintf("%1.5e", _unique_paths(model))
println(io, " scenarios : ", paths)
println(io, " existing cuts : ", existing_cuts)
println(io, "options")
println(io, " solver : ", parallel_scheme)
println(io, " risk measure : ", risk_measure)
println(io, " sampling scheme : ", typeof(sampling_scheme))
println(io, "subproblem structure")
a, b = variables
println(io, " ", rpad("VariableRef", pad), " : [", a, ", ", b, "]")
for k in sort!(collect(keys(constraint_types)))
F, S = constraint_types[k]
println(io, " ", rpad(k, pad), " : [", F, ", ", S, "]")
end
return
end
function print_iteration_header(io)
println(io, _RULE)
println(
io,
" iteration simulation bound time (s) solves pid",
)
println(io, _RULE)
return
end
print_value(x::Real) = lpad(Printf.@sprintf("%1.6e", x), 13)
print_value(x::Int) = Printf.@sprintf("%9d", x)
print_value3(x::Int) = Printf.@sprintf("%3d", x)
function print_iteration(io, log::Log)
print(io, log.serious_numerical_issue ? "†" : " ")
print(io, print_value(log.iteration))
print(io, log.duality_key)
print(io, " ", print_value(log.simulation_value))
print(io, " ", print_value(log.bound))
print(io, " ", print_value(log.time))
print(io, " ", print_value(log.total_solves))
print(io, " ", print_value3(log.pid))
println(io)
return
end
function print_footer(io, training_results::TrainingResults)
println(io, _RULE)
println(io, "status : ", training_results.status)
println(io, "total time (s) :", print_value(training_results.log[end].time))
println(io, "total solves : ", training_results.log[end].total_solves)
println(
io,
"best bound : ",
print_value(training_results.log[end].bound),
)
μ, σ =
confidence_interval(map(l -> l.simulation_value, training_results.log))
println(io, "simulation ci : ", print_value(μ), " ±", print_value(σ))
num_issues = sum(l -> l.serious_numerical_issue, training_results.log)
println(io, "numeric issues : ", num_issues)
println(io, _RULE)
println(io)
return
end
"""
confidence_interval(x::Vector{Float64}, z_score::Float64 = 1.96)
Return a confidence interval of `x` corresponding to the `z_score`.
`z_score` defaults to `1.96` for a 95% confidence interval.
"""
function confidence_interval(x::Vector{Float64}, z_score::Float64 = 1.96)
μ = Statistics.mean(x)
σ = z_score * Statistics.std(x) / sqrt(length(x))
return μ, σ
end
###
### Numerical stability checks
###
struct CoefficientRanges
matrix::Vector{Float64}
objective::Vector{Float64}
bounds::Vector{Float64}
rhs::Vector{Float64}
function CoefficientRanges()
return new([Inf, -Inf], [Inf, -Inf], [Inf, -Inf], [Inf, -Inf])
end
end
function _merge(x::Vector{Float64}, y::Vector{Float64})
x[1] = min(x[1], y[1])
x[2] = max(x[2], y[2])
return
end
function _merge(x::CoefficientRanges, y::CoefficientRanges)
_merge(x.matrix, y.matrix)
_merge(x.objective, y.objective)
_merge(x.bounds, y.bounds)
_merge(x.rhs, y.rhs)
return
end
function _stringify_bounds(bounds::Vector{Float64})
lower = bounds[1] < Inf ? _print_value(bounds[1]) : "0e+00"
upper = bounds[2] > -Inf ? _print_value(bounds[2]) : "0e+00"
return string("[", lower, ", ", upper, "]")
end
function _print_numerical_stability_report(
io::IO,
ranges::CoefficientRanges,
print::Bool,
warn::Bool,
)
warnings = Tuple{String,String}[]
_print_coefficients(io, "matrix", ranges.matrix, print, warnings)
_print_coefficients(io, "objective", ranges.objective, print, warnings)
_print_coefficients(io, "bounds", ranges.bounds, print, warnings)
_print_coefficients(io, "rhs", ranges.rhs, print, warnings)
if warn && !isempty(warnings)
println(io, "WARNING: numerical stability issues detected")
for (name, sense) in warnings
println(io, " - $(name) range contains $(sense) coefficients")
end
println(
io,
"Very large or small absolute values of coefficients\n",
"can cause numerical stability issues. Consider\n",
"reformulating the model.",
)
end
return
end
function _print_coefficients(
io::IO,
name::String,
range,
print::Bool,
warnings::Vector{Tuple{String,String}},
)
if print
println(
io,
" ",
rpad(string(name, " range"), 17),
_stringify_bounds(range),
)
end
if range[1] < 1e-4
push!(warnings, (name, "small"))
end
if range[2] > 1e7
push!(warnings, (name, "large"))
end
return
end
_print_value(x::Real) = Printf.@sprintf("%1.0e", x)
function _update_range(range::Vector{Float64}, value::Real)
if !(value ≈ 0.0)
range[1] = min(range[1], abs(value))
range[2] = max(range[2], abs(value))
end
return
end
function _update_range(range::Vector{Float64}, func::JuMP.GenericAffExpr)
for coefficient in values(func.terms)
_update_range(range, coefficient)
end
return
end
function _update_range(range::Vector{Float64}, func::MOI.LessThan)
_update_range(range, func.upper)
return
end
function _update_range(range::Vector{Float64}, func::MOI.GreaterThan)
_update_range(range, func.lower)
return
end
function _update_range(range::Vector{Float64}, func::MOI.EqualTo)
_update_range(range, func.value)
return
end
function _update_range(range::Vector{Float64}, func::MOI.Interval)
_update_range(range, func.upper)
_update_range(range, func.lower)
return
end
# Default fallback for unsupported constraints.
_update_range(range::Vector{Float64}, x) = nothing
function _coefficient_ranges(model::JuMP.Model)
ranges = CoefficientRanges()
_update_range(ranges.objective, JuMP.objective_function(model))
for var in JuMP.all_variables(model)
if JuMP.has_lower_bound(var)
_update_range(ranges.bounds, JuMP.lower_bound(var))
end
if JuMP.has_upper_bound(var)
_update_range(ranges.bounds, JuMP.upper_bound(var))
end
end
for (F, S) in JuMP.list_of_constraint_types(model)
F == JuMP.VariableRef && continue
for con in JuMP.all_constraints(model, F, S)
con_obj = JuMP.constraint_object(con)
_update_range(ranges.matrix, con_obj.func)
_update_range(ranges.rhs, con_obj.set)
end
end
return ranges
end
"""
numerical_stability_report(
[io::IO = stdout,]
model::PolicyGraph;
by_node::Bool = false,
print::Bool = true,
warn::Bool = true,
)
Print a report identifying possible numeric stability issues.
## Keyword arguments
- If `by_node`, print a report for each node in the graph.
- If `print`, print to `io`.
- If `warn`, warn if the coefficients may cause numerical issues.
"""
function numerical_stability_report(
io::IO,
model::PolicyGraph;
by_node::Bool = false,
print::Bool = true,
warn::Bool = true,
)
graph_ranges = CoefficientRanges()
node_keys = sort_nodes(collect(keys(model.nodes)))
for key in node_keys
node = model[key]
node_ranges = CoefficientRanges()
for noise in node.noise_terms
parameterize(node, noise.term)
node_ranges_2 = _coefficient_ranges(node.subproblem)
_merge(node_ranges, node_ranges_2)
end
if by_node
print && println(io, "numerical stability report for node: ", key)
_print_numerical_stability_report(io, node_ranges, print, warn)
end
_merge(graph_ranges, node_ranges)
end
if !by_node
print && println(io, "numerical stability report")
_print_numerical_stability_report(io, graph_ranges, print, warn)
end
return
end
function numerical_stability_report(model::PolicyGraph; kwargs...)
return numerical_stability_report(stdout, model; kwargs...)
end
###
### Machine readable log
###
"""
write_log_to_csv(model::PolicyGraph, filename::String)
Write the log of the most recent training to a csv for post-analysis.
Assumes that the model has been trained via [`SDDP.train`](@ref).
"""
function write_log_to_csv(model::PolicyGraph, filename::String)
if model.most_recent_training_results === nothing
error(
"Unable to write the log to file because the model has not " *
"been trained.",
)
end
open(filename, "w") do io
println(io, "iteration, simulation, bound, time")
for log in model.most_recent_training_results.log
println(
io,
log.iteration,
", ",
log.simulation_value,
", ",
log.bound,
", ",
log.time,
)
end
end
return
end