Skip to content

Commit

Permalink
SDDPTree -> SDDPGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 25, 2017
1 parent 2c2dfbf commit 9b6eed2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
26 changes: 13 additions & 13 deletions src/graph.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
export AbstractSDDPTree, haschildren, nchidren, children, getchild, getproba, getprobas, cutgen, numberofpaths
abstract type AbstractSDDPTree{S} end
export AbstractSDDPGraph, haschildren, nchidren, children, getchild, getproba, getprobas, cutgen, numberofpaths
abstract type AbstractSDDPGraph{S} end

mutable struct GraphSDDPTree{S} <: AbstractSDDPTree{S}
mutable struct SDDPGraph{S} <: AbstractSDDPGraph{S}
root::SDDPNode{S}
end

getmaster(g::GraphSDDPTree) = g.root, g.root
getmaster(g::SDDPGraph) = g.root, g.root

# Get children scenarios
haschildren(g::GraphSDDPTree, node::SDDPNode) = !isempty(node.children)
nchildren(g::GraphSDDPTree, node::SDDPNode) = length(node.children)
children(g::GraphSDDPTree, node::SDDPNode) = node.children
getchild(g::GraphSDDPTree, node::SDDPNode, i) = node.children[i]
haschildren(g::SDDPGraph, node::SDDPNode) = !isempty(node.children)
nchildren(g::SDDPGraph, node::SDDPNode) = length(node.children)
children(g::SDDPGraph, node::SDDPNode) = node.children
getchild(g::SDDPGraph, node::SDDPNode, i) = node.children[i]
# Get proba of children scenario
getproba(g::GraphSDDPTree, node::SDDPNode, i) = node.proba[i]
getprobas(g::GraphSDDPTree, node::SDDPNode) = node.proba
getproba(g::SDDPGraph, node::SDDPNode, i) = node.proba[i]
getprobas(g::SDDPGraph, node::SDDPNode) = node.proba

# Get number of paths
numberofpaths(g::GraphSDDPTree, num_stages) = numberofpaths(g.root, 1, num_stages)
numberofpaths(g::GraphSDDPTree, node::SDDPNode, t, num_stages) = numberofpaths(node, t, num_stages)
numberofpaths(g::SDDPGraph, num_stages) = numberofpaths(g.root, 1, num_stages)
numberofpaths(g::SDDPGraph, node::SDDPNode, t, num_stages) = numberofpaths(node, t, num_stages)

cutgen(g::GraphSDDPTree, node::SDDPNode) = node.nlds.cutgen
cutgen(g::SDDPGraph, node::SDDPNode) = node.nlds.cutgen
2 changes: 1 addition & 1 deletion src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ If `cutgen` is `MultiCutGenerator`, one variable `θ_i` is created for each scen
"""
function model2lattice(m::Model, num_stages, solver, pruningalgo::AbstractCutPruningAlgo, cutgen::AbstractOptimalityCutGenerator=MultiCutGenerator, detectlb::Bool=true, newcut::Symbol=:InvalidateSolver)
root = getSDDPNode(m, 1, num_stages, solver, nothing, pruningalgo, cutgen, detectlb, newcut)
GraphSDDPTree(root)
SDDPGraph(root)
end

function SDDPclear(m::Model)
Expand Down
4 changes: 2 additions & 2 deletions src/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function mergesamepaths{NodeT}(pathsd::Vector{Tuple{NodeT, Vector{SDDPPath}}}, s
newpathsd
end

function childjobs{NodeT}(g::AbstractSDDPTree, pathsd::Vector{Tuple{NodeT, Vector{SDDPPath}}}, pathsampler, t, num_stages)
function childjobs{NodeT}(g::AbstractSDDPGraph, pathsd::Vector{Tuple{NodeT, Vector{SDDPPath}}}, pathsampler, t, num_stages)
jobsd = Dict{NodeT, Vector{SDDPJob{NodeT}}}()
for (node, paths) in pathsd
if haschildren(g, node)
Expand All @@ -112,7 +112,7 @@ function childjobs{NodeT}(g::AbstractSDDPTree, pathsd::Vector{Tuple{NodeT, Vecto
jobsd
end

function jobstopaths{NodeT}(jobsd::Dict{NodeT, Vector{SDDPJob{NodeT}}}, g::AbstractSDDPTree)
function jobstopaths{NodeT}(jobsd::Dict{NodeT, Vector{SDDPJob{NodeT}}}, g::AbstractSDDPGraph)
pathsd = Tuple{NodeT, Vector{SDDPPath}}[]
for (node, jobs) in jobsd
K = [find(job.K .!= 0) for job in jobs]
Expand Down
6 changes: 3 additions & 3 deletions src/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function _samplepaths(npaths, pmf, semirandom::Bool, canmodifypmf::Bool)
end
infpaths(g, node) = fill(-1, nchildren(g, node))

function samplepaths(pathsampler::AbstractPathSampler, g::GraphSDDPTree, node, npaths::Vector{Int}, t, num_stages)
function samplepaths(pathsampler::AbstractPathSampler, g::AbstractSDDPGraph, node, npaths::Vector{Int}, t, num_stages)
npathss = Vector{Int}[similar(npaths) for i in 1:nchildren(g, node)]
for i in 1:length(npaths)
_npaths = samplepaths(pathsampler, g, node, npaths[i], t, num_stages)
Expand All @@ -63,7 +63,7 @@ end
struct ProbaPathSampler <: AbstractPathSampler
semirandom::Bool
end
function samplepaths(pathsampler::ProbaPathSampler, g::GraphSDDPTree, node, npaths::Int, t, num_stages)
function samplepaths(pathsampler::ProbaPathSampler, g::AbstractSDDPGraph, node, npaths::Int, t, num_stages)
if npaths == -1
infpaths(g, node)
else
Expand All @@ -74,7 +74,7 @@ end
struct NumPathsPathSampler <: AbstractPathSampler
semirandom::Bool
end
function samplepaths(pathsampler::NumPathsPathSampler, g::GraphSDDPTree, node, npaths::Int, t, num_stages)
function samplepaths(pathsampler::NumPathsPathSampler, g::AbstractSDDPGraph, node, npaths::Int, t, num_stages)
if npaths == -1
infpaths(g, node)
else
Expand Down
4 changes: 2 additions & 2 deletions src/sddp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ The paths will be selected according to `pathsampler` and equivalent paths might
The parameter `ztol` is also used to check whether a new cut is useful.
When a scenario is infeasible and `stopatinf` is true then no other scenario with the same ancestor is run. Note that since the order in which the different scenarios is run is not deterministic, this might introduce nondeterminism even if the sampling is deterministic.
"""
function iteration{S}(g::AbstractSDDPTree{S}, Ktot::Int, num_stages, verbose, pathsampler; ztol=1e-6, stopatinf=false, mergepaths=true, forwardcuts=false, backwardcuts=true)
function iteration{S}(g::AbstractSDDPGraph{S}, Ktot::Int, num_stages, verbose, pathsampler; ztol=1e-6, stopatinf=false, mergepaths=true, forwardcuts=false, backwardcuts=true)
stats = SDDPStats()

master, initialnode = getmaster(g)
Expand Down Expand Up @@ -138,7 +138,7 @@ The parameter `ztol` is also used to check whether a new cut is useful.
When a scenario is infeasible and `stopatinf` is true then no other scenario with the same ancestor is run. Note that since the order in which the different scenarios is run is not deterministic, this might introduce nondeterminism even if the sampling is deterministic.
By default, the cuts are added backward. However, if `forwardcuts` is set to `true` and `backwardcuts` is set to `false` the cuts are added forward.
"""
function SDDP(g::AbstractSDDPTree, num_stages; K::Int=25, stopcrit::AbstractStoppingCriterion=Pereira(), verbose=0, pathsampler::AbstractPathSampler=ProbaPathSampler(true), ztol=1e-6, stopatinf=false, mergepaths=true, forwardcuts=false, backwardcuts=true)
function SDDP(g::AbstractSDDPGraph, num_stages; K::Int=25, stopcrit::AbstractStoppingCriterion=Pereira(), verbose=0, pathsampler::AbstractPathSampler=ProbaPathSampler(true), ztol=1e-6, stopatinf=false, mergepaths=true, forwardcuts=false, backwardcuts=true)
mastersol = nothing
totalstats = SDDPStats()
stats = SDDPStats()
Expand Down
2 changes: 1 addition & 1 deletion src/waitandsee.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function meanstdpaths(paths::Vector{WSPath}, totalK)
meanstdpaths(z, proba, npaths, totalK)
end

function waitandsee(g::AbstractSDDPTree, num_stages, solver, totalK=25, verbose=0)
function waitandsee(g::AbstractSDDPGraph, num_stages, solver, totalK=25, verbose=0)
root = g.root
paths = WSPath[WSPath(root, NLDS[root.nlds], .0, 1., totalK)]
for t in 2:num_stages
Expand Down

0 comments on commit 9b6eed2

Please sign in to comment.