-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_iteration.py
188 lines (141 loc) · 4.52 KB
/
value_iteration.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import stormpy
from util import *
class X:
def __init__(self, id, value, lower, upper):
self.id = id
self.value = value
self.lower = lower
self.upper = upper
def __lt__(self, other):
return self.value < other.value
def get_lower(self):
return self.lower
def get_upper(self):
return self.upper
def get_prob(self):
return self.prob
def is_lower(self):
self.prob = self.lower
def is_upper(self):
self.prob = self.upper
def set_prob(self, prob):
self.prob = prob
def min_distr(vs, action, optimistic):
vals = [
X(
id = transition.column,
value = vs[transition.column],
lower = transition.value().lower(),
upper = transition.value().upper()
)
for transition in action.transitions
]
return min_distribution(vals, optimistic)
def min_distribution(vs, optimistic):
i = 0
vs = list(sorted(vs, reverse=optimistic))
limit = sum(v.get_lower() for v in vs)
while i < len(vs) and (limit - vs[i].get_lower() + vs[i].get_upper() < 1):
limit = limit - vs[i].get_lower() + vs[i].get_upper()
vs[i].is_upper()
i += 1
if i < len(vs):
vs[i].set_prob(1 - (limit - vs[i].get_lower()))
for k in range(i + 1, len(vs)):
vs[k].is_lower()
return {v.id: v.get_prob() for v in vs}
def next(model, vs, rewards, gamma):
vals = { state.id :
[
(
action.id,
(
rewards[(state.id, action.id)]
+ gamma * sum(
transition.value() * vs[transition.column]
for transition in action.transitions
)
)
)
for action in state.actions
]
for state in model.states
}
maxes = {
k: max(v, key=lambda x: x[1])
for k, v in vals.items()
}
return (
{ k: v[0] for k, v in maxes.items() },
{ k: v[1] for k, v in maxes.items() }
)
def interval_next(model, vs, rewards, gamma, optimistic):
vals = { state.id :
[
(
action.id,
(
rewards[(state.id, action.id)]
+ gamma * sum(
min_distr(vs, action, optimistic)[transition.column] * vs[transition.column]
for transition in action.transitions
)
)
)
for action in state.actions
]
for state in model.states
}
maxes = {
k: max(v, key=lambda x: x[1])
for k, v in vals.items()
}
return (
{ k: v[0] for k, v in maxes.items() },
{ k: v[1] for k, v in maxes.items() }
)
def apply_policy(model, scheduler):
builder = stormpy.SparseMatrixBuilder(
rows=0,
columns=0,
entries=0,
force_dimensions=False,
has_custom_row_grouping=False,
row_groups=0
)
for state in model.states:
action = state.actions[scheduler[state.id]]
for transition in action.transitions:
builder.add_next_value(state.id, transition.column, transition.value())
matrix = builder.build()
return stormpy.SparseDtmc(
stormpy.SparseModelComponents(
transition_matrix = matrix,
state_labeling = model.labeling,
reward_models = storm_state_rewards_from_model_with_policy(model, scheduler)
)
)
def value_iter(model, rewards, gamma, max_iter, precision = 0.01):
vs = {state.id: 0 for state in model.states}
error = 1
iter = 0
while (error > precision):
if iter > max_iter:
raise Exception(f"could not converge within {max_iter} iterations")
args, vs_next = next(model, vs, rewards, gamma)
error = max(abs(vs_next[i] - vs[i]) for i in vs)
vs = vs_next
iter += 1
return (args, vs)
def interval_value_iter(model, rewards, gamma, max_iter, precision = 0.01, optimistic=False):
vs = {state.id: 0 for state in model.states}
error = 1
iter = 0
while (error > precision):
if iter > max_iter:
raise Exception(f"could not converge within {max_iter} iterations")
args, vs_next = interval_next(model, vs, rewards, gamma, optimistic)
error = max(abs(vs_next[i] - vs[i]) for i in vs)
vs = vs_next
iter += 1
return (args, vs)