From 54136a05d69b26343c3a063f42d70a4e5a1ae8b0 Mon Sep 17 00:00:00 2001 From: Esa Honkamaa Date: Fri, 30 Aug 2024 13:57:22 +0300 Subject: [PATCH] README.jl updated and example working (node adding order correct) --- README.md | 2 +- examples/readme_test.jl | 27 --------------------------- src/decision_model.jl | 32 ++------------------------------ src/influence_diagram.jl | 4 ++++ 4 files changed, 7 insertions(+), 58 deletions(-) delete mode 100644 examples/readme_test.jl diff --git a/README.md b/README.md index 495ecc25..599456a5 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,9 @@ using DecisionProgramming diagram = InfluenceDiagram() add_node!(diagram, DecisionNode("A", [], ["a", "b"])) -add_node!(diagram, DecisionNode("D", ["B", "C"], ["k", "l"])) add_node!(diagram, ChanceNode("B", ["A"], ["x", "y"])) add_node!(diagram, ChanceNode("C", ["A"], ["v", "w"])) +add_node!(diagram, DecisionNode("D", ["B", "C"], ["k", "l"])) add_node!(diagram, ValueNode("V", ["D"])) generate_arcs!(diagram) diff --git a/examples/readme_test.jl b/examples/readme_test.jl deleted file mode 100644 index fb785173..00000000 --- a/examples/readme_test.jl +++ /dev/null @@ -1,27 +0,0 @@ -using DecisionProgramming - -diagram = InfluenceDiagram() - -add_node!(diagram, DecisionNode("A", [], ["a", "b"])) -add_node!(diagram, DecisionNode("D", ["B", "C"], ["k", "l"])) -add_node!(diagram, ChanceNode("B", ["A"], ["x", "y"])) -add_node!(diagram, ChanceNode("C", ["A"], ["v", "w"])) -add_node!(diagram, ValueNode("V", ["D"])) - -generate_arcs!(diagram) - -add_probabilities!(diagram, "B", [0.4 0.6; 0.6 0.4]) -add_probabilities!(diagram, "C", [0.7 0.3; 0.3 0.7]) -add_utilities!(diagram, "V", [1.5, 1.7]) - -using JuMP -model, z, variables = generate_model(diagram, model_type="RJT") - -using HiGHS -optimizer = optimizer_with_attributes( - () -> HiGHS.Optimizer() -) -set_optimizer(model, optimizer) -optimize!(model) - -Z = DecisionStrategy(diagram, z) \ No newline at end of file diff --git a/src/decision_model.jl b/src/decision_model.jl index 2a7935ff..1e5e67ec 100644 --- a/src/decision_model.jl +++ b/src/decision_model.jl @@ -434,43 +434,15 @@ end function factorization_constraints(model::Model, diagram::InfluenceDiagram, name::Name, μ_statevars::Array{VariableRef}, μ_bar_statevars::Array{VariableRef}, z::OrderedDict{Name, DecisionVariable}) - #I_j_mapping_in_cluster = [findfirst(isequal(node), diagram.RJT.clusters[name]) for node in diagram.I_j[name]] # Map the information set to the variables in the cluster - I_j_mapping_in_cluster = [findfirst(isequal(node), diagram.RJT.clusters[name]) for node in diagram.I_j[name]] - I_j_states_mapping_in_cluster = similar(I_j_mapping_in_cluster) - for node_index in 1:length(I_j_states_mapping_in_cluster) - I_j_states_mapping_in_cluster[node_index] = diagram.S[diagram.RJT.clusters[name][node_index]] - end - #println("kokeilu:") - #println(I_j_mapping_in_cluster) - #println(I_j_states_mapping_in_cluster) - #println("") + I_j_mapping_in_cluster = [findfirst(isequal(node), diagram.RJT.clusters[name]) for node in diagram.I_j[name]] # Map the information set to the variables in the cluster for index in CartesianIndices(μ_bar_statevars) for s_j in 1:length(diagram.States[name]) - #println(s_j) - #println(Tuple(index)[I_j_states_mapping_in_cluster]...,s_j) - #println(diagram.X[name]) - #println(typeof(diagram.X[name])) if isa(diagram.Nodes[name], ChanceNode) # μ_{C_v} = μ_{\bar{C}_v}*p @constraint(model, μ_statevars[Tuple(index)...,s_j] == diagram.X[name][Tuple(index)[I_j_mapping_in_cluster]...,s_j]*μ_bar_statevars[index]) elseif isa(diagram.Nodes[name], DecisionNode) # μ_{C_v} ≤ z - - #println(index) - #println(typeof(index)) - #println(I_j_states_mapping_in_cluster) - #println(s_j) - #println(z[name]) - #println(z[name].z) - #if I_j_mapping_in_cluster ==[2, 3] - # println(z[name].z[1, 1, 1]) - #end - #println([I_j_states_mapping_in_cluster]...,s_j) - #println(Tuple(index)[I_j_states_mapping_in_cluster]...,s_j) - #println(z[name].z[Tuple(index)[I_j_states_mapping_in_cluster]...,s_j]) - #println("") - - @constraint(model, μ_statevars[Tuple(index)...,s_j] <= z[name].z[Tuple(index)[I_j_states_mapping_in_cluster]...,s_j]) + @constraint(model, μ_statevars[Tuple(index)...,s_j] <= z[name].z[Tuple(index)[I_j_mapping_in_cluster]...,s_j]) end end end diff --git a/src/influence_diagram.jl b/src/influence_diagram.jl index b97026e3..331f8eec 100644 --- a/src/influence_diagram.jl +++ b/src/influence_diagram.jl @@ -573,6 +573,10 @@ function validate_node(diagram::InfluenceDiagram, @warn("Value node $name is redundant.") end end + + if !all([haskey(diagram.Nodes, name) for name in I_j]) + throw(DomainError("The nodes in the information set of node $name should be added before $name.")) +end end """