[r1813]: trunk / Toss / GGP / TranslateGameTest.ml Maximize Restore History

Download this file

TranslateGameTest.ml    469 lines (435 with data), 18.6 kB

  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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
open OUnit
open GDL
let parse_game_descr s =
GDLParser.parse_game_description KIFLexer.lex (Lexing.from_string s)
let parse_term s =
GDLParser.parse_term KIFLexer.lex (Lexing.from_string s)
let pte = parse_term
let state_of_file s =
let f = AuxIO.input_file s in
let res =
ArenaParser.parse_game_state Lexer.lex
(Lexing.from_string f) in
res
let load_rules fname =
let f = AuxIO.input_file fname in
let descr =
GDLParser.parse_game_description KIFLexer.lex (Lexing.from_string f) in
descr
let emb_str (game, state) (rname, emb) =
let struc = state.Arena.struc in
ContinuousRule.matching_str struc emb
let norm_move (rname,emb) =
rname, List.sort Pervasives.compare emb
let check_alternate players playout =
let rec aux turn playout =
match turn, playout with
| _, [] -> true
| [], _ -> aux players playout
| player::turn, state::playout ->
if GDL.func_graph "control" state <> [[|player|]] then false else
aux turn playout in
aux players playout
let game_test_case ~game_name ~player ~own_plnum ~opponent_plnum
~loc0_rule_name ~loc0_emb
~loc0_move ?loc0_noop ~loc1 ~loc1_rule_name ~loc1_emb ?loc1_noop
~loc1_move () =
let game = load_rules ("./GGP/examples/"^game_name^".gdl") in
let gdl, r_game, (r_inl_game, r_struc as res) =
TranslateGame.translate_game ~playing_as:(Const player) game in
let goal_name = game_name^"-simpl.toss" in
(* let goal = state_of_file ("./GGP/tests/"^goal_name) in *)
let goal_str = AuxIO.input_file ("./GGP/tests/" ^ goal_name) in
(* * let resf = open_out ("./GGP/tests/"^game_name^"-temp.toss") in
* *)
let res_str = Arena.state_str (r_game, r_struc) in
(* * output_string resf res_str;
close_out resf; * *)
(* let eq, msg = Arena.compare_diff goal res in *)
let goal_str, res_str =
Aux.normalize_spaces goal_str, Aux.normalize_spaces res_str in
let eq, msg = goal_str = res_str, "sorry, just comparing as strings" in
assert_bool ("tests for " ^ game_name ^ " failed (" ^ goal_name ^ ")")
(* "GGP/examples/"^game_name^".gdl to GGP/tests/"^goal_name^
", see GGP/tests/"^game_name^"-temp.toss: "^msg *)
eq;
(* * Sys.remove ("./GGP/tests/"^game_name^"-temp.toss"); * *)
let rname = loc0_rule_name in
let emb = Arena.matching_of_names res rname loc0_emb in
let emb_s = List.map (fun (v, e) ->
(v, Structure.elem_name (snd res).Arena.struc e)) emb in
let transl = TranslateGame.translate_outgoing_move gdl res rname emb_s in
assert_equal ~printer:(fun x->x) loc0_move transl;
let moves = match loc0_noop with
| Some loc0_noop -> [pte loc0_move; pte loc0_noop]
| None -> [pte loc0_move] in
let moves =
TranslateGame.translate_incoming_move gdl res
(* FIXME: should use the player ordering "plnum" right? *)
moves in
let move = List.assoc own_plnum moves in
assert_equal ~msg:"own incoming move" ~printer:(emb_str res)
(norm_move (rname, emb)) (norm_move move);
let req = (rname, emb, 0.1, []) in
let ((game,state), _) = Arena.apply_rule_int res req in
let res = game, {state with Arena.cur_loc = loc1} in
let rname = loc1_rule_name in
let emb =
Arena.matching_of_names res rname loc1_emb in
let moves = match loc1_noop with
| Some loc1_noop -> [pte loc1_noop; pte loc1_move]
| None -> [pte loc1_move] in
let moves =
TranslateGame.translate_incoming_move gdl res
(* FIXME: should use the player ordering "plnum" right? *)
moves in
let move = List.assoc opponent_plnum moves in
assert_equal ~msg:"opponent incoming move"
~printer:(emb_str res)
(norm_move (rname, emb)) (norm_move move)
let simult_test_case ~game_name ~player ~plnum ~moves
~rules_and_embs =
let game = load_rules ("./GGP/examples/"^game_name^".gdl") in
let gdl, r_game, (r_inl_game, r_struc as res) =
TranslateGame.translate_game ~playing_as:(Const player) game in
let goal_name = game_name^"-simpl.toss" in
(* let goal = state_of_file ("./GGP/tests/"^goal_name) in *)
let goal_str = AuxIO.input_file ("./GGP/tests/"^goal_name) in
(* * let resf = open_out ("./GGP/tests/"^game_name^"-temp.toss")
in * *)
let res_str = Arena.state_str (r_game, r_struc) in
(* * output_string resf res_str;
close_out resf; * *)
(* let eq, msg = Arena.compare_diff goal res in *)
let goal_str, res_str =
Aux.normalize_spaces goal_str, Aux.normalize_spaces res_str in
let eq, msg = goal_str = res_str, "sorry, just comparing as strings" in
assert_bool ("tests for " ^ game_name ^ " failed (" ^ goal_name ^ ")")
(*"GGP/examples/"^game_name^".gdl to GGP/tests/"^goal_name^
", see GGP/tests/"^game_name^"-temp.toss: "^msg*)
eq;
(* * Sys.remove ("./GGP/tests/"^game_name^"-temp.toss"); * *)
let embs = Array.map
(fun (rname, emb) -> Arena.matching_of_names res rname emb)
rules_and_embs in
(* skipping environment -- 0th -- not given in the input array *)
let own_rname, _ = rules_and_embs.(plnum-1) in
let emb_s = List.map (fun (v, e) ->
(v, Structure.elem_name (snd res).Arena.struc e)) embs.(plnum-1) in
let transl =
TranslateGame.translate_outgoing_move gdl res own_rname emb_s in
assert_equal ~printer:(fun x->x) moves.(plnum-1) transl;
let moves =
TranslateGame.translate_incoming_move gdl res
(Array.to_list (Array.map pte moves)) in
let move = List.assoc plnum moves in
assert_equal ~msg:"own incoming move" ~printer:(emb_str res)
(norm_move (own_rname, embs.(plnum-1))) (norm_move move);
let res =
List.fold_left Arena.apply_rewrite res moves in
(* TODO: perform a move by environment once it is nicely provided
by for example ReqHandler. *)
ignore res;
()
let tests = "TranslateGame" >::: [
"tictactoe" >::
(fun () ->
game_test_case ~game_name:"tictactoe" ~player:"xplayer"
~own_plnum:0 ~opponent_plnum:1
~loc0_rule_name:"mark_x6_y_noop"
~loc0_emb:[
"cell_x6_y__BLANK_", "cell_2_2__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc0_move:"(mark 2 2)" ~loc0_noop:"noop"
~loc1:1 ~loc1_rule_name:"noop_mark_x7_y0"
~loc1_emb:[
"cell_x7_y0__BLANK_", "cell_1_1__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc1_noop:"noop" ~loc1_move:"(mark 1 1)" ()
);
"tictactoe-different definition" >::
(fun () ->
game_test_case ~game_name:"tictactoe-other" ~player:"xPLAYER"
~own_plnum:0 ~opponent_plnum:1
~loc0_rule_name:"mARK_X9_Y9_nOOP"
~loc0_emb:[
"cELL_X9_Y9__BLANK_", "cELL_2_2__BLANK_";
"cONTROL__BLANK_", "cONTROL__BLANK_"]
~loc0_move:"(MARK 2 2)" ~loc0_noop:"NOOP"
~loc1:1 ~loc1_rule_name:"nOOP_mARK_X10_Y10"
~loc1_emb:[
"cELL_X10_Y10__BLANK_", "cELL_3_3__BLANK_";
"cONTROL__BLANK_", "cONTROL__BLANK_"]
~loc1_noop:"NOOP" ~loc1_move:"(MARK 3 3)" ()
);
"2player_normal_form_joint" >::
(fun () ->
simult_test_case ~game_name:"2player_normal_form_joint" ~player:"row"
~plnum:1 (* 0 is environment! *)
~moves:[|"r1"; "c1"|]
~rules_and_embs:[|
"m", ["did__BLANK__m", "did__BLANK__r1";
"synch_control_", "synch_control_"];
"m2", ["did__BLANK__m2", "did__BLANK__c1";
"synch_control_", "synch_control_"] |]
);
]
let bigtests = "TranslateGameBig" >::: [
"connect5" >::
(fun () ->
game_test_case ~game_name:"connect5" ~player:"x"
~own_plnum:0 ~opponent_plnum:1
~loc0_rule_name:"mark_x5_y5_noop"
~loc0_emb:[
"cell_x5_y5__BLANK_", "cell_e_f__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc0_move:"(mark e f)" ~loc0_noop:"noop"
~loc1:1 ~loc1_rule_name:"noop_mark_x6_y6"
~loc1_emb:[
"cell_x6_y6__BLANK_", "cell_f_g__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc1_noop:"noop" ~loc1_move:"(mark f g)" ()
);
(* *
"breakthrough" >::
(fun () ->
game_test_case ~game_name:"breakthrough" ~player:"white"
~own_plnum:0 ~opponent_plnum:1
~loc0_rule_name:"move_x2_y3_x3_y4_noop"
~loc0_emb:[
"cellholds_x2_y3__BLANK_", "cellholds_2_2__BLANK_";
"cellholds_x3_y4__BLANK_", "cellholds_1_3__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc0_move:"(move 2 2 1 3)" ~loc0_noop:"noop" ~loc1:1
~loc1_rule_name:"noop_move_x7_y9_x8_y10"
~loc1_emb:[
"cellholds_x7_y9__BLANK_", "cellholds_7_7__BLANK_";
"cellholds_x8_y10__BLANK_", "cellholds_6_6__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc1_noop:"noop" ~loc1_move:"(move 7 7 6 6)" ()
);
"connect4" >::
(fun () ->
(* Works with [filter_possibly_redundant] both true and false. *)
let old_filter_possibly_redundant =
!TranslateGame.filter_possibly_redundant in
TranslateGame.filter_possibly_redundant := false;
game_test_case ~game_name:"connect4" ~player:"white"
~own_plnum:0 ~opponent_plnum:1
~loc0_rule_name:"drop_c11_noop"
~loc0_emb:[
"cell_c11_y18__BLANK_", "cell_2_1__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc0_move:"(drop 2)" ~loc0_noop:"noop"
~loc1:1 ~loc1_rule_name:"noop_drop_c12"
~loc1_emb:[
"cell_c12_y20__BLANK_", "cell_2_2__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc1_noop:"noop" ~loc1_move:"(drop 2)" ();
TranslateGame.filter_possibly_redundant := old_filter_possibly_redundant;
);
"pacman3p" >::
(fun () ->
(* Works only with [filter_possibly_redundant] true. *)
let old_filter_possibly_redundant =
!TranslateGame.filter_possibly_redundant in
TranslateGame.filter_possibly_redundant := true;
simult_test_case ~game_name:"pacman3p" ~player:"pacman"
~plnum:1 (* 0 is environment! *)
~moves:[|"(move east)"; "(move nowhere)"; "(move nowhere)"|]
~rules_and_embs:[|
"move_east", [
"control__BLANK_", "control__BLANK_";
"gdl__counter", "gdl__counter";
"location__BLANK__x10_y10", "location__BLANK__6_3";
"location__BLANK__x_y", "location__BLANK__5_3";
"synch_control_", "synch_control_"];
"move_nowhere0", [
"location__BLANK__x11_y11", "location__BLANK__4_6";
"location__BLANK__x12_y12", "location__BLANK__4_6";
"synch_control_", "synch_control_"];
"move_nowhere1", [
"location__BLANK__x13_y13", "location__BLANK__5_6";
"location__BLANK__x14_y14", "location__BLANK__5_6";
"synch_control_", "synch_control_"]|];
TranslateGame.filter_possibly_redundant := old_filter_possibly_redundant;
);
"asteroids-scrambled" >::
(fun () ->
game_test_case ~game_name:"asteroids-scrambled" ~player:"ship"
~own_plnum:0 ~opponent_plnum:0
~loc0_rule_name:"turn_clock2"
~loc0_emb:[
"value___BLANK__old0", "value___BLANK__i10";
"value___BLANK__old", "value___BLANK__i10";
"value___BLANK__new0", "value___BLANK__i13";
"value___BLANK__new", "value___BLANK__i12";
"value___BLANK__east", "value___BLANK__east";
"value___BLANK__north", "value___BLANK__north";
"gdl__counter", "gdl__counter";
]
~loc0_move:"(turn clock)"
~loc1:0 ~loc1_rule_name:"thrust0"
~loc1_emb:[
"gdl__counter", "gdl__counter";
"value___BLANK__new", "value___BLANK__i14";
"value___BLANK__new0", "value___BLANK__i16";
"value___BLANK__old", "value___BLANK__i12";
"value___BLANK__old0", "value___BLANK__i13";
"value___BLANK__s11", "value___BLANK__i2";
"value___BLANK__s12", "value___BLANK__i3";
"value___BLANK__s15", "value___BLANK__i2"]
~loc1_move:"thrust" ();
);
"pawn_whopping" >::
(fun () ->
game_test_case ~game_name:"pawn_whopping" ~player:"x"
~own_plnum:0 ~opponent_plnum:1
~loc0_rule_name:"move_pv28_pv29_pv30_pv31_noop"
~loc0_emb:[
"cell_pv28_pv29__BLANK_", "cell_2_2__BLANK_";
"cell_pv30_pv31__BLANK_", "cell_2_4__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc0_move:"(move 2 2 2 4)" ~loc0_noop:"noop" ~loc1:1
~loc1_rule_name:"noop_move_pv12_pv13_pv14_pv15"
~loc1_emb:[
"cell_pv12_pv13__BLANK_", "cell_7_7__BLANK_";
"cell_pv14_pv15__BLANK_", "cell_7_6__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc1_noop:"noop" ~loc1_move:"(move 7 7 7 6)" ()
);
* *)
]
let set_debug_level i =
AuxIO.set_debug_level "GDL" (i-2);
AuxIO.set_debug_level "TranslateGame" i;
AuxIO.set_debug_level "TranslateFormula" i;
AuxIO.set_debug_level "GameSimpl" i;
AuxIO.set_debug_level "DiscreteRule" i;
()
let a () =
set_debug_level 4;
(try
game_test_case ~game_name:"pawn_whopping" ~player:"x"
~own_plnum:0 ~opponent_plnum:1
~loc0_rule_name:"move_pv28_pv29_pv30_pv31_noop"
~loc0_emb:[
"cell_pv28_pv29__BLANK_", "cell_2_2__BLANK_";
"cell_pv30_pv31__BLANK_", "cell_2_4__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc0_move:"(move 2 2 2 4)" ~loc0_noop:"noop" ~loc1:1
~loc1_rule_name:"noop_move_pv12_pv13_pv14_pv15"
~loc1_emb:[
"cell_pv12_pv13__BLANK_", "cell_7_7__BLANK_";
"cell_pv14_pv15__BLANK_", "cell_7_6__BLANK_";
"control__BLANK_", "control__BLANK_"]
~loc1_noop:"noop" ~loc1_move:"(move 7 7 7 6)" ();
with e -> AuxIO.print (Printexc.to_string e); raise e);
(* failwith "tested"; *)
()
let a () =
match test_filter
[(* "GDLBig:1:breakthrough" *) "GDLBig:0:connect5"]
bigtests
with
| Some tests -> ignore (run_test_tt ~verbose:true tests)
| None -> ()
let regenerate ~debug ~game_name ~player =
AuxIO.printf "Regenerating %s...\n%!" game_name;
let gamesimpl_dl = AuxIO.debug_level_for "GameSimpl"
and gdl_dl = AuxIO.debug_level_for "GDL"
and translateformula_dl = AuxIO.debug_level_for "TranslateFormula"
and translategame_dl = AuxIO.debug_level_for "TranslateGame"
and discreterule_dl = AuxIO.debug_level_for "DiscreteRule" in
if debug then (
AuxIO.set_debug_level "GameSimpl" 4;
AuxIO.set_debug_level "GDL" 2;
AuxIO.set_debug_level "TranslateFormula" 4;
AuxIO.set_debug_level "TranslateGame" 4;
AuxIO.set_debug_level "DiscreteRule" 4;
);
TranslateGame.generate_test_case := Some game_name;
let game = load_rules ("./GGP/examples/"^game_name^".gdl") in
ignore (TranslateGame.translate_game (Const player) game);
if debug then (
AuxIO.set_debug_level "GameSimpl" gamesimpl_dl;
AuxIO.set_debug_level "GDL" gdl_dl;
AuxIO.set_debug_level "TranslateFormula" translateformula_dl;
AuxIO.set_debug_level "TranslateGame" translategame_dl;
AuxIO.set_debug_level "DiscreteRule" discreterule_dl;
);
TranslateGame.generate_test_case := None
let a () =
regenerate ~debug:false ~game_name:"tictactoe" ~player:"xplayer";
regenerate ~debug:false ~game_name:"tictactoe-other" ~player:"xPLAYER";
regenerate ~debug:false ~game_name:"connect5" ~player:"x";
regenerate ~debug:false ~game_name:"breakthrough" ~player:"white";
regenerate ~debug:false ~game_name:"pawn_whopping" ~player:"x";
regenerate ~debug:false ~game_name:"connect4" ~player:"white";
regenerate ~debug:false ~game_name:"2player_normal_form_2010" ~player:"row";
regenerate ~debug:false ~game_name:"2player_normal_form_joint" ~player:"row";
regenerate ~debug:false ~game_name:"pacman3p" ~player:"pacman";
regenerate ~debug:false ~game_name:"asteroids-scrambled" ~player:"ship";
(* failwith "generated"; *)
()
let translate_file fname timeout =
try
let start = AuxIO.gettimeofday () in
(match timeout with
| None -> ()
| Some tout ->
TranslateGame.set_timeout
(fun () -> AuxIO.gettimeofday() -. start > float (tout)));
let descr = load_rules fname in
let gdl_data, game, (_, struc) =
TranslateGame.translate_game ~playing_as:(GDL.Const "") descr in
(true, Arena.state_str (game, struc))
with
| Aux.Timeout msg -> (false, "Timeout: " ^ msg)
| e -> (false, "Failed: " ^ (Printexc.to_string e))
let translate_dir_tests dirname from_file timeout =
let is_gdl fn = (String.length fn > 4) &&
String.sub fn ((String.length fn) - 4) 4 = ".gdl" in
let files = List.sort compare (List.filter is_gdl (AuxIO.list_dir dirname)) in
let from_file =
try let r = String.rindex from_file '/' in
String.sub from_file (r+1) ((String.length from_file)-r-1)
with Not_found -> from_file in
let files = if from_file = "" then files else
List.filter (fun f -> compare f from_file >= 0) files in
let mk_tst fname =
(fname ^ " (" ^ (string_of_int timeout) ^ "s)") >::
(fun () ->
let start = AuxIO.gettimeofday () in
TranslateGame.set_timeout
(fun () -> AuxIO.gettimeofday() -. start > float (timeout));
let res, msg = translate_file (dirname ^ fname) None in
let t = AuxIO.gettimeofday() -. start in
AuxIO.gc_compact ();
let final = if res then Printf.sprintf "Suceeded (%f sec.)\n%!" t else
Printf.sprintf "%s (%f sec)\n%!" msg t in
assert_bool final res
) in
("TranslateGame " ^ dirname) >::: (List.map mk_tst files)
let exec () =
OUnit.run_test_if_target "TranslateGameTest"
("TranslateGame" >::: [tests; bigtests])
let main () =
AuxIO.set_optimized_gc ();
let (file, testdir, timeout) = (ref "", ref "", ref 45) in
let opts = [
("-v", Arg.Unit (fun () -> set_debug_level 1), "be verbose");
("-d", Arg.Int (fun i -> set_debug_level i), "set debug level");
("-t", Arg.String (fun s -> testdir:= s), "run all tests from a directory");
("-f", Arg.String (fun s -> file := s),
"process file if no -t; start directory tests from this file if -t given");
("-s", Arg.Int (fun i -> timeout := i), "set timeout for tests (seconds)");
] in
Arg.parse opts (fun _ -> ()) "Try -help for help or one of the following.";
if !file <> "" && !testdir = "" then
AuxIO.print (snd (translate_file !file (Some !timeout)))
else if !testdir <> "" then
OUnit.run_test_if_target "TranslateGameTest"
(translate_dir_tests !testdir !file !timeout)
else exec ()
let _ = AuxIO.run_if_target "TranslateGameTest" main