-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlui.py
87 lines (67 loc) · 3.22 KB
/
lui.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
import stormpy
import stormpy.examples.files
from util import *
def init_strengths(model, lower, upper):
return {
(state.id, action.id, transition.column): (lower, upper)
for state in model.states
for action in state.actions
for transition in action.transitions
}
def lui_init(model, epsilon = 0.2 , lower=0, upper=10):
matrix = create_uMdp_matrix(model, epsilon)
strengths = init_strengths(model, lower, upper)
return update_interval_from_regular_mdp(model, matrix), strengths
def lui_step(model, measurement, strengths):
matrix, strengths = lui_create_matrix(model, measurement, strengths)
return update_interval_mdp(model, matrix), strengths
def lui_create_matrix(model, measurement, strengths):
builder = stormpy.IntervalSparseMatrixBuilder(
rows=0,
columns=0,
entries=0,
force_dimensions=False,
has_custom_row_grouping=True,
row_groups=0
)
nr_actions = 0
for state in model.states:
builder.new_row_group(nr_actions)
for action in state.actions:
if measurement.has_estimate(state.id, action.id):
lower_agreement = all(
measurement.get_probability(state.id, action.id, next.column) >= next.value().lower()
for next in action.transitions
)
upper_agreement = all(
measurement.get_probability(state.id, action.id, next.column) <= next.value().upper()
for next in action.transitions
)
for transition in action.transitions:
strength_lower, strength_upper = strengths[(state.id, action.id, transition.column)]
k = measurement.get_frequency(state.id, action.id, transition.column)
k_total = measurement.get_total_frequency(state.id, action.id)
lower = transition.value().lower()
if lower_agreement:
next_lower = ( strength_upper * lower + k ) / ( strength_upper + k_total )
else:
next_lower = ( strength_lower * lower + k ) / ( strength_lower + k_total )
upper = transition.value().upper()
if upper_agreement:
next_upper = ( strength_upper * upper + k ) / ( strength_upper + k_total )
else:
next_upper = ( strength_lower * upper + k ) / ( strength_lower + k_total )
strengths[(state.id, action.id, transition.column)] = (
strength_lower + k_total,
strength_upper + k_total
)
interval = pycarl.Interval(next_lower, next_upper)
act = action.id + nr_actions
builder.add_next_value(act, transition.column, interval)
else:
for transition in action.transitions:
act = action.id + nr_actions
builder.add_next_value(act, transition.column, transition.value())
nr_actions = nr_actions + len(state.actions)
matrix = builder.build()
return matrix, strengths