Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
Seminar-HFO
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
Seminar-HFO
Commits
41b577db
You need to sign in or sign up before continuing.
Commit
41b577db
authored
Aug 05, 2015
by
Matthew Hausknecht
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Encapsulated hfo interface in a namespace.
parent
5aa73777
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
165 additions
and
89 deletions
+165
-89
CMakeLists.txt
CMakeLists.txt
+1
-1
example/hfo_example_agent.cpp
example/hfo_example_agent.cpp
+8
-6
src/HFO.cpp
src/HFO.cpp
+84
-27
src/HFO.hpp
src/HFO.hpp
+24
-17
src/agent.cpp
src/agent.cpp
+43
-33
src/agent.h
src/agent.h
+3
-3
src/sample_player.cpp
src/sample_player.cpp
+1
-1
src/sample_player.h
src/sample_player.h
+1
-1
No files found.
CMakeLists.txt
View file @
41b577db
...
@@ -35,7 +35,7 @@ list(APPEND LINK_LIBS
...
@@ -35,7 +35,7 @@ list(APPEND LINK_LIBS
)
)
add_executable
(
sample_coach
${
SOURCE_DIR
}
/main_coach.cpp
${
SOURCE_DIR
}
/sample_coach.cpp
${
SOURCES
}
)
add_executable
(
sample_coach
${
SOURCE_DIR
}
/main_coach.cpp
${
SOURCE_DIR
}
/sample_coach.cpp
${
SOURCES
}
)
add_executable
(
sample_player
${
SOURCE_DIR
}
/
main_player.cpp
${
SOURCE_DIR
}
/sample_player.cpp
${
SOURCES
}
${
SOURCE_DIR
}
/agent.cpp
)
add_executable
(
sample_player
${
SOURCE_DIR
}
/
HFO.cpp
${
SOURCE_DIR
}
/main_player.cpp
${
SOURCE_DIR
}
/sample_player.cpp
${
SOURCES
}
)
add_executable
(
sample_trainer
${
SOURCE_DIR
}
/main_trainer.cpp
${
SOURCE_DIR
}
/sample_trainer.cpp
${
SOURCES
}
)
add_executable
(
sample_trainer
${
SOURCE_DIR
}
/main_trainer.cpp
${
SOURCE_DIR
}
/sample_trainer.cpp
${
SOURCES
}
)
add_executable
(
agent
${
SOURCE_DIR
}
/main_agent.cpp
${
SOURCE_DIR
}
/agent.cpp
${
SOURCES
}
)
add_executable
(
agent
${
SOURCE_DIR
}
/main_agent.cpp
${
SOURCE_DIR
}
/agent.cpp
${
SOURCES
}
)
add_library
(
hfo-lib SHARED
${
SOURCE_DIR
}
/HFO.hpp
${
SOURCE_DIR
}
/HFO.cpp
)
add_library
(
hfo-lib SHARED
${
SOURCE_DIR
}
/HFO.hpp
${
SOURCE_DIR
}
/HFO.cpp
)
...
...
example/hfo_example_agent.cpp
View file @
41b577db
...
@@ -4,23 +4,25 @@
...
@@ -4,23 +4,25 @@
#include <cstdlib>
#include <cstdlib>
using
namespace
std
;
using
namespace
std
;
using
namespace
hfo
;
// First Start the server: $> bin/start.py
// Before running this program, first Start HFO server:
// $./bin/start.py --offense-agents 1
int
main
()
{
int
main
()
{
// Create the HFO environment
// Create the HFO environment
HFOEnvironment
hfo
;
HFOEnvironment
hfo
;
// Connect t
he agent's server on the given port with the given
// Connect t
o the agent's server on port 6000 and request low-level
// feature set.
See possible feature sets in src/HFO.hpp
.
// feature set.
See manual for more information on feature sets
.
hfo
.
connectToAgentServer
(
6000
,
LOW_LEVEL_FEATURE_SET
);
hfo
.
connectToAgentServer
(
6000
,
LOW_LEVEL_FEATURE_SET
);
// Play 5 episodes
// Play 5 episodes
for
(
int
episode
=
0
;
episode
<
5
;
episode
++
)
{
for
(
int
episode
=
0
;
episode
<
5
;
episode
++
)
{
hfo_
status_t
status
=
IN_GAME
;
status_t
status
=
IN_GAME
;
while
(
status
==
IN_GAME
)
{
while
(
status
==
IN_GAME
)
{
// G
rab
the vector of state features for the current state
// G
et
the vector of state features for the current state
const
std
::
vector
<
float
>&
feature_vec
=
hfo
.
getState
();
const
std
::
vector
<
float
>&
feature_vec
=
hfo
.
getState
();
// Create a dash action
// Create a dash action
Action
a
=
{
DASH
,
0.
,
0.
};
Action
a
=
{
DASH
,
0.
0
,
0.0
};
// Perform the dash and recieve the current game status
// Perform the dash and recieve the current game status
status
=
hfo
.
act
(
a
);
status
=
hfo
.
act
(
a
);
}
}
...
...
src/HFO.cpp
View file @
41b577db
...
@@ -11,8 +11,43 @@
...
@@ -11,8 +11,43 @@
#include <iostream>
#include <iostream>
#include <sstream>
#include <sstream>
bool
HFOEnvironment
::
ParseHFOConfig
(
const
std
::
string
&
message
,
using
namespace
hfo
;
HFO_Config
&
config
)
{
std
::
string
HFOEnvironment
::
ActionToString
(
Action
action
)
{
std
::
stringstream
ss
;
switch
(
action
.
action
)
{
case
DASH
:
ss
<<
"Dash("
<<
action
.
arg1
<<
","
<<
action
.
arg2
<<
")"
;
break
;
case
TURN
:
ss
<<
"Turn("
<<
action
.
arg1
<<
")"
;
break
;
case
TACKLE
:
ss
<<
"Tackle("
<<
action
.
arg1
<<
")"
;
break
;
case
KICK
:
ss
<<
"Kick("
<<
action
.
arg1
<<
","
<<
action
.
arg2
<<
")"
;
break
;
case
MOVE
:
ss
<<
"Move"
;
break
;
case
SHOOT
:
ss
<<
"Shoot"
;
break
;
case
PASS
:
ss
<<
"Pass"
;
break
;
case
DRIBBLE
:
ss
<<
"Dribble"
;
break
;
case
QUIT
:
ss
<<
"Quit"
;
break
;
}
return
ss
.
str
();
};
bool
HFOEnvironment
::
ParseConfig
(
const
std
::
string
&
message
,
Config
&
config
)
{
config
.
num_offense
=
-
1
;
config
.
num_offense
=
-
1
;
config
.
num_defense
=
-
1
;
config
.
num_defense
=
-
1
;
std
::
istringstream
iss
(
message
);
std
::
istringstream
iss
(
message
);
...
@@ -56,19 +91,15 @@ bool HFOEnvironment::ParseHFOConfig(const std::string& message,
...
@@ -56,19 +91,15 @@ bool HFOEnvironment::ParseHFOConfig(const std::string& message,
return
true
;
return
true
;
};
};
void
error
(
const
char
*
msg
)
{
perror
(
msg
);
exit
(
0
);
}
HFOEnvironment
::
HFOEnvironment
()
{}
HFOEnvironment
::
HFOEnvironment
()
{}
HFOEnvironment
::~
HFOEnvironment
()
{
HFOEnvironment
::~
HFOEnvironment
()
{
// Send a quit action and close the connection to the agent's server
// Send a quit action and close the connection to the agent's server
action_t
quit
=
QUIT
;
action_t
quit
=
QUIT
;
if
(
send
(
sockfd
,
&
quit
,
sizeof
(
int
),
0
)
<
0
)
{
if
(
send
(
sockfd
,
&
quit
,
sizeof
(
int
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR sending from socket"
);
p
error
(
"[Agent Client] ERROR sending from socket"
);
}
}
close
(
sockfd
);
close
(
sockfd
);
exit
(
1
);
}
}
void
HFOEnvironment
::
connectToAgentServer
(
int
server_port
,
void
HFOEnvironment
::
connectToAgentServer
(
int
server_port
,
...
@@ -77,12 +108,13 @@ void HFOEnvironment::connectToAgentServer(int server_port,
...
@@ -77,12 +108,13 @@ void HFOEnvironment::connectToAgentServer(int server_port,
<<
server_port
<<
std
::
endl
;
<<
server_port
<<
std
::
endl
;
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
sockfd
<
0
)
{
if
(
sockfd
<
0
)
{
error
(
"ERROR opening socket"
);
perror
(
"ERROR opening socket"
);
exit
(
1
);
}
}
struct
hostent
*
server
=
gethostbyname
(
"localhost"
);
struct
hostent
*
server
=
gethostbyname
(
"localhost"
);
if
(
server
==
NULL
)
{
if
(
server
==
NULL
)
{
fprintf
(
stderr
,
"ERROR, no such host
\n
"
);
fprintf
(
stderr
,
"ERROR, no such host
\n
"
);
exit
(
0
);
exit
(
1
);
}
}
struct
sockaddr_in
serv_addr
;
struct
sockaddr_in
serv_addr
;
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
...
@@ -99,14 +131,18 @@ void HFOEnvironment::connectToAgentServer(int server_port,
...
@@ -99,14 +131,18 @@ void HFOEnvironment::connectToAgentServer(int server_port,
retry
--
;
retry
--
;
}
}
if
(
status
<
0
)
{
if
(
status
<
0
)
{
error
(
"[Agent Client] ERROR Unable to communicate with server"
);
perror
(
"[Agent Client] ERROR Unable to communicate with server"
);
close
(
sockfd
);
exit
(
1
);
}
}
std
::
cout
<<
"[Agent Client] Connected"
<<
std
::
endl
;
std
::
cout
<<
"[Agent Client] Connected"
<<
std
::
endl
;
handshakeAgentServer
(
feature_set
);
handshakeAgentServer
(
feature_set
);
// Get the initial game state
// Get the initial game state
feature_vec
.
resize
(
numFeatures
);
feature_vec
.
resize
(
numFeatures
);
if
(
recv
(
sockfd
,
&
(
feature_vec
.
front
()),
numFeatures
*
sizeof
(
float
),
0
)
<
0
)
{
if
(
recv
(
sockfd
,
&
(
feature_vec
.
front
()),
numFeatures
*
sizeof
(
float
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR recieving state features from socket"
);
perror
(
"[Agent Client] ERROR recieving state features from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
}
}
...
@@ -114,35 +150,50 @@ void HFOEnvironment::handshakeAgentServer(feature_set_t feature_set) {
...
@@ -114,35 +150,50 @@ void HFOEnvironment::handshakeAgentServer(feature_set_t feature_set) {
// Recieve float 123.2345
// Recieve float 123.2345
float
f
;
float
f
;
if
(
recv
(
sockfd
,
&
f
,
sizeof
(
float
),
0
)
<
0
)
{
if
(
recv
(
sockfd
,
&
f
,
sizeof
(
float
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR recv from socket"
);
perror
(
"[Agent Client] ERROR recv from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Check that error is within bounds
// Check that error is within bounds
if
(
abs
(
f
-
123.2345
)
>
1e-4
)
{
if
(
abs
(
f
-
123.2345
)
>
1e-4
)
{
error
(
"[Agent Client] Handshake failed. Improper float recieved."
);
perror
(
"[Agent Client] Handshake failed. Improper float recieved."
);
close
(
sockfd
);
exit
(
1
);
}
}
// Send float 5432.321
// Send float 5432.321
f
=
5432.321
;
f
=
5432.321
;
if
(
send
(
sockfd
,
&
f
,
sizeof
(
float
),
0
)
<
0
)
{
if
(
send
(
sockfd
,
&
f
,
sizeof
(
float
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR sending from socket"
);
perror
(
"[Agent Client] ERROR sending from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Send the feature set request
// Send the feature set request
if
(
send
(
sockfd
,
&
feature_set
,
sizeof
(
int
),
0
)
<
0
)
{
if
(
send
(
sockfd
,
&
feature_set
,
sizeof
(
int
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR sending from socket"
);
perror
(
"[Agent Client] ERROR sending from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Recieve the number of features
// Recieve the number of features
if
(
recv
(
sockfd
,
&
numFeatures
,
sizeof
(
int
),
0
)
<
0
)
{
if
(
recv
(
sockfd
,
&
numFeatures
,
sizeof
(
int
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR recv from socket"
);
perror
(
"[Agent Client] ERROR recv from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
if
(
send
(
sockfd
,
&
numFeatures
,
sizeof
(
int
),
0
)
<
0
)
{
if
(
send
(
sockfd
,
&
numFeatures
,
sizeof
(
int
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR sending from socket"
);
perror
(
"[Agent Client] ERROR sending from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Recieve the game status
// Recieve the game status
hfo_status_t
status
;
status_t
status
;
if
(
recv
(
sockfd
,
&
status
,
sizeof
(
hfo_status_t
),
0
)
<
0
)
{
if
(
recv
(
sockfd
,
&
status
,
sizeof
(
status_t
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR recv from socket"
);
perror
(
"[Agent Client] ERROR recv from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
if
(
status
!=
IN_GAME
)
{
if
(
status
!=
IN_GAME
)
{
std
::
cout
<<
"[Agent Client] Handshake failed: status check."
<<
std
::
endl
;
std
::
cout
<<
"[Agent Client] Handshake failed: status check."
<<
std
::
endl
;
close
(
sockfd
);
exit
(
1
);
exit
(
1
);
}
}
std
::
cout
<<
"[Agent Client] Handshake complete"
<<
std
::
endl
;
std
::
cout
<<
"[Agent Client] Handshake complete"
<<
std
::
endl
;
...
@@ -152,19 +203,25 @@ const std::vector<float>& HFOEnvironment::getState() {
...
@@ -152,19 +203,25 @@ const std::vector<float>& HFOEnvironment::getState() {
return
feature_vec
;
return
feature_vec
;
}
}
hfo_
status_t
HFOEnvironment
::
act
(
Action
action
)
{
status_t
HFOEnvironment
::
act
(
Action
action
)
{
hfo_
status_t
game_status
;
status_t
game_status
;
// Send the action
// Send the action
if
(
send
(
sockfd
,
&
action
,
sizeof
(
Action
),
0
)
<
0
)
{
if
(
send
(
sockfd
,
&
action
,
sizeof
(
Action
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR sending from socket"
);
perror
(
"[Agent Client] ERROR sending from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Get the game status
// Get the game status
if
(
recv
(
sockfd
,
&
game_status
,
sizeof
(
hfo_status_t
),
0
)
<
0
)
{
if
(
recv
(
sockfd
,
&
game_status
,
sizeof
(
status_t
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR recieving from socket"
);
perror
(
"[Agent Client] ERROR recieving from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Get the next game state
// Get the next game state
if
(
recv
(
sockfd
,
&
(
feature_vec
.
front
()),
numFeatures
*
sizeof
(
float
),
0
)
<
0
)
{
if
(
recv
(
sockfd
,
&
(
feature_vec
.
front
()),
numFeatures
*
sizeof
(
float
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR recieving state features from socket"
);
perror
(
"[Agent Client] ERROR recieving state features from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
return
game_status
;
return
game_status
;
}
}
src/HFO.hpp
View file @
41b577db
...
@@ -4,6 +4,8 @@
...
@@ -4,6 +4,8 @@
#include <string>
#include <string>
#include <vector>
#include <vector>
namespace
hfo
{
// For descriptions of the different feature sets see
// For descriptions of the different feature sets see
// https://github.com/mhauskn/HFO/blob/master/doc/manual.pdf
// https://github.com/mhauskn/HFO/blob/master/doc/manual.pdf
enum
feature_set_t
enum
feature_set_t
...
@@ -26,25 +28,27 @@ enum action_t
...
@@ -26,25 +28,27 @@ enum action_t
QUIT
// Special action to quit the game
QUIT
// Special action to quit the game
};
};
// The current status of the HFO game
// An Action consists of the discreet action as well as required
enum
hfo_status_t
// arguments (parameters).
{
IN_GAME
,
GOAL
,
CAPTURED_BY_DEFENSE
,
OUT_OF_BOUNDS
,
OUT_OF_TIME
};
struct
Action
{
struct
Action
{
action_t
action
;
action_t
action
;
float
arg1
;
float
arg1
;
float
arg2
;
float
arg2
;
};
};
// Status of a HFO game
enum
status_t
{
IN_GAME
,
// Game is currently active
GOAL
,
// A goal has been scored by the offense
CAPTURED_BY_DEFENSE
,
// The defense has captured the ball
OUT_OF_BOUNDS
,
// Ball has gone out of bounds
OUT_OF_TIME
// Trial has ended due to time limit
};
// Configuration of the HFO domain including the team names and player
// Configuration of the HFO domain including the team names and player
// numbers for each team. This
can be populated by ParseHFO
Config().
// numbers for each team. This
struct is populated by Parse
Config().
struct
HFO_
Config
{
struct
Config
{
std
::
string
offense_team_name
;
std
::
string
offense_team_name
;
std
::
string
defense_team_name
;
std
::
string
defense_team_name
;
int
num_offense
;
// Number of offensive players
int
num_offense
;
// Number of offensive players
...
@@ -53,15 +57,17 @@ struct HFO_Config {
...
@@ -53,15 +57,17 @@ struct HFO_Config {
std
::
vector
<
int
>
defense_nums
;
// Defensive player numbers
std
::
vector
<
int
>
defense_nums
;
// Defensive player numbers
};
};
class
HFOEnvironment
{
class
HFOEnvironment
{
public:
public:
HFOEnvironment
();
HFOEnvironment
();
~
HFOEnvironment
();
~
HFOEnvironment
();
// Parse a message sent from Trainer to construct an HFO config.
// Returns a string representation of an action.
// Returns a bool indicating if the struct was correctly parsed.
static
std
::
string
ActionToString
(
Action
action
);
static
bool
ParseHFOConfig
(
const
std
::
string
&
message
,
HFO_Config
&
config
);
// Parse a Trainer message to populate config. Returns a bool
// indicating if the struct was correctly parsed.
static
bool
ParseConfig
(
const
std
::
string
&
message
,
Config
&
config
);
// Connect to the server that controls the agent on the specified port.
// Connect to the server that controls the agent on the specified port.
void
connectToAgentServer
(
int
server_port
=
6000
,
void
connectToAgentServer
(
int
server_port
=
6000
,
...
@@ -71,7 +77,7 @@ class HFOEnvironment {
...
@@ -71,7 +77,7 @@ class HFOEnvironment {
const
std
::
vector
<
float
>&
getState
();
const
std
::
vector
<
float
>&
getState
();
// Take an action and recieve the resulting game status
// Take an action and recieve the resulting game status
hfo_
status_t
act
(
Action
action
);
status_t
act
(
Action
action
);
protected:
protected:
int
numFeatures
;
// The number of features in this domain
int
numFeatures
;
// The number of features in this domain
...
@@ -83,4 +89,5 @@ class HFOEnvironment {
...
@@ -83,4 +89,5 @@ class HFOEnvironment {
virtual
void
handshakeAgentServer
(
feature_set_t
feature_set
);
virtual
void
handshakeAgentServer
(
feature_set_t
feature_set
);
};
};
}
// namespace hfo
#endif
#endif
src/agent.cpp
View file @
41b577db
...
@@ -29,7 +29,6 @@
...
@@ -29,7 +29,6 @@
#endif
#endif
#include "agent.h"
#include "agent.h"
#include "HFO.cpp"
#include "strategy.h"
#include "strategy.h"
#include "field_analyzer.h"
#include "field_analyzer.h"
...
@@ -108,21 +107,7 @@
...
@@ -108,21 +107,7 @@
#include <netinet/in.h>
#include <netinet/in.h>
using
namespace
rcsc
;
using
namespace
rcsc
;
using
namespace
hfo
;
// Debugging tools to check for proper feature normalization
float
min_feat_val
=
1e8
;
float
max_feat_val
=
-
1e8
;
// Minimium and feature values
#define FEAT_MIN -1.
#define FEAT_MAX 1.
#define LOG_FEATURE(val) \
if (val <= min_feat_val) \
min_feat_val = val; \
if (val >= max_feat_val) \
max_feat_val = val; \
std::cout << "FEATURE " << val << " [" << min_feat_val << ", " << max_feat_val << "]" << std::endl;
Agent
::
Agent
()
Agent
::
Agent
()
:
PlayerAgent
(),
:
PlayerAgent
(),
...
@@ -250,21 +235,25 @@ void Agent::startServer(int server_port) {
...
@@ -250,21 +235,25 @@ void Agent::startServer(int server_port) {
struct
sockaddr_in
serv_addr
,
cli_addr
;
struct
sockaddr_in
serv_addr
,
cli_addr
;
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
sockfd
<
0
)
{
if
(
sockfd
<
0
)
{
error
(
"[Agent Server] ERROR opening socket"
);
perror
(
"[Agent Server] ERROR opening socket"
);
exit
(
1
);
}
}
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_addr
.
s_addr
=
INADDR_ANY
;
serv_addr
.
sin_addr
.
s_addr
=
INADDR_ANY
;
serv_addr
.
sin_port
=
htons
(
server_port
);
serv_addr
.
sin_port
=
htons
(
server_port
);
if
(
bind
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
if
(
bind
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
error
(
"[Agent Server] ERROR on binding"
);
perror
(
"[Agent Server] ERROR on binding"
);
exit
(
1
);
}
}
listen
(
sockfd
,
5
);
listen
(
sockfd
,
5
);
socklen_t
clilen
=
sizeof
(
cli_addr
);
socklen_t
clilen
=
sizeof
(
cli_addr
);
std
::
cout
<<
"[Agent Server] Waiting for client to connect... "
<<
std
::
endl
;
std
::
cout
<<
"[Agent Server] Waiting for client to connect... "
<<
std
::
endl
;
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
cli_addr
,
&
clilen
);
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
cli_addr
,
&
clilen
);
if
(
newsockfd
<
0
)
{
if
(
newsockfd
<
0
)
{
error
(
"[Agent Server] ERROR on accept"
);
perror
(
"[Agent Server] ERROR on accept"
);
close
(
sockfd
);
exit
(
1
);
}
}
std
::
cout
<<
"[Agent Server] Connected"
<<
std
::
endl
;
std
::
cout
<<
"[Agent Server] Connected"
<<
std
::
endl
;
server_running
=
true
;
server_running
=
true
;
...
@@ -274,20 +263,28 @@ void Agent::clientHandshake() {
...
@@ -274,20 +263,28 @@ void Agent::clientHandshake() {
// Send float 123.2345
// Send float 123.2345
float
f
=
123.2345
;
float
f
=
123.2345
;
if
(
send
(
newsockfd
,
&
f
,
sizeof
(
float
),
0
)
<
0
)
{
if
(
send
(
newsockfd
,
&
f
,
sizeof
(
float
),
0
)
<
0
)
{
error
(
"[Agent Server] ERROR sending from socket"
);
perror
(
"[Agent Server] ERROR sending from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Recieve float 5432.321
// Recieve float 5432.321
if
(
recv
(
newsockfd
,
&
f
,
sizeof
(
float
),
0
)
<
0
)
{
if
(
recv
(
newsockfd
,
&
f
,
sizeof
(
float
),
0
)
<
0
)
{
error
(
"[Agent Server] ERROR recv from socket"
);
perror
(
"[Agent Server] ERROR recv from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Check that error is within bounds
// Check that error is within bounds
if
(
abs
(
f
-
5432.321
)
>
1e-4
)
{
if
(
abs
(
f
-
5432.321
)
>
1e-4
)
{
error
(
"[Agent Server] Handshake failed. Improper float recieved."
);
perror
(
"[Agent Server] Handshake failed. Improper float recieved."
);
close
(
sockfd
);
exit
(
1
);
}
}
// Recieve the feature set to use
// Recieve the feature set to use
feature_set_t
feature_set
;
hfo
::
feature_set_t
feature_set
;
if
(
recv
(
newsockfd
,
&
feature_set
,
sizeof
(
int
),
0
)
<
0
)
{
if
(
recv
(
newsockfd
,
&
feature_set
,
sizeof
(
int
),
0
)
<
0
)
{
error
(
"[Agent Server] ERROR recv from socket"
);
perror
(
"[Agent Server] PERROR recv from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Create the corresponding FeatureExtractor
// Create the corresponding FeatureExtractor
if
(
feature_extractor
!=
NULL
)
{
if
(
feature_extractor
!=
NULL
)
{
...
@@ -299,15 +296,21 @@ void Agent::clientHandshake() {
...
@@ -299,15 +296,21 @@ void Agent::clientHandshake() {
int
numFeatures
=
feature_extractor
->
getNumFeatures
();
int
numFeatures
=
feature_extractor
->
getNumFeatures
();
assert
(
numFeatures
>
0
);
assert
(
numFeatures
>
0
);
if
(
send
(
newsockfd
,
&
numFeatures
,
sizeof
(
int
),
0
)
<
0
)
{
if
(
send
(
newsockfd
,
&
numFeatures
,
sizeof
(
int
),
0
)
<
0
)
{
error
(
"[Agent Server] ERROR sending from socket"
);
perror
(
"[Agent Server] ERROR sending from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Check that client has recieved correctly
// Check that client has recieved correctly
int
client_response
=
-
1
;
int
client_response
=
-
1
;
if
(
recv
(
newsockfd
,
&
client_response
,
sizeof
(
int
),
0
)
<
0
)
{
if
(
recv
(
newsockfd
,
&
client_response
,
sizeof
(
int
),
0
)
<
0
)
{
error
(
"[Agent Server] ERROR recv from socket"
);
perror
(
"[Agent Server] ERROR recv from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
if
(
client_response
!=
numFeatures
)
{
if
(
client_response
!=
numFeatures
)
{
error
(
"[Agent Server] Client incorrectly parsed the number of features."
);
perror
(
"[Agent Server] Client incorrectly parsed the number of features."
);
close
(
sockfd
);
exit
(
1
);
}
}
std
::
cout
<<
"[Agent Server] Handshake complete"
<<
std
::
endl
;
std
::
cout
<<
"[Agent Server] Handshake complete"
<<
std
::
endl
;
}
}
...
@@ -332,9 +335,9 @@ FeatureExtractor* Agent::getFeatureExtractor(feature_set_t feature_set_indx,
...
@@ -332,9 +335,9 @@ FeatureExtractor* Agent::getFeatureExtractor(feature_set_t feature_set_indx,
}
}
}
}
hfo_
status_t
Agent
::
getGameStatus
(
const
rcsc
::
AudioSensor
&
audio_sensor
,
status_t
Agent
::
getGameStatus
(
const
rcsc
::
AudioSensor
&
audio_sensor
,
long
&
lastTrainerMessageTime
)
{
long
&
lastTrainerMessageTime
)
{
hfo_
status_t
game_status
=
IN_GAME
;
status_t
game_status
=
IN_GAME
;
if
(
audio_sensor
.
trainerMessageTime
().
cycle
()
>
lastTrainerMessageTime
)
{
if
(
audio_sensor
.
trainerMessageTime
().
cycle
()
>
lastTrainerMessageTime
)
{
const
std
::
string
&
message
=
audio_sensor
.
trainerMessage
();
const
std
::
string
&
message
=
audio_sensor
.
trainerMessage
();
bool
recognized_message
=
true
;
bool
recognized_message
=
true
;
...
@@ -367,9 +370,11 @@ void Agent::actionImpl() {
...
@@ -367,9 +370,11 @@ void Agent::actionImpl() {
}
}
// Update and send the game status
// Update and send the game status
hfo_
status_t
game_status
=
getGameStatus
(
audioSensor
(),
lastTrainerMessageTime
);
status_t
game_status
=
getGameStatus
(
audioSensor
(),
lastTrainerMessageTime
);
if
(
send
(
newsockfd
,
&
game_status
,
sizeof
(
int
),
0
)
<
0
)
{
if
(
send
(
newsockfd
,
&
game_status
,
sizeof
(
int
),
0
)
<
0
)
{
error
(
"[Agent Server] ERROR sending from socket"
);
perror
(
"[Agent Server] ERROR sending from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
// Update and send the state features
// Update and send the state features
...
@@ -386,13 +391,16 @@ void Agent::actionImpl() {
...
@@ -386,13 +391,16 @@ void Agent::actionImpl() {
if
(
send
(
newsockfd
,
&
(
features
.
front
()),
if
(
send
(
newsockfd
,
&
(
features
.
front
()),
features
.
size
()
*
sizeof
(
float
),
0
)
<
0
)
{
features
.
size
()
*
sizeof
(
float
),
0
)
<
0
)
{
error
(
"[Agent Server] ERROR sending state features from socket"
);
perror
(
"[Agent Server] ERROR sending state features from socket"
);
exit
(
1
);
}
}
// Get the action
// Get the action
Action
action
;
Action
action
;
if
(
recv
(
newsockfd
,
&
action
,
sizeof
(
Action
),
0
)
<
0
)
{
if
(
recv
(
newsockfd
,
&
action
,
sizeof
(
Action
),
0
)
<
0
)
{
error
(
"[Agent Server] ERROR recv from socket"
);
perror
(
"[Agent Server] ERROR recv from socket"
);
close
(
sockfd
);
exit
(
1
);
}
}
switch
(
action
.
action
)
{
switch
(
action
.
action
)
{
case
DASH
:
case
DASH
:
...
@@ -421,10 +429,12 @@ void Agent::actionImpl() {
...
@@ -421,10 +429,12 @@ void Agent::actionImpl() {
break
;
break
;
case
QUIT
:
case
QUIT
:
std
::
cout
<<
"[Agent Server] Got quit from agent."
<<
std
::
endl
;
std
::
cout
<<
"[Agent Server] Got quit from agent."
<<
std
::
endl
;
close
(
sockfd
);
exit
(
0
);
exit
(
0
);
default:
default:
std
::
cerr
<<
"[Agent Server] ERROR Unsupported Action: "
std
::
cerr
<<
"[Agent Server] ERROR Unsupported Action: "
<<
action
.
action
<<
std
::
endl
;
<<
action
.
action
<<
std
::
endl
;
close
(
sockfd
);
exit
(
1
);
exit
(
1
);
}
}
...
...
src/agent.h
View file @
41b577db
...
@@ -43,11 +43,11 @@ public:
...
@@ -43,11 +43,11 @@ public:
virtual
FieldEvaluator
::
ConstPtr
getFieldEvaluator
()
const
;
virtual
FieldEvaluator
::
ConstPtr
getFieldEvaluator
()
const
;
// Get the current game status
// Get the current game status
static
hfo
_
status_t
getGameStatus
(
const
rcsc
::
AudioSensor
&
audio_sensor
,
static
hfo
::
status_t
getGameStatus
(
const
rcsc
::
AudioSensor
&
audio_sensor
,
long
&
lastTrainerMessageTime
);
long
&
lastTrainerMessageTime
);
// Returns the feature extractor corresponding to the feature_set_t
// Returns the feature extractor corresponding to the feature_set_t
static
FeatureExtractor
*
getFeatureExtractor
(
feature_set_t
feature_set
,
static
FeatureExtractor
*
getFeatureExtractor
(
hfo
::
feature_set_t
feature_set
,
int
num_teammates
,
int
num_teammates
,
int
num_opponents
,
int
num_opponents
,
bool
playing_offense
);
bool
playing_offense
);
...
...
src/sample_player.cpp
View file @
41b577db
...
@@ -233,7 +233,7 @@ bool SamplePlayer::getHFOConfig() {
...
@@ -233,7 +233,7 @@ bool SamplePlayer::getHFOConfig() {
const
AudioSensor
&
audio_sensor
=
audioSensor
();
const
AudioSensor
&
audio_sensor
=
audioSensor
();
if
(
audio_sensor
.
trainerMessageTime
().
cycle
()
>
lastTrainerMessageTime
)
{
if
(
audio_sensor
.
trainerMessageTime
().
cycle
()
>
lastTrainerMessageTime
)
{
const
std
::
string
&
message
=
audio_sensor
.
trainerMessage
();
const
std
::
string
&
message
=
audio_sensor
.
trainerMessage
();
if
(
HFOEnvironment
::
ParseHFO
Config
(
message
,
hfo_config
))
{
if
(
hfo
::
HFOEnvironment
::
Parse
Config
(
message
,
hfo_config
))
{
lastTrainerMessageTime
=
audio_sensor
.
trainerMessageTime
().
cycle
();
lastTrainerMessageTime
=
audio_sensor
.
trainerMessageTime
().
cycle
();
if
(
config
().
teamName
().
compare
(
hfo_config
.
offense_team_name
)
==
0
)
{
if
(
config
().
teamName
().
compare
(
hfo_config
.
offense_team_name
)
==
0
)
{
playing_offense
=
true
;
playing_offense
=
true
;
...
...
src/sample_player.h
View file @
41b577db
...
@@ -102,7 +102,7 @@ protected:
...
@@ -102,7 +102,7 @@ protected:
// Listens for a HFO Config message
// Listens for a HFO Config message
bool
getHFOConfig
();
bool
getHFOConfig
();
HFO_
Config
hfo_config
;
hfo
::
Config
hfo_config
;
FeatureExtractor
*
feature_extractor
;
FeatureExtractor
*
feature_extractor
;
long
lastTrainerMessageTime
;
long
lastTrainerMessageTime
;
int
num_teammates
,
num_opponents
;
int
num_teammates
,
num_opponents
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment