From 4fcb53a0748d2bd19bf099218dc3c66140ea5948 Mon Sep 17 00:00:00 2001 From: Bart Massey Date: Fri, 9 Nov 2012 18:40:12 -0800 Subject: [PATCH] added matlab version --- README.md | 6 ++-- matlab/gamevalue.m | 72 ++++++++++++++++++++++++++++++++++++++++++++++ matlab/negamax.m | 44 ++++++++++++++++++++++++++++ matlab/ttt.m | 8 ++++++ 4 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 matlab/gamevalue.m create mode 100644 matlab/negamax.m create mode 100644 matlab/ttt.m diff --git a/README.md b/README.md index c94b78d..e683cba 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,8 @@ settings I could manage. The C was compiled with -O2 and the Java with -O. The JavaScript was run with the d8 shell of the v8 interpreter [1], with the Java-based rhino interpreter shell [2], and with the js shell of SpiderMonkey -[3]. I don't have access to Matlab; I would be curious to -find out how its performance compares with Octave's on this -benchmark. +[3]. The Matlab version [4] was run on a different machine, +because I had access to Matlab there. Timings on my home machine (2GHz Intel Core 2 Quad Q9300) are as follows: @@ -27,6 +26,7 @@ are as follows: JavaScript [3]: 3.0s Python: 8.3s Nickle: 14s + Matlab [4]: 15s Octave: 3.5m Yes, Octave is 4000x slower than C on this benchmark! diff --git a/matlab/gamevalue.m b/matlab/gamevalue.m new file mode 100644 index 0000000..a54e7e7 --- /dev/null +++ b/matlab/gamevalue.m @@ -0,0 +1,72 @@ +% Copyright © 2012 Bart Massey +% [This program is licensed under the "MIT License"] +% Please see the file COPYING in the source +% distribution of this software for license terms. + +% Perfect Tic-Tac-Toe player in Matlab / Octave + + +% Returns the value of the game to the +% side on move if the game is over, or +% -2 if the game is still in progress. +function v = gamevalue(onmove, S) + % first scan for wins + for side = [-onmove, onmove] + v = side * onmove; + % scan for diagonal + n = 0; + for d = 1:3 + if S(d, d) == side + n = n + 1; + end + end + if n == 3 + return + end + % scan for opposite diagonal + n = 0; + for d = 1:3 + if S(d, 4 - d) == side + n = n + 1; + end + end + if n == 3 + return + end + % scan for rows + for r = 1:3 + n = 0; + for c = 1:3 + if S(r, c) == side + n = n + 1; + end + end + if n == 3 + return + end + end + % scan for cols + for c = 1:3 + n = 0; + for r = 1:3 + if S(r, c) == side + n = n + 1; + end + end + if n == 3 + return + end + end + end + % scan for blanks + v = -2; % game not over + for r = 1:3 + for c = 1:3 + if S(r, c) == 0 + return + end + end + end + % game is a draw + v = 0; +end diff --git a/matlab/negamax.m b/matlab/negamax.m new file mode 100644 index 0000000..d0ecf0c --- /dev/null +++ b/matlab/negamax.m @@ -0,0 +1,44 @@ +% Copyright ?? 2012 Bart Massey +% [This program is licensed under the "MIT License"] +% Please see the file COPYING in the source +% distribution of this software for license terms. + +% Perfect Tic-Tac-Toe player in Matlab / Octave + +% Pseudocode: + +% To calculate the negamax value of a position s: +% If s is a final state (game over) +% return the value of s to the side on move +% v <- -1 +% for each legal move m in s +% s' <- m(s) +% v' <- -negamax(s') +% if v' > v +% v <- v' +% return v + +function v = negamax(onmove, S) + % printf("tomove: %d\n", onmove); + % S + v = gamevalue(onmove, S); + if v ~= -2 % game is over + % printf("result: *%d\n", v); + return + end + v = -1; + for r = 1:3 + for c = 1:3 + if S(r, c) == 0 + % printf("examining %d %d\n", r, c); + S(r, c) = onmove; + v0 = -negamax(-onmove, S); + if (v0 > v) + v = v0; + end + S(r, c) = 0; + end + end + end + % printf("result: %d\n", v); +end diff --git a/matlab/ttt.m b/matlab/ttt.m new file mode 100644 index 0000000..d051bae --- /dev/null +++ b/matlab/ttt.m @@ -0,0 +1,8 @@ +% Copyright ?? 2012 Bart Massey +% [This program is licensed under the "MIT License"] +% Please see the file COPYING in the source +% distribution of this software for license terms. + +% Perfect Tic-Tac-Toe player in Matlab / Octave + +negamax(1, [0 0 0; 0 0 0; 0 0 0])