Skip to content
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
9 changes: 9 additions & 0 deletions docs/src/tutorial/first_steps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,15 @@ using SDDP

graph = SDDP.LinearGraph(3)

# Here's the graph:

## We need `open = false` to build the documentation. Remove if running locally.
SDDP.plot(graph, "model_graph.html"; open = false)

# ```@raw html
# <iframe src="../model_graph.html" style="width:100%;height:500px;"></iframe>
# ```

# #### Building the subproblem

# Next, we need to construct the associated subproblem for each node in `graph`.
Expand Down
32 changes: 32 additions & 0 deletions src/visualization/plot_graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

"""
plot(model::PolicyGraph[, filename::String]; open::Bool = true)
plot(model::Graph[, filename::String]; open::Bool = true)

This is an experimental function that plots the structure of the policy graph
`model` using Javascript.
Expand Down Expand Up @@ -73,3 +74,34 @@ function plot(
)
return
end

function plot(
graph::Graph,
filename::String = joinpath(tempdir(), Random.randstring() * ".html");
open::Bool = true,
)
data = Any[]
push!(data, "{data: {id: '$(graph.root_node)', shape: 'ellipse'}}")
names = graph.nodes |> keys |> collect |> sort
for name in filter(!=(graph.root_node), names)
push!(data, "{data: {id: '$(name)', meta: 'Node: $name'}}")
end
edge_id = 0
for name in names
for (child, probability) in graph.nodes[name]
edge_id += 1
meta = "From: $(name)\\nTo: $child\\nProbablity: $probability"
push!(
data,
"{data: {id: 'edge_$(edge_id)', source: '$name', target: '$child', meta: '$meta'}}",
)
end
end
fill_template(
filename,
"<!--DATA-->" => join(data, ",\n");
template = joinpath(dirname(@__FILE__), "graph.html"),
launch = open,
)
return
end
22 changes: 21 additions & 1 deletion test/visualization/visualization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function test_PublicationPlot_different_lengths()
return
end

function test_plot_graph()
function test_plot_policy_graph()
model = SDDP.LinearPolicyGraph(; stages = 3, lower_bound = 0.0) do sp, t
@variable(sp, 0 <= x <= 100, SDDP.State, initial_value = 0)
@variable(sp, 0 <= u_production <= 200)
Expand Down Expand Up @@ -120,6 +120,26 @@ function test_plot_graph()
return
end

function test_plot_graph()
graph = SDDP.LinearGraph(3)
dir = mktempdir()
filename = joinpath(dir, "plot_graph.html")
SDDP.plot(graph, filename; open = false)
contents = read(filename, String)
for line in [
"{data: {id: '0', shape: 'ellipse'}},",
"{data: {id: '1', meta: 'Node: 1'}},",
"{data: {id: '2', meta: 'Node: 2'}},",
"{data: {id: '3', meta: 'Node: 3'}},",
"{data: {id: 'edge_1', source: '0', target: '1', meta: 'From: 0\\nTo: 1\\nProbablity: 1.0'}},",
"{data: {id: 'edge_2', source: '1', target: '2', meta: 'From: 1\\nTo: 2\\nProbablity: 1.0'}},",
"{data: {id: 'edge_3', source: '2', target: '3', meta: 'From: 2\\nTo: 3\\nProbablity: 1.0'}},",
]
@test occursin(line, contents)
end
return
end

end # module

TestVisualization.runtests()
Loading