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
44277b38
Commit
44277b38
authored
Mar 06, 2015
by
Matthew Hausknecht
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Further progress on the cpp and python interfaces.
parent
fdef3ceb
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
213 additions
and
28 deletions
+213
-28
CMakeLists.txt
CMakeLists.txt
+12
-3
HFO.py
HFO.py
+17
-6
example/hfo_example_agent.cpp
example/hfo_example_agent.cpp
+24
-0
example/hfo_example_agent.py
example/hfo_example_agent.py
+5
-2
include/HFO.hpp
include/HFO.hpp
+40
-0
src/HFO.cpp
src/HFO.cpp
+90
-0
src/agent.cpp
src/agent.cpp
+10
-15
src/agent.h
src/agent.h
+15
-2
No files found.
CMakeLists.txt
View file @
44277b38
cmake_minimum_required
(
VERSION 2.8.3
)
cmake_minimum_required
(
VERSION 2.8.3
)
find_package
(
Threads REQUIRED
)
find_package
(
Threads REQUIRED
)
# find_package(XMLRPC REQUIRED c++)
project
(
hfo
)
project
(
hfo
)
# Change these to reflect the location of your librcsc-4.1.0 install
# Change these to reflect the location of your librcsc-4.1.0 install
...
@@ -11,10 +10,10 @@ set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
...
@@ -11,10 +10,10 @@ set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set
(
CMAKE_RUNTIME_OUTPUT_DIRECTORY bin
)
set
(
CMAKE_RUNTIME_OUTPUT_DIRECTORY bin
)
include_directories
(
include_directories
(
${
CMAKE_CURRENT_SOURCE_DIR
}
/include
${
SOURCE_DIR
}
${
SOURCE_DIR
}
${
SOURCE_DIR
}
/chain_action
${
SOURCE_DIR
}
/chain_action
${
LIBRCSC_INCLUDE
}
${
LIBRCSC_INCLUDE
}
# ${XMLRPC_INCLUDE_DIRS}
)
)
link_directories
(
link_directories
(
...
@@ -22,7 +21,7 @@ link_directories(
...
@@ -22,7 +21,7 @@ link_directories(
)
)
file
(
GLOB SOURCES
${
SOURCE_DIR
}
/*.cpp
${
SOURCE_DIR
}
/chain_action/*.cpp
)
file
(
GLOB SOURCES
${
SOURCE_DIR
}
/*.cpp
${
SOURCE_DIR
}
/chain_action/*.cpp
)
list
(
REMOVE_ITEM SOURCES
${
SOURCE_DIR
}
/main_coach.cpp
${
SOURCE_DIR
}
/main_player.cpp
${
SOURCE_DIR
}
/main_trainer.cpp
${
SOURCE_DIR
}
/main_agent.cpp
${
SOURCE_DIR
}
/sample_coach.cpp
${
SOURCE_DIR
}
/sample_player.cpp
${
SOURCE_DIR
}
/sample_trainer.cpp
${
SOURCE_DIR
}
/agent.cpp
)
list
(
REMOVE_ITEM SOURCES
${
SOURCE_DIR
}
/main_coach.cpp
${
SOURCE_DIR
}
/main_player.cpp
${
SOURCE_DIR
}
/main_trainer.cpp
${
SOURCE_DIR
}
/main_agent.cpp
${
SOURCE_DIR
}
/sample_coach.cpp
${
SOURCE_DIR
}
/sample_player.cpp
${
SOURCE_DIR
}
/sample_trainer.cpp
${
SOURCE_DIR
}
/agent.cpp
${
SOURCE_DIR
}
/HFO.cpp
)
list
(
APPEND LINK_LIBS
list
(
APPEND LINK_LIBS
rcsc_agent
rcsc_agent
...
@@ -40,8 +39,18 @@ add_executable(sample_coach ${SOURCE_DIR}/main_coach.cpp ${SOURCE_DIR}/sample_co
...
@@ -40,8 +39,18 @@ add_executable(sample_coach ${SOURCE_DIR}/main_coach.cpp ${SOURCE_DIR}/sample_co
add_executable
(
sample_player
${
SOURCE_DIR
}
/main_player.cpp
${
SOURCE_DIR
}
/sample_player.cpp
${
SOURCES
}
)
add_executable
(
sample_player
${
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
${
CMAKE_CURRENT_SOURCE_DIR
}
/include/HFO.hpp
${
SOURCE_DIR
}
/HFO.cpp
)
set_target_properties
(
hfo-lib PROPERTIES OUTPUT_NAME hfo
)
set_target_properties
(
hfo-lib PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${
CMAKE_CURRENT_SOURCE_DIR
}
/lib
)
target_link_libraries
(
sample_coach
${
LINK_LIBS
}
)
target_link_libraries
(
sample_coach
${
LINK_LIBS
}
)
target_link_libraries
(
sample_player
${
LINK_LIBS
}
)
target_link_libraries
(
sample_player
${
LINK_LIBS
}
)
target_link_libraries
(
sample_trainer
${
LINK_LIBS
}
)
target_link_libraries
(
sample_trainer
${
LINK_LIBS
}
)
target_link_libraries
(
agent
${
LINK_LIBS
}
)
target_link_libraries
(
agent
${
LINK_LIBS
}
)
link_directories
(
${
CMAKE_CURRENT_SOURCE_DIR
}
/lib
)
add_executable
(
hfo_example_agent
${
CMAKE_CURRENT_SOURCE_DIR
}
/example/hfo_example_agent.cpp
)
set_target_properties
(
hfo_example_agent PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${
CMAKE_CURRENT_SOURCE_DIR
}
/example
)
target_link_libraries
(
hfo_example_agent hfo
)
add_dependencies
(
hfo_example_agent hfo-lib
)
HFO.py
View file @
44277b38
import
socket
,
struct
,
thread
,
time
import
socket
,
struct
,
thread
,
time
class
Actions
:
''' An enum of the possible HFO actions
Dash(power, relative_direction)
Turn(direction)
Tackle(direction)
Kick(power, direction)
'''
DASH
,
TURN
,
TACKLE
,
KICK
=
range
(
4
)
class
HFOEnvironment
(
object
):
class
HFOEnvironment
(
object
):
'''The HFOEnvironment is designed to be the main point of contact
'''
The HFOEnvironment is designed to be the main point of contact
between a learning agent and the Half-Field-Offense domain.
between a learning agent and the Half-Field-Offense domain.
'''
'''
...
@@ -23,12 +35,11 @@ class HFOEnvironment(object):
...
@@ -23,12 +35,11 @@ class HFOEnvironment(object):
continue
continue
else
:
else
:
break
break
print
'[Agent Client] Connected'
,
server_port
print
'[Agent Client] Connected'
self
.
handshakeAgentServer
()
self
.
handshakeAgentServer
()
def
handshakeAgentServer
(
self
):
def
handshakeAgentServer
(
self
):
'''Handshake with the agent's server. Returns the number of state
'''Handshake with the agent's server. '''
features in the domain. '''
# Recieve float 123.2345
# Recieve float 123.2345
data
=
self
.
socket
.
recv
(
struct
.
calcsize
(
"f"
))
data
=
self
.
socket
.
recv
(
struct
.
calcsize
(
"f"
))
f
=
struct
.
unpack
(
"f"
,
data
)[
0
]
f
=
struct
.
unpack
(
"f"
,
data
)[
0
]
...
@@ -53,9 +64,9 @@ class HFOEnvironment(object):
...
@@ -53,9 +64,9 @@ class HFOEnvironment(object):
features
=
struct
.
unpack
(
'f'
*
self
.
numFeatures
,
data
)
features
=
struct
.
unpack
(
'f'
*
self
.
numFeatures
,
data
)
return
features
return
features
def
act
(
self
,
action
_number
):
def
act
(
self
,
action
):
''' Send an action and recieve the resulting reward from the environment.'''
''' Send an action and recieve the resulting reward from the environment.'''
self
.
socket
.
send
(
struct
.
pack
(
"i
"
,
action_number
))
self
.
socket
.
send
(
struct
.
pack
(
"i
ff"
,
*
action
))
# TODO: Get the rewards from the domain
# TODO: Get the rewards from the domain
return
0
return
0
...
...
example/hfo_example_agent.cpp
0 → 100644
View file @
44277b38
#include <iostream>
#include <vector>
#include <HFO.hpp>
using
namespace
std
;
// First Start the server by calling start.py in bin
int
main
()
{
// Create the HFO environment
HFOEnvironment
hfo
;
// Connect the agent's server which should be listening if
// ./bin/start.py was called.
hfo
.
connectToAgentServer
();
// Continue until finished
while
(
true
)
{
// Grab the vector of state features for the current state
const
std
::
vector
<
float
>&
feature_vec
=
hfo
.
getState
();
// Create a dash action
Action
a
=
{
DASH
,
100.
,
0.
};
// Perform the dash and recieve the reward
float
reward
=
hfo
.
act
(
a
);
}
};
example/agent.py
→
example/
hfo_example_
agent.py
View file @
44277b38
...
@@ -3,10 +3,13 @@
...
@@ -3,10 +3,13 @@
import
imp
import
imp
# First Start the server by calling start.py in bin
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
# First Start the server by calling start.py in bin
# Load the HFO library
# Load the HFO library
hfo_module
=
imp
.
load_source
(
'HFO'
,
'../HFO.py'
)
hfo_module
=
imp
.
load_source
(
'HFO'
,
'../HFO.py'
)
# Get the possible actions
actions
=
hfo_module
.
Actions
# Create the HFO Environment
# Create the HFO Environment
hfo
=
hfo_module
.
HFOEnvironment
()
hfo
=
hfo_module
.
HFOEnvironment
()
hfo
.
connectToAgentServer
()
hfo
.
connectToAgentServer
()
...
@@ -15,6 +18,6 @@ if __name__ == '__main__':
...
@@ -15,6 +18,6 @@ if __name__ == '__main__':
# Grab the state features from the environment
# Grab the state features from the environment
features
=
hfo
.
getState
()
features
=
hfo
.
getState
()
# Take an action and get the reward
# Take an action and get the reward
reward
=
hfo
.
act
(
0
)
reward
=
hfo
.
act
(
(
actions
.
KICK
,
100
,
12.3
)
)
# Cleanup when finished
# Cleanup when finished
hfo
.
cleanup
()
hfo
.
cleanup
()
include/HFO.hpp
0 → 100644
View file @
44277b38
#ifndef __HFO_HPP__
#define __HFO_HPP__
#include <vector>
#include "agent.h"
class
HFOEnvironment
{
public:
HFOEnvironment
();
~
HFOEnvironment
();
// Connect to the server that controls the agent on the specified port.
void
connectToAgentServer
(
int
server_port
=
6008
);
// Get the current state of the domain. Returns a reference to feature_vec.
const
std
::
vector
<
float
>&
getState
();
// The actions available to the agent
// enum action_t
// {
// DASH, // Dash(power, relative_direction)
// TURN, // Turn(direction)
// TACKLE, // Tackle(direction)
// KICK // Kick(power, direction)
// };
// Take an action and recieve the resulting reward.
float
act
(
Action
action
);
protected:
int
numFeatures
;
// The number of features in this domain
int
sockfd
;
// Socket file desriptor for connection to agent server
std
::
vector
<
float
>
feature_vec
;
// Holds the features
// Handshake with the agent server to ensure data is being correctly
// passed. Also sets the number of features to expect.
virtual
void
handshakeAgentServer
();
};
#endif
src/HFO.cpp
0 → 100644
View file @
44277b38
#include "HFO.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <iostream>
void
error
(
const
char
*
msg
)
{
perror
(
msg
);
exit
(
0
);
}
HFOEnvironment
::
HFOEnvironment
()
{}
HFOEnvironment
::~
HFOEnvironment
()
{
close
(
sockfd
);
}
void
HFOEnvironment
::
connectToAgentServer
(
int
server_port
)
{
std
::
cout
<<
"[Agent Client] Connecting to Agent Server on port "
<<
server_port
<<
std
::
endl
;
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
sockfd
<
0
)
{
error
(
"ERROR opening socket"
);
}
struct
hostent
*
server
=
gethostbyname
(
"localhost"
);
if
(
server
==
NULL
)
{
fprintf
(
stderr
,
"ERROR, no such host
\n
"
);
exit
(
0
);
}
struct
sockaddr_in
serv_addr
;
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
serv_addr
.
sin_family
=
AF_INET
;
bcopy
((
char
*
)
server
->
h_addr
,
(
char
*
)
&
serv_addr
.
sin_addr
.
s_addr
,
server
->
h_length
);
serv_addr
.
sin_port
=
htons
(
server_port
);
int
status
=
-
1
;
while
(
status
<
0
)
{
status
=
connect
(
sockfd
,(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
sleep
(
1
);
}
std
::
cout
<<
"[Agent Client] Connected"
<<
std
::
endl
;
handshakeAgentServer
();
}
void
HFOEnvironment
::
handshakeAgentServer
()
{
// Recieve float 123.2345
float
f
;
if
(
recv
(
sockfd
,
&
f
,
sizeof
(
float
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR recv from socket"
);
}
// Check that error is within bounds
if
(
abs
(
f
-
123.2345
)
>
1e-4
)
{
error
(
"[Agent Client] Handshake failed. Improper float recieved."
);
}
// Send float 5432.321
f
=
5432.321
;
if
(
send
(
sockfd
,
&
f
,
sizeof
(
float
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR sending from socket"
);
}
// Recieve the number of features
if
(
recv
(
sockfd
,
&
numFeatures
,
sizeof
(
int
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR recv from socket"
);
}
if
(
send
(
sockfd
,
&
numFeatures
,
sizeof
(
int
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR sending from socket"
);
}
std
::
cout
<<
"[Agent Client] Handshake complete"
<<
std
::
endl
;
}
const
std
::
vector
<
float
>&
HFOEnvironment
::
getState
()
{
if
(
feature_vec
.
size
()
!=
numFeatures
)
{
feature_vec
.
resize
(
numFeatures
);
}
if
(
recv
(
sockfd
,
&
(
feature_vec
.
front
()),
numFeatures
*
sizeof
(
float
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR recieving state features from socket"
);
}
return
feature_vec
;
}
float
HFOEnvironment
::
act
(
Action
action
)
{
if
(
send
(
sockfd
,
&
action
,
sizeof
(
Action
),
0
)
<
0
)
{
error
(
"[Agent Client] ERROR sending from socket"
);
}
return
0.
;
}
src/agent.cpp
View file @
44277b38
...
@@ -446,34 +446,29 @@ void Agent::actionImpl() {
...
@@ -446,34 +446,29 @@ void Agent::actionImpl() {
}
}
// Get the action
// Get the action
action_t
action
;
Action
action
;
if
(
recv
(
newsockfd
,
&
action
,
sizeof
(
int
),
0
)
<
0
)
{
if
(
recv
(
newsockfd
,
&
action
,
sizeof
(
Action
),
0
)
<
0
)
{
error
(
"[Agent Server] ERROR recv from socket"
);
error
(
"[Agent Server] ERROR recv from socket"
);
}
}
switch
(
action
)
{
switch
(
action
.
action
)
{
case
DASH
:
case
DASH
:
this
->
doDash
(
100.
,
0
);
this
->
doDash
(
action
.
arg1
,
action
.
arg2
);
break
;
break
;
case
TURN
:
case
TURN
:
this
->
doTurn
(
10
);
this
->
doTurn
(
action
.
arg1
);
break
;
break
;
case
TACKLE
:
case
TACKLE
:
this
->
doTackle
(
0
,
false
);
this
->
doTackle
(
action
.
arg1
,
false
);
break
;
break
;
case
KICK
:
case
KICK
:
this
->
doKick
(
100.
,
0
);
this
->
doKick
(
action
.
arg1
,
action
.
arg2
);
break
;
break
;
default:
default:
error
(
"[Agent Server] Unsupported Action!"
);
std
::
cerr
<<
"[Agent Server] ERROR Unsupported Action: "
<<
action
.
action
<<
std
::
endl
;
exit
(
1
);
}
}
// char buffer[256];
// bzero(buffer,256);
// if (read(newsockfd,buffer,255) < 0) {
// error("[Agent Server] ERROR reading from socket");
// }
// printf("Here is the message: %s\n",buffer);
// TODO: How to get rewards?
// TODO: How to get rewards?
// For now let's not worry about turning the neck or setting the vision.
// For now let's not worry about turning the neck or setting the vision.
...
...
src/agent.h
View file @
44277b38
...
@@ -34,14 +34,27 @@
...
@@ -34,14 +34,27 @@
#include <rcsc/player/player_agent.h>
#include <rcsc/player/player_agent.h>
#include <vector>
#include <vector>
// The actions available to the agent
enum
action_t
{
DASH
,
// Dash(power, relative_direction)
TURN
,
// Turn(direction)
TACKLE
,
// Tackle(direction)
KICK
// Kick(power, direction)
};
struct
Action
{
action_t
action
;
float
arg1
;
float
arg2
;
};
class
Agent
:
public
rcsc
::
PlayerAgent
{
class
Agent
:
public
rcsc
::
PlayerAgent
{
public:
public:
Agent
();
Agent
();
virtual
~
Agent
();
virtual
~
Agent
();
virtual
FieldEvaluator
::
ConstPtr
getFieldEvaluator
()
const
;
virtual
FieldEvaluator
::
ConstPtr
getFieldEvaluator
()
const
;
enum
action_t
{
DASH
,
TURN
,
TACKLE
,
KICK
};
protected:
protected:
// You can override this method. But you must call
// You can override this method. But you must call
// PlayerAgent::initImpl() in this method.
// PlayerAgent::initImpl() in this method.
...
...
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