Skip to content

Commit 8bfad27

Browse files
progress pomdp
1 parent c5d012a commit 8bfad27

File tree

6 files changed

+13408
-13340
lines changed

6 files changed

+13408
-13340
lines changed

docs/getting_started/grid.ipynb

Lines changed: 113 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"outputs": [],
1919
"source": [
2020
"import stormvogel.model\n",
21+
"from stormvogel.model import EmptyAction\n",
2122
"from stormvogel.layout import Layout\n",
2223
"from stormvogel.show import show"
2324
]
@@ -102,19 +103,19 @@
102103
" \n",
103104
" escaped = pomdp.new_state(\"escaped\")\n",
104105
" escaped.set_observation(END)\n",
105-
" reward_model.set(escaped, 0)\n",
106+
" #reward_model.set(escaped, 0)\n",
106107
" \n",
107108
" grid = [[None for x in range(width)] for y in range(height)]\n",
108109
" for x in range(width):\n",
109110
" for y in range(height):\n",
110111
" if level_list[y][x] == WALKABLE:\n",
111112
" grid[y][x] = pomdp.new_state([\"t\", f\"({x},{y})\"])\n",
112113
" grid[y][x].set_observation(UNKNOWN)\n",
113-
" reward_model.set(grid[y][x], -1)\n",
114+
" #reward_model.set_state_action_reward(grid[y][x], -1)\n",
114115
" if level_list[y][x] == EXIT:\n",
115116
" grid[y][x] = pomdp.new_state([\"e\", f\"({x},{y})\"])\n",
116117
" grid[y][x].set_observation(UNKNOWN)\n",
117-
" reward_model.set(grid[y][x], 100)\n",
118+
" #reward_model.set_state_action_reward(grid[y][x], 100)\n",
118119
" grid[y][x].add_transitions([(1, escaped)])\n",
119120
" dirs = {d: pomdp.new_action(d) for d in [UP, DOWN, LEFT, RIGHT]}\n",
120121
" positions = {}\n",
@@ -125,14 +126,19 @@
125126
" for d,action in dirs.items():\n",
126127
" positions[str(grid[y][x].id)] = {\"x\": x * position_scalar, \"y\": y * position_scalar}\n",
127128
" ((res_x, res_y), observation) = direction_result(x,y,d,level_list,width,height)\n",
129+
"\n",
130+
" \n",
131+
" \n",
128132
" if not observation == OUT_OF_BOUNDS:\n",
129133
" took_dir = pomdp.new_state([d, f\"({x},{y})\"])\n",
130-
" reward_model.set(took_dir, 0)\n",
131134
" grid[y][x].add_transitions([(action, took_dir)])\n",
135+
" reward_model.set_state_action_reward(grid[y][x], action, 0)\n",
132136
" # print(took_dir)\n",
133137
" took_dir.add_transitions([(1, grid[res_y][res_x])])\n",
138+
" reward_model.set_state_action_reward(took_dir, EmptyAction, -1)\n",
134139
" took_dir.set_observation(observation)\n",
135140
" pomdp.add_self_loops()\n",
141+
" reward_model.set_unset_rewards(0)\n",
136142
" return pomdp, positions"
137143
]
138144
},
@@ -187,16 +193,82 @@
187193
"metadata": {},
188194
"outputs": [
189195
{
190-
"ename": "TypeError",
191-
"evalue": "new_pomdp() got an unexpected keyword argument 'create_initial_state'",
192-
"output_type": "error",
193-
"traceback": [
194-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
195-
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
196-
"Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m pomdp, positions \u001b[38;5;241m=\u001b[39m \u001b[43mgrid_world\u001b[49m\u001b[43m(\u001b[49m\u001b[43mLEVEL\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3\u001b[0m vis \u001b[38;5;241m=\u001b[39m show(pomdp, layout\u001b[38;5;241m=\u001b[39mLayout(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlayouts/grid.json\u001b[39m\u001b[38;5;124m\"\u001b[39m), separate_labels\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mt\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124me\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n",
197-
"Cell \u001b[0;32mIn[2], line 69\u001b[0m, in \u001b[0;36mgrid_world\u001b[0;34m(level, position_scalar)\u001b[0m\n\u001b[1;32m 67\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Create a grid world with an actor.\"\"\"\u001b[39;00m\n\u001b[1;32m 68\u001b[0m level_list, width, height \u001b[38;5;241m=\u001b[39m parse_level(level)\n\u001b[0;32m---> 69\u001b[0m pomdp \u001b[38;5;241m=\u001b[39m \u001b[43mstormvogel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnew_pomdp\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcreate_initial_state\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 70\u001b[0m reward_model \u001b[38;5;241m=\u001b[39m pomdp\u001b[38;5;241m.\u001b[39madd_rewards(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 72\u001b[0m escaped \u001b[38;5;241m=\u001b[39m pomdp\u001b[38;5;241m.\u001b[39mnew_state(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mescaped\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
198-
"\u001b[0;31mTypeError\u001b[0m: new_pomdp() got an unexpected keyword argument 'create_initial_state'"
199-
]
196+
"data": {
197+
"application/vnd.jupyter.widget-view+json": {
198+
"model_id": "676e37cbe8a846d19550bccc6d5c4dd5",
199+
"version_major": 2,
200+
"version_minor": 0
201+
},
202+
"text/plain": [
203+
"Output()"
204+
]
205+
},
206+
"metadata": {},
207+
"output_type": "display_data"
208+
},
209+
{
210+
"data": {
211+
"text/html": [
212+
"<script>fetch('http://127.0.0.1:8889/vDdOnHCrvh/MESSAGE/' + 'test message')</script>"
213+
],
214+
"text/plain": [
215+
"<IPython.core.display.HTML object>"
216+
]
217+
},
218+
"metadata": {},
219+
"output_type": "display_data"
220+
},
221+
{
222+
"data": {
223+
"text/html": [
224+
"<script>fetch('http://127.0.0.1:8889/vDdOnHCrvh/MESSAGE/' + 'test message')</script>"
225+
],
226+
"text/plain": [
227+
"<IPython.core.display.HTML object>"
228+
]
229+
},
230+
"metadata": {},
231+
"output_type": "display_data"
232+
},
233+
{
234+
"data": {
235+
"text/html": [
236+
"<script>fetch('http://127.0.0.1:8889/vDdOnHCrvh/MESSAGE/' + 'test message')</script>"
237+
],
238+
"text/plain": [
239+
"<IPython.core.display.HTML object>"
240+
]
241+
},
242+
"metadata": {},
243+
"output_type": "display_data"
244+
},
245+
{
246+
"data": {
247+
"application/vnd.jupyter.widget-view+json": {
248+
"model_id": "b3f4a3d048254e1cb8573a25408495ac",
249+
"version_major": 2,
250+
"version_minor": 0
251+
},
252+
"text/plain": [
253+
"Output()"
254+
]
255+
},
256+
"metadata": {},
257+
"output_type": "display_data"
258+
},
259+
{
260+
"data": {
261+
"application/vnd.jupyter.widget-view+json": {
262+
"model_id": "848d2f62271c4a4488bcebe203d3a59e",
263+
"version_major": 2,
264+
"version_minor": 0
265+
},
266+
"text/plain": [
267+
"HBox(children=(Output(), Output()))"
268+
]
269+
},
270+
"metadata": {},
271+
"output_type": "display_data"
200272
}
201273
],
202274
"source": [
@@ -223,37 +295,48 @@
223295
},
224296
{
225297
"cell_type": "code",
226-
"execution_count": 35,
298+
"execution_count": 6,
227299
"id": "8d5b0e8e-47db-4dfb-a533-a8181ec04751",
228300
"metadata": {},
229301
"outputs": [
230302
{
231303
"name": "stdout",
232304
"output_type": "stream",
233305
"text": [
234-
"ERROR (Model.cpp:71): Invalid size (22) of state action reward vector (expected:34).\n"
235-
]
236-
},
237-
{
238-
"ename": "RuntimeError",
239-
"evalue": "IllegalArgumentException: Invalid size (22) of state action reward vector (expected:34).",
240-
"output_type": "error",
241-
"traceback": [
242-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
243-
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
244-
"Cell \u001b[0;32mIn[35], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mstormvogel\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmapping\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m stormvogel_to_stormpy\n\u001b[0;32m----> 4\u001b[0m stormpy_model \u001b[38;5;241m=\u001b[39m \u001b[43mstormvogel_to_stormpy\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpomdp\u001b[49m\u001b[43m)\u001b[49m\n",
245-
"File \u001b[0;32m~/git/env/lib/python3.12/site-packages/stormvogel/mapping.py:300\u001b[0m, in \u001b[0;36mstormvogel_to_stormpy\u001b[0;34m(model)\u001b[0m\n\u001b[1;32m 298\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m map_ctmc(model)\n\u001b[1;32m 299\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m model\u001b[38;5;241m.\u001b[39mget_type() \u001b[38;5;241m==\u001b[39m stormvogel\u001b[38;5;241m.\u001b[39mmodel\u001b[38;5;241m.\u001b[39mModelType\u001b[38;5;241m.\u001b[39mPOMDP:\n\u001b[0;32m--> 300\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mmap_pomdp\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 301\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m model\u001b[38;5;241m.\u001b[39mget_type() \u001b[38;5;241m==\u001b[39m stormvogel\u001b[38;5;241m.\u001b[39mmodel\u001b[38;5;241m.\u001b[39mModelType\u001b[38;5;241m.\u001b[39mMA:\n\u001b[1;32m 302\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m map_ma(model)\n",
246-
"File \u001b[0;32m~/git/env/lib/python3.12/site-packages/stormvogel/mapping.py:225\u001b[0m, in \u001b[0;36mstormvogel_to_stormpy.<locals>.map_pomdp\u001b[0;34m(model)\u001b[0m\n\u001b[1;32m 223\u001b[0m components\u001b[38;5;241m.\u001b[39mobservability_classes \u001b[38;5;241m=\u001b[39m observations\n\u001b[1;32m 224\u001b[0m components\u001b[38;5;241m.\u001b[39mchoice_labeling \u001b[38;5;241m=\u001b[39m choice_labeling\n\u001b[0;32m--> 225\u001b[0m pomdp \u001b[38;5;241m=\u001b[39m \u001b[43mstormpy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstorage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mSparsePomdp\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcomponents\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 227\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m pomdp\n",
247-
"\u001b[0;31mRuntimeError\u001b[0m: IllegalArgumentException: Invalid size (22) of state action reward vector (expected:34)."
306+
"-------------------------------------------------------------- \n",
307+
"Model type: \tPOMDP (sparse)\n",
308+
"States: \t22\n",
309+
"Transitions: \t34\n",
310+
"Choices: \t34\n",
311+
"Observations: \t5\n",
312+
"Reward Models: (default)\n",
313+
"State Labels: \t12 labels\n",
314+
" * (1,2) -> 1 item(s)\n",
315+
" * escaped -> 1 item(s)\n",
316+
" * ← -> 4 item(s)\n",
317+
" * (1,1) -> 5 item(s)\n",
318+
" * (2,1) -> 5 item(s)\n",
319+
" * t -> 4 item(s)\n",
320+
" * ↑ -> 4 item(s)\n",
321+
" * (3,2) -> 5 item(s)\n",
322+
" * → -> 4 item(s)\n",
323+
" * e -> 1 item(s)\n",
324+
" * ↓ -> 4 item(s)\n",
325+
" * (3,1) -> 5 item(s)\n",
326+
"Choice Labels: \t0 labels\n",
327+
"-------------------------------------------------------------- \n",
328+
"\n"
248329
]
249330
}
250331
],
251332
"source": [
252333
"from stormvogel.mapping import stormvogel_to_stormpy\n",
253334
"\n",
254335
"stormpy_model = stormvogel_to_stormpy(pomdp)\n",
336+
"print(stormpy_model)\n",
255337
"\n",
256-
"# TODO use stormpy to find the best policy/schedule, i.e. escape the maze as quickly as possible."
338+
"# TODO use stormpy to find the best policy/schedule, i.e. escape the maze as quickly as possible.\n",
339+
"# Ask Pim or Linus for help?"
257340
]
258341
},
259342
{

docs/getting_started/pinkgreen.json

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"__fake_macros": {
3+
"__group_macro": {
4+
"borderWidth": 1,
5+
"color": {
6+
"background": "white",
7+
"border": "black",
8+
"highlight": {
9+
"background": "white",
10+
"border": "red"
11+
}
12+
},
13+
"shape": "ellipse",
14+
"mass": 1,
15+
"font": {
16+
"color": "black",
17+
"size": 14
18+
}
19+
}
20+
},
21+
"groups": {
22+
"states": {
23+
"borderWidth": 1,
24+
"color": {
25+
"background": "#8ff0a4",
26+
"border": "black",
27+
"highlight": {
28+
"background": "white",
29+
"border": "red"
30+
}
31+
},
32+
"shape": "ellipse",
33+
"mass": 1,
34+
"font": {
35+
"color": "black",
36+
"size": 14
37+
}
38+
},
39+
"actions": {
40+
"borderWidth": 1,
41+
"color": {
42+
"background": "#ffc0cb",
43+
"border": "black",
44+
"highlight": {
45+
"background": "white",
46+
"border": "red"
47+
}
48+
},
49+
"shape": "box",
50+
"mass": 1,
51+
"font": {
52+
"color": "black",
53+
"size": 14
54+
}
55+
},
56+
"scheduled_actions": {
57+
"borderWidth": 1,
58+
"color": {
59+
"background": "pink",
60+
"border": "black",
61+
"highlight": {
62+
"background": "white",
63+
"border": "red"
64+
}
65+
},
66+
"shape": "box",
67+
"mass": 1,
68+
"font": {
69+
"color": "black",
70+
"size": 14
71+
},
72+
"schedColor": false
73+
}
74+
},
75+
"reload_button": false,
76+
"edges": {
77+
"arrows": "to",
78+
"font": {
79+
"color": "black",
80+
"size": 14
81+
},
82+
"color": {
83+
"color": "black"
84+
}
85+
},
86+
"numbers": {
87+
"fractions": true,
88+
"digits": 5
89+
},
90+
"state_properties": {
91+
"show_results": true,
92+
"result_symbol": "\u2606",
93+
"show_rewards": false,
94+
"reward_symbol": "\u20ac",
95+
"show_observations": true,
96+
"observation_symbol": "\u0298"
97+
},
98+
"layout": {
99+
"randomSeed": 5
100+
},
101+
"misc": {
102+
"enable_physics": true,
103+
"width": 800,
104+
"height": 600,
105+
"explore": false
106+
},
107+
"saving": {
108+
"relative_path": true,
109+
"filename": "pinkgreen.json",
110+
"save_button": false,
111+
"load_button": false
112+
},
113+
"positions": {
114+
"0": {
115+
"x": 12,
116+
"y": -136
117+
},
118+
"1": {
119+
"x": 96,
120+
"y": -33
121+
},
122+
"2": {
123+
"x": -90,
124+
"y": -53
125+
},
126+
"3": {
127+
"x": 13,
128+
"y": 72
129+
},
130+
"4": {
131+
"x": -19,
132+
"y": 26
133+
},
134+
"5": {
135+
"x": -47,
136+
"y": 161
137+
}
138+
},
139+
"width": 800,
140+
"height": 600,
141+
"physics": true,
142+
"results_and_rewards": {
143+
"show_results": true,
144+
"resultSymbol": "\u2606",
145+
"show_rewards": true
146+
}
147+
}

0 commit comments

Comments
 (0)