Permalink
Browse files

got the main task working

  • Loading branch information...
1 parent 32f0165 commit 20bd125e22765e7634f40aa6addd894d9e6a0bec @themrb committed May 26, 2012
Showing with 161 additions and 254 deletions.
  1. +0 −139 src/.#agent.adb#
  2. +119 −102 src/agent.adb
  3. +16 −1 src/agent.ads
  4. +2 −1 src/boards.adb
  5. +12 −4 src/client.cpp
  6. +10 −5 src/client.cpp~
  7. +0 −1 src/data2.csv
  8. +2 −1 src/temporaldifference.adb
View
139 src/.#agent.adb#
@@ -1,139 +0,0 @@
-with Ada.Text_IO;
-use Ada.Text_IO;
-with Boards; use Boards;
-with GameTree; use GameTree;
-with MinMax; use MinMax;
-with Configure; use Configure;
-with Exceptions; use Exceptions;
-with TemporalDifference; use TemporalDifference;
-with GNAT.Traceback; use GNAT.Traceback;
-with GNAT.Traceback.Symbolic; use GNAT.Traceback.Symbolic;
-
-package body Agent is
-
-
-
- ccurrentstate : CBoardState;
- cplayercolour : Integer;
- cnextmovey : Integer;
- cnextmovex : Integer;
- pragma import(cpp, ccurrentstate, "currentcstate");
- pragma import(cpp, cplayercolour, "playercolour");
- pragma import(cpp, cnextmovey, "nextmovey");
- pragma import(cpp, cnextmovex, "nextmovex");
-
- procedure Ada_Subroutine is
- currentstate : GameBoard;
- player : BoardPoint;
- treeroot : GameTree_Type;
- move : Place;
- value : BoardValue;
- turnsleft : TurnsNo := 0;
- begin
- if(not Configure.initialised) then
- LoadWeights;
- Configure.initialised := True;
- StoreWeights;
- end if;
- Put("Ada_Subroutine has been invoked from C++. " & Integer'Image(testint));
- testint := 15;
- Ada.Text_IO.Put_Line("");
-
- if (cplayercolour = 1) then
- player := White;
- elsif (cplayercolour = 2) then
- player := Black;
- end if;
-
- for I in Dimension'Range loop
- for J in Dimension'Range loop
- currentstate(I,J) := BoardPoint'Val(ccurrentstate(I,J));
- if (currentstate(I,J) = Empty) then
- turnsleft := turnsleft + 1;
- end if;
- end loop;
- --Ada.Text_IO.Put_Line("");
- end loop;
-
- if (ValidMove(player,currentstate,0,0) > 0) then
- cnextmovey := 0;
- cnextmovex := 0;
- return;
- elsif (ValidMove(player,currentstate,0,9) > 0) then
- cnextmovey := 0;
- cnextmovex := 9;
- return;
- elsif (ValidMove(player,currentstate,9,9) > 0) then
- cnextmovey := 9;
- cnextmovex := 9;
- return;
- elsif (ValidMove(player,currentstate,9,0) > 0) then
- cnextmovey := 9;
- cnextmovex := 0;
- return;
- end if;
-
-
- treeroot.state.justWent := NextPlayer(player);
- treeroot.state.current_state := currentstate;
- treeroot.state.turnsleft := turnsleft;
-
- --Put_Line("No storage error yet");
- if (turnsleft < 16) then
- NegaMax(player, treeroot, 15, value, BoardValue'First, BoardValue'Last, move);
- else
- NegaMax(player, treeroot, 7, value, BoardValue'First, BoardValue'Last, move);
- -- declare
- -- tempprob : Probability;
- -- bestprob : Probability := 0.0;
- -- bestmove : Place;
- -- children : ExpandedChildren := Expand(treeroot);
- -- begin
- -- for I in 0 .. children.branching-1 loop
- -- tempprob := MonteCarlo(player,children.children(I),200);
- -- if (tempprob >= bestprob) then
- -- bestmove := children.children(I).state.spot;
- -- bestprob := tempprob;
- -- end if;
- -- end loop;
- -- Put_Line("Probability of winning : " & Long_Float'Image(bestprob));
- -- move := bestmove;
- -- end;
- end if;
-
- --Put_Line("No storage error after max");
- --Put_Line("testing monte carlo " & Long_Float'Image(MonteCarlo(player,treeroot,100)));
- declare
- temppieces : Natural := ValidMove(player, currentstate, move(x), move(y));
- begin
- Put_Line("We'll get " & TurnsNo'Image(temppieces) & "for moving at" & Dimension'Image(move(x)) & "," & Dimension'Image(move(y)));
- end;
- cnextmovey := Integer(move(x));
- cnextmovex := Integer(move(y));
-
- -- Move_Loop:
- -- for I in Dimension range Dimension'Range loop
- -- for J in Dimension range Dimension'Range loop
- -- piecestaken := ValidMove(player, currentstate, I, J);
- -- if (piecestaken > 0) then
- -- if (piecestaken > bestpiecestaken) then
- -- bestpiecestaken := piecestaken;
- -- cnextmovey := Integer(I);
- -- cnextmovex := Integer(J);
- -- Put_Line("Seen move " & I'Img &J'Img);
- -- --PrintBoard(currentstate);
- -- end if;
- -- end if;
- -- end loop;
- -- end loop Move_Loop;
- exception
- when E : others =>
- Show_Exception (E);
- end Ada_Subroutine;
-
- procedure GreedyMove(board : in GameBoard; xmove : out Dimension; ymove : out Dimension) is
- begin
- null;
- end GreedyMove;
-
-end Agent;
View
221 src/agent.adb
@@ -20,115 +20,132 @@ package body Agent is
pragma import(cpp, cnextmovey, "nextmovey");
pragma import(cpp, cnextmovex, "nextmovex");
- procedure Ada_Subroutine is
- currentstate : GameBoard;
- player : BoardPoint;
- treeroot : GameTree_Type;
- move : Place;
- value : BoardValue;
- turnsleft : TurnsNo := 0;
+ task body Main is
+
begin
- if(not Configure.initialised) then
+ accept Initialise do
+ Put_Line("is it weights?");
LoadWeights;
- Configure.initialised := True;
- StoreWeights;
- end if;
- Put("Ada_Subroutine has been invoked from C++. " & Integer'Image(testint));
- testint := 15;
- Ada.Text_IO.Put_Line("");
-
- if (cplayercolour = 1) then
- player := White;
- elsif (cplayercolour = 2) then
- player := Black;
- end if;
-
- for I in Dimension'Range loop
- for J in Dimension'Range loop
- currentstate(I,J) := BoardPoint'Val(ccurrentstate(I,J));
- if (currentstate(I,J) = Empty) then
- turnsleft := turnsleft + 1;
- end if;
- end loop;
- --Ada.Text_IO.Put_Line("");
- end loop;
-
- if (ValidMove(player,currentstate,0,0) > 0) then
- cnextmovey := 0;
- cnextmovex := 0;
- return;
- elsif (ValidMove(player,currentstate,0,9) > 0) then
- cnextmovey := 0;
- cnextmovex := 9;
- return;
- elsif (ValidMove(player,currentstate,9,9) > 0) then
- cnextmovey := 9;
- cnextmovex := 9;
- return;
- elsif (ValidMove(player,currentstate,9,0) > 0) then
- cnextmovey := 9;
- cnextmovex := 0;
- return;
- end if;
-
-
- treeroot.state.justWent := NextPlayer(player);
- treeroot.state.current_state := currentstate;
- treeroot.state.turnsleft := turnsleft;
-
- --Put_Line("No storage error yet");
- if (turnsleft < 16) then
- NegaMax(player, treeroot, 15, value, BoardValue'First, BoardValue'Last, move);
- else
- NegaMax(player, treeroot, 7, value, BoardValue'First, BoardValue'Last, move);
- -- declare
- -- tempprob : Probability;
- -- bestprob : Probability := 0.0;
- -- bestmove : Place;
- -- children : ExpandedChildren := Expand(treeroot);
- -- begin
- -- for I in 0 .. children.branching-1 loop
- -- tempprob := MonteCarlo(player,children.children(I),200);
- -- if (tempprob >= bestprob) then
- -- bestmove := children.children(I).state.spot;
- -- bestprob := tempprob;
- -- end if;
- -- end loop;
- -- Put_Line("Probability of winning : " & Long_Float'Image(bestprob));
- -- move := bestmove;
- -- end;
- end if;
-
- --Put_Line("No storage error after max");
- --Put_Line("testing monte carlo " & Long_Float'Image(MonteCarlo(player,treeroot,100)));
- declare
- temppieces : Natural := ValidMove(player, currentstate, move(x), move(y));
- begin
- Put_Line("We'll get " & TurnsNo'Image(temppieces) & "for moving at" & Dimension'Image(move(x)) & "," & Dimension'Image(move(y)));
- end;
- cnextmovey := Integer(move(x));
- cnextmovex := Integer(move(y));
-
- -- Move_Loop:
- -- for I in Dimension range Dimension'Range loop
- -- for J in Dimension range Dimension'Range loop
- -- piecestaken := ValidMove(player, currentstate, I, J);
- -- if (piecestaken > 0) then
- -- if (piecestaken > bestpiecestaken) then
- -- bestpiecestaken := piecestaken;
- -- cnextmovey := Integer(I);
- -- cnextmovex := Integer(J);
- -- Put_Line("Seen move " & I'Img &J'Img);
- -- --PrintBoard(currentstate);
- -- end if;
- -- end if;
- -- end loop;
- -- end loop Move_Loop;
+ Put_Line("not weights?");
+
+ if (cplayercolour = 1) then
+ player := White;
+ elsif (cplayercolour = 2) then
+ player := Black;
+ end if;
+
+ -- spawn explorer tasks as required
+ -- this is basically our set up to be ready to play the game.
+ -- the clock hasn't started yet - free time
+ end Initialise;
+
+ Main_Loop:
+ loop
+ select
+ accept NewMove do
+
+ declare
+ currentstate : GameBoard;
+ treeroot : GameTree_Type;
+ move : Place;
+ value : BoardValue;
+ turnsleft : TurnsNo := 0;
+ begin
+ -- Read in CPP values for the board
+ for I in Dimension'Range loop
+ for J in Dimension'Range loop
+ currentstate(I,J) := BoardPoint'Val(ccurrentstate(I,J));
+ if (currentstate(I,J) = Empty) then
+ turnsleft := turnsleft + 1;
+ end if;
+ end loop;
+ end loop;
+
+ -- Initialise game tree
+ treeroot.state.justWent := NextPlayer(player);
+ treeroot.state.current_state := currentstate;
+ treeroot.state.turnsleft := turnsleft;
+
+ if (turnsleft < 16) then
+ NegaMax(player, treeroot, 15, value, BoardValue'First, BoardValue'Last, move);
+ else
+ NegaMax(player, treeroot, 7, value, BoardValue'First, BoardValue'Last, move);
+ -- declare
+ -- tempprob : Probability;
+ -- bestprob : Probability := 0.0;
+ -- bestmove : Place;
+ -- children : ExpandedChildren := Expand(treeroot);
+ -- begin
+ -- for I in 0 .. children.branching-1 loop
+ -- tempprob := MonteCarlo(player,children.children(I),200);
+ -- if (tempprob >= bestprob) then
+ -- bestmove := children.children(I).state.spot;
+ -- bestprob := tempprob;
+ -- end if;
+ -- end loop;
+ -- Put_Line("Probability of winning : " & Long_Float'Image(bestprob));
+ -- move := bestmove;
+ -- end;
+ end if;
+
+ --Put_Line("No storage error after max");
+ --Put_Line("testing monte carlo " & Long_Float'Image(MonteCarlo(player,treeroot,100)));
+ declare
+ temppieces : Natural := ValidMove(player, currentstate, move(x), move(y));
+ begin
+ Put_Line("We'll get " & TurnsNo'Image(temppieces) & "for moving at" & Dimension'Image(move(x)) & "," & Dimension'Image(move(y)));
+ end;
+ cnextmovey := Integer(move(x));
+ cnextmovex := Integer(move(y));
+
+ -- Move_Loop:
+ -- for I in Dimension range Dimension'Range loop
+ -- for J in Dimension range Dimension'Range loop
+ -- piecestaken := ValidMove(player, currentstate, I, J);
+ -- if (piecestaken > 0) then
+ -- if (piecestaken > bestpiecestaken) then
+ -- bestpiecestaken := piecestaken;
+ -- cnextmovey := Integer(I);
+ -- cnextmovex := Integer(J);
+ -- Put_Line("Seen move " & I'Img &J'Img);
+ -- --PrintBoard(currentstate);
+ -- end if;
+ -- end if;
+ -- end loop;
+ -- end loop Move_Loop;
+ end;
+ end NewMove;
+ or
+ accept GameEnd do
+ StoreWeights;
+ -- also free time
+ end GameEnd;
+ exit Main_Loop;
+ end select;
+ end loop Main_Loop;
+ end Main;
+
+ procedure StartUp is
+ begin
+ MainTask := new Main;
+ MainTask.Initialise;
+ end StartUp;
+
+ procedure Ada_Subroutine is
+ begin
+
+ MainTask.NewMove;
+
exception
when E : others =>
Show_Exception (E);
end Ada_Subroutine;
+ procedure GameEnd is
+ begin
+ MainTask.GameEnd;
+ end GameEnd;
+
procedure GreedyMove(board : in GameBoard; xmove : out Dimension; ymove : out Dimension) is
begin
null;
View
17 src/agent.ads
@@ -1,8 +1,23 @@
+with Boards; use Boards;
+
package Agent is
+ task type Main is
+ entry Initialise;
+ entry NewMove;
+ entry GameEnd;
+ end Main;
+
+ type MainAccess is access all Main;
+ MainTask : MainAccess;
+
+ procedure StartUp;
procedure Ada_Subroutine;
+ procedure GameEnd;
+ pragma export(CPP, StartUp );
pragma export(CPP, Ada_Subroutine );
+ pragma export(CPP, GameEnd );
- testint : Integer := 0;
+ player : BoardPoint;
end Agent;
View
3 src/boards.adb
@@ -14,7 +14,8 @@ package body Boards is
elsif (player = Black) then
return White;
else
- Put_Line("Dude, the previous player was " & player'Img);
+ null;
+ --Put_Line("Dude, the previous player was " & player'Img);
end if;
return Empty;
end NextPlayer;
View
16 src/client.cpp
@@ -23,7 +23,9 @@ extern "C" {
void adainit(void);
void adafinal(void);
+ void startup( void );
void ada_subroutine( void );
+ void gameend( void );
}
using namespace std;
@@ -35,6 +37,7 @@ int playercolour;
int main(int argc, char *argv[])
{
+
BoardState player;
string host = DEFAULT_HOST;
uint16_t port = DEFAULT_PORT;
@@ -53,7 +56,11 @@ int main(int argc, char *argv[])
cerr << "CLIENT: player is BLACK" << endl;
} else {
cerr << "CLIENT: unknown player \"" << argv[1] << "\"" << endl;
- }
+ }
+
+ // Boot up Ada, since we now know which player we are
+ adainit();
+ startup();
if (argc == 3) {
string hostString(argv[2]);
@@ -94,7 +101,7 @@ int main(int argc, char *argv[])
// send connect message to server
ConnectMessage message(player, player == WHITE ?
- "404 Player Not Found" : "404 Player Not Found");
+ "404 Player Not Found" : "Team Adawesome: Reverwesome's Vengence");
if (!message.send(client_socket)) {
cerr << "CLIENT: could not connect to server" << endl;
exit(-1);
@@ -104,8 +111,6 @@ int main(int argc, char *argv[])
ServerMessage messageIn;
ClientMessage messageOut;
- adainit();
-
while (1) {
if (!messageIn.receive(client_socket)) {
cerr << "CLIENT: receive error" << endl;
@@ -169,6 +174,9 @@ int main(int argc, char *argv[])
exit(-1);
}
}
+ gameend();
+
+ cerr << "asking ada to terminate";
adafinal();
return 0;
View
15 src/client.cpp~
@@ -23,8 +23,9 @@ extern "C" {
void adainit(void);
void adafinal(void);
+ void startup( void );
void ada_subroutine( void );
- void StoreWeights( void );
+ void gameend( void );
}
using namespace std;
@@ -36,6 +37,10 @@ int playercolour;
int main(int argc, char *argv[])
{
+ // Boot up Ada
+ adainit();
+ startup();
+
BoardState player;
string host = DEFAULT_HOST;
uint16_t port = DEFAULT_PORT;
@@ -95,7 +100,7 @@ int main(int argc, char *argv[])
// send connect message to server
ConnectMessage message(player, player == WHITE ?
- "404 Player Not Found" : "404 Player Not Found");
+ "404 Player Not Found" : "Team Adawesome: Reverwesome's Vengence");
if (!message.send(client_socket)) {
cerr << "CLIENT: could not connect to server" << endl;
exit(-1);
@@ -105,8 +110,6 @@ int main(int argc, char *argv[])
ServerMessage messageIn;
ClientMessage messageOut;
- adainit();
-
while (1) {
if (!messageIn.receive(client_socket)) {
cerr << "CLIENT: receive error" << endl;
@@ -170,7 +173,9 @@ int main(int argc, char *argv[])
exit(-1);
}
}
- StoreWeights();
+ gameend();
+
+ cerr << "asking ada to terminate";
adafinal();
return 0;
View
1 src/data2.csv
@@ -4,4 +4,3 @@
1.00000E+00, 1.00000E+00, 1.00000E+00, 1.00000E+00,
1.00000E+00, 1.00000E+00, 1.00000E+00, 1.00000E+00, 1.00000E+00,
1.00000E+01
-15
View
3 src/temporaldifference.adb
@@ -70,6 +70,7 @@ package body TemporalDifference is
Sub : String := Slice(Subs, i);
begin
if(Line_No < 5) then
+ Put_Line(Sub);
pieceWeights(Line_No, Dimension(i)-1) := Float'Value(Sub);
elsif (Line_No = 5 and i = 1) then
mobilityWeight := Float'Value(Sub);
@@ -90,7 +91,7 @@ package body TemporalDifference is
procedure StoreWeights is
CSV_File : File_Type;
- Filename : constant String := "data2.csv";
+ Filename : constant String := "data.csv";
Line_No : Natural := 0;
Subs : Slice_Set;
Next_Line : Unbounded_String;

0 comments on commit 20bd125

Please sign in to comment.