Commit 731070be authored by sanmit's avatar sanmit

Added player index to defense captures. [Interface breaking change]

parent 2687b320
...@@ -220,8 +220,9 @@ class Trainer(object): ...@@ -220,8 +220,9 @@ class Trainer(object):
self._numGoalFrames += self._frame - self._lastTrialStart self._numGoalFrames += self._frame - self._lastTrialStart
elif event == 'OUT_OF_BOUNDS': elif event == 'OUT_OF_BOUNDS':
self._numBallsOOB += 1 self._numBallsOOB += 1
elif event == 'CAPTURED_BY_DEFENSE': elif 'CAPTURED_BY_DEFENSE' in event:
self._numBallsCaptured += 1 self._numBallsCaptured += 1
event = "CAPTURED_BY_DEFENSE" # This clears the defender number from the printout below, but it doesn't really matter here.
elif event == 'OUT_OF_TIME': elif event == 'OUT_OF_TIME':
self._numOutOfTime += 1 self._numOutOfTime += 1
elif event == 'HFO_FINISHED': elif event == 'HFO_FINISHED':
......
...@@ -25,6 +25,7 @@ int main(int argc, char** argv) { ...@@ -25,6 +25,7 @@ int main(int argc, char** argv) {
// Play 5 episodes // Play 5 episodes
for (int episode=0; ; episode++) { for (int episode=0; ; episode++) {
int step = 0; int step = 0;
vector<int> game_status;
status_t status = IN_GAME; status_t status = IN_GAME;
while (status == IN_GAME) { while (status == IN_GAME) {
// Get the vector of state features for the current state // Get the vector of state features for the current state
...@@ -40,7 +41,8 @@ int main(int argc, char** argv) { ...@@ -40,7 +41,8 @@ int main(int argc, char** argv) {
// Do something with outgoing communication // Do something with outgoing communication
hfo.say("Message"); hfo.say("Message");
// Advance the environment and get the game status // Advance the environment and get the game status
status = hfo.step(); game_status = hfo.step();
status = (status_t)game_status[0];
step+=2; step+=2;
} }
} }
......
...@@ -37,20 +37,20 @@ if __name__ == '__main__': ...@@ -37,20 +37,20 @@ if __name__ == '__main__':
hfo_env.act(HFO_Actions.DASH, 20.0, 0) hfo_env.act(HFO_Actions.DASH, 20.0, 0)
# Do something with outgoing communication # Do something with outgoing communication
hfo_env.say('Message') hfo_env.say('Message')
status = hfo_env.step() (status, playerIndex) = hfo_env.step()
print 'Episode', episode, 'ended with', print 'Episode', episode, 'ended with',
# Check what the outcome of the episode was # Check what the outcome of the episode was
if status == HFO_Status.GOAL: if status == HFO_Status.GOAL:
print 'goal' print 'goal'
elif status == HFO_Status.CAPTURED_BY_DEFENSE: elif status == HFO_Status.CAPTURED_BY_DEFENSE:
print 'captured by defense' print 'captured by defense', playerIndex
elif status == HFO_Status.OUT_OF_BOUNDS: elif status == HFO_Status.OUT_OF_BOUNDS:
print 'out of bounds' print 'out of bounds'
elif status == HFO_Status.OUT_OF_TIME: elif status == HFO_Status.OUT_OF_TIME:
print 'out of time' print 'out of time'
else: else:
print 'Unknown status', status print 'Unknown status', status
exit() exit()
# Cleanup when finished # Cleanup when finished
hfo_env.cleanup() hfo_env.cleanup()
...@@ -28,7 +28,7 @@ def play_hfo(num): ...@@ -28,7 +28,7 @@ def play_hfo(num):
hfo_env.act(get_random_action()) hfo_env.act(get_random_action())
else: else:
hfo_env.act(HFO_Actions.MOVE) hfo_env.act(HFO_Actions.MOVE)
status = hfo_env.step() (status, playerIndex) = hfo_env.step()
except: except:
pass pass
finally: finally:
......
...@@ -17,6 +17,7 @@ int main() { ...@@ -17,6 +17,7 @@ int main() {
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++) {
vector<int> game_status;
status_t status = IN_GAME; status_t status = IN_GAME;
while (status == IN_GAME) { while (status == IN_GAME) {
// Get the vector of state features for the current state // Get the vector of state features for the current state
...@@ -24,7 +25,8 @@ int main() { ...@@ -24,7 +25,8 @@ int main() {
// Perform the dash // Perform the dash
hfo.act(DASH, 20.0); hfo.act(DASH, 20.0);
// Advance the environment and recieve current game status // Advance the environment and recieve current game status
status = hfo.step(); game_status = hfo.step();
status = (status_t)game_status[0];
} }
// Check what the outcome of the episode was // Check what the outcome of the episode was
cout << "Episode " << episode << " ended with status: "; cout << "Episode " << episode << " ended with status: ";
......
...@@ -27,13 +27,13 @@ if __name__ == '__main__': ...@@ -27,13 +27,13 @@ if __name__ == '__main__':
features = hfo.getState() features = hfo.getState()
# Take an action and get the current game status # Take an action and get the current game status
hfo.act(HFO_Actions.DASH, 20.0, 0) hfo.act(HFO_Actions.DASH, 20.0, 0)
hfo.step() (status, playerIndex) = hfo.step()
print 'Episode', episode, 'ended with', print 'Episode', episode, 'ended with',
# Check what the outcome of the episode was # Check what the outcome of the episode was
if status == HFO_Status.GOAL: if status == HFO_Status.GOAL:
print 'goal' print 'goal'
elif status == HFO_Status.CAPTURED_BY_DEFENSE: elif status == HFO_Status.CAPTURED_BY_DEFENSE:
print 'captured by defense' print 'captured by defense', playerIndex
elif status == HFO_Status.OUT_OF_BOUNDS: elif status == HFO_Status.OUT_OF_BOUNDS:
print 'out of bounds' print 'out of bounds'
elif status == HFO_Status.OUT_OF_TIME: elif status == HFO_Status.OUT_OF_TIME:
......
...@@ -27,6 +27,7 @@ int main(int argc, char** argv) { ...@@ -27,6 +27,7 @@ int main(int argc, char** argv) {
hfo.connectToAgentServer(port, HIGH_LEVEL_FEATURE_SET); hfo.connectToAgentServer(port, HIGH_LEVEL_FEATURE_SET);
// Play 5 episodes // Play 5 episodes
for (int episode=0; ; episode++) { for (int episode=0; ; episode++) {
vector<int> game_status;
status_t status = IN_GAME; status_t status = IN_GAME;
while (status == IN_GAME) { while (status == IN_GAME) {
// Get the vector of state features for the current state // Get the vector of state features for the current state
...@@ -34,7 +35,28 @@ int main(int argc, char** argv) { ...@@ -34,7 +35,28 @@ int main(int argc, char** argv) {
// Perform the action // Perform the action
hfo.act(get_random_high_lv_action()); hfo.act(get_random_high_lv_action());
// Advance the environment and get the game status // Advance the environment and get the game status
status = hfo.step(); game_status = hfo.step();
status = (status_t)game_status[0];
}
// Check what the outcome of the episode was
cout << "Episode " << episode << " ended with status: ";
switch (status) {
case GOAL:
cout << "goal" << endl;
break;
case CAPTURED_BY_DEFENSE:
cout << "captured by defense " << game_status[1] << endl;
break;
case OUT_OF_BOUNDS:
cout << "out of bounds" << endl;
break;
case OUT_OF_TIME:
cout << "out of time" << endl;
break;
default:
cout << "Unknown status " << status << endl;
exit(1);
} }
} }
hfo.act(QUIT); hfo.act(QUIT);
......
...@@ -51,6 +51,7 @@ int main(int argc, char** argv) { ...@@ -51,6 +51,7 @@ int main(int argc, char** argv) {
hfo.connectToAgentServer(port, LOW_LEVEL_FEATURE_SET); hfo.connectToAgentServer(port, LOW_LEVEL_FEATURE_SET);
// Play 5 episodes // Play 5 episodes
for (int episode=0; ; episode++) { for (int episode=0; ; episode++) {
vector<int> game_status;
status_t status = IN_GAME; status_t status = IN_GAME;
while (status == IN_GAME) { while (status == IN_GAME) {
// Get the vector of state features for the current state // Get the vector of state features for the current state
...@@ -58,7 +59,8 @@ int main(int argc, char** argv) { ...@@ -58,7 +59,8 @@ int main(int argc, char** argv) {
// Perform the action and recieve the current game status // Perform the action and recieve the current game status
hfo.act(get_random_low_lv_action(), arg1, arg2); hfo.act(get_random_low_lv_action(), arg1, arg2);
status = hfo.step(); game_status = hfo.step();
status = (status_t)game_status[0];
} }
} }
}; };
...@@ -24,6 +24,7 @@ int main(int argc, char** argv) { ...@@ -24,6 +24,7 @@ int main(int argc, char** argv) {
// feature set. See manual for more information on feature sets. // feature set. See manual for more information on feature sets.
hfo.connectToAgentServer(port, HIGH_LEVEL_FEATURE_SET); hfo.connectToAgentServer(port, HIGH_LEVEL_FEATURE_SET);
for (int episode=0; ; episode++) { for (int episode=0; ; episode++) {
vector<int> game_status;
status_t status = IN_GAME; status_t status = IN_GAME;
int step = 0; int step = 0;
while (status == IN_GAME) { while (status == IN_GAME) {
...@@ -34,7 +35,8 @@ int main(int argc, char** argv) { ...@@ -34,7 +35,8 @@ int main(int argc, char** argv) {
float target_y = cos((step % 360) * PI/180); float target_y = cos((step % 360) * PI/180);
hfo.act(DRIBBLE_TO, target_x, target_y); hfo.act(DRIBBLE_TO, target_x, target_y);
// Advance the environment and get the game status // Advance the environment and get the game status
status = hfo.step(); game_status = hfo.step();
status = (status_t)game_status[0];
step += 2; step += 2;
} }
} }
......
...@@ -22,6 +22,7 @@ int main(int argc, char** argv) { ...@@ -22,6 +22,7 @@ int main(int argc, char** argv) {
// feature set. See manual for more information on feature sets. // feature set. See manual for more information on feature sets.
hfo.connectToAgentServer(port, HIGH_LEVEL_FEATURE_SET); hfo.connectToAgentServer(port, HIGH_LEVEL_FEATURE_SET);
for (int episode=0; ; episode++) { for (int episode=0; ; episode++) {
vector<int> game_status;
status_t status = IN_GAME; status_t status = IN_GAME;
while (status == IN_GAME) { while (status == IN_GAME) {
// Get the vector of state features for the current state // Get the vector of state features for the current state
...@@ -44,8 +45,34 @@ int main(int argc, char** argv) { ...@@ -44,8 +45,34 @@ int main(int argc, char** argv) {
hfo.act(INTERCEPT); hfo.act(INTERCEPT);
} }
// Advance the environment and get the game status // Advance the environment and get the game status
status = hfo.step(); game_status = hfo.step();
status = (status_t)game_status[0];
} }
// Check what the outcome of the episode was
cout << "Episode " << episode << " ended with status: ";
switch (status) {
case GOAL:
cout << "goal" << endl;
break;
case CAPTURED_BY_DEFENSE:
cout << "captured by defense " << game_status[1] << endl;
break;
case OUT_OF_BOUNDS:
cout << "out of bounds" << endl;
break;
case OUT_OF_TIME:
cout << "out of time" << endl;
break;
default:
cout << "Unknown status " << status << endl;
exit(1);
}
} }
hfo.act(QUIT); hfo.act(QUIT);
}; };
...@@ -22,6 +22,7 @@ int main(int argc, char** argv) { ...@@ -22,6 +22,7 @@ int main(int argc, char** argv) {
float target_x = 1.0; float target_x = 1.0;
float target_y = 1.0; float target_y = 1.0;
for (int episode=0; ; episode++) { for (int episode=0; ; episode++) {
vector<int> game_status;
status_t status = IN_GAME; status_t status = IN_GAME;
if (episode % 2 != 0) { if (episode % 2 != 0) {
target_x *= -1; target_x *= -1;
...@@ -35,7 +36,8 @@ int main(int argc, char** argv) { ...@@ -35,7 +36,8 @@ int main(int argc, char** argv) {
// Perform the action // Perform the action
hfo.act(MOVE_TO, target_x, target_y); hfo.act(MOVE_TO, target_x, target_y);
// Advance the environment and get the game status // Advance the environment and get the game status
status = hfo.step(); game_status = hfo.step();
status = (status_t)game_status[0];
} }
} }
hfo.act(QUIT); hfo.act(QUIT);
......
...@@ -117,8 +117,8 @@ class HFOEnvironment(object): ...@@ -117,8 +117,8 @@ class HFOEnvironment(object):
# Send what we recieved # Send what we recieved
self.socket.send(struct.pack("i", self.numFeatures)) self.socket.send(struct.pack("i", self.numFeatures))
# Get the current game status # Get the current game status
data = self.socket.recv(struct.calcsize("i")) data = self.socket.recv(struct.calcsize("i")*2)
status = struct.unpack("i", data)[0] status = struct.unpack("ii", data)[0]
assert status == HFO_Status.IN_GAME, "Status check failed" assert status == HFO_Status.IN_GAME, "Status check failed"
print '[Agent Client] Handshake complete' print '[Agent Client] Handshake complete'
...@@ -157,8 +157,9 @@ class HFOEnvironment(object): ...@@ -157,8 +157,9 @@ class HFOEnvironment(object):
self.say_msg = '' self.say_msg = ''
# Get the current game status # Get the current game status
data = self.socket.recv(struct.calcsize("i")) data = self.socket.recv(struct.calcsize("i") * 2)
status = struct.unpack("i", data)[0] status = struct.unpack("ii", data)[0]
playerIndex = struct.unpack("ii", data)[1]
# Get the next state features # Get the next state features
state_data = self.socket.recv(struct.calcsize('f')*self.numFeatures) state_data = self.socket.recv(struct.calcsize('f')*self.numFeatures)
...@@ -175,7 +176,7 @@ class HFOEnvironment(object): ...@@ -175,7 +176,7 @@ class HFOEnvironment(object):
hearMsgData = self.socket.recv(struct.calcsize('c')*hearMsgLength) hearMsgData = self.socket.recv(struct.calcsize('c')*hearMsgLength)
self.hear_msg = struct.unpack(str(hearMsgLength)+'s', hearMsgData)[0] self.hear_msg = struct.unpack(str(hearMsgLength)+'s', hearMsgData)[0]
return status return (status, playerIndex)
def cleanup(self): def cleanup(self):
''' Send a quit and close the connection to the agent's server. ''' ''' Send a quit and close the connection to the agent's server. '''
......
...@@ -248,13 +248,13 @@ void HFOEnvironment::handshakeAgentServer(feature_set_t feature_set) { ...@@ -248,13 +248,13 @@ void HFOEnvironment::handshakeAgentServer(feature_set_t feature_set) {
exit(1); exit(1);
} }
// Recieve the game status // Recieve the game status
status_t status; std::vector<int> game_status(2, -1);
if (recv(sockfd, &status, sizeof(status_t), 0) < 0) { if (recv(sockfd, &(game_status.front()), 2 * sizeof(int), 0) < 0) {
perror("[Agent Client] ERROR recv from socket"); perror("[Agent Client] ERROR receiving game status from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
if (status != IN_GAME) { if ((status_t)game_status[0] != IN_GAME) {
std::cout << "[Agent Client] Handshake failed: status check." << std::endl; std::cout << "[Agent Client] Handshake failed: status check." << std::endl;
close(sockfd); close(sockfd);
exit(1); exit(1);
...@@ -289,8 +289,8 @@ std::string HFOEnvironment::hear() { ...@@ -289,8 +289,8 @@ std::string HFOEnvironment::hear() {
return hear_msg; return hear_msg;
} }
status_t HFOEnvironment::step() { std::vector<int> HFOEnvironment::step() {
status_t game_status; std::vector<int> game_status(2, -1);
// Send the action_type // Send the action_type
if (send(sockfd, &requested_action, sizeof(action_t), 0) < 0) { if (send(sockfd, &requested_action, sizeof(action_t), 0) < 0) {
...@@ -328,14 +328,14 @@ status_t HFOEnvironment::step() { ...@@ -328,14 +328,14 @@ status_t HFOEnvironment::step() {
say_msg.clear(); say_msg.clear();
// Get the game status // Get the game status
if (recv(sockfd, &game_status, sizeof(status_t), 0) < 0) { if (recv(sockfd, &(game_status.front()), 2 * sizeof(int), 0) < 0) {
perror("[Agent Client] ERROR recieving from socket"); perror("[Agent Client] ERROR receiving game status from socket");
close(sockfd); close(sockfd);
exit(1); 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) {
perror("[Agent Client] ERROR recieving state features from socket"); perror("[Agent Client] ERROR receiving state features from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
...@@ -345,7 +345,7 @@ status_t HFOEnvironment::step() { ...@@ -345,7 +345,7 @@ status_t HFOEnvironment::step() {
// Message length // Message length
uint32_t msgLength; uint32_t msgLength;
if (recv(sockfd, &msgLength, sizeof(uint32_t), 0) < 0){ if (recv(sockfd, &msgLength, sizeof(uint32_t), 0) < 0){
perror("[Agent Client] ERROR recieving hear message length from socket"); perror("[Agent Client] ERROR receiving hear message length from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
...@@ -354,7 +354,7 @@ status_t HFOEnvironment::step() { ...@@ -354,7 +354,7 @@ status_t HFOEnvironment::step() {
std::vector<char> hearMsgBuffer; std::vector<char> hearMsgBuffer;
hearMsgBuffer.resize(msgLength); hearMsgBuffer.resize(msgLength);
if (recv(sockfd, &hearMsgBuffer[0], msgLength, 0) < 0){ if (recv(sockfd, &hearMsgBuffer[0], msgLength, 0) < 0){
perror("[Agent Client] ERROR recieving hear message from socket"); perror("[Agent Client] ERROR receiving hear message from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
......
...@@ -94,7 +94,7 @@ class HFOEnvironment { ...@@ -94,7 +94,7 @@ class HFOEnvironment {
// Indicates the agent is done and the environment should // Indicates the agent is done and the environment should
// progress. Returns the game status after the step // progress. Returns the game status after the step
virtual status_t step(); virtual std::vector<int> step();
protected: protected:
int numFeatures; // The number of features in this domain int numFeatures; // The number of features in this domain
......
...@@ -364,15 +364,19 @@ FeatureExtractor* Agent::getFeatureExtractor(feature_set_t feature_set_indx, ...@@ -364,15 +364,19 @@ FeatureExtractor* Agent::getFeatureExtractor(feature_set_t feature_set_indx,
} }
} }
status_t Agent::getGameStatus(const rcsc::AudioSensor& audio_sensor, std::vector<int> Agent::getGameStatus(const rcsc::AudioSensor& audio_sensor,
long& lastTrainerMessageTime) { long& lastTrainerMessageTime) {
std::vector<int> status;
status_t game_status = IN_GAME; status_t game_status = IN_GAME;
int playerIndex = -1; // Keeps track of which defender stopped the shot
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;
if (message.compare("GOAL") == 0) { if (message.compare("GOAL") == 0) {
game_status = GOAL; game_status = GOAL;
} else if (message.compare("CAPTURED_BY_DEFENSE") == 0) { } else if (message.find("CAPTURED_BY_DEFENSE") != std::string::npos) {
playerIndex = atoi((message.substr(message.find("-")+1)).c_str());
game_status = CAPTURED_BY_DEFENSE; game_status = CAPTURED_BY_DEFENSE;
} else if (message.compare("OUT_OF_BOUNDS") == 0) { } else if (message.compare("OUT_OF_BOUNDS") == 0) {
game_status = OUT_OF_BOUNDS; game_status = OUT_OF_BOUNDS;
...@@ -385,7 +389,9 @@ status_t Agent::getGameStatus(const rcsc::AudioSensor& audio_sensor, ...@@ -385,7 +389,9 @@ status_t Agent::getGameStatus(const rcsc::AudioSensor& audio_sensor,
lastTrainerMessageTime = audio_sensor.trainerMessageTime().cycle(); lastTrainerMessageTime = audio_sensor.trainerMessageTime().cycle();
} }
} }
return game_status; status.push_back(game_status);
status.push_back(playerIndex);
return status;
} }
/*! /*!
...@@ -403,20 +409,20 @@ void Agent::actionImpl() { ...@@ -403,20 +409,20 @@ void Agent::actionImpl() {
} }
// Update and send the game status // Update and send the game status
status_t game_status = getGameStatus(audioSensor(), lastTrainerMessageTime); std::vector<int> game_status = getGameStatus(audioSensor(), lastTrainerMessageTime);
if (send(newsockfd, &game_status, sizeof(int), 0) < 0) { if (send(newsockfd, &(game_status.front()), game_status.size() * sizeof(int), 0) < 0){
perror("[Agent Server] ERROR sending from socket"); perror("[Agent Server] ERROR sending game state from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
// Update and send the state features // Update and send the state features
const std::vector<float>& features = const std::vector<float>& features =
feature_extractor->ExtractFeatures(this->world()); feature_extractor->ExtractFeatures(this->world());
#ifdef ELOG #ifdef ELOG
if (config().record()) { if (config().record()) {
elog.addText(Logger::WORLD, "GameStatus %d", game_status); elog.addText(Logger::WORLD, "GameStatus %d", game_status[0]);
elog.flush(); elog.flush();
feature_extractor->LogFeatures(); feature_extractor->LogFeatures();
} }
......
...@@ -43,7 +43,7 @@ public: ...@@ -43,7 +43,7 @@ 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 std::vector<int> 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
......
...@@ -269,8 +269,9 @@ SamplePlayer::actionImpl() ...@@ -269,8 +269,9 @@ SamplePlayer::actionImpl()
hfo::LOW_LEVEL_FEATURE_SET, num_teammates, num_opponents, playing_offense); hfo::LOW_LEVEL_FEATURE_SET, num_teammates, num_opponents, playing_offense);
} }
} else { } else {
hfo::status_t game_status = Agent::getGameStatus( std::vector<int> full_status = Agent::getGameStatus(
audioSensor(), lastTrainerMessageTime); 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);
elog.flush(); elog.flush();
feature_extractor->ExtractFeatures(this->world()); feature_extractor->ExtractFeatures(this->world());
......
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