-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDP_chance_constraints_2.jl
102 lines (76 loc) · 2.9 KB
/
DP_chance_constraints_2.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Logging
using JuMP, Gurobi
using DecisionProgramming
N = 4
@info("Creating the influence diagram.")
diagram = InfluenceDiagram()
add_node!(diagram, ChanceNode("H1", [], ["ill", "healthy"]))
for i in 1:N-1
# Testing result
add_node!(diagram, ChanceNode("T$i", ["H$i"], ["positive", "negative"]))
# Decision to treat
add_node!(diagram, DecisionNode("D$i", ["T$i"], ["treat", "pass"]))
# Cost of treatment
add_node!(diagram, ValueNode("C$i", ["D$i"]))
# Health of next period
add_node!(diagram, ChanceNode("H$(i+1)", ["H$(i)", "D$(i)"], ["ill", "healthy"]))
end
add_node!(diagram, ValueNode("MP", ["H$N"]))
generate_arcs!(diagram)
# Add probabilities for node H1
add_probabilities!(diagram, "H1", [0.1, 0.9])
# Declare proability matrix for health nodes H_2, ... H_N-1, which have identical information sets and states
X_H = ProbabilityMatrix(diagram, "H2")
X_H["healthy", "pass", :] = [0.2, 0.8]
X_H["healthy", "treat", :] = [0.1, 0.9]
X_H["ill", "pass", :] = [0.9, 0.1]
X_H["ill", "treat", :] = [0.5, 0.5]
# Declare proability matrix for test result nodes T_1...T_N
X_T = ProbabilityMatrix(diagram, "T1")
X_T["ill", "positive"] = 0.8
X_T["ill", "negative"] = 0.2
X_T["healthy", "negative"] = 0.9
X_T["healthy", "positive"] = 0.1
for i in 1:N-1
add_probabilities!(diagram, "T$i", X_T)
add_probabilities!(diagram, "H$(i+1)", X_H)
end
for i in 1:N-1
add_utilities!(diagram, "C$i", [-100.0, 0.0])
end
add_utilities!(diagram, "MP", [300.0, 1000.0])
generate_diagram!(diagram, positive_path_utility = true)
@info("Creating the decision model.")
model = Model()
z = DecisionVariables(model, diagram)
x_s = PathCompatibilityVariables(model, diagram, z, probability_cut = false)
less_than = filter(x -> diagram.U(x[1]) < 300 ,x_s)
@constraint(model, sum(x for (s,x) in less_than) <= 0.1)
# This is extra. Discuss with Olli
@constraint(model,sum(x for (s,x) in x_s) == 1)
EV = expected_value(model, diagram, x_s)
@objective(model, Max, EV)
@info("Starting the optimization process.")
optimizer = optimizer_with_attributes(
() -> Gurobi.Optimizer(Gurobi.Env()),
"IntFeasTol" => 1e-9,
)
set_optimizer(model, optimizer)
optimize!(model)
@info("Extracting results.")
Z = DecisionStrategy(z)
S_probabilities = StateProbabilities(diagram, Z)
U_distribution = UtilityDistribution(diagram, Z)
@info("Printing decision strategy:")
print_decision_strategy(diagram, Z, S_probabilities)
@info("Printing utility distribution.")
print_utility_distribution(U_distribution)
@info("Printing statistics")
print_statistics(U_distribution)
@info("State probabilities:")
print_state_probabilities(diagram, S_probabilities, [["H$i" for i in 1:N]...])
print_state_probabilities(diagram, S_probabilities, [["T$i" for i in 1:N-1]...])
print_state_probabilities(diagram, S_probabilities, [["D$i" for i in 1:N-1]...])
prob = sum(value.(x) for (s,x) in less_than)
println(prob)
println(sum(value.(x) for (s,x) in x_s))