From 2d5d357d279ad2b235189961907e701428a9e78e Mon Sep 17 00:00:00 2001 From: arthur-brigatto <103693830+arthur-brigatto@users.noreply.github.com> Date: Mon, 13 May 2024 13:28:20 -0300 Subject: [PATCH 01/18] Change sample_backward_noise_terms(backward_sampling_scheme, child_node) to sample_backward_noise_terms(backward_sampling_scheme, child_node, outgoing_state) --- src/algorithm.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithm.jl b/src/algorithm.jl index 92bce7d93..1bfa6a971 100644 --- a/src/algorithm.jl +++ b/src/algorithm.jl @@ -660,7 +660,7 @@ function solve_all_children( end child_node = model[child.term] for noise in - sample_backward_noise_terms(backward_sampling_scheme, child_node) + sample_backward_noise_terms(backward_sampling_scheme, child_node, outgoing_state) if length(scenario_path) == length_scenario_path push!(scenario_path, (child.term, noise.term)) else From 315e062f5c1e50f83fc0eb886f804665fcba1e3e Mon Sep 17 00:00:00 2001 From: arthur-brigatto <103693830+arthur-brigatto@users.noreply.github.com> Date: Mon, 13 May 2024 13:29:44 -0300 Subject: [PATCH 02/18] Change sample_backward_noise_terms to sample_backward_noise_terms_with_state --- src/plugins/backward_sampling_schemes.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/backward_sampling_schemes.jl b/src/plugins/backward_sampling_schemes.jl index c06106dcb..be5832130 100644 --- a/src/plugins/backward_sampling_schemes.jl +++ b/src/plugins/backward_sampling_schemes.jl @@ -10,7 +10,7 @@ Backward sampler that returns all noises of the corresponding node. """ struct CompleteSampler <: AbstractBackwardSamplingScheme end -sample_backward_noise_terms(::CompleteSampler, node) = node.noise_terms +sample_backward_noise_terms_with_state(::CompleteSampler, node::Node, state::Dict{Symbol,Float64}) = node.noise_terms """ MonteCarloSampler(number_of_samples::Int) @@ -22,7 +22,7 @@ struct MonteCarloSampler <: AbstractBackwardSamplingScheme number_of_samples::Int end -function sample_backward_noise_terms(sampler::MonteCarloSampler, node::Node) +function sample_backward_noise_terms(sampler::MonteCarloSampler, node::Node, state::Dict{Symbol,Float64}) prob = 1 / sampler.number_of_samples return [ Noise(sample_noise(node.noise_terms), prob) for From 3515e10dbabfdbd41f2e6c95a8ef0ecce2a2e625 Mon Sep 17 00:00:00 2001 From: arthur-brigatto <103693830+arthur-brigatto@users.noreply.github.com> Date: Mon, 13 May 2024 18:59:49 -0300 Subject: [PATCH 03/18] Update headers.jl --- src/plugins/headers.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/plugins/headers.jl b/src/plugins/headers.jl index 809b96750..c98560acb 100644 --- a/src/plugins/headers.jl +++ b/src/plugins/headers.jl @@ -112,6 +112,7 @@ abstract type AbstractBackwardSamplingScheme end sample_backward_noise_terms( backward_sampling_scheme::AbstractBackwardSamplingScheme, node::Node{T}, + state::Dict{Symbol,Float64}, )::Vector{Noise} Returns a `Vector{Noise}` of noises sampled from `node.noise_terms` using From dccf1aa186befea060f1e7c9b1f6ba455604795c Mon Sep 17 00:00:00 2001 From: arthur-brigatto <103693830+arthur-brigatto@users.noreply.github.com> Date: Mon, 13 May 2024 20:09:58 -0300 Subject: [PATCH 04/18] Add method sample_backward_noise_terms_with_state --- src/plugins/headers.jl | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/plugins/headers.jl b/src/plugins/headers.jl index c98560acb..de201094d 100644 --- a/src/plugins/headers.jl +++ b/src/plugins/headers.jl @@ -116,10 +116,28 @@ abstract type AbstractBackwardSamplingScheme end )::Vector{Noise} Returns a `Vector{Noise}` of noises sampled from `node.noise_terms` using -`backward_sampling_scheme` +`backward_sampling_scheme`. """ function sample_backward_noise_terms end +""" + sample_backward_noise_terms_with_state( + sampler::AbstractBackwardSamplingScheme, + node::Node, + state::Dict{Symbol,Float64}, + )::Vector{Noise} + +Returns a `Vector{Noise}` of noises sampled conditionally on the sate `state` using +`backward_sampling_scheme`. +""" +function sample_backward_noise_terms_with_state( + sampler::AbstractBackwardSamplingScheme, + node::Node, + state::Dict{Symbol,Float64}, +) + return sample_backward_noise_terms(sampler, node) +end + # =========================== duality_handlers =========================== # """ From 5ce9df55aee6c1f7fec6ca6c1f0df236a91ed7b3 Mon Sep 17 00:00:00 2001 From: arthur-brigatto <103693830+arthur-brigatto@users.noreply.github.com> Date: Mon, 13 May 2024 20:10:47 -0300 Subject: [PATCH 05/18] Update src/plugins/backward_sampling_schemes.jl Co-authored-by: Oscar Dowson --- src/plugins/backward_sampling_schemes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/backward_sampling_schemes.jl b/src/plugins/backward_sampling_schemes.jl index be5832130..c546a16c9 100644 --- a/src/plugins/backward_sampling_schemes.jl +++ b/src/plugins/backward_sampling_schemes.jl @@ -22,7 +22,7 @@ struct MonteCarloSampler <: AbstractBackwardSamplingScheme number_of_samples::Int end -function sample_backward_noise_terms(sampler::MonteCarloSampler, node::Node, state::Dict{Symbol,Float64}) +function sample_backward_noise_terms(sampler::MonteCarloSampler, node::Node) prob = 1 / sampler.number_of_samples return [ Noise(sample_noise(node.noise_terms), prob) for From 150cc978330a1a5e4f82df02234b16a16aa2ed9c Mon Sep 17 00:00:00 2001 From: arthur-brigatto <103693830+arthur-brigatto@users.noreply.github.com> Date: Mon, 13 May 2024 20:10:53 -0300 Subject: [PATCH 06/18] Update src/algorithm.jl Co-authored-by: Oscar Dowson --- src/algorithm.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/algorithm.jl b/src/algorithm.jl index 1bfa6a971..ba03ee652 100644 --- a/src/algorithm.jl +++ b/src/algorithm.jl @@ -660,7 +660,11 @@ function solve_all_children( end child_node = model[child.term] for noise in - sample_backward_noise_terms(backward_sampling_scheme, child_node, outgoing_state) + sample_backward_noise_terms_with_state( + backward_sampling_scheme, + child_node, + outgoing_state, + ) if length(scenario_path) == length_scenario_path push!(scenario_path, (child.term, noise.term)) else From 2de3c78db99f1c4e60cd038819b5c0a8108ae3dd Mon Sep 17 00:00:00 2001 From: arthur-brigatto <103693830+arthur-brigatto@users.noreply.github.com> Date: Mon, 13 May 2024 20:11:04 -0300 Subject: [PATCH 07/18] Update src/plugins/backward_sampling_schemes.jl Co-authored-by: Oscar Dowson --- src/plugins/backward_sampling_schemes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/backward_sampling_schemes.jl b/src/plugins/backward_sampling_schemes.jl index c546a16c9..e185c0592 100644 --- a/src/plugins/backward_sampling_schemes.jl +++ b/src/plugins/backward_sampling_schemes.jl @@ -10,7 +10,7 @@ Backward sampler that returns all noises of the corresponding node. """ struct CompleteSampler <: AbstractBackwardSamplingScheme end -sample_backward_noise_terms_with_state(::CompleteSampler, node::Node, state::Dict{Symbol,Float64}) = node.noise_terms +sample_backward_noise_terms(::CompleteSampler, node::Node) = node.noise_terms """ MonteCarloSampler(number_of_samples::Int) From 4fbb11e2ce7bd61deed6040fb9165c178be2e675 Mon Sep 17 00:00:00 2001 From: arthur-brigatto <103693830+arthur-brigatto@users.noreply.github.com> Date: Mon, 13 May 2024 20:12:36 -0300 Subject: [PATCH 08/18] Update src/plugins/headers.jl Co-authored-by: Oscar Dowson --- src/plugins/headers.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plugins/headers.jl b/src/plugins/headers.jl index de201094d..b629071fc 100644 --- a/src/plugins/headers.jl +++ b/src/plugins/headers.jl @@ -112,7 +112,6 @@ abstract type AbstractBackwardSamplingScheme end sample_backward_noise_terms( backward_sampling_scheme::AbstractBackwardSamplingScheme, node::Node{T}, - state::Dict{Symbol,Float64}, )::Vector{Noise} Returns a `Vector{Noise}` of noises sampled from `node.noise_terms` using From 55a0b7c0051bde3059a4612a24bd304432083064 Mon Sep 17 00:00:00 2001 From: arthur-brigatto <103693830+arthur-brigatto@users.noreply.github.com> Date: Mon, 13 May 2024 20:22:24 -0300 Subject: [PATCH 09/18] Update src/plugins/headers.jl Co-authored-by: Oscar Dowson --- src/plugins/headers.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/headers.jl b/src/plugins/headers.jl index b629071fc..600a85c43 100644 --- a/src/plugins/headers.jl +++ b/src/plugins/headers.jl @@ -126,7 +126,7 @@ function sample_backward_noise_terms end state::Dict{Symbol,Float64}, )::Vector{Noise} -Returns a `Vector{Noise}` of noises sampled conditionally on the sate `state` using +Returns a `Vector{Noise}` of noises sampled conditionally on the `state` using `backward_sampling_scheme`. """ function sample_backward_noise_terms_with_state( From 68ceeb568cf971894f13136c4b78ed0b5ce61208 Mon Sep 17 00:00:00 2001 From: arthur-brigatto <103693830+arthur-brigatto@users.noreply.github.com> Date: Mon, 13 May 2024 20:22:30 -0300 Subject: [PATCH 10/18] Update src/plugins/headers.jl Co-authored-by: Oscar Dowson --- src/plugins/headers.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/headers.jl b/src/plugins/headers.jl index 600a85c43..86b475401 100644 --- a/src/plugins/headers.jl +++ b/src/plugins/headers.jl @@ -127,7 +127,7 @@ function sample_backward_noise_terms end )::Vector{Noise} Returns a `Vector{Noise}` of noises sampled conditionally on the `state` using -`backward_sampling_scheme`. +`sampler`. """ function sample_backward_noise_terms_with_state( sampler::AbstractBackwardSamplingScheme, From 177721d88fad7ecb9f6a865d77fb1191530eab87 Mon Sep 17 00:00:00 2001 From: arthur-brigatto Date: Wed, 22 May 2024 13:42:16 -0300 Subject: [PATCH 11/18] Add test --- test/plugins/backward_sampling_schemes.jl | 60 +++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/plugins/backward_sampling_schemes.jl b/test/plugins/backward_sampling_schemes.jl index 8e2d2a9d9..5dcdf36b2 100644 --- a/test/plugins/backward_sampling_schemes.jl +++ b/test/plugins/backward_sampling_schemes.jl @@ -7,6 +7,7 @@ module TestBackwardPassSamplingSchemes using SDDP using Test +using HiGHS function runtests() for name in names(@__MODULE__; all = true) @@ -89,6 +90,65 @@ function test_MonteCarloSampler_100() return end +mutable struct WithStateSampler <: SDDP.AbstractBackwardSamplingScheme + number_of_samples::Int +end +function test_WithStateSampler() + function sample_backward_noise_terms_with_state( + sampler::WithStateSampler, + node::SDDP.Node, + state::Dict{Symbol,Float64}, + ) + if state[:x] / node.index == 0.0 + return [ + SDDP.Noise((ϵ = 1.0,), 1 / sampler.number_of_samples) for + i in 1:sampler.number_of_samples + ] + elseif state[:x] / node.index == 1.0 + return [ + SDDP.Noise((ϵ = 0.0,), 1 / sampler.number_of_samples) for + i in 1:sampler.number_of_samples + ] + end + end + model = SDDP.LinearPolicyGraph( + stages = 5, + lower_bound = 0.0, + direct_mode = false, + optimizer = HiGHS.Optimizer, + ) do node, stage + @variable(node, x, SDDP.State, initial_value = 0.0) + @variable(node, ϵ) + SDDP.parameterize(node, stage * [1, 3], [0.9, 0.1]) do ω + return JuMP.fix(ϵ, ω) + end + @constraint(node, x.out == ϵ) + end + forward_trajectory = SDDP.forward_pass( + model, + SDDP.Options(model, Dict(:x => 1.0)), + SDDP.DefaultForwardPass(), + ) + for node_index in 1:length(forward_trajectory.scenario_path) + state = forward_trajectory.sampled_states[node_index] + terms = sample_backward_noise_terms_with_state( + WithStateSampler(100), + model[node_index], + state, + ) + for term in terms + @test term.probability == 0.01 + if state == 0.0 + @test backward_noise.term.ϵ == 1.0 + elseif state == 1.0 + @test backward_noise.term.ϵ == 0.0 + end + end + end + + return +end + end # module TestBackwardPassSamplingSchemes.runtests() From 32c3ca08c65b883065f7ee33e243b5dc968a7985 Mon Sep 17 00:00:00 2001 From: arthur-brigatto Date: Wed, 22 May 2024 13:53:42 -0300 Subject: [PATCH 12/18] Add test --- test/plugins/backward_sampling_schemes.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/plugins/backward_sampling_schemes.jl b/test/plugins/backward_sampling_schemes.jl index 5dcdf36b2..0051aedf9 100644 --- a/test/plugins/backward_sampling_schemes.jl +++ b/test/plugins/backward_sampling_schemes.jl @@ -99,14 +99,14 @@ function test_WithStateSampler() node::SDDP.Node, state::Dict{Symbol,Float64}, ) - if state[:x] / node.index == 0.0 + if state[:x] / node.index == 1.0 return [ - SDDP.Noise((ϵ = 1.0,), 1 / sampler.number_of_samples) for + SDDP.Noise((ϵ = 3.0,), 1 / sampler.number_of_samples) for i in 1:sampler.number_of_samples ] - elseif state[:x] / node.index == 1.0 + elseif state[:x] / node.index == 3.0 return [ - SDDP.Noise((ϵ = 0.0,), 1 / sampler.number_of_samples) for + SDDP.Noise((ϵ = 1.0,), 1 / sampler.number_of_samples) for i in 1:sampler.number_of_samples ] end @@ -138,10 +138,10 @@ function test_WithStateSampler() ) for term in terms @test term.probability == 0.01 - if state == 0.0 - @test backward_noise.term.ϵ == 1.0 - elseif state == 1.0 - @test backward_noise.term.ϵ == 0.0 + if state[:x] / node_index == 1.0 + @test term.term.ϵ == 3.0 + elseif state[:x] / node_index == 3.0 + @test term.term.ϵ == 1.0 end end end From 772076b57e0b79d3fbed8469ee26ea001c4686c4 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 4 Jun 2024 13:57:20 +1200 Subject: [PATCH 13/18] Update backward_sampling_schemes.jl --- test/plugins/backward_sampling_schemes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/plugins/backward_sampling_schemes.jl b/test/plugins/backward_sampling_schemes.jl index 0051aedf9..810725351 100644 --- a/test/plugins/backward_sampling_schemes.jl +++ b/test/plugins/backward_sampling_schemes.jl @@ -93,6 +93,7 @@ end mutable struct WithStateSampler <: SDDP.AbstractBackwardSamplingScheme number_of_samples::Int end + function test_WithStateSampler() function sample_backward_noise_terms_with_state( sampler::WithStateSampler, @@ -145,7 +146,6 @@ function test_WithStateSampler() end end end - return end From 8010fe9df1f1f05b042f3f1dc32cd4e586995755 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 4 Jun 2024 13:58:04 +1200 Subject: [PATCH 14/18] Update algorithm.jl --- src/algorithm.jl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/algorithm.jl b/src/algorithm.jl index ba03ee652..9f7b46555 100644 --- a/src/algorithm.jl +++ b/src/algorithm.jl @@ -659,12 +659,11 @@ function solve_all_children( continue end child_node = model[child.term] - for noise in - sample_backward_noise_terms_with_state( - backward_sampling_scheme, - child_node, - outgoing_state, - ) + for noise in sample_backward_noise_terms_with_state( + backward_sampling_scheme, + child_node, + outgoing_state, + ) if length(scenario_path) == length_scenario_path push!(scenario_path, (child.term, noise.term)) else From b84f8dca2c50341710c2c86a6ba31930614ef6e5 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 4 Jun 2024 13:58:24 +1200 Subject: [PATCH 15/18] Update src/plugins/backward_sampling_schemes.jl --- src/plugins/backward_sampling_schemes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/backward_sampling_schemes.jl b/src/plugins/backward_sampling_schemes.jl index e185c0592..c06106dcb 100644 --- a/src/plugins/backward_sampling_schemes.jl +++ b/src/plugins/backward_sampling_schemes.jl @@ -10,7 +10,7 @@ Backward sampler that returns all noises of the corresponding node. """ struct CompleteSampler <: AbstractBackwardSamplingScheme end -sample_backward_noise_terms(::CompleteSampler, node::Node) = node.noise_terms +sample_backward_noise_terms(::CompleteSampler, node) = node.noise_terms """ MonteCarloSampler(number_of_samples::Int) From 64f81dabf4c0a59276f586f10399766b07d1b090 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 4 Jun 2024 13:58:49 +1200 Subject: [PATCH 16/18] Update src/plugins/headers.jl --- src/plugins/headers.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/headers.jl b/src/plugins/headers.jl index 86b475401..0f19ecff5 100644 --- a/src/plugins/headers.jl +++ b/src/plugins/headers.jl @@ -132,7 +132,7 @@ Returns a `Vector{Noise}` of noises sampled conditionally on the `state` using function sample_backward_noise_terms_with_state( sampler::AbstractBackwardSamplingScheme, node::Node, - state::Dict{Symbol,Float64}, + ::Dict{Symbol,Float64}, ) return sample_backward_noise_terms(sampler, node) end From 92f9187fa402e306fdda5429d05b22be385addff Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 4 Jun 2024 13:59:30 +1200 Subject: [PATCH 17/18] Update test/plugins/backward_sampling_schemes.jl --- test/plugins/backward_sampling_schemes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/plugins/backward_sampling_schemes.jl b/test/plugins/backward_sampling_schemes.jl index 810725351..8c172bd13 100644 --- a/test/plugins/backward_sampling_schemes.jl +++ b/test/plugins/backward_sampling_schemes.jl @@ -7,7 +7,7 @@ module TestBackwardPassSamplingSchemes using SDDP using Test -using HiGHS +import HiGHS function runtests() for name in names(@__MODULE__; all = true) From dff939586cb5bb45186f9c470c070b35016b95af Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 4 Jun 2024 14:24:17 +1200 Subject: [PATCH 18/18] Update src/plugins/headers.jl --- src/plugins/headers.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/headers.jl b/src/plugins/headers.jl index 0f19ecff5..38cfb179c 100644 --- a/src/plugins/headers.jl +++ b/src/plugins/headers.jl @@ -132,7 +132,7 @@ Returns a `Vector{Noise}` of noises sampled conditionally on the `state` using function sample_backward_noise_terms_with_state( sampler::AbstractBackwardSamplingScheme, node::Node, - ::Dict{Symbol,Float64}, + ::Dict{Symbol,Float64}, ) return sample_backward_noise_terms(sampler, node) end