1// coffee example
2// a robot uses battery, computational and physical resources to obtain and deliver coffee to a single user
3// the robot can move from an office location to a shop
4// it may possibly be raining outside between the two, and the robot can get wet
5// the robot can get an umbrella that helps it not get wet when outside
6// the robot's job is to get the user coffee when she doesn't have any
7// and to avoid getting wet
8//
9( variables
10  ( huc yes no )    // huc: user has coffee
11  ( w drenched sprinkled dry ) // w  : robot is wet
12  ( hrc yes no )    // hrc: robot has coffee
13  ( r heavy light no )  // r  : raining outside?
14  ( u yes no )    // u  : robot has umbrella
15  ( l office shop )   // l  : robot's location
16  ( batt dead low high )  // batt: battery state
17)
18//
19// some CPTS may be unnormalized -
20// this means all CPTs will be automatically normalized when read in
21unnormalised
22//
23// some pre-defined dds
24// robot keeps coffee if it has it
25dd samehrc
26( hrc ( yes ( hrc'	(yes	(1.00)) (no	(0.0))))
27              (no	(hrc' ( yes ( 0.00 ) ) ( no ( 1.0 ) ) ) ) )
28enddd
29// user keeps coffee if it has it
30// example of an unnormalized CPT
31//dd defaulthuc
32//  (huc  (yes    (huc' (yes  (9.0)) (no  (1.0))))
33//    (no (huc' (yes  (0.00)) (no (10.0)))))
34//enddd
35dd defaulthuc
36( huc ( yes ( huc'	(yes	(0.9)) (no	(0.1))))
37              (no	(huc' ( yes ( 0.00 ) ) ( no ( 1.0 ) ) ) ) )
38enddd
39// robot keeps umbrella if it has it
40dd sameu
41( u ( yes ( u'	(yes	(1.00))  (no	(0.0))))
42            (no	(u' ( yes ( 0.00 ) ) ( no ( 1.0 ) ) ) ) )
43enddd
44// robot's state of wetness stays the same
45dd samew
46( w ( drenched ( w'	(drenched	(1.0))	(sprinkled	(0.0))	(dry	(0.0))))
47                 (sprinkled	(w' ( drenched ( 0.0 ) ) ( sprinkled ( 1.0 ) ) ( dry ( 0.0 ) ) ) )
48  ( dry ( w'	(drenched	(0.0))	(sprinkled	(0.0))	(dry	(1.0)))))
49          enddd
50          // robot's location stays the same
51          dd samel
52          ( l ( office ( l'	(office	(1.00))  (shop	(0.0))))
53                         (shop	(l' ( office ( 0.00 ) ) ( shop ( 1.0 ) ) ) ) )
54          enddd
55// location changes if robot moves
56          dd moveloc
57          ( l ( office ( l'	(office	(0.10)) (shop	(0.90))))
58                         (shop	(l' ( office ( 0.90 ) ) ( shop ( 0.10 ) ) ) ) )
59          enddd
60// state of rain can progress from no <-> light <-> heavy
61          dd defaultrain
62          ( r ( heavy ( r'	(heavy	(0.9)) (light	(0.1)) (no	(0.0))))
63                        (light	(r' ( heavy ( 0.1 ) ) ( light ( 0.8 ) ) ( no ( 0.1 ) ) ) )
64            ( no ( r'	(heavy	(0.0)) (light	(0.1)) (no	(0.9)))))
65                   enddd
66                   // battery slowly discharges
67                   dd defaultbatt
68                   (batt	(dead	(batt' ( dead ( 1.0 ) ) ( low ( 0.0 ) ) ( high ( 0.0 ) ) ) )
69            ( low ( batt'	(dead	(0.2))	(low	(0.8))	(high	(0.0))))
70                    (high	(batt' ( dead ( 0.0 ) ) ( low ( 0.1 ) ) ( high ( 0.9 ) ) ) ) )
71          enddd
72// do nothing - exogenous events may affect huc and r
73// and the battery will discharge as usual
74          action nothing
75          huc ( defaulthuc )
76          hrc ( samehrc )
77          w ( samew )
78          r ( defaultrain )
79          u ( sameu )
80          l ( samel )
81          batt ( defaultbatt )
82          endaction
83// move to shop if in office and vice-versa
84// robot can get wet along the way if its raining and robot has no umbrella
85// this works less well if battery is low
86// doesn't work at all if battery is dead
87// its uphill from office->shop so the cost is a little more (wear on motors)
88          action move
89          huc ( defaulthuc )
90          hrc ( samehrc )
91          w ( w ( drenched ( 1.00 0.0 0.0 ) )
92              ( sprinkled ( r ( heavy ( u ( yes ( 0.10 0.9 0.0 ) )
93                                        ( no ( 0.90 0.1  0.0 ) ) ) )
94                            ( light ( u ( yes ( 0.05 0.95 0.0 ) )
95                                      ( no ( 0.70 0.3 0.0 ) ) ) )
96                            ( no ( 0.0 0.9 0.1 ) ) ) )
97              ( dry ( r ( heavy ( u ( yes ( 0.1 0.1 0.8 ) )
98                                  ( no ( 0.9 0.1 0.0 ) ) ) )
99                      ( light ( u ( yes ( 0.05 0.05 0.9 ) )
100                                ( no ( 0.2 0.7 0.1 ) ) ) )
101                      ( no ( 0.0 0.1 0.9 ) ) ) ) )
102          r ( defaultrain )
103          u ( sameu )
104          l [*  [+ ( moveloc ) ( batt ( dead ( 0.0 ) ) ( low ( 1.0 ) ) ( high ( 0.0 ) ) )]
105             ( batt ( dead ( samel ) ) ( low ( 1.0 ) ) ( high ( 1.0 ) ) )]
106//  l (batt (dead (samel))  (low  (moveloc))  (high (moveloc)))
107          batt ( defaultbatt )
108          cost ( l ( office ( 0.3 ) )
109                 ( shop ( 0.1 ) ) )
110          endaction
111// robot delivers coffee to user
112          action delc
113          huc ( huc ( yes ( 0.90 0.10 ) )
114                ( no ( hrc ( yes ( l ( office ( 0.80 0.20 ) )
115                                   ( shop ( 0.00 1.00 ) ) ) )
116                       ( no ( 0.00 1.00 ) ) ) ) )
117          hrc ( hrc ( yes ( l ( office ( 0.10 0.90 ) )
118                            ( shop ( 0.20 0.80 ) ) ) )
119                ( no ( 0.00 1.00 ) ) )
120          w ( samew )
121          r ( defaultrain )
122          u ( sameu )
123          l ( samel )
124          batt ( defaultbatt )
125          endaction
126// robot gets the umbrella
127          action getu
128          huc ( defaulthuc )
129          hrc ( samehrc )
130          w ( samew )
131          r ( defaultrain )
132          u ( u ( yes ( 1.00 0.00 ) )
133              ( no ( l ( office ( 0.90 0.10 ) )
134                     ( shop ( 0.00 1.00 ) ) ) ) )
135          batt ( defaultbatt )
136          l ( samel )
137          cost ( l ( office ( 0.2 ) )
138                 ( shop ( 0.0 ) ) )
139          endaction
140// robot buys coffee carefully
141// this only works if the robot's in the shop
142// the robot can get wet here because of coffee spillage on receiving coffee
143// this costs more in computation power (more complex motions?)
144// but the probability of getting wet is smaller than in buyc_fast
145          action buyc_careful
146          huc ( defaulthuc )
147          hrc ( hrc ( yes ( 1.00 0.00 ) )
148                ( no ( l ( office ( 0.00 1.00 ) )
149                       ( shop ( 1.0 0.00 ) ) ) ) )
150          w ( hrc ( no ( hrc'	(yes	(w	(drenched  (1.00 0.0 0.00))
151                         (sprinkled (0.0 1.0 0.0))
152                         (dry	   (0.0 0.1 0.9))))
153                         (no	(samew))))
154                         (yes	(samew)))
155                         r	(defaultrain)
156                         u	(sameu)
157                         batt	(defaultbatt)
158                         l	(samel)
159                         cost 	(l	(office	(0.0))
160                         (shop	(0.5)))
161                         endaction
162                         // robot buys coffee quickly
163                         // again, this only works if the robot's in the shop
164// the robot can get wet here because of coffee spillage on receiving coffee
165// but the probability is higher here than in buyc_careful
166                         action buyc_fast
167                         huc ( defaulthuc )
168                         hrc ( hrc ( yes ( 1.00 0.00 ) )
169                               ( no ( l ( office ( 0.00 1.00 ) )
170                                      ( shop ( 0.90 0.10 ) ) ) ) )
171                         w ( hrc ( no ( hrc'	(yes	(w	(drenched  (1.00 0.0 0.00))
172                                        (sprinkled (0.0 1.0 0.0))
173                                        (dry	   (0.0 0.3 0.7))))
174                                        (no	(samew))))
175                                        (yes	(samew)))
176                                        r	(defaultrain)
177                                        u	(sameu)
178                                        l	(samel)
179                                        batt	(defaultbatt)
180                                        cost 	(l	(office	(0.0))
181                                        (shop	(0.1)))
182                                        endaction
183                                        // recharge the battery
184                                        action recharge
185                                        huc	(defaulthuc)
186                                        hrc	(samehrc)
187                                        w	(samew)
188                                        r	(defaultrain)
189                                        u	(sameu)
190                                        l	(samel)
191                                        batt	(l	(office		(batt' ( dead ( 0.0 ) ) ( low ( 0.0 ) ) ( high ( 1.0 ) ) ) )
192                             ( shop ( defaultbatt ) ) )
193                         endaction
194                         action dryoff
195                         huc ( defaulthuc )
196                         hrc ( samehrc )
197                         w ( w ( drenched ( 0.1  0.2 0.7 ) )
198                             ( sprinkled ( 0.0  0.1 0.9 ) )
199                             ( dry ( 0.0  0.0 1.0 ) ) )
200                         r ( defaultrain )
201                         u ( sameu )
202                         l ( samel )
203                         batt ( defaultbatt )
204                         endaction
205
206                         reward [+ ( huc ( yes ( 1.0 ) )
207                                     ( no ( 0.0 ) ) )
208                                 ( w ( drenched ( -0.5 ) )
209                                   ( sprinkled ( -0.1 ) )
210                                   ( dry ( 0.0 ) ) )]
211                         discount 0.900000
212                         tolerance 0.1
213