Commit baa4413c authored by sanmit's avatar sanmit

Updated ref to broadcast last player to touch the ball

parent 731070be
......@@ -215,9 +215,10 @@ class Trainer(object):
assert body[0] == 'referee', 'Expected referee message.'
_,ts,event = body
self._frame = int(ts)
if event == 'GOAL':
if 'GOAL' in event:
self._numGoals += 1
self._numGoalFrames += self._frame - self._lastTrialStart
event = 'GOAL'
elif event == 'OUT_OF_BOUNDS':
self._numBallsOOB += 1
elif 'CAPTURED_BY_DEFENSE' in event:
......
......@@ -25,7 +25,6 @@ int main(int argc, char** argv) {
// Play 5 episodes
for (int episode=0; ; episode++) {
int step = 0;
vector<int> game_status;
status_t status = IN_GAME;
while (status == IN_GAME) {
// Get the vector of state features for the current state
......@@ -41,8 +40,7 @@ int main(int argc, char** argv) {
// Do something with outgoing communication
hfo.say("Message");
// Advance the environment and get the game status
game_status = hfo.step();
status = (status_t)game_status[0];
status = hfo.step();
step+=2;
}
}
......
......@@ -37,14 +37,14 @@ if __name__ == '__main__':
hfo_env.act(HFO_Actions.DASH, 20.0, 0)
# Do something with outgoing communication
hfo_env.say('Message')
(status, playerIndex) = hfo_env.step()
status = hfo_env.step()
print 'Episode', episode, 'ended with',
# Check what the outcome of the episode was
if status == HFO_Status.GOAL:
print 'goal'
elif status == HFO_Status.CAPTURED_BY_DEFENSE:
print 'captured by defense', playerIndex
print 'captured by defense'
elif status == HFO_Status.OUT_OF_BOUNDS:
print 'out of bounds'
elif status == HFO_Status.OUT_OF_TIME:
......
......@@ -28,7 +28,7 @@ def play_hfo(num):
hfo_env.act(get_random_action())
else:
hfo_env.act(HFO_Actions.MOVE)
(status, playerIndex) = hfo_env.step()
status = hfo_env.step()
except:
pass
finally:
......
......@@ -17,7 +17,6 @@ int main() {
hfo.connectToAgentServer(6000, LOW_LEVEL_FEATURE_SET);
// Play 5 episodes
for (int episode=0; episode<5; episode++) {
vector<int> game_status;
status_t status = IN_GAME;
while (status == IN_GAME) {
// Get the vector of state features for the current state
......@@ -25,8 +24,7 @@ int main() {
// Perform the dash
hfo.act(DASH, 20.0);
// Advance the environment and recieve current game status
game_status = hfo.step();
status = (status_t)game_status[0];
status = hfo.step();
}
// Check what the outcome of the episode was
cout << "Episode " << episode << " ended with status: ";
......
......@@ -20,20 +20,20 @@ if __name__ == '__main__':
# feature set. See feature sets in hfo.py/hfo.hpp.
hfo.connectToAgentServer(port, HFO_Features.HIGH_LEVEL_FEATURE_SET)
# Play 5 episodes
for episode in xrange(5):
for episode in xrange(100):
status = HFO_Status.IN_GAME
while status == HFO_Status.IN_GAME:
# Grab the state features from the environment
features = hfo.getState()
# Take an action and get the current game status
hfo.act(HFO_Actions.DASH, 20.0, 0)
(status, playerIndex) = hfo.step()
status = hfo.step()
print 'Episode', episode, 'ended with',
# Check what the outcome of the episode was
if status == HFO_Status.GOAL:
print 'goal'
print 'goal', hfo.playerOnBall().unum
elif status == HFO_Status.CAPTURED_BY_DEFENSE:
print 'captured by defense', playerIndex
print 'captured by defense', hfo.playerOnBall().unum
elif status == HFO_Status.OUT_OF_BOUNDS:
print 'out of bounds'
elif status == HFO_Status.OUT_OF_TIME:
......
......@@ -27,7 +27,6 @@ int main(int argc, char** argv) {
hfo.connectToAgentServer(port, HIGH_LEVEL_FEATURE_SET);
// Play 5 episodes
for (int episode=0; ; episode++) {
vector<int> game_status;
status_t status = IN_GAME;
while (status == IN_GAME) {
// Get the vector of state features for the current state
......@@ -35,8 +34,7 @@ int main(int argc, char** argv) {
// Perform the action
hfo.act(get_random_high_lv_action());
// Advance the environment and get the game status
game_status = hfo.step();
status = (status_t)game_status[0];
status = hfo.step();
}
// Check what the outcome of the episode was
......@@ -46,7 +44,7 @@ int main(int argc, char** argv) {
cout << "goal" << endl;
break;
case CAPTURED_BY_DEFENSE:
cout << "captured by defense " << game_status[1] << endl;
cout << "captured by defense " << endl;
break;
case OUT_OF_BOUNDS:
cout << "out of bounds" << endl;
......
......@@ -51,7 +51,6 @@ int main(int argc, char** argv) {
hfo.connectToAgentServer(port, LOW_LEVEL_FEATURE_SET);
// Play 5 episodes
for (int episode=0; ; episode++) {
vector<int> game_status;
status_t status = IN_GAME;
while (status == IN_GAME) {
// Get the vector of state features for the current state
......@@ -59,8 +58,7 @@ int main(int argc, char** argv) {
// Perform the action and recieve the current game status
hfo.act(get_random_low_lv_action(), arg1, arg2);
game_status = hfo.step();
status = (status_t)game_status[0];
status = hfo.step();
}
}
};
......@@ -24,7 +24,6 @@ int main(int argc, char** argv) {
// feature set. See manual for more information on feature sets.
hfo.connectToAgentServer(port, HIGH_LEVEL_FEATURE_SET);
for (int episode=0; ; episode++) {
vector<int> game_status;
status_t status = IN_GAME;
int step = 0;
while (status == IN_GAME) {
......@@ -35,8 +34,7 @@ int main(int argc, char** argv) {
float target_y = cos((step % 360) * PI/180);
hfo.act(DRIBBLE_TO, target_x, target_y);
// Advance the environment and get the game status
game_status = hfo.step();
status = (status_t)game_status[0];
status = hfo.step();
step += 2;
}
}
......
......@@ -22,7 +22,6 @@ int main(int argc, char** argv) {
// feature set. See manual for more information on feature sets.
hfo.connectToAgentServer(port, HIGH_LEVEL_FEATURE_SET);
for (int episode=0; ; episode++) {
vector<int> game_status;
status_t status = IN_GAME;
while (status == IN_GAME) {
// Get the vector of state features for the current state
......@@ -45,18 +44,17 @@ int main(int argc, char** argv) {
hfo.act(INTERCEPT);
}
// Advance the environment and get the game status
game_status = hfo.step();
status = (status_t)game_status[0];
status = hfo.step();
}
// Check what the outcome of the episode was
cout << "Episode " << episode << " ended with status: ";
switch (status) {
case GOAL:
cout << "goal" << endl;
cout << "goal " << hfo.playerOnBall().unum << endl;
break;
case CAPTURED_BY_DEFENSE:
cout << "captured by defense " << game_status[1] << endl;
cout << "captured by defense " << hfo.playerOnBall().unum << endl;
break;
case OUT_OF_BOUNDS:
cout << "out of bounds" << endl;
......
......@@ -22,7 +22,6 @@ int main(int argc, char** argv) {
float target_x = 1.0;
float target_y = 1.0;
for (int episode=0; ; episode++) {
vector<int> game_status;
status_t status = IN_GAME;
if (episode % 2 != 0) {
target_x *= -1;
......@@ -36,8 +35,7 @@ int main(int argc, char** argv) {
// Perform the action
hfo.act(MOVE_TO, target_x, target_y);
// Advance the environment and get the game status
game_status = hfo.step();
status = (status_t)game_status[0];
status = hfo.step();
}
}
hfo.act(QUIT);
......
import socket, struct, thread, time
import socket, struct, thread, time, collections
class HFO_Features:
''' An enum of the possible HFO feature sets. For descriptions see
......@@ -30,10 +30,17 @@ class HFO_Actions:
DASH, TURN, TACKLE, KICK, KICK_TO, MOVE_TO, DRIBBLE_TO, INTERCEPT, \
MOVE, SHOOT, PASS, DRIBBLE, CATCH, NOOP, QUIT = range(15)
HFO_Player = collections.namedtuple("HFO_Player", "side unum")
class HFO_Status:
''' Current status of the HFO game. '''
IN_GAME, GOAL, CAPTURED_BY_DEFENSE, OUT_OF_BOUNDS, OUT_OF_TIME = range(5)
class HFO_SideID:
''' Team side for a player or object'''
RIGHT, NEUTRAL, LEFT = range(-1, 2)
class HFOEnvironment(object):
''' The HFOEnvironment is designed to be the main point of contact
......@@ -47,6 +54,7 @@ class HFOEnvironment(object):
self.requested_action = None # Action to execute and parameters
self.say_msg = '' # Outgoing message to say
self.hear_msg = '' # Incoming heard message
self.player_on_ball = None # Current player holding the ball
def NumParams(self, action_type):
''' Returns the number of required parameters for each action type. '''
......@@ -117,8 +125,8 @@ class HFOEnvironment(object):
# Send what we recieved
self.socket.send(struct.pack("i", self.numFeatures))
# Get the current game status
data = self.socket.recv(struct.calcsize("i")*2)
status = struct.unpack("ii", data)[0]
data = self.socket.recv(struct.calcsize("iii"))
status = struct.unpack("iii", data)[0]
assert status == HFO_Status.IN_GAME, "Status check failed"
print '[Agent Client] Handshake complete'
......@@ -144,6 +152,10 @@ class HFOEnvironment(object):
''' Receive incoming communications from other players. '''
return self.hear_msg
def playerOnBall(self):
''' Get the current player holding the ball'''
return self.player_on_ball
def step(self):
''' Indicates the agent is done and the environment should
progress. Returns the game status after the step'''
......@@ -157,9 +169,9 @@ class HFOEnvironment(object):
self.say_msg = ''
# Get the current game status
data = self.socket.recv(struct.calcsize("i") * 2)
status = struct.unpack("ii", data)[0]
playerIndex = struct.unpack("ii", data)[1]
data = self.socket.recv(struct.calcsize("iii"))
status = struct.unpack("iii", data)[0]
self.player_on_ball = HFO_Player(struct.unpack("iii", data)[1], struct.unpack("iii",data)[2])
# Get the next state features
state_data = self.socket.recv(struct.calcsize('f')*self.numFeatures)
......@@ -176,7 +188,7 @@ class HFOEnvironment(object):
hearMsgData = self.socket.recv(struct.calcsize('c')*hearMsgLength)
self.hear_msg = struct.unpack(str(hearMsgLength)+'s', hearMsgData)[0]
return (status, playerIndex)
return status
def cleanup(self):
''' Send a quit and close the connection to the agent's server. '''
......
......@@ -248,13 +248,13 @@ void HFOEnvironment::handshakeAgentServer(feature_set_t feature_set) {
exit(1);
}
// Recieve the game status
std::vector<int> game_status(2, -1);
if (recv(sockfd, &(game_status.front()), 2 * sizeof(int), 0) < 0) {
std::vector<int> game_status(3, -1);
if (recv(sockfd, &(game_status.front()), 3 * sizeof(int), 0) < 0) {
perror("[Agent Client] ERROR receiving game status from socket");
close(sockfd);
exit(1);
}
if ((status_t)game_status[0] != IN_GAME) {
if (game_status[0] != IN_GAME) {
std::cout << "[Agent Client] Handshake failed: status check." << std::endl;
close(sockfd);
exit(1);
......@@ -289,8 +289,12 @@ std::string HFOEnvironment::hear() {
return hear_msg;
}
std::vector<int> HFOEnvironment::step() {
std::vector<int> game_status(2, -1);
Player HFOEnvironment::playerOnBall(){
return player_on_ball;
}
status_t HFOEnvironment::step() {
status_t game_status;
// Send the action_type
if (send(sockfd, &requested_action, sizeof(action_t), 0) < 0) {
......@@ -328,11 +332,16 @@ std::vector<int> HFOEnvironment::step() {
say_msg.clear();
// Get the game status
if (recv(sockfd, &(game_status.front()), 2 * sizeof(int), 0) < 0) {
std::vector<int> full_status(3,-1);
if (recv(sockfd, &(full_status.front()), 3 * sizeof(int), 0) < 0) {
perror("[Agent Client] ERROR receiving game status from socket");
close(sockfd);
exit(1);
}
game_status = (status_t)full_status[0];
player_on_ball.side = (rcsc::SideID)full_status[1];
player_on_ball.unum = full_status[2];
// Get the next game state
if (recv(sockfd, &(feature_vec.front()), numFeatures * sizeof(float), 0) < 0) {
perror("[Agent Client] ERROR receiving state features from socket");
......
......@@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include <rcsc/types.h>
namespace hfo {
......@@ -63,6 +64,11 @@ struct Config {
std::vector<int> defense_nums; // Defensive player numbers
};
struct Player {
rcsc::SideID side; // SideID is an enum: 1 = LEFT, 0 = NEUTRAL, -1 = RIGHT
int unum;
};
class HFOEnvironment {
public:
HFOEnvironment();
......@@ -92,9 +98,12 @@ class HFOEnvironment {
virtual void say(const std::string& message);
virtual std::string hear();
// Get the current player holding the ball
virtual Player playerOnBall();
// Indicates the agent is done and the environment should
// progress. Returns the game status after the step
virtual std::vector<int> step();
virtual status_t step();
protected:
int numFeatures; // The number of features in this domain
......@@ -103,6 +112,7 @@ class HFOEnvironment {
action_t requested_action; // Action requested
std::vector<float> action_params; // Action parameters
std::string say_msg, hear_msg; // Messages to hear/say
Player player_on_ball;
// Handshake with the agent server to ensure data is being correctly
// passed. Also sets the number of features to expect.
......
......@@ -366,23 +366,40 @@ FeatureExtractor* Agent::getFeatureExtractor(feature_set_t feature_set_indx,
std::vector<int> Agent::getGameStatus(const rcsc::AudioSensor& audio_sensor,
long& lastTrainerMessageTime) {
std::vector<int> status;
status_t game_status = IN_GAME;
int playerIndex = -1; // Keeps track of which defender stopped the shot
int playerTeam = 1; // 1 = offense, -1 = defense, 0 = no possession
if (audio_sensor.trainerMessageTime().cycle() > lastTrainerMessageTime) {
const std::string& message = audio_sensor.trainerMessage();
bool recognized_message = true;
if (message.compare("GOAL") == 0) {
if (message.find("GOAL") != std::string::npos){
playerIndex = atoi((message.substr(message.find("-")+1)).c_str());
playerTeam = 1;
game_status = GOAL;
} else if (message.find("CAPTURED_BY_DEFENSE") != std::string::npos) {
playerIndex = atoi((message.substr(message.find("-")+1)).c_str());
playerTeam = -1;
game_status = CAPTURED_BY_DEFENSE;
} else if (message.compare("OUT_OF_BOUNDS") == 0) {
game_status = OUT_OF_BOUNDS;
} else if (message.compare("OUT_OF_TIME") == 0) {
game_status = OUT_OF_TIME;
} else {
} else if (message.find("IN_GAME") != std::string::npos){
switch (message.at(message.find("-")+1)){
case 'L':
playerTeam = 1;
break;
case 'R':
playerTeam = -1;
break;
case 'U':
playerTeam = 0;
break;
}
playerIndex = atoi((message.substr(message.find("-")+2)).c_str());
}
else {
recognized_message = false;
}
if (recognized_message) {
......@@ -390,6 +407,7 @@ std::vector<int> Agent::getGameStatus(const rcsc::AudioSensor& audio_sensor,
}
}
status.push_back(game_status);
status.push_back(playerTeam);
status.push_back(playerIndex);
return status;
}
......
......@@ -34,6 +34,7 @@
#include "feature_extractor.h"
#include <rcsc/player/player_agent.h>
#include <rcsc/types.h>
#include <vector>
class Agent : public rcsc::PlayerAgent {
......
......@@ -269,10 +269,9 @@ SamplePlayer::actionImpl()
hfo::LOW_LEVEL_FEATURE_SET, num_teammates, num_opponents, playing_offense);
}
} else {
std::vector<int> full_status = Agent::getGameStatus(
std::vector<int> game_status = Agent::getGameStatus(
audioSensor(), lastTrainerMessageTime);
hfo::status_t game_status = (hfo::status_t)full_status[0];
elog.addText(Logger::WORLD, "GameStatus %d", game_status);
elog.addText(Logger::WORLD, "GameStatus %d", game_status[0]);
elog.flush();
feature_extractor->ExtractFeatures(this->world());
feature_extractor->LogFeatures();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment