From 54c461ac269b3a7f43ba33a98ccb78432da210c1 Mon Sep 17 00:00:00 2001 From: Vincent Leclere Date: Wed, 25 Jul 2018 16:02:57 +0200 Subject: [PATCH 1/4] default sampler --- src/algorithm.jl | 26 ++++++++++++++++++++++++-- src/attributes.jl | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/algorithm.jl b/src/algorithm.jl index 825adb2..66534a3 100644 --- a/src/algorithm.jl +++ b/src/algorithm.jl @@ -112,11 +112,33 @@ algorithm `algo` with verbose level `verbose`. """ function sample_scenarios end +function sample_scenarios(sp::AbstractStochasticProgram, n::Int, depth_max = 1000;to::TimerOutput, verbose) + scenarios = [] + for i = 1:n + push!(scenarios,sample_scenario(sp, depth_max)) + end + return scenarios +end + +function sample_scenario(sp::AbstractStochasticProgram, depth_max = 1000;to::TimerOutput, verbose) + node = get(sp,MasterNode()) + s = [] + it = 0 + while !is_leaf(sp,node) && it < depth_max + tr = get(sp,RandomTransition,node) + node = tr.Target + push!(s, tr) + it += 1 + end + #TODO update timer output + return s +end + """ compute_bounds(algo::AbstractAlgorithm, paths::AbstractPaths, verbose) -Return a tuple `(z_UB, σ)` where z_UB reprensets the upperbound computed by the -algorithm `algo` by using paths `paths` generated during the forward pass and σ +Return a tuple `(z_UB, σ)` where z_UB represents the upperbound computed by Monte Carlo with the +algorithm `algo` by using paths `paths` and σ represents the standard deviation of this upper bound. """ function compute_bounds end diff --git a/src/attributes.jl b/src/attributes.jl index fb96a35..7daeda5 100644 --- a/src/attributes.jl +++ b/src/attributes.jl @@ -218,3 +218,35 @@ struct SourceSolution <: AbstractTransitionAttribute end The current bound to the objective of the node. """ struct TransitionObjectiveValueBound <: AbstractNodeAttribute end + +""" + RandomTransition <: AbstractNodeAttribute + +return a randomly selected transition from node `node` + ### Examples + + ```julia + get(model, RandomTransition(), node) + ``` +""" +struct RandomTransition <: AbstractNodeAttribute end +function get(sp::AbstractStochasticProgram, tr::RandomTransition,node::Int) + r = rand() + cdf = 0 + for tr in get(sp,OutTransitions(),node) + if cdf >= r + cdf += get(sp, Probability(), tr) + else + return tr + end + end + error("sum of probability $cdf < 1") +end + +struct IsLeaf <: AbstractNodeAttribute end +function get(sp::AbstractStochasticProgram, node::Int) + return isempty(get(sp,OutTransitions(),node)) +end +function is_empty(sp::AbstractStochasticProgram, node::Int) + return get(sp,IsLeaf(),node) +end From da5f42c56bf3a4c1c2bc6bf9a9ebd5b6f951bb02 Mon Sep 17 00:00:00 2001 From: Vincent Leclere Date: Wed, 25 Jul 2018 16:23:51 +0200 Subject: [PATCH 2/4] pass test --- src/algorithm.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/algorithm.jl b/src/algorithm.jl index 66534a3..d97080d 100644 --- a/src/algorithm.jl +++ b/src/algorithm.jl @@ -112,7 +112,7 @@ algorithm `algo` with verbose level `verbose`. """ function sample_scenarios end -function sample_scenarios(sp::AbstractStochasticProgram, n::Int, depth_max = 1000;to::TimerOutput, verbose) +function sample_scenarios(sp::AbstractStochasticProgram, n::Int, depth_max = 1000,to::TimerOutput=TimerOutput(), verbose::Int=0) scenarios = [] for i = 1:n push!(scenarios,sample_scenario(sp, depth_max)) @@ -120,7 +120,7 @@ function sample_scenarios(sp::AbstractStochasticProgram, n::Int, depth_max = 100 return scenarios end -function sample_scenario(sp::AbstractStochasticProgram, depth_max = 1000;to::TimerOutput, verbose) +function sample_scenario(sp::AbstractStochasticProgram, depth_max = 1000,to::TimerOutput=TimerOutput(), verbose::Int=0) node = get(sp,MasterNode()) s = [] it = 0 From 86f82f42ffb1586ec4fd7d8b0cdbb79237cd214c Mon Sep 17 00:00:00 2001 From: Vincent Leclere Date: Wed, 25 Jul 2018 16:44:37 +0200 Subject: [PATCH 3/4] changes requested by fp --- src/algorithm.jl | 10 ++++++---- src/attributes.jl | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/algorithm.jl b/src/algorithm.jl index d97080d..41ae12a 100644 --- a/src/algorithm.jl +++ b/src/algorithm.jl @@ -112,17 +112,19 @@ algorithm `algo` with verbose level `verbose`. """ function sample_scenarios end -function sample_scenarios(sp::AbstractStochasticProgram, n::Int, depth_max = 1000,to::TimerOutput=TimerOutput(), verbose::Int=0) +function sample_scenarios(sp::AbstractStochasticProgram, n::Int, + depth_max=1000, to::TimerOutput=TimerOutput(), verbose::Int=0) scenarios = [] - for i = 1:n + for i in 1:n push!(scenarios,sample_scenario(sp, depth_max)) end return scenarios end -function sample_scenario(sp::AbstractStochasticProgram, depth_max = 1000,to::TimerOutput=TimerOutput(), verbose::Int=0) +function sample_scenario(sp::AbstractStochasticProgram, depth_max=1000, + to::TimerOutput=TimerOutput(), verbose::Int=0) node = get(sp,MasterNode()) - s = [] + s = Vector{:= r cdf += get(sp, Probability(), tr) @@ -245,8 +245,8 @@ end struct IsLeaf <: AbstractNodeAttribute end function get(sp::AbstractStochasticProgram, node::Int) - return isempty(get(sp,OutTransitions(),node)) + return isempty(get(sp, OutTransitions(), node)) end function is_empty(sp::AbstractStochasticProgram, node::Int) - return get(sp,IsLeaf(),node) + return get(sp, IsLeaf(), node) end From 6ae5a6d0a83c90872624af5be50389971db624e8 Mon Sep 17 00:00:00 2001 From: Vincent Leclere Date: Wed, 25 Jul 2018 16:49:33 +0200 Subject: [PATCH 4/4] indent --- src/attributes.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/attributes.jl b/src/attributes.jl index 818dcb6..a02f377 100644 --- a/src/attributes.jl +++ b/src/attributes.jl @@ -223,11 +223,11 @@ struct TransitionObjectiveValueBound <: AbstractNodeAttribute end RandomTransition <: AbstractNodeAttribute return a randomly selected transition from node `node` - ### Examples +### Examples - ```julia - get(model, RandomTransition(), node) - ``` +```julia +get(model, RandomTransition(), node) +``` """ struct RandomTransition <: AbstractNodeAttribute end function get(sp::AbstractStochasticProgram, tr::RandomTransition, node::Int)