Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 24, 2023
1 parent 322f233 commit 3ece814
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/MSPFormat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function _parse_lattice(filename::String)
SDDP.add_node(graph, key)
end
for (key, value) in data
for (child, probability) in value["successors"]
for (child, probability) in sort(value["successors"])
SDDP.add_edge(graph, key => child, probability)
end
end
Expand Down Expand Up @@ -91,7 +91,7 @@ function _reduce_lattice(graph, data)
)
parent = "root"
while !isempty(graph.nodes[parent])
for (node, probability) in graph.nodes[parent]
for (node, probability) in sort(graph.nodes[parent])
t = string(data[node]["stage"])
push!(graph_data[t]["sample_space"], data[node]["state"])
push!(graph_data[t]["probability"], probability)
Expand Down
26 changes: 26 additions & 0 deletions test/MSPFormat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ function test_SimpleHydroThermal_round_trip()
return
end

function test_electric_non_stagewise()
model_stagewise = MSPFormat.read_from_file(
joinpath(@__DIR__, "electric.problem.json"),
joinpath(@__DIR__, "electric.lattice.json"),
)
@test length(model_stagewise.nodes) == 2
@test model_stagewise["0"].children == [SDDP.Noise("1", 1.0)]
@test length(model_stagewise["0"].noise_terms) == 1
@test model_stagewise["1"].children == SDDP.Noise{String}[]
@test length(model_stagewise["1"].noise_terms) == 3
model_tree = MSPFormat.read_from_file(
joinpath(@__DIR__, "electric.problem.json"),
joinpath(@__DIR__, "electric-tree.lattice.json"),
)
@test length(model_tree.nodes) == 5
@test model_tree["0"].children == SDDP.Noise.(["2", "3"], [0.3, 0.7])
@test model_tree["1"].children == SDDP.Noise.(["3", "4"], [0.4, 0.6])
@test model_tree["2"].children == SDDP.Noise{String}[]
@test model_tree["3"].children == SDDP.Noise{String}[]
@test model_tree["4"].children == SDDP.Noise{String}[]
for i in 0:4
@test length(model_tree["$i"].noise_terms) == 1
end
return
end

function test_electric()
problem = joinpath(@__DIR__, "electric")
model = MSPFormat.read_from_file(problem)
Expand Down
7 changes: 7 additions & 0 deletions test/electric-tree.lattice.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"0": {"stage": 0, "state": {}, "successors": {"2": 0.3, "3": 0.7}},
"1": {"stage": 0, "state": {}, "successors": {"3": 0.4, "4": 0.6}},
"2": {"stage": 1, "state": {"d": 2.0}, "successors": {}},
"3": {"stage": 1, "state": {"d": 4.0}, "successors": {}},
"4": {"stage": 1, "state": {"d": 6.0}, "successors": {}}
}

0 comments on commit 3ece814

Please sign in to comment.