[r1813]: trunk / Toss / Learn / LearnGameTest.ml  Maximize  Restore  History

Download this file

145 lines (128 with data), 4.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
open OUnit
let formula_of_string s =
FormulaParser.parse_formula Lexer.lex (Lexing.from_string s)
let struc_of_string ?(diag=false) s =
if diag then
let s = "START " ^ s ^ " with Da (x, y) = ex u (R(x, u) and C(u, y));" ^
" Db (x, y) = ex u (C(x, u) and R(y, u))" in
match ArenaParser.parse_game_defs Lexer.lex (Lexing.from_string s) with
| Arena.StartStruc struc -> struc
| _ -> failwith "LearnGameTest:struc_of_string: not a structure"
else
StructureParser.parse_structure Lexer.lex (Lexing.from_string s)
let tests = "LearnGame" >::: [
"simple test game" >::
(fun () ->
let partylist0 = [
List.map struc_of_string [
"[ | P:1 {}; Q:1 {} | ] \"
. .
. .
\"" ;
"[ | P:1 {}; Q:1 {} | ] \"
. .
. P
\"" ;
"[ | P:1 {}; Q:1 {} | ] \"
. .
Q P
\"" ;]] in
let partylist1 = [
List.map struc_of_string [
"[ | P:1 {}; Q:1 {} | ] \"
. .
. .
\"" ;
"[ | P:1 {}; Q:1 {} | ] \"
. .
P .
\"" ;
"[ | P:1 {}; Q:1 {} | ] \"
. .
P Q
\"" ;]] in
let res_game =
"PLAYERS 1, 2
REL Win1() = ex x0 (Q(x0) and ex x1 R(x0, x1))
REL Win2() = ex x1 (Q(x1) and ex x0 R(x0, x1))
RULE Mv1r0:
[e1 | P:1 {}; Q:1 {}; R:2 {} | ] -> [e1 | P (e1); Q:1 {}; R:2 {} | ]
emb R,Q,P
pre (true) and not Win2()
RULE Mv2r0:
[e1 | P:1 {}; Q:1 {}; R:2 {} | ] -> [e1 | P:1 {}; Q (e1); R:2 {} | ]
emb R,Q,P
pre (true) and not Win1()
LOC 0 {
PLAYER 1 { PAYOFF : (Win1()) - :(Win2())
MOVES [Mv1r0 -> 1]}
PLAYER 2 { PAYOFF : (Win2()) - :(Win1()) }
}
LOC 1 {
PLAYER 1 { PAYOFF :(Win1()) - :(Win2()) }
PLAYER 2 { PAYOFF :(Win2()) - :(Win1())
MOVES [Mv2r0 -> 0]}
}
START [ | P:1 {}; Q:1 {} | ] R R \"
. .
\"" in
assert_equal ~printer:(fun x -> x) res_game
((LearnGame.learnFromParties ~win0:partylist0 ~win1:partylist1
~notwon:[] ~wrong:[]));
);
]
let get_strucs s =
let cl = String.index s '\n' in
let pref, st_s = String.sub s 0 cl, String.sub s cl ((String.length s)-cl) in
let strucstr s = pref ^ " \n\"" ^ s ^ "\n\"" in
let getstruc s = let st = strucstr s in struc_of_string ~diag:true st in
List.map getstruc (List.filter (fun s-> s <> "") (Aux.split_empty_lines st_s))
let learn_game dir testname =
let tnlen = String.length testname in
let is_test fn =
String.length fn > tnlen && String.sub fn 0 tnlen = testname in
let tfiles = List.map (fun fn -> dir ^ "/" ^ fn)
(List.sort compare (List.filter is_test (AuxIO.list_dir dir))) in
let is_group g fn = String.sub fn ((String.length fn) - 4) 4 = "." ^ g in
let get_struc fn = try get_strucs (AuxIO.input_file fn) with
err -> AuxIO.print ("Error in " ^ fn ^ "\n"); raise err in
let strucs_of_files fs = List.map get_struc fs in
let (win0, win1, notwon, wrong) =
(strucs_of_files (List.filter (is_group "wn0") tfiles),
strucs_of_files (List.filter (is_group "wn1") tfiles),
strucs_of_files (List.filter (is_group "nwn") tfiles),
strucs_of_files (List.filter (is_group "wrg") tfiles)) in
(LearnGame.learnFromParties ~win0 ~win1 ~notwon ~wrong) ^ "\n"
let test_learn_game name =
let res = AuxIO.input_file ("./Learn/examples/" ^ name ^ ".toss") in
assert_equal ~printer:(fun s -> s) (Aux.normalize_spaces res)
(Aux.normalize_spaces (learn_game "Learn/examples" name))
let bigtests = "LearnGame" >::: [
"Tic-Tac-Toe001" >:: (fun () -> test_learn_game "Tic-Tac-Toe001");
]
let extratests = "LearnGame" >::: [
"Tic-Tac-Toe002" >:: (fun () -> test_learn_game "Tic-Tac-Toe002");
"Breakthrough001" >:: (fun () -> test_learn_game "Breakthrough001");
"Gomoku001" >:: (fun () -> test_learn_game "Gomoku001");
"Connect4001" >:: (fun () -> test_learn_game "Connect4001");
"Pawn-Whopping001" >:: (fun () ->
LearnGame.tc := false;
test_learn_game "Pawn-Whopping001"
);
]
let main () =
AuxIO.set_optimized_gc ();
let (testname, dir) = (ref "", ref "examples") in
let dbg_level i = (AuxIO.set_debug_level "LearnGame" i) in
let opts = [
("-v", Arg.Unit (fun () -> dbg_level 1), "be verbose");
("-d", Arg.Int (fun i -> dbg_level i), "set debug level");
("-f", Arg.String (fun s -> testname := s), "process files");
("-notc", Arg.Unit (fun () -> LearnGame.tc := false), "no TC operator");
("-dir", Arg.String (fun s -> dir := s), "set files directory");
] in
Arg.parse opts (fun _ -> ()) "Try -help for help or one of the following.";
if !testname <> "" then (
AuxIO.print (learn_game !dir !testname)
) else ignore (OUnit.run_test_tt ~verbose:true tests)
let _ = AuxIO.run_if_target "LearnGameTest" main