diff --git a/src/MSPFormat.jl b/src/MSPFormat.jl index 68e6fb2b9..1d7b3d4d0 100644 --- a/src/MSPFormat.jl +++ b/src/MSPFormat.jl @@ -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 @@ -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) diff --git a/test/MSPFormat.jl b/test/MSPFormat.jl index 68c8b50c4..776f93b9c 100644 --- a/test/MSPFormat.jl +++ b/test/MSPFormat.jl @@ -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) diff --git a/test/electric-tree.lattice.json b/test/electric-tree.lattice.json new file mode 100644 index 000000000..d84b670b0 --- /dev/null +++ b/test/electric-tree.lattice.json @@ -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": {}} +}