Skip to content
Open
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
28 changes: 26 additions & 2 deletions src/algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,35 @@ 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)
scenarios = []
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)
node = get(sp,MasterNode())
s = Vector{:<AbstractTransition}
it = 0
while !is_leaf(sp,node) && it < depth_max
tr = get(sp,RandomTransition,node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RandomTransition -> RandomTransition ()

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
Expand Down
32 changes: 32 additions & 0 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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