-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.ml
404 lines (333 loc) · 9.44 KB
/
game.ml
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
(*open Util*)
module type S =
sig
module A : Arena.S
type t
type objective
val set_arena : t -> A.t -> t
val make : A.t -> t
val arena : t -> A.t
val objective : t -> A.M.Player.t -> objective
val set_objective : t -> A.M.Player.t -> objective -> t
val players : t -> Arena.Util(A).PlayerSet.t
val start : t -> A.G.V.t option
val set_start : t -> A.G.V.t -> t
end
module type BUCHI =
sig
include S
val buchi_objective : objective -> (A.G.V.t -> int)
end
module type BUCHI_DEFINABLE =
sig
include S
module Auto : Auto.P
with type Alphabet.elt = A.G.V.t
type buchi_objective =
| Internal of (A.G.V.t -> int)
| Automaton of (Auto.t * (Auto.States.elt -> int))
val to_buchi : t -> objective -> buchi_objective
end
module MakeBuchi =
functor (A:Arena.S) ->
struct
module A = A
module AU = Arena.Util(A)
module PlayerSet = AU.PlayerSet
module StateSet = AU.StateSet
module PlayerMap = Map.Make(A.M.Player)
type objective = A.G.V.t -> int
type t =
{ arena: A.t;
players: PlayerSet.t;
objectives: objective PlayerMap.t;
start: A.G.V.t option
}
let start g = g.start
let set_start g s = {g with start= Some s}
let arena g = g.arena
let objective g p =
try PlayerMap.find p g.objectives
with _ -> (fun x -> 0)
let states g =
A.G.fold_vertex (fun s v -> StateSet.add s v)
(arena g) StateSet.empty
let players g = g.players
let set_objective g p ol =
{ g with
players = PlayerSet.add p (players g);
objectives = PlayerMap.add p ol g.objectives}
let make arena =
{ arena = arena ;
players = PlayerSet.empty (* fix this? *);
objectives = PlayerMap.empty;
start = None
}
let set_arena g a = {g with arena = a }
let add_edge = A.G.add_edge_e
let add_vertex = A.G.add_vertex
let make_objective o = o
let buchi_objective o = o
end
module Make =
functor (A:Arena.S) ->
struct
module A = A
module AU = Arena.Util(A)
module StateSet = AU.StateSet
module PlayerSet = AU.PlayerSet
module PlayerMap = Map.Make(A.M.Player)
module Auto = Auto.Make(Util.Int)(StateSet)
type target = A.G.V.t -> int
type objective =
| Buchi of target
| Reach of target
| Safety of StateSet.t
| OAuto of Auto.t
type buchi_objective =
| Internal of (A.G.V.t -> int)
| Automaton of (Auto.t * (Auto.States.elt -> int))
let buchi t = Buchi t
let reach t = Reach t
let safety t = Safety t
let automaton a = OAuto a
type t =
{ arena: A.t;
players: PlayerSet.t;
objectives: objective PlayerMap.t;
start: A.G.V.t option
}
let arena g = g.arena
let objective g p =
try PlayerMap.find p g.objectives
with _ -> buchi (fun x -> 0)
let states g =
A.G.fold_vertex (fun s v -> StateSet.add s v)
(arena g) StateSet.empty
let to_buchi g = function
| Buchi target ->
Internal target
| Reach payoff ->
let a = Auto.add_state Auto.empty 0 in
let a = Auto.set_initial a 0 in
let a = A.G.fold_vertex
(fun s a -> Auto.add_state a (payoff s)) (arena g) a
in
let a =
A.G.fold_vertex
(fun s a ->
let p = payoff s in
Auto.States.fold
(fun t a ->
Auto.add_to_transition a t s (max p t))
(Auto.states a) a
) (arena g) a
in Automaton (a, fun t -> t)
| Safety target ->
let a = Auto.add_state (Auto.add_state Auto.empty 1) 2 in
let a = Auto.set_initial a 1 in
let a = Auto.add_final a 1 in
let a = Auto.add_transition a 2 (states g) 2 in
let a = Auto.add_transition a 1 target 2 in
let a = Auto.add_transition a 1
(StateSet.diff (states g) target) 1 in
Automaton (a, function 2 -> 0 | 1 -> 1 | x -> failwith ("Found value "^string_of_int x^" in the automaton"))
| OAuto a -> Automaton (a,
(fun s ->
if Auto.States.mem s (Auto.final a)
then 1 else 0))
let players g = g.players
let set_objective g p ol =
{ g with
players = PlayerSet.add p (players g);
objectives = PlayerMap.add p ol g.objectives}
let make arena =
{ arena = arena ;
players = PlayerSet.empty (* fix this? *);
objectives = PlayerMap.empty;
start = None
}
let set_arena g a = {g with arena = a }
let add_edge = A.G.add_edge_e
let add_vertex = A.G.add_vertex
let start g = g.start
let set_start g i = { g with start = Some i}
end
module Output =
functor (G:BUCHI) ->
functor (P:
sig
val state : G.A.G.V.t -> string
val action : G.A.M.Action.t -> string
val player : G.A.M.Player.t -> int
end) ->
struct
module AU = Arena.Util(G.A)
module PlayerSet = AU.PlayerSet
module ArenaPrinter = Arena.DotPrinter(G.A)(P)
let output_arena filename game =
let out_channel = open_out filename in
ArenaPrinter.output_graph out_channel (G.arena game);
close_out out_channel
let output filename game =
let arena_file = filename^".dot" in
let game_file = filename^".game" in
output_arena arena_file game;
let out_channel = open_out game_file in
Printf.fprintf out_channel "arena %S\n" (Filename.basename arena_file);
(match G.start game with
| None -> ()
| Some v ->
Printf.fprintf out_channel "start %s\n"
(P.state v));
PlayerSet.iter
(fun player1 ->
let objective =
G.buchi_objective (G.objective game player1) in
Printf.fprintf out_channel "objective %d buchi" (P.player player1);
let _ =
G.A.G.fold_vertex
(fun vertex is_first ->
let value = objective vertex in
if value <> 0 || is_first
then
(if not is_first
then
Printf.fprintf out_channel " ; ";
Printf.fprintf out_channel
" %s -> %d " (P.state vertex) value;
false)
else is_first
) (G.arena game) true
in Printf.fprintf out_channel "\n"
) (G.players game);
close_out out_channel
end
module ToBuchi =
functor (G:BUCHI_DEFINABLE) ->
struct
module PlayerMap = Map.Make(G.A.M.Player)
module Prod = Product.Make(G.A)(G.Auto)(G.A.M.Player)
module OldAU = Arena.Util(G.A)
module OldPlayerSet = OldAU.PlayerSet
module A = Prod.P
module AU = Arena.Util(A)
module StateSet= AU.StateSet
module PlayerSet = AU.PlayerSet
type objective = StateSet.elt -> int
type t =
{ arena: A.t;
players: PlayerSet.t;
objectives: objective PlayerMap.t;
start: A.G.V.t option
}
let arena g = g.arena
let players g = g.players
let objective g p =
try PlayerMap.find p g.objectives
with _ -> (fun x -> 0)
let make arena =
{ arena = arena ;
players = PlayerSet.empty (* !!! fix this *);
objectives = PlayerMap.empty;
start = None
}
let start g = g.start
let set_start g s = {g with start= Some s}
let set_arena g a = {g with arena = a }
let add_edge = A.G.add_edge_e
let add_vertex = A.G.add_vertex
let set_objective g p ol =
{ g with
players = PlayerSet.add p (players g);
objectives = PlayerMap.add p ol g.objectives}
let buchi_objective obj = obj
let from_buchi g = failwith "unimplemented from_buchi"
let arena_from_buchi a =
let p = Prod.projection a in
p
let player_list g =
OldPlayerSet.fold (fun p list -> p :: list) (G.players g) []
let to_buchi g =
let arena = Prod.of_graph (G.arena g) in
let arena =
OldPlayerSet.fold
(fun p arena ->
match G.to_buchi g (G.objective g p) with
| G.Automaton (auto,_) -> Prod.product arena p auto
| G.Internal _ -> arena)
(G.players g) arena
in
let game =
OldPlayerSet.fold
(fun p game ->
match G.to_buchi g (G.objective g p) with
| G.Automaton (_,payoff) ->
(* !! repetion of the computation of the product *)
let new_payoff s =
payoff (Prod.get s p)
in set_objective game p new_payoff
| G.Internal payoff ->
let new_payoff s = payoff (Prod.proj s)
in set_objective game p new_payoff
) (G.players g) (make arena)
in
match G.start g with
| None -> game
| Some start ->
let prod player s =
match G.to_buchi g (G.objective g player) with
| G.Automaton (auto,_) ->
Prod.state_product s player (G.Auto.initial auto)
| G.Internal _ -> s
in
let i = OldPlayerSet.fold prod (G.players g) (Prod.inj start)
in set_start game i
let initial g =
let prod player s =
match G.to_buchi g (G.objective g player) with
| G.Automaton (auto,_) ->
Prod.state_product s player (G.Auto.initial auto)
| G.Internal _ -> s
in
let add_vertex state set =
StateSet.add
(OldPlayerSet.fold prod (G.players g) (Prod.inj state))
set
in
G.A.G.fold_vertex add_vertex (G.arena g)
(StateSet.empty)
module DotPrinter =
functor (P:
sig
val conf : G.A.G.V.t -> string
val state : G.Auto.States.elt -> string
val action : G.A.M.Action.t -> string
end ) ->
struct
module PP = Prod.DotPrinter(P)
open PP
let string_of_state = string_of_state
let fprint_graph = fprint_graph
let output_graph = output_graph
let objectives game player =
PlayerSet.fold
(fun p string ->
let payoff = buchi_objective
(objective game p) in
let string =
Printf.sprintf "%splayer %s : \n"
string (player p)
in
A.G.fold_vertex
(fun s string ->
let pay = payoff s
in if pay > 0 then
Printf.sprintf "%s%s -> %d\n"
string (string_of_state s) pay
else string)
(arena game) string
)
(players game) ""
end
end