160 Source code
53 ( newnodes , newedges ) 54 e l s e
55 l e t n e w n o d e l i s t = 56 n o d e l i s t
57 |> L i s t . map ( Graph . NeighbourNodes g ) 58 |> L i s t . c o n c a t
59 |> L i s t . append n o d e l i s t 60 |> S e t . o f L i s t
61 |> S e t . t o L i s t
62 ExpandView g ( n−1) n e w n o d e l i s t 63
64 // S h a r e th e p e r c e p t i o n between a g e n ts , based on th e zone th e y 65 // a r e i n .
66 // i n t l i s t −> i n t −> Node l i s t ∗ E d g e In f o l i s t −> Agent 67 // −> Node l i s t ∗ E d g e In f o l i s t −> Node l i s t ∗ E d g e In f o l i s t 68 l e t i n t e r n a l S h a r e P e r c e p t z o n e l i s t team ( n o d e s e t , e d g e s e t ) a g e n t 69 ( nodes : Node l i s t , e d g e s : E d g e In f o l i s t ) =
70 l e t t , = a g e n t . ID
71 i f t = team && L i s t . e x i s t s (f u n x −> x = a g e n t . Node ) z o n e l i s t
72 then
73 l e t newnodeset =
74 nodes
75 |> S e t . o f L i s t
76 |> (+) ( n o d e s e t |> S e t . o f L i s t ) 77 |> S e t . t o L i s t
78 l e t n e w e d g e s e t =
79 e d g e s
80 |> S e t . o f L i s t
81 |> (+) ( e d g e s e t |> S e t . o f L i s t ) 82 |> S e t . t o L i s t
83 ( newnodeset , n e w e d g e s e t ) 84 e l s e ( n o d e s e t , e d g e s e t ) 85
86 // P r e p a r e / c r e a t e th e p e r c e p t i o n s f o r a l l a g e n t s .
87 // i n t −> Graph −> ( Agent ∗ Message l i s t ) −> TeamStatus l i s t 88 // −> P e r c e p t l i s t
89 l e t i n t e r n a l P r e p a r e P e r c e p t s stepnum ( g : Graph ) ml 90 ( s t a t u s : TeamStatus l i s t ) m i l e s t o n e s =
91 l e t g e t s c o r e ( t i d , ) = s t a t u s . [ t i d ] . S c o r e 92 l e t getmoney ( t i d , ) = s t a t u s . [ t i d ] . Money 93 l e t a l l a g e n t s =
94 Graph . G e tA l l A g e n ts g
95 |> L i s t . s o r tB y (f u n a −> a . ID )
96 // S o r t e d to g e t th e c o r r r e c t o r d e r o f a g e n t s i n th e GUI 97 l e t getmsgmap map ( agent , msgs ) =
98 l e t team , = a g e n t . ID
99 msgs
100 |> L i s t . map (f u n a −> agent , a )
101 |> MapAppend map team
102 l e t m e s s a g e s =
103 ml |> L i s t . f o l d getmsgmap Map . empty 104 l e t getmsg ( team , a g e n t ) =
105 match Map . t r y F i n d team m e s s a g e s with 106 | Some msgs −>
107 msgs
B.21 Simulation.fs 161
108 |> L i s t . c h o o s e 109 (f u n ( a , msg ) −>
110 l e t , s e n d e r = a . ID
111 match msg with
112 | B r o a d c a s t m−> Some ( s e n d e r ,m)
113 | S i n g l e ( r ,m) when r = a g e n t −> Some ( s e n d e r ,m)
114 | −> None )
115 | −> [ ]
116 l e t g e t a g e n t s ( team , a g e n t ) =
117 a l l a g e n t s |> L i s t . f i l t e r ( Agent . In s p e c te d B y ( team , a g e n t ) ) 118 l e t v i s ( a : Agent ) = Max 1 a . V i s i b i l i t y
119 l e t v i s i b i l i t i e s = 120 a l l a g e n t s
121 |> L i s t . map (f u n a −> ExpandView g ( v i s a ) [ a . Node ] )
122 l e t team a =
123 l e t ( t , ) = a . ID
124 t
125 l e t a c c e p t a g e n t ( node : Node ) =
126 node . DominatingTeam = Some ( team a g e n t ) 127 l e t f a i l = f a l s e
128 l e t z o n e s = 129 a l l a g e n t s 130 |> L i s t . map
131 (f u n a −> Graph . GetZone ( a c c e p t a ) f a i l g a . Node ) 132 |> L i s t . map
133 (f u n zone −>
134 match zone with
135 | Some z −> z
136 | −> [ ] )
137 l e t s h a r e d v i s =
138 L i s t . z i p 3 a l l a g e n t s v i s i b i l i t i e s z o n e s 139 |> L i s t . map
140 (f u n ( a , v , z ) −>
141 L i s t . f o l d 2 ( S h a r e P e r c e p t z ( team a ) ) v 142 a l l a g e n t s v i s i b i l i t i e s )
143 l e t makepercept ( a : Agent ) ( nodes , e d g e s ) =
144 { StepNum = stepnum
145 S c o r e = g e t s c o r e a . ID
146 Money = getmoney a . ID
147 Agent = a
148 Nodes = nodes |> L i s t . map ( Node . Anonymize a . ID ) 149 Edges = e d g e s |> L i s t . map ( Edge . Anonymize a . ID ) 150 OtherAgents = g e t a g e n t s a . ID
151 M i l e s t o n e s = m i l e s t o n e s
152 Messages = getmsg a . ID }
153 L i s t . map2 makepercept a l l a g e n t s s h a r e d v i s 154
155 // F a i l a c t i o n s by a c e r t a i n p e r c e n t a g e c h a n c e . 156 // S i m u l a t i o n S e t t i n g s −> ’ a l i s t −> ’ a l i s t 157 l e t i n t e r n a l F a i l A c t i o n s s e t t i n g s a c t i o n s = 158 l e t r = new Random ( DateTime . Now . T i c k s |> i n t )
159 l e t addrandom v =
160 ( r . Next ( 1 0 0 ) , v ) 161 a c t i o n s
162 |> L i s t . map addrandom
162 Source code
163 |> L i s t . f i l t e r (f u n ( a , ) −> a >= s e t t i n g s . F a i l C h a n c e ) 164 |> L i s t . map (f u n ( , a ) −> a )
165
166 // Send p e r c e p t i o n s to a g e n ts , and r e t r i e v e t h e i r wanted a c t i o n s 167 // and m e s s a g e s th e y wish to send .
168 // S i m u l a t i o n S e t t i n g s −> Map<i n t ∗ i n t , IAgent>−> P e r c e p t l i s t 169 // −> ( Agent ∗ A c ti o n ) l i s t ∗ ( Agent ∗ Message l i s t ) l i s t 170 l e t i n t e r n a l S e n d P e r c e p ts s e t t i n g s ( a g e n t s : Map< , IAgent>) 171 p e r c e p t s =
172 l e t a n s w e r s = 173 p e r c e p t s 174 |> L i s t . map
175 (f u n p −> ( p . Agent , a g e n t s . [ p . Agent . ID ] . MakeStep ( p ) ) )
176 l e t msgs =
177 a n s w e r s
178 |> L i s t . map (f u n ( agent , ( , msgs ) ) −> ( agent , msgs ) ) 179 l e t a c t i o n s =
180 a n s w e r s
181 |> L i s t . map (f u n ( a , ( b , ) ) −> ( a , b ) ) 182 |> F a i l A c t i o n s s e t t i n g s
183 ( a c t i o n s , msgs ) 184
185 // F i l t e r f o r d e t e r m i n i n g whether a g i v e n a c t i o n i s e i t h e r 186 // a t t a c k o r parry , o r n e i t h e r .
187 // ’ a ∗ A c ti o n −> b o o l
188 l e t i n t e r n a l A t t a c k P a r r y F i l t e r ( , a c t i o n ) = 189 match a c t i o n with
190 | Parry | Attack ( , ) −> t r u e 191 | −> f a l s e
192
193 // Determine whether a g i v e n a g e n t and th e i d o f a n o t h e r agent , 194 // a r e o p p o n e n ts .
195 // Agent −> i n t ∗ ’ a −> b o o l
196 l e t i n t e r n a l IsOpponent a ( t i d , ) = 197 l e t ( t i d 2 , ) = a . ID
198 t i d <> t i d 2 199
200 // Get a l i s t o f th e a g e n t s who has th e e n e r g y to p e r f o r m th e 201 // p a r r y a c t i o n , can do i t and i s n ’ t d i s a b l e d . The a g e n t s 202 // e n e r g y w i l l be r e d u e d b e f o r e r e t u r n i n g them .
203 // S i m u l a t i o n S e t t i n g s −> ( Agent ∗ A c ti o n ) l i s t −> Agent l i s t 204 l e t i n t e r n a l P a r r i e s s e t t i n g s a c t i o n s =
205 a c t i o n s
206 |> L i s t . f i l t e r 207 (f u n ( a , ac ) −>
208 a . Energy >= s e t t i n g s . ParryCost 209 && ac = Parry
210 && not ( Agent . I s D i s a b l e d a ) 211 && Agent . CanPerform a ac ) 212 |> L i s t . map (f u n ( a , ) −> a )
213 |> L i s t . map ( Agent . ReduceEnergy s e t t i n g s . ParryCost ) 214
215 // Execute a l l a t t a c k and p a r r y a c t i o n s . In r e a l i t y , o n l y a t t a c k 216 // a c t i o n s w i l l be e x e c u te d , d e p e n d i n g on whether o r not th e 217 // t a r g e t i s p a r r y i n g . A s u c c e s s f u l a t t a c k s o f c o u r s e a l s o
B.21 Simulation.fs 163
218 // depends on th e e n e r g y , h e a l t h and p o s i t i o n o f th e 219 // a t t a c k i n g agent , and whether o r not i t can p e r f o r m th e 220 // a t t a c k a c t i o n i n th e f i r s t p l a c e .
221 // Has s i d e e f f e c t s .
222 // S i m u l a t i o n S e t t i n g s −> TeamStatus l i s t
223 l e t i n t e r n a l E x e c u te A tta c k P a r r y s e t t i n g s s t a t u s 224 ( ( nodes , ) a s g : Graph ) a c t i o n s =
225 l e t p a r r i e s = P a r r i e s s e t t i n g s a c t i o n s
226 l e t E x e c u te A tta c k H e l p e r ( s t a t u s , a t t a c k e d ) ( agent , a c t i o n ) 227 ( t i d , a i d ) =
228 l e t i s p a r r y i n g = // I s t a r g e t p a r r y i n g ?
229 L i s t . e x i s t s (f u n a −> a . ID = ( t i d , a i d ) ) p a r r i e s 230 l e t t a r g e t = // Find t a r g e t a g e n t i n graph
231 nodes . [ a g e n t . Node ] . Agents
232 |> L i s t . t r y F i n d (f u n a −> a . ID = ( t i d , a i d ) ) 233 l e t n e w a t t a c k e r = // Reduce e n e r g y f o r a t t a c k e r 234 Agent . ReduceEnergy s e t t i n g s . AttackCost a g e n t 235 match t a r g e t , i s p a r r y i n g with
236 | Some t a r g e t a g e n t , f a l s e when
237 not ( Agent . I s D i s a b l e d t a r g e t a g e n t ) −>
238 // S u c c e s s f u l a t t a c k 239 l e t n e w h e a l th =
240 Max 0 ( t a r g e t a g e n t . H e a l th − a g e n t . S t r e n g t h ) 241 l e t newag = { t a r g e t a g e n t with H e a l th = n e w h e a l th }
242 Graph . ReplaceAgent g newag
243 Graph . ReplaceAgent g n e w a t t a c k e r
244 l e t teamid , = a g e n t . ID
245 l e t n e w s ta t = L i s t R e p l a c e teamid TS . I n c A t t a c k s s t a t u s 246 ( newstat , ( t i d , a i d ) : : a t t a c k e d )
247 | Some t a r g e t a g e n t , t r u e −> // S u c c e s s f u l p a r r y 248 Graph . ReplaceAgent g n e w a t t a c k e r
249 ( L i s t R e p l a c e t i d TS . I n c P a r r i e s s t a t u s , a t t a c k e d ) 250 | −> // Unable to f i n d t a r g e t a g e n t i n graph 251 ( s t a t u s , a t t a c k e d )
252 l e t E x e c u te A tta c k ( s t a t u s , a t t a c k e d ) ( agent , a c t i o n ) = 253 match a c t i o n with
254 | Attack ( t i d , a i d ) when
255 not ( Agent . I s D i s a b l e d a g e n t ) 256 && IsOpponent a g e n t ( t i d , a i d )
257 && a g e n t . Energy >= s e t t i n g s . AttackCost 258 && Agent . CanPerform a g e n t ( Attack ( 0 , 0 ) ) −>
259 E x e c u te A tta c k H e l p e r ( s t a t u s , a t t a c k e d ) ( agent , a c t i o n )
260 ( t i d , a i d )
261 | −> ( s t a t u s , a t t a c k e d ) // U n s u c c e s s f u l a t t a c k 262 // Reduce e n e r g y f o r a l l p a r r y i n g a g e n t s
263 L i s t . i t e r ( Graph . ReplaceAgent g ) p a r r i e s 264 L i s t . f o l d E x e c u te A tta c k ( s t a t u s , [ ] ) a c t i o n s 265
266 // Execute s i n g l e a c t i o n o t h e r than a t t a c k and p a r r y . 267 // Has s i d e e f f e c t s .
268 // S i m u l a t i o n S e t t i n g s −> Graph −> ( i n t ∗ i n t ) l i s t
269 // −> TeamStatus l i s t −> Agent ∗ A c ti o n −> TeamStatus l i s t 270 l e t i n t e r n a l E x e c u te O th e r A c ti o n s e t t i n g s
271 ( ( nodes , e d g e s ) a s g : Graph ) a t t a c k e d s t a t u s ( a , a c t i o n ) = 272 l e t a g e n t = Graph . GetUpdatedAgent g a
164 Source code
273 i f Agent . CanPerform a g e n t a c t i o n
274 then
275 l e t ( teamid , a g e n t i d ) = a g e n t . ID 276 l e t i s a t t a c k e d =
277 L i s t . e x i s t s (f u n i d −> i d = a g e n t . ID ) a t t a c k e d 278 l e t d i s a b l e d = Agent . I s D i s a b l e d a g e n t
279 l e t r e d u c e n =
280 l e t newa = Graph . GetUpdatedAgent g a
281 Agent . ReduceEnergy n newa |> Graph . ReplaceAgent g 282 match a c t i o n , i s a t t a c k e d , d i s a b l e d with
283 // Recharge − a g e n t hasn ’ t been a t t a c k e d 284 | Recharge , f a l s e , −>
285 Agent . RechargeA s e t t i n g s a g e n t
286 |> Graph . ReplaceAgent g
287 s t a t u s
288 // Move a g e n t
289 | Goto ( n ) , , when
290 a g e n t . Energy >= s e t t i n g s . F a i l e d G o to C o s t 291 && 0 <= n && n < Array . l e n g t h nodes −>
292 Graph . MoveAgent s e t t i n g s g a g e n t n |> i g n o r e
293 s t a t u s
294 // Probe node − a g e n t s hasn ’ t been a t t a c k e d and i s n ’ t 295 // d i s a b l e d
296 | Probe , f a l s e , f a l s e when
297 a g e n t . Energy >= s e t t i n g s . ProbeCost
298 && not ( Node . ProbedBy a g e n t . ID nodes . [ a g e n t . Node ] ) −>
299 l e t node = nodes . [ a g e n t . Node ]
300 nodes . [ a g e n t . Node ] <− Node . AddAgentProbed node a g e n t . ID 301 r e d u c e s e t t i n g s . ProbeCost
302 L i s t R e p l a c e teamid TS . I n c P r o b e s s t a t u s
303 // Survey e d g e s − a g e n t hasn ’ t been a t t a c k e d and i s n ’ t 304 // d i s a b l e d
305 | Survey , f a l s e , f a l s e when
306 a g e n t . Energy >= s e t t i n g s . SurveyCost −>
307 r e d u c e s e t t i n g s . SurveyCost
308 l e t num = Graph . SurveyEdges g a g e n t . Node a g e n t . ID 309 L i s t R e p l a c e teamid (TS . I n c S u r v e y s num) s t a t u s
310 // I n s p e c t an opponent a g e n t − a g e n t hasn ’ t been a t t a c k e d 311 // and i s n ’ t d i s a b l e d
312 | I n s p e c t , f a l s e , f a l s e when
313 a g e n t . Energy >= s e t t i n g s . I n s p e c t C o s t −>
314 r e d u c e s e t t i n g s . I n s p e c t C o s t
315 l e t num = Graph . I n s p e c t A g e n t s g a g e n t . Node a g e n t . ID 316 L i s t R e p l a c e teamid (TS . I n c I n s p e c t i o n s num) s t a t u s 317 // R e p a i r a f r i e n d l y a g e n t − a g e n t hasn ’ t been a t t a c k e d 318 | R e p a i r ( a i d ) , f a l s e , when
319 a i d <> a g e n t i d && a g e n t . Energy >= s e t t i n g s . R e p a i r C o s t −>
320 l e t t a r g e t = 321 L i s t . t r y F i n d
322 (f u n a −> a . ID = ( teamid , a i d ) ) 323 nodes . [ a g e n t . Node ] . Agents
324 match t a r g e t with
325 | Some a −>
326 Agent . RepairA a |> Graph . ReplaceAgent g 327 r e d u c e s e t t i n g s . R e p a i r C o s t
B.21 Simulation.fs 165
328 | −> ( )
329 s t a t u s
330 // Upgrade − a g e n t hasn ’ t been a t t a c k e d and i s n ’ t 331 // d i s a b l e d
332 | Buy ( up ) , f a l s e , f a l s e when
333 a g e n t . Energy >= s e t t i n g s . BuyCost −>
334 l e t money = s t a t u s . [ teamid ] . Money 335 l e t p r i c e = G e tP r i c e s e t t i n g s up 336 i f money>= p r i c e
337 then
338 a g e n t
339 |> Agent . ReduceEnergy s e t t i n g s . BuyCost
340 |> Agent . Upgrade up
341 |> Graph . ReplaceAgent g
342 L i s t R e p l a c e teamid (TS . ReduceMoney p r i c e ) s t a t u s 343 e l s e s t a t u s
344 | −> s t a t u s 345 e l s e s t a t u s 346
347 // Execute a l l a c t i o n s o t h e r than p a r r y and a t t a c k 348 // Has s i d e e f f e c t s
349 // S i m u l a t i o n S e t t i n g s −> TeamStatus l i s t −> Graph 350 // −> ( i n t ∗ i n t ) l i s t −> ( Agent ∗ A c ti o n ) l i s t 351 // −> TeamStatus l i s t
352 l e t i n t e r n a l E x e c u te O th e r A c ti o n s s e t t i n g s s t a t u s graph a t t a c k e d 353 a c t i o n s =
354 L i s t . f o l d ( E x e c u te O th e r A c ti o n s e t t i n g s graph a t t a c k e d ) s t a t u s 355 a c t i o n s
356
357 // C o l o r a graph u s i n g th e grap c o l o r i n g a l g o r i t h m . 358 // Has s i d e e f f e c t s .
359 // Graph −> u n i t
360 l e t i n t e r n a l ColorGraph graph = 361 Graph . R e s e t C o l o r i n g graph 362 Graph . ColorDominatedNodes graph 363 Graph . C o l o r N e i g h b o u r s graph 364 Graph . C o l o r Z o n e s graph 365 Graph . C o l o r E d g e s graph 366
367 // Update a s i n g l e m i l e s t o n e and team s t a t u s ’ 368 // i n t l i s −> TeamStatus l i s t ∗ M i l e s t o n e S t a t u s 369 // −> TeamStatus l i s t ∗ M i l e s t o n e S t a t u s 370 l e t i n t e r n a l U p d a te M i l e s to n e z o n e s c o r e s 371 ( s t a t u s , ( m i l e s t o n e , compset ) ) = 372 l e t t e a m l i s t =
373 L i s t . z i p z o n e s c o r e s s t a t u s
374 |> L i s t . mapi (f u n n ( a , b ) −> ( n , a , b ) ) 375 l e t f ( team , s c o r e , t s ) =
376 match m i l e s t o n e with
377 | ZoneValues ( , , ) −> s c o r e
378 | P r o b e d V e r t i c e s ( , , ) −> t s . Probes 379 | SurveyedEdges ( , , ) −> t s . S u r v e y s
380 | I n s p e c t e d V e h i c l e s ( , , ) −> t s . I n s p e c t i o n s 381 | S u c c e s s f u l A t t a c k s ( , , ) −> t s . A tta c k s 382 | S u c c e s s f u l P a r r i e s ( , , ) −> t s . P a r r i e s
166 Source code
383 match m i l e s t o n e with 384 | ZoneValues (num , f s t , s c n ) 385 | P r o b e d V e r t i c e s (num , f s t , s c n ) 386 | SurveyedEdges (num , f s t , s c n ) 387 | I n s p e c t e d V e h i c l e s (num , f s t , s c n ) 388 | S u c c e s s f u l A t t a c k s (num , f s t , s c n ) 389 | S u c c e s s f u l P a r r i e s (num , f s t , s c n ) −>
390 l e t c o m p l e te d = 391 t e a m l i s t 392 |> L i s t . c h o o s e
393 (f u n ( ( team , , ) a s a ) −>
394 i f f a >= num then Some team e l s e None ) 395 |> S e t . o f L i s t
396 l e t newcomp = c o m p l e te d − compset
397 l e t amount = i f compset . IsEmpty then f s t e l s e s c n 398 l e t i n c ( t s : TeamStatus l i s t ) n =
399 L i s t R e p l a c e n (TS . In c r e a s e M o n e y amount ) t s 400 l e t n e w s ta tu s =
401 newcomp
402 |> S e t . t o L i s t
403 |> L i s t . f o l d i n c s t a t u s
404 ( n e w s ta tu s , ( m i l e s t o n e , compset + c o m p l e te d ) ) 405
406 // Update a l l m i l e s t o n e s and team s t a t u s ’
407 // Graph −> M i l e s t o n e S t a t u s l i s t −> TeamStatus l i s t 408 // −> M i l e s t o n e S t a t u s l i s t ∗ TeamStatus l i s t
409 l e t i n t e r n a l U p d a te M i l e s to n e s A n d S ta tu s ( ( nodes , ) : Graph ) 410 m i l e s t o n e s s t a t u s =
411 l e t g e t w e i g h t n node =
412 i f Node . ProbedBy ( n , 0 ) node
413 then node . Weight
414 e l s e 1
415 l e t g e t s t e p s c o r e n =
416 nodes
417 |> Array . t o L i s t
418 |> L i s t . f i l t e r (f u n node −> node . DominatingTeam = Some n ) 419 |> L i s t . map ( g e t w e i g h t n )
420 |> L i s t . sum 421 l e t z o n e s c o r e s =
422 [f o r a i n 0 . . ( L i s t . l e n g t h s t a t u s − 1 ) do y i e l d g e t s t e p s c o r e a ] 423 l e t te m p s ta tu s = r e f s t a t u s
424 l e t n e w m i l e s t o n e s = 425 m i l e s t o n e s
426 |> L i s t . map
427 (f u n ms −>
428 l e t newts , newms =
429 U p d a te M i l e s to n e z o n e s c o r e s ( ! te m p s ta tu s , ms)
430 te m p s ta tu s := newts
431 newms )
432 l e t s t e p s c o r e s = 433 z o n e s c o r e s 434 |> L i s t . map2
435 (f u n ( s t : TeamStatus ) s c o r e −> s c o r e + s t . Money )
436 ! te m p s ta tu s
437 l e t n e w s ta tu s =
B.21 Simulation.fs 167
438 ! te m p s ta tu s
439 |> L i s t . map2 TS . I n c r e a s e S c o r e s t e p s c o r e s 440 ( n e w m i l e s to n e s , n e w s ta tu s )
441
442 // Main a l g o r i t h m .
443 // C a l c u l a t e a s i n g l e s i m u l a t i o n s t e p . Do t h i s r e c u r s i v e l y 444 // u n t i l a l l s t e p s have been c a l c u l a t e d .
445 // Has s i d e e f f e c t s . 446 // S i m u l a t i o n S e t t i n g s
447 // −> ( Graph −> TeamStatus l i s t −> i n t −> u n i t ) 448 // −> ( i n t −> P e r c e p t −> u n i t )
449 // −> b o o l
450 // −> Map<i n t∗i n t , IAgent>
451 // −> i n t 452 // −> Graph
453 // −> ( Agent ∗ Message l i s t ) l i s t 454 // −> M i l e s t o n e S t a t u s l i s t
455 // −> TeamStatus l i s t 456 // −> u n i t
457 l e t r e c i n t e r n a l S i m u l a t i o n S t e p s e t t i n g s drawgraph d r a w p e r c e p t 458 viewp a g e n t s stepnum graph m e s s a g e s m i l e s t o n e s s t a t u s = 459 i f stepnum > s e t t i n g s . Length
460 then
461 SendMsg ”Done” // s i m u l a t i o n e n d e th 462 e l s e
463 SendMsg
464 ( s p r i n t f ” C a l c u l a t i n g s t e p %d o f %d” stepnum 465 s e t t i n g s . Length )
466 l e t p e r c e p t s =
467 P r e p a r e P e r c e p t s stepnum graph m e s s a g e s s t a t u s m i l e s t o n e s 468 l e t ( a c t i o n s , msgs ) =
469 S e n d P e r c e p ts s e t t i n g s a g e n t s p e r c e p t s 470 l e t ( a p a c t i o n s , r e s t a c t i o n s ) =
471 L i s t S p l i t A t t a c k P a r r y F i l t e r a c t i o n s 472 l e t ( s t a t u s 2 , a t t a c k e d ) =
473 E x e c u te A tta c k P a r r y s e t t i n g s s t a t u s graph a p a c t i o n s 474 l e t s t a t u s 3 =
475 E x e c u te O th e r A c ti o n s s e t t i n g s s t a t u s 2 graph a t t a c k e d 476 r e s t a c t i o n s
477 ColorGraph graph
478 l e t ( newms , n e w s ta tu s ) =
479 U p d a te M i l e s to n e s A n d S ta tu s graph m i l e s t o n e s s t a t u s 3 480 drawgraph graph n e w s ta tu s stepnum
481 i f viewp
482 then L i s t . i t e r ( d r a w p e r c e p t stepnum ) p e r c e p t s
483 S i m u l a t i o n S t e p s e t t i n g s drawgraph d r a w p e r c e p t viewp a g e n t s 484 ( stepnum +1) graph msgs newms n e w s ta tu s
485
486 // Add a g e n t s to a graph , d e f i n e d by t h e i r r o l e s . The a g e n t s 487 // w i l l be p o s i t i o n e d randomly on th e graph . The a g e n t s a r e 488 // c r e a t e d a c c o r d i n g to th e v a l u e s i n a g i v e n i n t l i s t and b o o l 489 // l i s t .
490 // Has s i d e e f f e c t s .
491 // Graph −> ( i n t l i s t ∗ b o o l l i s t ∗ ’ a l i s t ) l i s t −> u n i t 492 l e t i n t e r n a l NodeAddAgents ( ( nodes , ) a s g ) r o l e s =
168 Source code
493 l e t n = Array . l e n g t h nodes
494 l e t r = new Random ( DateTime . Now . T i c k s |> i n t )
495 l e t add a =
496 l e t r a n d i d = r . Next ( n ) 497 Graph . AddAgent g r a n d i d a
498 l e t m a k e r o l e a g e n t i d ( s t a t l i s t , a c l i s t , n a m e l i s t ) = 499 l e t a d d a g e n ts teamid =
500 Agent . C r e a te ( ( teamid , a g e n t i d ) , s t a t l i s t , a c l i s t ) 501 L i s t . mapi a d d a g e n ts n a m e l i s t
502 |> L i s t . c h o o s e (f u n a −> a ) 503 |> L i s t . i t e r add
504 L i s t . i t e r i m a k e r o l e r o l e s 505
506 // C r e a te th e AI o b j e c t s a c c o r d i n g to th e d e f i n e d r o l e s . 507 // T e l l s th e c r e a t e d o b j e c t th e maximum a l l o w e d r e s p o n s e time . 508 // i n t −> ( ’ a l i s t ∗ ’ b l i s t ∗ s t r i n g l i s t ) l i s t
509 // Map<i n t∗i n t , IAgent>
510 l e t i n t e r n a l CreateAI s e t t i n g s nums r o l e s = 511 l e t makeagent a g e n t i d teamid name =
512 ( ( teamid , a g e n t i d ) , MAS2011 . Agents . GetAgent name ) 513 l e t makeagents a g e n t i d ( , , s l i s t ) =
514 L i s t . mapi ( makeagent a g e n t i d ) s l i s t 515 l e t a g e n t s =
516 r o l e s |> L i s t . mapi makeagents |> L i s t . c o n c a t 517 L i s t . i t e r
518 (f u n ( , ( a : IAgent ) ) −> a . A d d S e tti n g s s e t t i n g s ) 519 a g e n t s
520 L i s t . i t e r
521 (f u n ( , ( a : IAgent ) ) −> a . SetNumNodesEdgesSteps nums ) 522 a g e n t s
523 a g e n t s |> Map . o f L i s t 524
525 // S t a r t a s i m u l a t i o n with a g i v e n l i s t o f v a l u e s , c o l o r s , 526 // r o l e s and m i l e s t o n e s , and a b o o l e a n v a l u e f o r whether o r not 527 // th e a g e n ts ’ p e r c e p t i o n s a r e to be drawn .
528 // C r e a t e s th e s i m u l a t i o n i n a t h r e a d . Aborts th e e x i s t i n g 529 // s i m u l a t i o n , i f any .
530 // Has s i d e e f f e c t s . 531 // i n t l i s t
532 // −> Gdk . C o l o r l i s t ∗ ( i n t l i s t ∗ b o o l l i s t ∗ s t r i n g l i s t ) l i s t 533 // −> ( s t r i n g ∗ i n t l i s t ) l i s t −> b o o l −> u n i t
534 l e t S ta r tS i m s ( g d k c o l o r s , r o l e s ) m viewp =
535 match s with
536 | nt : : na : : s l : : f p : : r t : : r e c n : : r e c d : : nn : : w : : h : : minn : : maxn : : 537 mine : : maxe : : a c o s t : : p a c o s t : : p r c o s t : : s c o s t : : i c o s t : : b c o s t : : 538 r c o s t : : f g c o s t : : u p b a tt : : u p s h i e : : u p s e n s : : upsabo : : [ ]
539 −>
540 t h r e a d . Abort ( ) 541 l e t s e t t i n g s = 542 { F a i l C h a n c e = f p
543 Length = s l
544 MaxAgentResponse = r t
545 RecoverNormal = r e c n
546 R e c o v e r D i s a b l e d = r e c d
547 AttackCost = a c o s t
B.21 Simulation.fs 169
548 ParryCost = p a c o s t
549 ProbeCost = p r c o s t
550 SurveyCost = s c o s t
551 I n s p e c t C o s t = i c o s t
552 BuyCost = b c o s t
553 R e p a i r C o s t = r c o s t 554 F a i l e d G o to C o s t = f g c o s t 555 U p g r a d e B a tt e r y P r i c e = u p b a tt 556 U p g r a d e S e n s o r P r i c e = u p s e n s 557 U p g r a d e S h i e l d P r i c e = u p s h i e
558 U p g r a d e S a b o t a g e D e v i c e P r i c e = upsabo }
559 l e t graph =
560 Graph . C r e a te ( nn , w−1 ,h−1 ,minn , maxn+1 , mine , maxe+1) 561 l e t nums = ( nn , Graph . NumEdges graph , s l )
562 NodeAddAgents graph r o l e s
563 l e t c o l o r s = L i s t . map GdkColor g d k c o l o r s 564 l e t a g e n t s = CreateAI s e t t i n g s nums r o l e s 565 l e t drawg = DrawGraph c o l o r s (w, h )
566 l e t drawp = DrawPercept c o l o r s (w, h )
567 l e t s t a t u s = L i s t . i n i t nt (f u n −> TS . S t a r t S t a t u s ) 568 l e t m i l e s t o n e s = L i s t . c h o o s e P a r s e M i l e s t o n e m 569 R e s e t S t e p s ( )
570 drawg graph s t a t u s 0 571 t h r e a d <−
572 new Thread
573 (new T h r e a d S ta r t
574 (f u n −>
575 S i m u l a t i o n S t e p s e t t i n g s drawg drawp viewp a g e n t s 1 576 graph [ ] m i l e s t o n e s s t a t u s ) )
577 t h r e a d . S t a r t ( )
578 | −> p r i n t f n ” wrong l e n g t h o f s : %d” ( L i s t . l e n g t h s )