1
1
from stormvogel import pgc
2
2
import stormvogel .model
3
3
import math
4
- import stormpy
5
4
import stormvogel .mapping
6
5
7
6
8
7
def test_pgc_mdp ():
9
8
# we build the model with pgc:
10
9
N = 2
11
10
p = 0.5
12
- initial_state = pgc .State ({ "x" : math .floor (N / 2 )} )
11
+ initial_state = pgc .State (x = math .floor (N / 2 ))
13
12
14
13
left = pgc .Action (["left" ])
15
14
right = pgc .Action (["right" ])
@@ -19,23 +18,21 @@ def available_actions(s: pgc.State):
19
18
20
19
def delta (s : pgc .State , action : pgc .Action ):
21
20
if action == left :
22
- print (s .f )
23
- print (s .f ["x" ])
24
21
return (
25
22
[
26
- (p , pgc .State ({ "x" : s . f [ "x" ] + 1 } )),
27
- (1 - p , pgc .State ({ "x" : s . f [ "x" ]} )),
23
+ (p , pgc .State (x = s . x + 1 )),
24
+ (1 - p , pgc .State (x = s . x )),
28
25
]
29
- if s .f [ "x" ] < N
26
+ if s .x < N
30
27
else []
31
28
)
32
29
elif action == right :
33
30
return (
34
31
[
35
- (p , pgc .State ({ "x" : s . f [ "x" ] - 1 } )),
36
- (1 - p , pgc .State ({ "x" : s . f [ "x" ]} )),
32
+ (p , pgc .State (x = s . x - 1 )),
33
+ (1 - p , pgc .State (x = s . x )),
37
34
]
38
- if s .f [ "x" ] > 0
35
+ if s .x > 0
39
36
else []
40
37
)
41
38
@@ -63,41 +60,38 @@ def delta(s: pgc.State, action: pgc.Action):
63
60
model .add_transitions (state2 , stormvogel .model .Transition ({right : branch2 }))
64
61
model .add_transitions (state0 , stormvogel .model .Transition ({left : branch0 }))
65
62
66
- print (model )
67
- print (pgc_model )
68
-
69
63
assert model == pgc_model
70
64
71
65
72
66
def test_pgc_dtmc ():
73
67
# we build the model with pgc:
74
68
p = 0.5
75
- initial_state = pgc .State ({ "s" : 0 } )
69
+ initial_state = pgc .State (s = 0 )
76
70
77
71
def delta (s : pgc .State ):
78
- match s .f [ "s" ] :
72
+ match s .s :
79
73
case 0 :
80
- return [(p , pgc .State ({ "s" : 1 } )), (1 - p , pgc .State ({ "s" : 2 } ))]
74
+ return [(p , pgc .State (s = 1 )), (1 - p , pgc .State (s = 2 ))]
81
75
case 1 :
82
- return [(p , pgc .State ({ "s" : 3 } )), (1 - p , pgc .State ({ "s" : 4 } ))]
76
+ return [(p , pgc .State (s = 3 )), (1 - p , pgc .State (s = 4 ))]
83
77
case 2 :
84
- return [(p , pgc .State ({ "s" : 5 } )), (1 - p , pgc .State ({ "s" : 6 } ))]
78
+ return [(p , pgc .State (s = 5 )), (1 - p , pgc .State (s = 6 ))]
85
79
case 3 :
86
- return [(p , pgc .State ({ "s" : 1 } )), (1 - p , pgc .State ({ "s" : 7 , "d" : 1 } ))]
80
+ return [(p , pgc .State (s = 1 )), (1 - p , pgc .State (s = 7 , d = 1 ))]
87
81
case 4 :
88
82
return [
89
- (p , pgc .State ({ "s" : 7 , "d" : 2 } )),
90
- (1 - p , pgc .State ({ "s" : 7 , "d" : 3 } )),
83
+ (p , pgc .State (s = 7 , d = 2 )),
84
+ (1 - p , pgc .State (s = 7 , d = 3 )),
91
85
]
92
86
case 5 :
93
87
return [
94
- (p , pgc .State ({ "s" : 7 , "d" : 4 } )),
95
- (1 - p , pgc .State ({ "s" : 7 , "d" : 5 } )),
88
+ (p , pgc .State (s = 7 , d = 4 )),
89
+ (1 - p , pgc .State (s = 7 , d = 5 )),
96
90
]
97
91
case 6 :
98
- return [(p , pgc .State ({ "s" : 2 } )), (1 - p , pgc .State ({ "s" : 7 , "d" : 6 } ))]
92
+ return [(p , pgc .State (s = 2 )), (1 - p , pgc .State (s = 7 , d = 6 ))]
99
93
case 7 :
100
- return [(1 , pgc .State ({ "s" : 7 } ))]
94
+ return [(1 , pgc .State (s = 7 ))]
101
95
102
96
pgc_model = stormvogel .pgc .build_pgc (
103
97
delta = delta ,
@@ -106,12 +100,65 @@ def delta(s: pgc.State):
106
100
)
107
101
108
102
# we build the model in the regular way:
109
- path = stormpy .examples .files .prism_dtmc_die
110
- prism_program = stormpy .parse_prism_program (path )
111
- formula_str = "P=? [F s=7 & d=2]"
112
- properties = stormpy .parse_properties (formula_str , prism_program )
113
- model = stormpy .build_model (prism_program , properties )
114
- print (dir (model .states [0 ]))
115
- stormvogel_model = stormvogel .mapping .stormpy_to_stormvogel (model )
116
- print (pgc_model )
117
- print (stormvogel_model )
103
+ model = stormvogel .model .new_dtmc ()
104
+ model .states [0 ].features = {"s" : 0 }
105
+ model .set_transitions (
106
+ model .get_initial_state (),
107
+ [
108
+ (1 / 2 , model .new_state (features = {"s" : 1 })),
109
+ (1 / 2 , model .new_state (features = {"s" : 2 })),
110
+ ],
111
+ )
112
+ model .set_transitions (
113
+ model .get_state_by_id (1 ),
114
+ [
115
+ (1 / 2 , model .new_state (features = {"s" : 3 })),
116
+ (1 / 2 , model .new_state (features = {"s" : 4 })),
117
+ ],
118
+ )
119
+ model .set_transitions (
120
+ model .get_state_by_id (2 ),
121
+ [
122
+ (1 / 2 , model .new_state (features = {"s" : 5 })),
123
+ (1 / 2 , model .new_state (features = {"s" : 6 })),
124
+ ],
125
+ )
126
+ model .set_transitions (
127
+ model .get_state_by_id (3 ),
128
+ [
129
+ (1 / 2 , model .get_state_by_id (1 )),
130
+ (1 / 2 , model .new_state (features = {"s" : 7 , "d" : 1 })),
131
+ ],
132
+ )
133
+ model .set_transitions (
134
+ model .get_state_by_id (4 ),
135
+ [
136
+ (1 / 2 , model .new_state (features = {"s" : 7 , "d" : 2 })),
137
+ (1 / 2 , model .new_state (features = {"s" : 7 , "d" : 3 })),
138
+ ],
139
+ )
140
+ model .set_transitions (
141
+ model .get_state_by_id (5 ),
142
+ [
143
+ (1 / 2 , model .new_state (features = {"s" : 7 , "d" : 4 })),
144
+ (1 / 2 , model .new_state (features = {"s" : 7 , "d" : 5 })),
145
+ ],
146
+ )
147
+ model .set_transitions (
148
+ model .get_state_by_id (6 ),
149
+ [
150
+ (1 / 2 , model .get_state_by_id (2 )),
151
+ (1 / 2 , model .new_state (features = {"s" : 7 , "d" : 6 })),
152
+ ],
153
+ )
154
+ model .set_transitions (
155
+ model .get_state_by_id (7 ), [(1 , model .new_state (features = {"s" : 7 }))]
156
+ )
157
+ model .set_transitions (model .get_state_by_id (8 ), [(1 , model .get_state_by_id (13 ))])
158
+ model .set_transitions (model .get_state_by_id (9 ), [(1 , model .get_state_by_id (13 ))])
159
+ model .set_transitions (model .get_state_by_id (10 ), [(1 , model .get_state_by_id (13 ))])
160
+ model .set_transitions (model .get_state_by_id (11 ), [(1 , model .get_state_by_id (13 ))])
161
+ model .set_transitions (model .get_state_by_id (12 ), [(1 , model .get_state_by_id (13 ))])
162
+ model .set_transitions (model .get_state_by_id (13 ), [(1 , model .get_state_by_id (13 ))])
163
+
164
+ assert pgc_model == model
0 commit comments