Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for node_name_parser in write_to_file #567

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SDDP"
uuid = "f4570300-c277-11e8-125c-4912f86ce65d"
authors = ["Oscar Dowson <[email protected]"]
version = "1.0.0"
version = "1.1.0"

[deps]
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Expand Down
8 changes: 8 additions & 0 deletions docs/src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ CurrentModule = SDDP
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v1.1.0 (January 12, 2023)

### Added

* Added the `node_name_parser` argument to [`SDDP.write_cuts_to_file`](@ref)
and added the option to skip nodes in [`SDDP.read_cuts_from_file`](@ref)
(#565)

## v1.0.0 (January 3, 2023)

Although we're bumping MAJOR version, this is a non-breaking release. Going
Expand Down
24 changes: 20 additions & 4 deletions src/plugins/bellman_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,24 @@ function _add_locals_if_necessary(
end

"""
write_cuts_to_file(model::PolicyGraph{T}, filename::String) where {T}
write_cuts_to_file(
model::PolicyGraph{T},
filename::String;
node_name_parser::Function = string,
) where {T}

Write the cuts that form the policy in `model` to `filename` in JSON format.

`node_name_parser` is a function which converts the name of each node into a
string representation. It has the signature: `node_name_parser(::T)::String`.

See also [`SDDP.read_cuts_from_file`](@ref).
"""
function write_cuts_to_file(model::PolicyGraph{T}, filename::String) where {T}
function write_cuts_to_file(
model::PolicyGraph{T},
filename::String;
node_name_parser::Function = string,
) where {T}
cuts = Dict{String,Any}[]
for (node_name, node) in model.nodes
if node.objective_state !== nothing || node.belief_state !== nothing
Expand All @@ -603,7 +614,7 @@ function write_cuts_to_file(model::PolicyGraph{T}, filename::String) where {T}
)
end
node_cuts = Dict(
"node" => string(node_name),
"node" => node_name_parser(node_name),
"single_cuts" => Dict{String,Any}[],
"multi_cuts" => Dict{String,Any}[],
"risk_set_cuts" => Vector{Float64}[],
Expand Down Expand Up @@ -687,6 +698,8 @@ type `T`, provide a function `node_name_parser` with the signature
`node_name_parser(T, name::String)::T where {T}` that returns the name of each
node given the string name `name`.

If `node_name_parser` returns `nothing`, those cuts are skipped.

See also [`SDDP.write_cuts_to_file`](@ref).
"""
function read_cuts_from_file(
Expand All @@ -696,7 +709,10 @@ function read_cuts_from_file(
) where {T}
cuts = JSON.parsefile(filename, use_mmap = false)
for node_cuts in cuts
node_name = node_name_parser(T, node_cuts["node"])::T
node_name = node_name_parser(T, node_cuts["node"])::Union{Nothing,T}
if node_name === nothing
continue # Skip reading these cuts
end
node = model[node_name]
bf = node.bellman_function
# Loop through and add the single-cuts.
Expand Down
45 changes: 45 additions & 0 deletions test/plugins/bellman_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,51 @@ function test_read_write_cuts_to_file_ValueFunction()
return
end

function test_read_read_cuts_from_file_nothing()
graph = SDDP.Graph(
"root_node",
["week"],
[("root_node" => "week", 1.0), ("week" => "week", 0.9)],
)
model = _create_model(graph)
SDDP.train(model; iteration_limit = 50, print_level = 0)
@test SDDP.calculate_bound(model) ≈ 119.167 atol = 0.1
V = SDDP.ValueFunction(model, node = "week")
value_f = SDDP.evaluate(V, reservoir = 10)
SDDP.write_cuts_to_file(
model,
"model.cuts.json";
node_name_parser = s -> "myname_$s",
)
model_2 = _create_model(graph)
@test SDDP.calculate_bound(model_2) ≈ 9.17 atol = 0.1
function parser(::Type{String}, x::String)
@test startswith(x, "myname_")
return replace(x, "myname_" => "")
end
SDDP.read_cuts_from_file(
model_2,
"model.cuts.json",
node_name_parser = parser,
)
N = num_constraints(
model_2["week"].subproblem;
count_variable_in_set_constraints = true,
)
SDDP.read_cuts_from_file(
model_2,
"model.cuts.json",
node_name_parser = (::Any, s) -> nothing,
)
N2 = num_constraints(
model_2["week"].subproblem;
count_variable_in_set_constraints = true,
)
@test N == N2
rm("model.cuts.json")
return
end

function test_add_all_cuts_SINGLE_CUT()
model = SDDP.LinearPolicyGraph(
stages = 3,
Expand Down