-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
210 lines (148 loc) · 6.93 KB
/
main.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import json
import stormpy
import stormpy.examples.files
import pycarl
import random
from util import *
from map import *
from lui import *
from frequentist import *
from pac_learning import *
from ucrl2 import *
from simulation import *
from value_iteration import *
def main_pac(init_model, paths_per_run, formula, rewards, gamma, max_iter=1000):
if len(paths_per_run) < 1:
raise Exception("empty paths_per_run")
data = []
# initial data without scheduler
model = pac_init(init_model)
policy = None
paths_so_far = 0
measurement = Measurement()
# with scheduler
for nr_paths in paths_per_run:
measurement = simulate(init_model, measurement=measurement, num_paths = nr_paths, policy=policy)
paths_so_far += nr_paths
model = pac_step(model, measurement)
policy, _ = interval_value_iter(model, rewards, gamma=gamma, max_iter=max_iter, optimistic=True)
robust_policy, _ = interval_value_iter(model, rewards, gamma=gamma, max_iter=max_iter)
model_dtmc = apply_policy(init_model, robust_policy)
result = stormpy.model_checking(model_dtmc, formula)
initial_state = model.initial_states[0]
value = result.at(initial_state)
data.append((paths_so_far, value))
return data
def main_frequentist(init_model, paths_per_run, formula, rewards, gamma, max_iter=1000):
if len(paths_per_run) < 1:
raise Exception("empty paths_per_run")
data = []
policy = None
paths_so_far = 0
measurement = Measurement()
# with scheduler
for nr_paths in paths_per_run:
measurement = simulate(init_model, measurement=measurement, num_paths = nr_paths, policy=policy)
paths_so_far += nr_paths
model = frequentist(model=init_model, measurement=measurement)
robust_policy, _ = value_iter(model, rewards, gamma=gamma, max_iter=max_iter)
model_dtmc = apply_policy(init_model, robust_policy)
result = stormpy.model_checking(model_dtmc, formula)
initial_state = model.initial_states[0]
value = result.at(initial_state)
data.append((paths_so_far, value))
return data
def main_map(init_model, paths_per_run, formula, rewards, gamma, max_iter=1000):
if len(paths_per_run) < 1:
raise Exception("empty paths_per_run")
data = []
policy = None
paths_so_far = 0
measurement = Measurement()
prior = init_uniform_prior(init_model, 10)
# with scheduler
for nr_paths in paths_per_run:
measurement = simulate(init_model, measurement=measurement, num_paths = nr_paths, policy=policy)
paths_so_far += nr_paths
model = map(model=init_model, measurement=measurement, prior=prior)
robust_policy, _ = value_iter(model, rewards, gamma=gamma, max_iter=max_iter)
model_dtmc = apply_policy(init_model, robust_policy)
result = stormpy.model_checking(model_dtmc, formula)
initial_state = model.initial_states[0]
value = result.at(initial_state)
data.append((paths_so_far, value))
return data
def main_lui(init_model, paths_per_run, formula, rewards, gamma, max_iter=1000):
if len(paths_per_run) < 1:
raise Exception("empty paths_per_run")
data = []
# initial data without scheduler
model, strengths = lui_init(init_model)
policy = None
paths_so_far = 0
# with scheduler
for nr_paths in paths_per_run:
measurement = simulate(init_model, num_paths = nr_paths, policy=policy)
paths_so_far += nr_paths
model, strengths = lui_step(model, measurement, strengths)
policy, _ = interval_value_iter(model, rewards, gamma=gamma, max_iter=max_iter, optimistic=True)
robust_policy, _ = interval_value_iter(model, rewards, gamma=gamma, max_iter=max_iter)
model_dtmc = apply_policy(init_model, robust_policy)
result = stormpy.model_checking(model_dtmc, formula)
initial_state = model.initial_states[0]
value = result.at(initial_state)
data.append((paths_so_far, value))
return data
def main_ucrl2(init_model, loops, formula, gamma, rewards):
_, data = ucrl2(init_model, formula, loops=loops, delta=0.1, gamma=gamma, error_bound=0.01, rewards=rewards)
return data
if __name__ == "__main__":
random.seed(10)
paths_per_run = list(10 * (2**i) for i in range(10))
gamma = 0.01
program = stormpy.parse_prism_program('models/bet_fav.prism')
prop = "R=? [F \"done\"]"
properties = stormpy.parse_properties(prop, program, None)
formula=properties[0]
model = stormpy.build_model(program, properties)
rewards = state_action_rewards_from_model(model)
df1 = {
"map": main_map(model, paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"frequentist": main_frequentist(model, paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"lui": main_lui(model, paths_per_run=paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"pac": main_pac(model, paths_per_run=paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"ucrl2": main_ucrl2(model, loops=15, formula=formula, gamma=gamma, rewards=rewards)
}
program = stormpy.parse_prism_program('models/bet_unfav.prism')
prop = "R=? [F \"done\"]"
properties = stormpy.parse_properties(prop, program, None)
formula=properties[0]
model = stormpy.build_model(program, properties)
rewards = state_action_rewards_from_model(model)
df2 = {
"map": main_map(model, paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"frequentist": main_frequentist(model, paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"lui": main_lui(model, paths_per_run=paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"pac": main_pac(model, paths_per_run=paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"ucrl2": main_ucrl2(model, loops=15, formula=formula, gamma=gamma, rewards=rewards)
}
program = stormpy.parse_prism_program('models/bandit.prism')
prop = "R=? [F \"goal\"]"
properties = stormpy.parse_properties(prop, program, None)
formula=properties[0]
model = stormpy.build_model(program, properties)
rewards = state_action_rewards_from_model(model)
df3 = {
"map": main_map(model, paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"frequentist": main_frequentist(model, paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"lui": main_lui(model, paths_per_run=paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"pac": main_pac(model, paths_per_run=paths_per_run, formula=formula, gamma = gamma, rewards=rewards),
"ucrl2": main_ucrl2(model, loops=90, formula=formula, gamma=gamma, rewards=rewards)
}
df = {
'bet_fav': df1,
'bet_unfav': df2,
'bandit': df3
}
with open('data.json', 'w') as f:
json.dump(df, f)