[Toss-devel-svn] SF.net SVN: toss:[1645] trunk/Toss
Status: Beta
Brought to you by:
lukaszkaiser
|
From: <luk...@us...> - 2012-01-18 02:45:54
|
Revision: 1645
http://toss.svn.sourceforge.net/toss/?rev=1645&view=rev
Author: lukaszkaiser
Date: 2012-01-18 02:45:45 +0000 (Wed, 18 Jan 2012)
Log Message:
-----------
Redoing learn tests, plays in separate files, removing Picture.
Modified Paths:
--------------
trunk/Toss/Formula/AuxIO.ml
trunk/Toss/Formula/AuxIO.mli
trunk/Toss/Learn/LearnGame.ml
trunk/Toss/Learn/LearnGame.mli
trunk/Toss/Learn/LearnGameTest.ml
trunk/Toss/Learn/Makefile
trunk/Toss/Server/Makefile
trunk/Toss/Server/ReqHandler.ml
trunk/Toss/Server/Tests.ml
trunk/Toss/WebClient/Main.js
Added Paths:
-----------
trunk/Toss/Learn/examples/
trunk/Toss/Learn/examples/Breakthrough001_01.nwn
trunk/Toss/Learn/examples/Breakthrough001_01.wn0
trunk/Toss/Learn/examples/Breakthrough001_01.wn1
trunk/Toss/Learn/examples/Breakthrough001_02.nwn
trunk/Toss/Learn/examples/Breakthrough001_03.nwn
trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.nwn
trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.wn0
trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.wn1
trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.nwn
trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.wn0
trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.wn1
trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.nwn
trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.wn0
trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.wn1
trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.nwn
trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.wn0
trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.wn1
trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.nwn
trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.wn0
trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.wn1
trunk/Toss/Learn/examples/Tic-Tac-Toe001_06.wn0
trunk/Toss/Learn/examples/Tic-Tac-Toe001_06.wn1
Removed Paths:
-------------
trunk/Toss/Server/Picture.ml
trunk/Toss/Server/Picture.mli
trunk/Toss/Server/PictureTest.ml
trunk/Toss/Server/def_pics/
Modified: trunk/Toss/Formula/AuxIO.ml
===================================================================
--- trunk/Toss/Formula/AuxIO.ml 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Formula/AuxIO.ml 2012-01-18 02:45:45 UTC (rev 1645)
@@ -27,6 +27,11 @@
with End_of_file -> ());
Buffer.contents buf
+let input_fname fn =
+ let f = open_in fn in
+ let res = input_file f in
+ close_in f; res
+
let list_dir dirname =
let files, dir_handle = (ref [], Unix.opendir dirname) in
let rec add () = files := (Unix.readdir dir_handle) :: !files; add () in
Modified: trunk/Toss/Formula/AuxIO.mli
===================================================================
--- trunk/Toss/Formula/AuxIO.mli 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Formula/AuxIO.mli 2012-01-18 02:45:45 UTC (rev 1645)
@@ -12,6 +12,9 @@
(** Input a file to a string. *)
val input_file : in_channel -> string
+(** Input a file with given filename to a string. *)
+val input_fname : string -> string
+
(** List the contents of a directory *)
val list_dir : string -> string list
Modified: trunk/Toss/Learn/LearnGame.ml
===================================================================
--- trunk/Toss/Learn/LearnGame.ml 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Learn/LearnGame.ml 2012-01-18 02:45:45 UTC (rev 1645)
@@ -68,51 +68,48 @@
[] (evens ~acc:[i] (List.length party)) ) )
[] partylist)
-let learnFromParties ~win0 ~win1 ~tie ~wrong =
+let learnFromParties ~win0 ~win1 ~notwon ~wrong =
let win0f = winFormula
(List.map (fun x -> List.hd (List.rev x)) win0)
(List.flatten ((List.map (fun x-> List.tl (List.rev x))
- win0) @ win1 @ tie)) in
+ win0) @ win1 @ notwon)) in
let win1f = winFormula
(List.map (fun x -> List.hd (List.rev x)) win1)
(List.flatten ((List.map (fun x-> List.tl (List.rev x))
- win1) @ win0 @ tie)) in
+ win1) @ win0 @ notwon)) in
- let moves0 = movesi 0 (win0 @ win1) in
- let moves1 = movesi 1 (win0 @ win1) in
+ let moves0 = movesi 0 (win0 @ win1 @ notwon) in
+ let moves1 = movesi 1 (win0 @ win1 @ notwon) in
- "PLAYERS 1, 2\n"^
+ let cmpll l1 l2 = (List.length l2) - (List.length l1) in
+ let longest = List.hd (List.sort cmpll (win0 @ win1 @ notwon)) in
+
+ "PLAYERS 1, 2\n" ^
"REL Win1() = "^ (Formula.sprint win0f) ^"\n"^
"REL Win2() = "^ (Formula.sprint win1f) ^"\n"^
- "RULE Mv1: " ^
- (List.fold_left
- (fun old x->
- old ^ "\n"^
- (Structure.str (fst x))^" -> "^(Structure.str
- (snd x)) ^
- "\nemb "^(String.concat "," (List.map fst (Structure.rel_signature
- (fst x)) )) ^ " " ^
- "pre not Win2()" )
- "" moves0) ^"\n"^
- "RULE Mv2: " ^
- (List.fold_left
- (fun old x->
- old^"\n"^
- (Structure.str (fst x))^" -> "^(Structure.str
- (snd x)) ^
- "\nemb "^(String.concat "," (List.map fst (Structure.rel_signature
- (fst x)) )) ^ " " ^
- "pre not Win1()" )
- "" moves1) ^"\n"^
+ (fst (List.fold_left
+ (fun (old, i) x ->
+ (old ^ "\n" ^ "RULE Mv1-" ^ (string_of_int i) ^ ": \n" ^
+ (Structure.str (fst x)) ^ " -> " ^ (Structure.str (snd x)) ^ "\nemb "^
+ (String.concat "," (List.map fst (Structure.rel_signature (fst x)) ))^
+ "\npre not Win2()"), i+1)
+ ("", 0) moves0)) ^ "\n\n" ^
+ (fst (List.fold_left
+ (fun (old, i) x ->
+ (old ^ "\n" ^ "RULE Mv2-" ^ (string_of_int i) ^ ": \n" ^
+ (Structure.str (fst x)) ^ " -> " ^ (Structure.str (snd x)) ^ "\nemb "^
+ (String.concat "," (List.map fst (Structure.rel_signature (fst x)) ))^
+ "\npre not Win1()"), i+1)
+ ("",0) moves1)) ^ "\n\n" ^
"LOC 0 {
PLAYER 1 { PAYOFF : (Win1()) - :(Win2())
MOVES [Mv1 -> 1]}
PLAYER 2 { PAYOFF : (Win2()) - :(Win1()) }
}
-LOC 1{
+LOC 1 {
PLAYER 1 { PAYOFF :(Win1()) - :(Win2()) }
PLAYER 2 { PAYOFF :(Win2()) - :(Win1())
MOVES [Mv2 -> 0] }
}" ^"\n" ^
- "MODEL "^(Structure.str (List.hd (List.hd win0)))
+ "MODEL "^(Structure.str (List.hd longest))
Modified: trunk/Toss/Learn/LearnGame.mli
===================================================================
--- trunk/Toss/Learn/LearnGame.mli 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Learn/LearnGame.mli 2012-01-18 02:45:45 UTC (rev 1645)
@@ -11,7 +11,7 @@
val learnFromParties:
win0: Structure.structure list list ->
win1: Structure.structure list list ->
- tie: Structure.structure list list ->
+ notwon: Structure.structure list list ->
wrong: Structure.structure list list -> string
Modified: trunk/Toss/Learn/LearnGameTest.ml
===================================================================
--- trunk/Toss/Learn/LearnGameTest.ml 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Learn/LearnGameTest.ml 2012-01-18 02:45:45 UTC (rev 1645)
@@ -48,18 +48,24 @@
"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 Mv1:
+
+RULE Mv1-0:
[1 | P:1 {}; Q:1 {}; R:2 {} | ] -> [1 | P (1); Q:1 {}; R:2 {} | ]
-emb R,Q,P pre not Win2()
-RULE Mv2:
+emb R,Q,P
+pre not Win2()
+
+
+RULE Mv2-0:
[1 | P:1 {}; Q:1 {}; R:2 {} | ] -> [1 | P:1 {}; Q (1); R:2 {} | ]
-emb R,Q,P pre not Win1()
+emb R,Q,P
+pre not Win1()
+
LOC 0 {
PLAYER 1 { PAYOFF : (Win1()) - :(Win2())
MOVES [Mv1 -> 1]}
PLAYER 2 { PAYOFF : (Win2()) - :(Win1()) }
}
-LOC 1{
+LOC 1 {
PLAYER 1 { PAYOFF :(Win1()) - :(Win2()) }
PLAYER 2 { PAYOFF :(Win2()) - :(Win1())
MOVES [Mv2 -> 0] }
@@ -70,331 +76,46 @@
\"" in
assert_equal ~printer:(fun x -> x) res_game
((LearnGame.learnFromParties ~win0:partylist0 ~win1:partylist1
- ~tie:[] ~wrong:[]));
+ ~notwon:[] ~wrong:[]));
);
]
-let bigtests = "LearnGame" >::: [
- "tic-tac-toe" >::
- (fun () ->
- Distinguish.set_debug_level 0; (* set to 1 to get some info printed out *)
- let partylist0 = [
- List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. . .
-. . .
-. . .
-. . .
-. . .
-\"" ;
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. . .
-. . .
-. . .
-. . .
-\"" ;
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-P . .
-. . .
-. . .
-. . .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q Q .
-. . .
-P . .
-. . .
-. . .
-. . .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q Q .
-. . .
-P P .
-. . .
-. . .
-. . .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q Q Q
-. . .
-P P .
-. . .
-. . .
-. . .
-\"";
- ]; List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. . .
-. . .
-. . .
-. . .
-. . .
-\"" ;
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. . .
-. . .
-. . .
-. . .
-\"" ;
-"[ | P:1 {}; Q:1 {} | ] \"
-Q P .
-. . .
-. . .
-. . .
-. . .
-. . .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q P .
-. . .
-Q . .
-. . .
-. . .
-. . .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q P .
-. . .
-Q P .
-. . .
-. . .
-. . .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q P .
-. . .
-Q P .
-. . .
-Q . .
-. . .
-\"";]; List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. . .
-. . .
-. . .
-. . .
-Q Q Q
-\"";]; List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. Q .
-. . .
-. Q .
-. . .
-. Q .
-\"";]; List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. . Q
-. . .
-. Q .
-. . .
-Q . .
-\"";]
-] in
- let partylist1 = [
- List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. . .
-. . .
-. . .
-. . .
-. . .
-\"" ;
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. . .
-. . .
-. . .
-. . .
-\"" ;
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. . .
-. . .
-. . .
-P . .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. Q .
-. . .
-. . .
-P . .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. Q .
-. . .
-. . .
-P P .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. Q Q
-. . .
-. . .
-P P .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . P
-. . .
-. Q Q
-. . .
-. . .
-P P .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q Q P
-. . .
-. Q Q
-. . .
-. . .
-P P .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q Q P
-. . .
-. Q Q
-. . .
-. . .
-P P P
-\"";
- ]; List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. . .
-. . .
-. . .
-. . .
-. . .
-\"" ;
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. . .
-. . .
-. . .
-. . .
-\"" ;
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. . .
-. . .
-. . .
-P . .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. Q .
-. . .
-. . .
-P . .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. Q .
-. . .
-. . .
-P P .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. Q Q
-. . .
-. . .
-P P .
-\"";
-"[ | P:1 {}; Q:1 {} | ] \"
-Q . .
-. . .
-. Q Q
-. . .
-. . .
-P P P
-\"";
- ]
- ; List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. . .
-. . .
-. . .
-. . .
-P P P
-\"";]
- ; List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. P .
-. . .
-. P .
-. . .
-. P .
-\"";]
- ; List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. . P
-. . .
-. P .
-. . .
-P . .
-\"";]
-] in
-let tie = [
- List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. P .
-. . .
-. Q .
-. . .
-. P .
-\"";]
- ; List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. . Q
-. . .
-. P .
-. . .
-P . .
-\"";]
- ; List.map (struc_of_string ~diag:true) [
-"[ | P:1 {}; Q:1 {} | ] \"
-. . .
-. . P
-. . .
-. P .
-. . .
-Q . .
-\"";]
-] in
-assert_equal ~printer:(fun x -> x) ""
- ((LearnGame.learnFromParties ~win0:partylist0 ~win1:partylist1
- ~tie ~wrong:[]));
- );
+let get_strucs s =
+ let split_list ?(bound=None) pat s =
+ let r = Str.regexp_string pat in
+ match bound with None-> Str.split r s | Some b-> Str.bounded_split r s b in
+ 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 s = List.filter (fun s -> s <> "") (split_list "\n\n" st_s) in
+ List.map (fun s -> struc_of_string ~diag:true (pref ^ " \n\"" ^ s ^"\n\"")) s
-]
+let main () =
+ Aux.set_optimized_gc ();
+ let (testname, dir) = (ref "", ref "examples") in
+ let dbg_level i = (LearnGame.set_debug_level 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");
+ ("-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 (
+ 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 strucs_of_files fs =
+ List.map (fun fn -> get_strucs (AuxIO.input_fname fn)) 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
+ print_endline (LearnGame.learnFromParties ~win0 ~win1 ~notwon ~wrong)
+ ) else ignore (OUnit.run_test_tt ~verbose:true tests)
+
+let _ = AuxIO.run_if_target "LearnGameTest" main
Modified: trunk/Toss/Learn/Makefile
===================================================================
--- trunk/Toss/Learn/Makefile 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Learn/Makefile 2012-01-18 02:45:45 UTC (rev 1645)
@@ -12,11 +12,22 @@
DistinguishTest:
LearnGameTest:
-
tests:
make -C .. LearnTestsVerbose
+LearnGameTest.native:
+ make -C .. Learn/LearnGameTest.native
+
+%.learn:
+ make -C .. Learn/LearnGameTest.native
+ ../LearnGameTest.native -f $(basename $@)
+
+learntests:
+ make Tic-Tac-Toe001.learn
+ make Breakthrough001.learn
+
+
.PHONY: clean
clean:
Added: trunk/Toss/Learn/examples/Breakthrough001_01.nwn
===================================================================
--- trunk/Toss/Learn/examples/Breakthrough001_01.nwn (rev 0)
+++ trunk/Toss/Learn/examples/Breakthrough001_01.nwn 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,235 @@
+[ | B:1 {}; W:1 {} | ]
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..B B..B B..B
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W W..W W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..B B..B B..B
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... W.. ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..B ...B B..B
+ ... ... ... ...
+ ... ...B ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... W.. ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..B ...B B..B
+ ... ... ... ...
+ ... ...B ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+ ... W.. ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..B ...B B..B
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ...B ...
+ ... ... ... ...
+ ... W.. ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..B ...B B..B
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... W..B ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..B ...B B..B
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... W.. ...
+ ... ... ... ...
+ ... ...B ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..B ...B B..B
+ ... ... ... ...
+ ... ...W ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+ ... ...B ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..B ...B B..B
+ ... ... ... ...
+ ... ...W ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... B.. ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..W ...B B..B
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... B.. ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..W ...B B..B
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W B.. W..W W..
+... ... ... ...
+W..W W..W W..W W..W
+
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+B..B B..W ...B B..B
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W ...W W..W W..W
+
+
+ ... ... ... ...
+B B.. B..B B..B B..
+... ... ... ...
+B..B B..B ..B B..B
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W W.. W..W W..
+... ... ... ...
+W..W ...W W..W W..W
+
Added: trunk/Toss/Learn/examples/Breakthrough001_01.wn0
===================================================================
--- trunk/Toss/Learn/examples/Breakthrough001_01.wn0 (rev 0)
+++ trunk/Toss/Learn/examples/Breakthrough001_01.wn0 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,18 @@
+[ | B:1 {}; W:1 {} | ]
+
+ ... ... ... ...
+B B.. W.. B..B B..
+... ... ... ...
+B..B ... ... B..B
+ ... ... ... ...
+ ...B ... ... ...
+... ... ... ...
+... ... B..B ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W ... W..W W..
+... ... ... ...
+W..W ...W W..W W..W
Added: trunk/Toss/Learn/examples/Breakthrough001_01.wn1
===================================================================
--- trunk/Toss/Learn/examples/Breakthrough001_01.wn1 (rev 0)
+++ trunk/Toss/Learn/examples/Breakthrough001_01.wn1 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,18 @@
+[ | B:1 {}; W:1 {} | ]
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+... B..B ... ...
+ ... ... ... ...
+W ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W ... W..W ... ...
+... ... ... ...
+W.. ... ... W..
+ ... ... ... ...
+ ... ... ... W..
+... ... ... ...
+... ... B.. ...W
Added: trunk/Toss/Learn/examples/Breakthrough001_02.nwn
===================================================================
--- trunk/Toss/Learn/examples/Breakthrough001_02.nwn (rev 0)
+++ trunk/Toss/Learn/examples/Breakthrough001_02.nwn 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,18 @@
+[ | B:1 {}; W:1 {} | ]
+
+ ... ... ... ...
+B B.. ... B..B B..
+... ... ... ...
+B..B ... ... B..B
+ ... ... ... ...
+ ...B ... ... ...
+... ... ... ...
+... ... B..B ...
+ ... ... ... ...
+ ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W W..W ... W..W W..
+... ... ... ...
+W..W ...W W..W W..W
Added: trunk/Toss/Learn/examples/Breakthrough001_03.nwn
===================================================================
--- trunk/Toss/Learn/examples/Breakthrough001_03.nwn (rev 0)
+++ trunk/Toss/Learn/examples/Breakthrough001_03.nwn 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,18 @@
+[ | B:1 {}; W:1 {} | ]
+
+ ... ... ... ...
+B B..B B..B B..B B..
+... ... ... ...
+... B..B ... ...
+ ... ... ... ...
+W ... ... ... ...
+... ... ... ...
+... ... ... ...
+ ... ... ... ...
+W ... W..W ... ...
+... ... ... ...
+W.. ... ... W..
+ ... ... ... ...
+ ... ... ... W..
+... ... ... ...
+... ... ... ...W
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.nwn
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.nwn (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.nwn 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+P P Q
+. . .
+Q Q P
+. . .
+P Q Q
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.wn0
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.wn0 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.wn0 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,45 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. . .
+. . .
+. . .
+. . .
+. . .
+
+Q . .
+. . .
+. . .
+. . .
+. . .
+. . .
+
+Q . .
+. . .
+P . .
+. . .
+. . .
+. . .
+
+Q Q .
+. . .
+P . .
+. . .
+. . .
+. . .
+
+Q Q .
+. . .
+P P .
+. . .
+. . .
+. . .
+
+
+Q Q Q
+. . .
+P P .
+. . .
+. . .
+. . .
+
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.wn1
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.wn1 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_01.wn1 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,64 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. . .
+. . .
+. . .
+. . .
+. . .
+
+Q . .
+. . .
+. . .
+. . .
+. . .
+. . .
+
+Q . .
+. . .
+. . .
+. . .
+. . .
+P . .
+
+Q . .
+. . .
+. Q .
+. . .
+. . .
+P . .
+
+Q . .
+. . .
+. Q .
+. . .
+. . .
+P P .
+
+Q . .
+. . .
+. Q Q
+. . .
+. . .
+P P .
+
+Q . P
+. . .
+. Q Q
+. . .
+. . .
+P P .
+
+Q Q P
+. . .
+. Q Q
+. . .
+. . .
+P P .
+
+Q Q P
+. . .
+. Q Q
+. . .
+. . .
+P P P
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.nwn
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.nwn (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.nwn 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+Q P P
+. . .
+P Q Q
+. . .
+Q Q P
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.wn0
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.wn0 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.wn0 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,43 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. . .
+. . .
+. . .
+. . .
+. . .
+
+Q . .
+. . .
+. . .
+. . .
+. . .
+. . .
+
+Q P .
+. . .
+. . .
+. . .
+. . .
+. . .
+
+Q P .
+. . .
+Q . .
+. . .
+. . .
+. . .
+
+Q P .
+. . .
+Q P .
+. . .
+. . .
+. . .
+
+Q P .
+. . .
+Q P .
+. . .
+Q . .
+. . .
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.wn1
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.wn1 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_02.wn1 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,50 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. . .
+. . .
+. . .
+. . .
+. . .
+
+Q . .
+. . .
+. . .
+. . .
+. . .
+. . .
+
+Q . .
+. . .
+. . .
+. . .
+. . .
+P . .
+
+Q . .
+. . .
+. Q .
+. . .
+. . .
+P . .
+
+Q . .
+. . .
+. Q .
+. . .
+. . .
+P P .
+
+Q . .
+. . .
+. Q Q
+. . .
+. . .
+P P .
+
+Q . .
+. . .
+. Q Q
+. . .
+. . .
+P P P
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.nwn
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.nwn (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.nwn 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. . .
+. . .
+. P .
+. . .
+P . P
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.wn0
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.wn0 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.wn0 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. . .
+. . .
+. . .
+. . .
+Q Q Q
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.wn1
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.wn1 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_03.wn1 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. . .
+. . .
+. . .
+. . .
+P P P
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.nwn
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.nwn (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.nwn 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+P . P
+. . .
+. P .
+. . .
+. . .
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.wn0
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.wn0 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.wn0 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,9 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. Q .
+. . .
+. Q .
+. . .
+. Q .
+
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.wn1
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.wn1 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_04.wn1 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. P .
+. . .
+. P .
+. . .
+. P .
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.nwn
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.nwn (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.nwn 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. . P
+. . .
+. P .
+. . .
+. P .
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.wn0
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.wn0 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.wn0 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. . Q
+. . .
+. Q .
+. . .
+Q . .
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.wn1
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.wn1 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_05.wn1 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+. . P
+. . .
+. P .
+. . .
+P . .
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_06.wn0
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_06.wn0 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_06.wn0 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+Q . .
+. . .
+. Q .
+. . .
+. . Q
Added: trunk/Toss/Learn/examples/Tic-Tac-Toe001_06.wn1
===================================================================
--- trunk/Toss/Learn/examples/Tic-Tac-Toe001_06.wn1 (rev 0)
+++ trunk/Toss/Learn/examples/Tic-Tac-Toe001_06.wn1 2012-01-18 02:45:45 UTC (rev 1645)
@@ -0,0 +1,8 @@
+[ | P:1 {}; Q:1 {} | ]
+
+. . .
+P . .
+. . .
+. P .
+. . .
+. . P
Modified: trunk/Toss/Server/Makefile
===================================================================
--- trunk/Toss/Server/Makefile 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Server/Makefile 2012-01-18 02:45:45 UTC (rev 1645)
@@ -3,7 +3,6 @@
%Test:
make -C .. Server/$@Verbose
-PictureTest:
ReqHandlerTest:
LearnGameTest:
Deleted: trunk/Toss/Server/Picture.ml
===================================================================
--- trunk/Toss/Server/Picture.ml 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Server/Picture.ml 2012-01-18 02:45:45 UTC (rev 1645)
@@ -1,410 +0,0 @@
-(* Processing Pictures to create Structures *)
-
-let debug_level = ref 0
-let set_debug_level i = (debug_level := i;)
-
-
-(* --------- Basic Picture Functions --------- *)
-
-type picture = (int * int * int) array array
-
-(* Read a picture from a scanning buffer. *)
-let read_pic buf =
- let (width, height) = Scanf.bscanf buf "P3 %d %d 255" (fun x y -> (x, y)) in
- let pic = Array.make_matrix width height (0, 0, 0) in
- for j = 0 to height-1 do
- for i = 0 to width-1 do
- pic.(i).(j) <- Scanf.bscanf buf " %d %d %d" (fun x y z -> (x, y, z))
- done
- done;
- pic
-
-(* Print a matrix to the formatter [f], use [elem_f] for elements. *)
-let fprint_matrix f elem_f start mid m =
- let (width, height) = (Array.length m, Array.length (m.(0))) in
- Format.fprintf f "%s %d %d %s\n%!" start width height mid;
- for j = 0 to height-1 do
- for i = 0 to width-1 do
- Format.fprintf f "%a" elem_f m.(i).(j);
- done;
- Format.fprintf f "\n%!";
- done
-
-(* Print a picture in the simple PPM format to a formatter. *)
-let fprint_pic f pic =
- let pr fmt (a, b, c) = Format.fprintf fmt " %d %d %d\n" a b c in
- fprint_matrix f pr "P3" "255" pic
-
-(* Print a picture in the simple PPM format to standard output. *)
-let print_pic pic = fprint_pic Format.std_formatter pic
-
-
-(* Flip a picture. *)
-let flip pic =
- let (width, height) = (Array.length pic, Array.length (pic.(0))) in
- let flpic = Array.make_matrix height width (0, 0, 0) in
- for i = 0 to width-1 do
- for j = 0 to height-1 do
- flpic.(j).(i) <- pic.(i).(j)
- done
- done;
- flpic
-
-
-(* Cut a picture to the given rectangle. *)
-let cut (x1, y1) (x2, y2) pic =
- let (orig_w, orig_h) = (Array.length pic, Array.length (pic.(0))) in
- let x2 = if x2 <= 0 then orig_w + x2 - 1 else x2 in
- let y2 = if y2 <= 0 then orig_h + y2 - 1 else y2 in
- if x2 < x1+1 || y2 < y1+1 || orig_w<x2+1 || orig_h<y2+1 || x1<0 || y1<0 then
- failwith (Printf.sprintf "cut: wrong dimensions %i %i %i %i" x1 x2 y1 y2);
- let cutpic = Array.make_matrix (x2-x1+1) (y2-y1+1) (0, 0, 0) in
- for i = 0 to x2-x1 do
- for j = 0 to y2-y1 do
- cutpic.(i).(j) <- pic.(i+x1).(j+y1)
- done
- done;
- cutpic
-
-
-(* Apply the filter function [f] to each pixel in a picture. *)
-let apply_filter f pic =
- let (width, height) = (Array.length pic, Array.length (pic.(0))) in
- let fpic = Array.make_matrix width height (0, 0, 0) in
- for i = 0 to width-1 do
- for j = 0 to height-1 do
- fpic.(i).(j) <- f i j width height pic
- done
- done;
- fpic
-
-
-(* ------------ Change Detection ------------ *)
-
-let diff_filter maxdiff (distx, disty) x y w h pic =
- let res = ref false in
- for i = -distx to distx do
- for j = -disty to disty do
- if x+i >= 0 && x+i < w && y+j >= 0 && y+j < h then
- let (r1, g1, b1) = pic.(x).(y) in
- let (r2, g2, b2) = pic.(x+i).(y+j) in
- let (rd, gd, bd) = maxdiff in
- if rd >= abs (r1-r2) && gd >= abs (g1-g2) && bd >= abs (b1-b2) then
- res := false
- else res := true
- done
- done;
- if !res then (255, 255, 255) else (0, 0, 0)
-
-(* Calculate color difference, accept maxdiff differences up to dist. *)
-let diff ?(maxdiff=(1,1,1)) ?(dist=(1,1)) =
- apply_filter (diff_filter maxdiff dist)
-
-
-(* ------------ Simple Segmentation ------------ *)
-
-let all_in_color cl ((x1, y1), (x2, y2)) pic =
- let (w, h) = (Array.length pic, Array.length (pic.(0))) in
- if x2 < x1 || y2 < y1 || w < x2+1 || h < y2+1 || x1 < 0 || y1 < 0 then
- failwith (Printf.sprintf "all_in_color: wrong dim %i %i %i %i" x1 y1 x2 y2);
- let res = ref true in
- for i = x1 to x2 do
- for j = y1 to y2 do
- if pic.(i).(j) <> cl then res := false
- done
- done;
- !res
-
-let rec next_x cl i j w h pic =
- if pic.(i).(j) = cl then (i, j) else
- if i+1 < w then next_x cl (i+1) j w h pic else raise Not_found
-
-let rec next_y cl i j w h pic =
- if pic.(i).(j) = cl then (i, j) else
- if j+1 < h then next_y cl i (j+1) w h pic else raise Not_found
-
-let next_color cl i j w h pic =
- try
- let (i1, _) = next_x cl i j w h pic in
- if i1+1 < w && pic.(i1+1).(j) = cl then (i1, j) else raise Not_found
- with Not_found ->
- let (_, j1) = next_y cl 0 (j+1) w h pic in
- if j1+1 < h && pic.(i).(j1+1) = cl then (0, j1) else raise Not_found
-
-(* Make a row-first column-next black-white tour of a picture. *)
-let bw_tour pic =
- let (width, height) = (Array.length pic, Array.length (pic.(0))) in
- let (i, j, newi, newj) = (ref 0, ref 0, ref 0, ref 0) in
- let (rects, intv) = (ref [], ref []) in
- try
- while true do
- intv := [];
- while !j = !newj do
- let (ni, nj) = next_color (0, 0, 0) !i !j width height pic in
- newi := ni;
- let (nni, nnj) = next_color (255, 255, 255) ni nj width height pic in
- if nnj = !j then intv := (ni, nni-1) :: !intv;
- i := nni; j := !newj; newj := nnj;
- done;
- if !intv != [] then intv := (!newi, width-1) :: !intv;
- rects := (List.map (fun v-> v, (!j,!newj-1)) !intv) @ !rects;
- j := !newj; i := 0
- done;
- failwith "bw_tour: unreachable"
- with Not_found ->
- if !intv != [] then intv := (!newi, width-1) :: !intv;
- rects := (List.map (fun v-> v, (!j,height-1)) !intv) @ !rects;
- List.rev_map (fun ((a, b), (c, d)) -> (a, c), (b, d)) !rects
-
-let rect_dist ((x1, y1), (x2, y2)) ((a1, b1), (a2, b2)) pic =
- let (w, h, d) = (min (x2-x1) (a2-a1), min (y2-y1) (b2-b1), ref 0) in
- for i = 0 to w-1 do
- for j = 0 to h-1 do
- let (x, y, z), (a, b, c) = pic.(x1+i).(y1+j), pic.(a1+i).(b1+j) in
- d := !d + (abs (x-a)) + (abs (y-b)) + (abs (z-c))
- done
- done;
- (float !d) /. (float (w*h))
-
-let rect_dist_offset (x, y) ((x1, y1), (x2, y2)) ((a1, b1), (a2, b2)) pic =
- rect_dist ((x1+x, y1+y), (x2+x, y2+y)) ((a1+x, b1+y), (a2+x, b2+y)) pic
-
-(* Very basic picture segmentation, should work for grids. *)
-let segment offset threshold pic =
- let df = diff (cut (offset, offset) (-offset, -offset) pic) in
- let rects = bw_tour df in
- let assign_name (dict, i, bi) rect =
- let (a, b), (c, d) = rect in
- try
- let (r, n) =
- List.find (fun (r,_) ->
- rect_dist_offset (offset, offset) r rect pic < threshold) dict in
- if !debug_level > 0 then
- Printf.printf " (%i, %i) - (%i, %i) %s found \n%!" a b c d n;
- ((rect, n) :: dict, i, bi)
- with Not_found ->
- if all_in_color (0, 0, 0) rect df then (
- let n = Printf.sprintf "B%i" bi in
- if !debug_level > 0 then
- Printf.printf " (%i, %i) - (%i, %i) %s assigned \n%!" a b c d n;
- ((rect, n) :: dict, i, bi+1)
- ) else (
- let n = Printf.sprintf "P%i" i in
- if !debug_level > 0 then
- Printf.printf " (%i, %i) - (%i, %i) %s assigned \n%!" a b c d n;
- ((rect, n) :: dict, i+1, bi)
- ) in
- let (res, _, _) = List.fold_left assign_name ([], 1, 0) rects in
- List.rev res
-
-
-(* ------------- Structure from Segmented Data ------------ *)
-
-(* Create a structure from segmented data. *)
-let make_struc dict =
- let (prev_ys, prev_xs, maxdx, maxdy) =
- (ref (0, 0), ref (0, 0), ref 0, ref 0) in
- let add_el (struc, i, j) (((x1, y1), (x2, y2)), pred) =
- let (ni, nj) =
- if (y1, y2) = !prev_ys then (
- maxdx := max !maxdx (abs ((fst !prev_xs) - x1));
- prev_xs := (x1, x2);
- (i+1, j)
- ) else (
- maxdy := max !maxdy (abs ((fst !prev_ys) - y1));
- prev_xs := (x1, x2);
- prev_ys := (y1, y2);
- (1, j+1)
- ) in
- let name = try Structure.board_coords_name (ni, nj) with Not_found ->
- Printf.sprintf "e%i,%i" ni nj in
- let (s1, elem) = Structure.add_new_elem struc ~name () in
- let s2 = Structure.add_fun s1 "x" (elem, float (x1+x2) /. 2.) in
- let s3 = Structure.add_fun s2 "y" (elem, float (y1+y2) /. (2.)) in
- let s4 = Structure.add_fun s3 "x1" (elem, float x1) in
- let s5 = Structure.add_fun s4 "y1" (elem, float y1) in
- let s6 = Structure.add_fun s5 "x2" (elem, float x2) in
- let s7 = Structure.add_fun s6 "y2" (elem, float y2) in
- let s8 = Structure.add_fun s7 "vx" (elem, 0.) in
- let new_s = Structure.add_fun s8 "vy" (elem, 0.) in
- if pred = "B0" then (new_s, ni, nj) else
- (Structure.add_rel new_s pred [|elem|], ni, nj) in
- let (s, _, _) =
- List.fold_left add_el (Structure.empty_structure (), 1, 0) dict in
- (s, !maxdx, !maxdy)
-
-
-(* Minimal type of elements in a structure which is part-positive. *)
-let postp s rels els =
- let app_rel_phi (st, fos, vs, i) e =
- let r, v = "Elem" ^ (string_of_int i), Structure.elem_name st e in
- (Structure.add_rel st r [|e|],
- Formula.Rel (r, [|Formula.fo_var_of_string v|]) :: fos, v :: vs, i+1) in
- let (struc, els_phis, vars, _) = List.fold_left app_rel_phi (s,[],[],0) els in
- let neg_true = function Formula.Not _ -> Formula.And [] | x -> x in
- let pos phi =
- Formula.flatten (FormulaMap.map_to_literals neg_true (fun x->x) phi) in
- let pos_ok phi = let psi = pos phi in if psi = Formula.And [] then false else
- Solver.M.check struc (Formula.And (psi :: els_phis)) in
- let ts = List.map pos (FormulaOps.mintp pos_ok rels vars) in
- let tfvs = List.map (fun f-> (f,List.length (FormulaSubst.free_vars f))) ts in
- let maxfv = List.fold_left (fun m (_, x) -> max m x) 0 tfvs in
- List.map fst (List.filter (fun (f, x) -> x = maxfv) tfvs)
-
-let tp_rule drels (left, right, delems) =
- let not_drel (r,_) = not (List.mem r drels) in
- let crels = List.filter not_drel (Structure.rel_signature left) in
- if !debug_level > 0 then Printf.printf "CRels %i\n%!" (List.length crels);
- let tp = postp left crels delems in
- if !debug_level > -1 then
- Format.eprintf "@[%a@]@ \n%!" Formula.fprint (Formula.And tp);
- let cut s = List.fold_left Structure.del_elem s
- (List.filter (fun e -> not (List.mem e delems)) (Structure.elements s)) in
- (cut left, cut right, tp)
-
-let geom_rule drels (left, right, delems) =
- let get_dim s e = (Structure.fun_val s "x" e, Structure.fun_val s "y" e) in
- let rect s els =
- let upd_rect (x1, y1, x2, y2) e =
- let (x, y) = get_dim s e in (min x1 x, min y1 y, max x2 x, max y2 y) in
- let (x, y) = get_dim s (List.hd els) in
- List.fold_left upd_rect (x, y, x, y) (List.tl els) in
- let in_rect s (x1, y1, x2, y2) e =
- let (x, y) = get_dim s e in (x1 < x && x < x2 && y1 < y && y < y2) in
- let (x1, y1, x2, y2) = rect left delems in
- let r = (x1 -. 0.5, y1 -. 0.5, x2 +. 0.5, y2 +. 0.5) in
- let els = List.filter (in_rect left r) (Structure.elements left) in
- let new_els = List.filter (fun e -> not (List.mem e delems)) els in
- if !debug_level > 0 then
- Format.printf "%s\n%!" (String.concat ", " (List.map string_of_int els));
- let cut s = List.fold_left Structure.del_elem s
- (List.filter (fun e -> not (List.mem e els)) (Structure.elements s)) in
- let is_unary r = List.assoc r (Structure.rel_signature left) = 1 in
- let un_drels = Aux.unique_sorted (List.filter is_unary drels) in
- let delopt s r = Structure.del_rels s r (List.map (fun e -> [|e|]) new_els) in
- let delopts s = List.fold_left delopt s un_drels in
- (delopts (cut left), delopts (cut right), delems)
-
-let addopts drels (left, right, delems) =
- let is_unary r = List.assoc r (Structure.rel_signature left) = 1 in
- let un_drels = Aux.unique_sorted (List.filter is_unary drels) in
- let un_opt_drels = List.map (fun r -> "_opt_" ^ r) un_drels in
- let els = Structure.elements left in
- let new_els = List.filter (fun e -> not (List.mem e delems)) els in
- let addopt s r = Structure.add_rels s r (List.map (fun e -> [|e|]) new_els) in
- let addoptrels s = List.fold_left addopt s un_opt_drels in
- (addoptrels left, addoptrels right, [])
-
-let print_rule emb (name, (l, r, pre_l)) =
- let emb_s = String.concat ", " (Aux.unique_sorted emb) in
- let pre_s = Formula.sprint (Formula.And pre_l) in
- let sprints () s = Structure.sprint s in
- Format.sprintf "RULE %s:@ @[<2>%a@]@ ->@ @[<2>%a@]@ emb %s pre %s"
- name sprints l sprints r emb_s pre_s
-
-let formula_of_string s =
- FormulaParser.parse_formula Lexer.lex (Lexing.from_string s)
-
-let read_strucs rels offset threshold gname suffix =
- let get_struc fn =
- let pic = read_pic (Scanf.Scanning.from_file fn) in
- let (struc, dx, dy) = make_struc (segment offset threshold pic) in
- let formula_r = formula_of_string (Printf.sprintf (
- ":y(a) = :y(b) and :x(a) < :x(b) and :x(b) < :x(a) + %i.8") dx) in
- let formula_c = formula_of_string (Printf.sprintf (
- ":x(a) = :x(b) and :y(b) < :y(a) and :y(a) < :y(b) + %i.8") dy) in
- let row, col = ("R", ["a"; "b"], formula_r), ("C", ["a"; "b"], formula_c) in
- Arena.add_def_rels struc (row :: col :: rels) in
- let name i = Printf.sprintf "%s%s%02i.ppm" gname suffix i in
- let (strucs, i) = (ref [], ref 0) in
- while Sys.file_exists (name !i) do
- strucs := get_struc (name !i) :: !strucs; incr i;
- done;
- List.rev !strucs
-
-let make_cond drels (right, wrong, delem_rels) =
- let sg = Structure.rel_signature right in
- let is_unary r = List.assoc r sg = 1 in
- let name e = Structure.elem_name right e in
- let mk_atom e r = Formula.Rel (r, [|Formula.fo_var_of_string (name e)|]) in
- let preds (e, rels) =
- Formula.And (List.map (mk_atom e) (List.filter is_unary rels)) in
- let ex_var (e, _) = Formula.var_of_string (name e) in
- let ex_vars = List.map ex_var delem_rels in
- let basic = Formula.flatten (Formula.And (
- (List.fold_left (fun l x -> (preds x) :: l) [] delem_rels))) in
- if not (Solver.M.check wrong basic) then Formula.Ex (ex_vars, basic) else (
- let app_s s =
- let app_rel_phi (st, arels, fos, vs, i) (e, _) =
- let r, v = "Elem" ^ (string_of_int i), Structure.elem_name st e in
- (Structure.add_rel st r [|e|], (r, 1) :: arels,
- Formula.Rel (r, [|Formula.fo_var_of_string v|]) :: fos, v::vs, i+1) in
- List.fold_left app_rel_phi (s, [], [], [], 0) delem_rels in
- let (right_el, arels, afos, vars, _) = app_s right in
- let (wrong_el, _, _, _, _) = app_s wrong in
- let csg = List.filter (fun (r,_) -> not (List.mem r drels)) sg in
- let ok phi_in =
- let phi = Formula.And [phi_in; basic] in
- let psi = Formula.And (phi :: afos) in
- Solver.M.check right_el psi && not (Solver.M.check wrong_el phi) in
- let w = FormulaOps.mintp ok csg vars in
- let minimize phi =
- let atoms = FormulaMap.get_atoms phi in
- let subst_atom a b x = if x = a then b else x in
- let phi0 f a = FormulaMap.map_to_atoms (subst_atom a (Formula.Or[])) f in
- let phi1 f a = FormulaMap.map_to_atoms (subst_atom a (Formula.And[])) f in
- let mini f a =
- let (f0, f1) = (phi0 f a, phi1 f a) in
- Formula.flatten (if ok f0 then f0 else if ok f1 then f1 else f) in
- List.fold_left mini phi atoms in
- let mw = List.map minimize w in
- if !debug_level > -1 then
- Format.eprintf "@[%a@]@ \n%!" Formula.fprint (Formula.And (basic :: mw));
- if !debug_level > -1 then
- Format.eprintf "@[%a@]@ \n%!" Formula.fprint
- (Aux.unsome (Distinguish.distinguish_upto ~qr:1 ~k:2 [right] [wrong]));
- Formula.flatten (Formula.Ex (ex_vars, Formula.And (basic :: mw)))
- )
-
-(* Make a game from sequence of pictures. *)
-let make_game ?(rels=[]) ?(offset=2) ?(threshold=70.) ?(types=false) fname =
- let flen = String.length fname in
- let gname = if (flen > 6 && fname.[flen-4] = '.') then
- String.sub fname 0 (flen - 6) else fname in
- let seq = read_strucs rels offset threshold gname "" in
- let win1s = Array.of_list (read_strucs rels offset threshold gname "Win1") in
- let win2s = Array.of_list (read_strucs rels offset threshold gname "Win2") in
- if seq = [] then failwith "Empty picture sequence for game play.";
- if !debug_level > 0 then Printf.printf "Read %i move pics, %i+%i win.\n%!"
- (List.length seq) (Array.length win1s) (Array.length win2s);
- let diff_struc (s, drels, prev) cur =
- let dels, drels_l = List.split (Structure.diff_elems prev cur) in
- ((prev, cur, dels) :: s, (List.concat drels_l) @ drels, cur) in
- if !debug_level > 0 then Printf.printf "Diffstrucs computed.\n%!";
- let (s, dr,_) = List.fold_left diff_struc ([],[],List.hd seq) (List.tl seq) in
- let rules_geom = List.rev_map (geom_rule dr) s in
- let rules = if not types then List.map (addopts dr) rules_geom else
- List.map (tp_rule dr) rules_geom in
- let wi i =
- formula_of_string (if i mod 2 = 0 then "not Win2()" else "not Win1()") in
- let add_win i (l, r, pre) = (Printf.sprintf "Mv%i" i, (l, r, (wi i)::pre)) in
- let wrs = Array.mapi (fun i r -> add_win i r) (Array.of_list rules) in
- let emb = List.map fst (Structure.rel_signature (List.hd seq)) @ dr in
- let rs = String.concat "\n" (List.map (print_rule emb) (Array.to_list wrs)) in
- let allms = Array.to_list (Array.mapi (fun i _ -> i) (Array.of_list rules)) in
- let (mvi1, mvi2) = List.partition (fun i -> i mod 2 = 0) allms in
- let make_mv loc i = Printf.sprintf "[Mv%i -> %s]" i loc in
- let mvs1 = String.concat "; " (List.map (make_mv "1") mvi1) in
- let mvs2 = String.concat "; " (List.map (make_mv "0") mvi2) in
- let pay1 = "PAYOFF :(Win1()) - :(Win2())" in
- let pay2 = "PAYOFF :(Win2()) - :(Win1())" in
- let loc0 = Printf.sprintf "LOC 0 {\n PLAYER 1 { %s }\n PLAYER 2 { %s }\n}"
- (pay1 ^ "\n MOVES " ^ mvs1 ^ "\n") pay2 in
- let loc1 = Printf.sprintf "LOC 1 {\n PLAYER 1 { %s }\n PLAYER 2 { %s }\n}"
- pay1 (pay2 ^ "\n MOVES " ^ mvs2 ^ "\n") in
- let model_s = Structure.sprint (List.hd seq) in
- let dws a i = (a.(2*i+1), a.(2*i), Structure.diff_elems a.(2*i+1) a.(2*i)) in
- let (win1, win2) = (make_cond dr (dws win1s 0), make_cond dr (dws win2s 0)) in
- let beg = Printf.sprintf "PLAYERS 1, 2\nREL Win1() = %s\nREL Win2() = %s"
- (Formula.sprint win1) (Formula.sprint win2) in
- Printf.sprintf "%s\n%s\n%s\n%s\nMODEL\n%s\n" beg rs loc0 loc1 model_s
Deleted: trunk/Toss/Server/Picture.mli
===================================================================
--- trunk/Toss/Server/Picture.mli 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Server/Picture.mli 2012-01-18 02:45:45 UTC (rev 1645)
@@ -1,54 +0,0 @@
-(** Processing pictures to create structures *)
-
-(** {2 Debugging} *)
-
-val set_debug_level : int -> unit
-
-
-(** {2 Basic Picture Functions} *)
-
-type picture = (int * int * int) array array
-
-
-(** Read a picture from a scanning buffer. *)
-val read_pic : Scanf.Scanning.scanbuf -> picture
-
-(** Print a picture in the simple PPM format to a formatter. *)
-val fprint_pic : Format.formatter -> picture -> unit
-
-(** Print a picture in the simple PPM format to standard output. *)
-val print_pic : picture -> unit
-
-(** Flip a picture. *)
-val flip : picture -> picture
-
-(** Cut a picture to the given rectangle. *)
-val cut : int * int -> int * int -> picture -> picture
-
-(** Apply the filter function [f] to each pixel in a picture. *)
-val apply_filter : (int -> int -> int -> int -> picture -> int * int * int) ->
- picture -> picture
-
-
-(** {2 Change Detection} *)
-
-(** Calculate color difference, accept maxdiff differences up to dist. *)
-val diff : ?maxdiff: int * int * int -> ?dist: int * int -> picture -> picture
-
-
-(** {2 Simple Segmentation} *)
-
-(** Very basic picture segmentation, should work for grids. *)
-val segment : int -> float -> picture ->
- (((int * int) * (int * int)) * string) list
-
-
-(** {2 Structure from Segmented Data} *)
-
-(** Create a structure from segmented data. *)
-val make_struc : (((int * int) * (int * int)) * string) list ->
- Structure.structure * int * int
-
-(** Create a game from sequence of images. *)
-val make_game : ?rels : (string * string list * Formula.formula) list ->
- ?offset : int -> ?threshold : float -> ?types : bool -> string -> string
Deleted: trunk/Toss/Server/PictureTest.ml
===================================================================
--- trunk/Toss/Server/PictureTest.ml 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Server/PictureTest.ml 2012-01-18 02:45:45 UTC (rev 1645)
@@ -1,51 +0,0 @@
-open OUnit
-
-Picture.set_debug_level 0
-
-let tests = "Picture" >::: [
- "segmentation size for breakthrough" >::
- (fun () ->
- let fname = "./www/img/Breakthrough.ppm" in
- let pic = Picture.read_pic (Scanf.Scanning.from_file fname) in
- let seg = Picture.segment 2 70. pic in
- assert_equal ~printer:string_of_int 64 (List.length seg)
- );
-
- "breakthrough structure P1 size" >::
- (fun () ->
- let fname = "./www/img/Breakthrough.ppm" in
- let pic = Picture.read_pic (Scanf.Scanning.from_file fname) in
- let seg = Picture.segment 2 70. pic in
- let (struc, _, _) = Picture.make_struc seg in
- assert_equal ~printer:string_of_int 16 (Structure.rel_size struc "P1")
- );
-]
-
-
-let main () =
- Aux.set_optimized_gc ();
- let (file, game, use_types) = (ref "", ref "", ref false) in
- let dbg_level i = (Picture.set_debug_level 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 -> file := s), "process file");
- ("-g", Arg.String (fun s -> game := s), "process files for a game");
- ("-tp", Arg.Unit (fun () -> use_types := true), "use formulas in rules");
- ] in
- Arg.parse opts (fun _ -> ()) "Try -help for help or one of the following.";
- if !file <> "" then (
- let pic = Picture.read_pic (Scanf.Scanning.from_file !file) in
- let (struc, dx, dy) = Picture.make_struc (Picture.segment 2 70. pic) in
- let formula_r = Printf.sprintf
- ":y(a) = :y(b) and :x(a) < :x(b) and :x(b) < :x(a) + %i.8" dx in
- let formula_c = Printf.sprintf
- ":x(a) = :x(b) and :y(b) < :y(a) and :y(a) < :y(b) + %i.8" dy in
- Printf.printf "MODEL \n %s \n with \n R(a, b) = %s;\n C(a, b) = %s\n\n%!"
- (Structure.sprint struc) formula_r formula_c;
- ) else if !game <> "" then (
- print_endline (Picture.make_game ~types:!use_types !game)
- ) else ignore (OUnit.run_test_tt ~verbose:true tests)
-
-
-let _ = AuxIO.run_if_target "PictureTest" main
Modified: trunk/Toss/Server/ReqHandler.ml
===================================================================
--- trunk/Toss/Server/ReqHandler.ml 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Server/ReqHandler.ml 2012-01-18 02:45:45 UTC (rev 1645)
@@ -688,7 +688,7 @@
let plays_int = List.map (fun (a, b) -> (int_of_string a, b)) plays in
let (w0, other) = List.partition (fun (_, b) -> b = "0") plays_int in
let (w1, other) = List.partition (fun (_, b) -> b = "1") other in
- let (tie, other) = List.partition (fun (_, b) -> b = "2") other in
+ let (notwon, other) = List.partition (fun (_, b) -> b = "2") other in
let (wrong, _) = List.partition (fun (_, b) -> b = "3") other in
(* Get the play with given id from DB - as a sequence of structures. *)
let playFromDB pid =
@@ -702,24 +702,24 @@
game [source]: [wins0] which are now supposed to be won by Player 0,
[wins1] - now won by Player 1, [tie] - now a tie, and [wrong] which
are not correct plays of the newly constructed game. *)
- let learnFromDB source wins0 wins1 tie wrong =
+ let learnFromDB source wins0 wins1 nw wrong =
if !debug_level > 0 then (
let pl l = String.concat ", " (List.map string_of_int l) in
print_endline ("Learning from "^ source ^" w0: "^ (pl wins0) ^" w1: "^
- (pl wins1)^" tie: "^(pl tie) ^" wrong: "^ (pl wrong));
+ (pl wins1)^" notwon: "^(pl nw)^" wrong: "^(pl wrong));
);
- let (wins0, wins1, tie, wrong) =
+ let (wins0, wins1, notwon, wrong) =
(List.map playFromDB wins0, List.map playFromDB wins1,
- List.map playFromDB tie, List.map playFromDB wrong) in
+ List.map playFromDB nw, List.map playFromDB wrong) in
let struc_of_string s =
StructureParser.parse_structure Lexer.lex (Lexing.from_string s) in
LearnGame.learnFromParties
~win0:(List.map (List.map struc_of_string) wins0)
~win1:(List.map (List.map struc_of_string) wins1)
- ~tie:(List.map (List.map struc_of_string) tie)
+ ~notwon:(List.map (List.map struc_of_string) notwon)
~wrong:(List.map (List.map struc_of_string) wrong) in
learnFromDB game (List.map fst w0) (List.map fst w1)
- (List.map fst tie) (List.map fst wrong) in
+ (List.map fst notwon) (List.map fst wrong) in
let (tcmd, data) = split_two "#" msg in
let resp, new_cookies = match tcmd with
| "USERNAME" ->
Modified: trunk/Toss/Server/Tests.ml
===================================================================
--- trunk/Toss/Server/Tests.ml 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/Server/Tests.ml 2012-01-18 02:45:45 UTC (rev 1645)
@@ -42,11 +42,10 @@
let learn_tests = "Learn", [
"DistinguishTest", [DistinguishTest.tests; DistinguishTest.bigtests];
- "LearnGameTest", [LearnGameTest.tests; LearnGameTest.bigtests];
+ "LearnGameTest", [LearnGameTest.tests];
]
let server_tests = "Server", [
- "PictureTest", [PictureTest.tests];
"ReqHandlerTest", [ReqHandlerTest.tests];
]
Modified: trunk/Toss/WebClient/Main.js
===================================================================
--- trunk/Toss/WebClient/Main.js 2012-01-17 23:33:40 UTC (rev 1644)
+++ trunk/Toss/WebClient/Main.js 2012-01-18 02:45:45 UTC (rev 1645)
@@ -249,7 +249,7 @@
'<option class="play_select_opt" value="-1">skip</option>' +
'<option class="play_select_opt" value="0">wins0</option>' +
'<option class="play_select_opt" value="1">wins1</option>' +
- '<option class="play_select_opt" value="2">tie</option>' +
+ '<option class="play_select_opt" value="2">notwon</option>' +
'<option class="play_select_opt" value="3">wrong</option></select>';
} else {
li.innerHTML = bs;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|