[Toss-devel-svn] SF.net SVN: toss:[1347] trunk/Toss
Status: Beta
Brought to you by:
lukaszkaiser
|
From: <luk...@us...> - 2011-03-07 23:33:24
|
Revision: 1347
http://toss.svn.sourceforge.net/toss/?rev=1347&view=rev
Author: lukaszkaiser
Date: 2011-03-07 23:33:14 +0000 (Mon, 07 Mar 2011)
Log Message:
-----------
Debugging GameTree, using timeout in Client.
Modified Paths:
--------------
trunk/Toss/Client/SystemDisplay.py
trunk/Toss/Client/Wrapper.py
trunk/Toss/Formula/Aux.ml
trunk/Toss/Formula/Aux.mli
trunk/Toss/Play/GameTree.ml
trunk/Toss/Play/GameTreeTest.ml
trunk/Toss/Server/Server.ml
trunk/Toss/TossFullTest.ml
Modified: trunk/Toss/Client/SystemDisplay.py
===================================================================
--- trunk/Toss/Client/SystemDisplay.py 2011-03-07 12:43:57 UTC (rev 1346)
+++ trunk/Toss/Client/SystemDisplay.py 2011-03-07 23:33:14 UTC (rev 1347)
@@ -43,10 +43,10 @@
suggest_bt = self.toolbar.addAction (QIcon(":/pics/move.svg"),"Hint")
QObject.connect(suggest_bt, SIGNAL("triggered ()"), self.suggest)
- self.__sg_iters = 2
- dp = self.system.get_data("depth")
- if dp != "none": self.__sg_iters = int(dp)
- self.sg_iters_bt = self.toolbar.addAction ("Depth: " +
+ self.__sg_iters = 5
+ #dp = self.system.get_data("depth")
+ #if dp != "none": self.__sg_iters = int(dp)
+ self.sg_iters_bt = self.toolbar.addAction ("Timeout: " +
str(self.__sg_iters))
QObject.connect(self.sg_iters_bt, SIGNAL("triggered ()"),
self.set_sg_iters)
@@ -153,11 +153,11 @@
self.run_iters_bt.setText ("Length: " + str(ri))
def set_sg_iters (self):
- (si, ok) = QInputDialog.getInt (self, "Game Tree Depth to Analyze",
- "Set depth of suggestions: ",
- self.__sg_iters, 1, 10, 1)
+ (si, ok) = QInputDialog.getInt (self, "Hint Timeout",
+ "Set hint timeout (seconds): ",
+ self.__sg_iters, 2, 1000, 1)
self.__sg_iters = si
- self.sg_iters_bt.setText ("Depth: " + str(si))
+ self.sg_iters_bt.setText ("Timeout: " + str(si))
def set_adv_ratio (self):
(ar, ok) = QInputDialog.getInt (self, "Advancement Agresiveness Ratio",
Modified: trunk/Toss/Client/Wrapper.py
===================================================================
--- trunk/Toss/Client/Wrapper.py 2011-03-07 12:43:57 UTC (rev 1346)
+++ trunk/Toss/Client/Wrapper.py 2011-03-07 23:33:14 UTC (rev 1347)
@@ -403,7 +403,7 @@
# "EVAL LOC MOVES advancement_ratio location TIMEOUT time_in_sec iters_or_depth_limit method optional_playout_horizon"
# syntax variant 2:
# "EVAL LOC MOVES [{0: heuristic_player_0_loc_0; 1: heuristic_player_1_loc_0}; {0: heuristic_player_0_loc_1; 1: heuristic_player_1_loc_1}] advancement_ratio location TIMEOUT time_in_sec iters_or_depth_limit method optional_playout_horizon"
- m = self.msg ("EVAL LOC MOVES " + str(adv_ratio) + ".0 " + str(loc) +" TIMEOUT 1200 "+ str(no_iters) + " alpha_beta_ord")
+ m = self.msg ("EVAL LOC MOVES " + str(adv_ratio) + ".0 " + str(loc) +" TIMEOUT "+ str(no_iters) + " 50500 alpha_beta_ord")
self.set_time (ts, t)
msg = [s.strip() for s in m.split(';')]
emb = dict()
Modified: trunk/Toss/Formula/Aux.ml
===================================================================
--- trunk/Toss/Formula/Aux.ml 2011-03-07 12:43:57 UTC (rev 1346)
+++ trunk/Toss/Formula/Aux.ml 2011-03-07 23:33:14 UTC (rev 1347)
@@ -336,6 +336,20 @@
done;
!r
+let array_find_all_max cmp a =
+ let n = Array.length a in
+ if n=0 then []
+ else
+ let best = ref (Array.unsafe_get a (n-1)) in
+ let beste = ref [!best] in
+ for i = n-2 downto 0 do
+ let e = Array.unsafe_get a i in
+ let res = cmp e !best in
+ if res > 0 then (best := e; beste := [e])
+ else if res = 0 then beste := e :: !beste
+ done;
+ !beste
+
let array_argfind_all_max cmp a =
let n = Array.length a in
if n=0 then []
Modified: trunk/Toss/Formula/Aux.mli
===================================================================
--- trunk/Toss/Formula/Aux.mli 2011-03-07 12:43:57 UTC (rev 1346)
+++ trunk/Toss/Formula/Aux.mli 2011-03-07 23:33:14 UTC (rev 1347)
@@ -192,6 +192,8 @@
arrays are of different lengths. *)
val array_for_all2 : ('a -> 'b -> bool) -> 'a array -> 'b array -> bool
+(** Find all maximal elements in an array. *)
+val array_find_all_max : ('a -> 'a -> int) -> 'a array -> 'a list
(** Find indices of all maximal elements in an array. *)
val array_argfind_all_max : ('a -> 'a -> int) -> 'a array -> int list
Modified: trunk/Toss/Play/GameTree.ml
===================================================================
--- trunk/Toss/Play/GameTree.ml 2011-03-07 12:43:57 UTC (rev 1346)
+++ trunk/Toss/Play/GameTree.ml 2011-03-07 23:33:14 UTC (rev 1347)
@@ -137,7 +137,7 @@
let (heurs, is_calc) =
if ab && issome !last_vals && issome !stop_vals &&
((Aux.unsome !last_vals).(pl) > (Aux.unsome !stop_vals).(pl)) then
- (Aux.unsome !stop_vals, false)
+ (Aux.unsome !last_vals, false)
else (
let resh = Array.map calc heurs.(state.Arena.cur_loc) in
if not (issome !last_vals) then last_vals := Some (resh) else
@@ -202,9 +202,19 @@
| Leaf (state, _, _) ->
Aux.random_elem (Array.to_list (Move.list_moves game state))
| Node (_, p, info, succ) ->
+ let cmp (_, c1) (_, c2) =
+ let nval child = (node_values child).(p) in
+ let (e1, e2) = (node_is_exact c1, node_is_exact c2) in
+ if e1 && not e2 then 1 else if e2 && not e1 then -1 else
+ if nval c1 > nval c2 then 1 else if nval c2> nval c1 then -1 else 0 in
+ let maxs_exact = Aux.array_find_all_max cmp succ in
let mval = info.heurs.(p) in
+ let maxs = if maxs_exact <> [] then maxs_exact else
+ Aux.array_find_all (fun (_,c) -> (node_values c).(p) = mval) succ in
let nonleaf = function Leaf _ -> false | _ -> true in
- let maxs= Aux.array_find_all (fun (_,c) -> (node_values c).(p)=mval) succ in
+ let move_s (m, n) = Move.move_gs_str_short (state n) m in
+ if !debug_level > 0 then print_endline
+ ("\nBest Moves: " ^ (String.concat ", " (List.map move_s maxs)));
if List.exists (fun x -> nonleaf (snd x)) maxs then
let (m, t) = Aux.random_elem maxs in (m, state t)
else ( (* Do *not* take a shallow leaf if possible. *)
Modified: trunk/Toss/Play/GameTreeTest.ml
===================================================================
--- trunk/Toss/Play/GameTreeTest.ml 2011-03-07 12:43:57 UTC (rev 1346)
+++ trunk/Toss/Play/GameTreeTest.ml 2011-03-07 23:33:14 UTC (rev 1347)
@@ -91,10 +91,28 @@
(* print_endline (GameTree.str string_of_int u1); *)
assert_equal ~printer:(fun x -> string_of_int x) 250 (GameTree.size u1);
);
+
+ "maximax suggest move: Tic-Tac-Toe defense" >::
+ (fun () ->
+ let struc = "MODEL [ | P:1 {}; Q:1 {} | ] \"
+
+ . . .
+
+ . P .
+
+ . P Q
+\"" in
+ let (g, s) = state_of_file "./examples/Tic-Tac-Toe.toss" ~struc ~loc:1 in
+ (* GameTree.set_debug_level 1; *)
+ let h = Heuristic.default_heuristic ~struc:s.Arena.struc ~advr:4. g in
+ let (m, ns) = GameTree.maximax_unfold_choose 9 g s h in
+ assert_equal ~printer:(fun x -> x) "Circle{1:b3}"
+ (Move.move_gs_str_short s m);
+ );
]
let bigtests = "GameTreeBig" >::: [
- "maximax suggest move: Gomoku" >::
+ "maximax suggest move: Gomoku defense" >::
(fun () ->
let struc = "MODEL [ | P:1 {}; Q:1 {} | ] \"
... ... ... ...
@@ -116,11 +134,9 @@
\" with DiagA (x, y) = ex u (R(x, u) and C(u, y));
DiagB (x, y) = ex u (R(x, u) and C(y, u))" in
let (g, s) = state_of_file "./examples/Gomoku.toss" ~struc ~loc:1 in
- ignore (Unix.alarm 20);
- GameTree.set_debug_level 1;
+ (* GameTree.set_debug_level 1; *)
let h = Heuristic.default_heuristic ~struc:s.Arena.struc ~advr:4. g in
- let (m, ns) = GameTree.maximax_unfold_choose 200 g s h in
- Game.cancel_timeout ();
+ let (m, ns) = GameTree.maximax_unfold_choose 160 g s h in
assert_equal ~printer:(fun x -> x) "Circle{1:d8}"
(Move.move_gs_str_short s m);
);
Modified: trunk/Toss/Server/Server.ml
===================================================================
--- trunk/Toss/Server/Server.ml 2011-03-07 12:43:57 UTC (rev 1346)
+++ trunk/Toss/Server/Server.ml 2011-03-07 23:33:14 UTC (rev 1347)
@@ -4,7 +4,7 @@
let set_debug_level i =
debug_level := i;
if i > 5 then Solver.set_debug_level 1;
- GameTree.set_debug_level 1;
+ if i > 0 then GameTree.set_debug_level 1;
Game.set_debug_level i;
Heuristic.debug_level := i - 3
@@ -193,13 +193,26 @@
play := Some p; play_state := Some ps;
p, ps in
ignore (Unix.alarm timer);
- let res = Game.suggest ~effort p ps in
- Game.cancel_timeout ();
- match res with
- | Some (move, new_state) ->
- play_state := Some new_state;
- Move.move_gs_str !state move
- | None -> "None"
+ if !no_gtree then
+ let res = Game.suggest ~effort p ps in
+ Game.cancel_timeout ();
+ match res with
+ | Some (move, new_state) ->
+ play_state := Some new_state;
+ Move.move_gs_str !state move
+ | None -> "None"
+ else
+ let heur = match !game_modified, !g_heur with
+ | false, Some h -> h
+ | true, _ | _, None ->
+ g_heur := Some (Heuristic.default_heuristic
+ ~struc:(snd !state).Arena.struc
+ ~advr:4. (fst !state));
+ Aux.unsome !g_heur in
+ let (move, _) = GameTree.maximax_unfold_choose effort
+ (fst !state) (snd !state) heur in
+ Game.cancel_timeout ();
+ Move.move_gs_str !state move
)
| Aux.Left (Arena.ApplyRule (r_name, mtch, t, p) as req) -> (
Modified: trunk/Toss/TossFullTest.ml
===================================================================
--- trunk/Toss/TossFullTest.ml 2011-03-07 12:43:57 UTC (rev 1346)
+++ trunk/Toss/TossFullTest.ml 2011-03-07 23:33:14 UTC (rev 1347)
@@ -10,6 +10,7 @@
let play_tests_big = "Play" >::: [
HeuristicTest.bigtests;
+ GameTreeTest.bigtests;
GameTest.bigtests;
]
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|