Skip to content

Commit

Permalink
readme example working, but cpp not
Browse files Browse the repository at this point in the history
  • Loading branch information
esahon committed Aug 30, 2024
1 parent 499f4aa commit 3928054
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
27 changes: 27 additions & 0 deletions examples/readme_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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)
32 changes: 30 additions & 2 deletions src/decision_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,43 @@ 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]] # 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("")
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
@constraint(model, μ_statevars[Tuple(index)...,s_j] <= z[name].z[Tuple(index)[I_j_mapping_in_cluster]...,s_j])

#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])
end
end
end
Expand Down

0 comments on commit 3928054

Please sign in to comment.