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):
self._numGoalFrames += self._frame - self._lastTrialStart
elif event == 'OUT_OF_BOUNDS':
self._numBallsOOB += 1
elif event == 'CAPTURED_BY_DEFENSE':
elif 'CAPTURED_BY_DEFENSE' in event:
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':
self._numOutOfTime += 1
elif event == 'HFO_FINISHED':
......
......@@ -25,6 +25,7 @@ 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
......@@ -40,7 +41,8 @@ int main(int argc, char** argv) {
// Do something with outgoing communication
hfo.say("Message");
// Advance the environment and get the game status
status = hfo.step();
game_status = hfo.step();
status = (status_t)game_status[0];
step+=2;
}
}
......
......@@ -37,20 +37,20 @@ if __name__ == '__main__':
hfo_env.act(HFO_Actions.DASH, 20.0, 0)
# Do something with outgoing communication
hfo_env.say('Message')
status = hfo_env.step()
(status, playerIndex) = 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'
elif status == HFO_Status.OUT_OF_BOUNDS:
print 'out of bounds'
elif status == HFO_Status.OUT_OF_TIME:
print 'out of time'
else:
print 'Unknown status', status
exit()
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
elif status == HFO_Status.OUT_OF_BOUNDS:
print 'out of bounds'
elif status == HFO_Status.OUT_OF_TIME:
print 'out of time'
else:
print 'Unknown status', status
exit()
# Cleanup when finished
hfo_env.cleanup()
......@@ -28,7 +28,7 @@ def play_hfo(num):
hfo_env.act(get_random_action())
else:
hfo_env.act(HFO_Actions.MOVE)
status = hfo_env.step()
(status, playerIndex) = hfo_env.step()
except:
pass
finally:
......
......@@ -17,6 +17,7 @@ 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
......@@ -24,7 +25,8 @@ int main() {
// Perform the dash
hfo.act(DASH, 20.0);
// 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
cout << "Episode " << episode << " ended with status: ";
......
......@@ -27,13 +27,13 @@ if __name__ == '__main__':
features = hfo.getState()
# Take an action and get the current game status
hfo.act(HFO_Actions.DASH, 20.0, 0)
hfo.step()
(status, playerIndex) = hfo.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'
print 'captured by defense', playerIndex
elif status == HFO_Status.OUT_OF_BOUNDS:
print 'out of bounds'
elif status == HFO_Status.OUT_OF_TIME:
......
......@@ -27,6 +27,7 @@ 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
......@@ -34,7 +35,28 @@ int main(int argc, char** argv) {
// Perform the action
hfo.act(get_random_high_lv_action());
// 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);
......
......@@ -51,6 +51,7 @@ 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
......@@ -58,7 +59,8 @@ int main(int argc, char** argv) {
// Perform the action and recieve the current game status
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) {
// 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) {
......@@ -34,7 +35,8 @@ 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
status = hfo.step();
game_status = hfo.step();
status = (status_t)game_status[0];
step += 2;
}
}
......
......@@ -22,6 +22,7 @@ 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
......@@ -44,8 +45,34 @@ int main(int argc, char** argv) {
hfo.act(INTERCEPT);
}
// 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);
};
......@@ -22,6 +22,7 @@ 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;
......@@ -35,7 +36,8 @@ 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
status = hfo.step();
game_status = hfo.step();
status = (status_t)game_status[0];
}
}
hfo.act(QUIT);
......
......@@ -117,8 +117,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"))
status = struct.unpack("i", data)[0]
data = self.socket.recv(struct.calcsize("i")*2)
status = struct.unpack("ii", data)[0]
assert status == HFO_Status.IN_GAME, "Status check failed"
print '[Agent Client] Handshake complete'
......@@ -157,8 +157,9 @@ class HFOEnvironment(object):
self.say_msg = ''
# Get the current game status
data = self.socket.recv(struct.calcsize("i"))
status = struct.unpack("i", data)[0]
data = self.socket.recv(struct.calcsize("i") * 2)
status = struct.unpack("ii", data)[0]
playerIndex = struct.unpack("ii", data)[1]
# Get the next state features
state_data = self.socket.recv(struct.calcsize('f')*self.numFeatures)
......@@ -175,7 +176,7 @@ class HFOEnvironment(object):
hearMsgData = self.socket.recv(struct.calcsize('c')*hearMsgLength)
self.hear_msg = struct.unpack(str(hearMsgLength)+'s', hearMsgData)[0]
return status
return (status, playerIndex)
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
status_t status;
if (recv(sockfd, &status, sizeof(status_t), 0) < 0) {
perror("[Agent Client] ERROR recv from socket");
std::vector<int> game_status(2, -1);
if (recv(sockfd, &(game_status.front()), 2 * sizeof(int), 0) < 0) {
perror("[Agent Client] ERROR receiving game status from socket");
close(sockfd);
exit(1);
}
if (status != IN_GAME) {
if ((status_t)game_status[0] != IN_GAME) {
std::cout << "[Agent Client] Handshake failed: status check." << std::endl;
close(sockfd);
exit(1);
......@@ -289,8 +289,8 @@ std::string HFOEnvironment::hear() {
return hear_msg;
}
status_t HFOEnvironment::step() {
status_t game_status;
std::vector<int> HFOEnvironment::step() {
std::vector<int> game_status(2, -1);
// Send the action_type
if (send(sockfd, &requested_action, sizeof(action_t), 0) < 0) {
......@@ -328,14 +328,14 @@ status_t HFOEnvironment::step() {
say_msg.clear();
// Get the game status
if (recv(sockfd, &game_status, sizeof(status_t), 0) < 0) {
perror("[Agent Client] ERROR recieving from socket");
if (recv(sockfd, &(game_status.front()), 2 * sizeof(int), 0) < 0) {
perror("[Agent Client] ERROR receiving game status from socket");
close(sockfd);
exit(1);
}
// Get the next game state
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);
exit(1);
}
......@@ -345,7 +345,7 @@ status_t HFOEnvironment::step() {
// Message length
uint32_t msgLength;
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);
exit(1);
}
......@@ -354,7 +354,7 @@ status_t HFOEnvironment::step() {
std::vector<char> hearMsgBuffer;
hearMsgBuffer.resize(msgLength);
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);
exit(1);
}
......
......@@ -94,7 +94,7 @@ class HFOEnvironment {
// Indicates the agent is done and the environment should
// progress. Returns the game status after the step
virtual status_t step();
virtual std::vector<int> step();
protected:
int numFeatures; // The number of features in this domain
......
......@@ -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) {
std::vector<int> status;
status_t game_status = IN_GAME;
int playerIndex = -1; // Keeps track of which defender stopped the shot
if (audio_sensor.trainerMessageTime().cycle() > lastTrainerMessageTime) {
const std::string& message = audio_sensor.trainerMessage();
bool recognized_message = true;
if (message.compare("GOAL") == 0) {
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;
} else if (message.compare("OUT_OF_BOUNDS") == 0) {
game_status = OUT_OF_BOUNDS;
......@@ -385,7 +389,9 @@ status_t Agent::getGameStatus(const rcsc::AudioSensor& audio_sensor,
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() {
}
// Update and send the game status
status_t game_status = getGameStatus(audioSensor(), lastTrainerMessageTime);
if (send(newsockfd, &game_status, sizeof(int), 0) < 0) {
perror("[Agent Server] ERROR sending from socket");
std::vector<int> game_status = getGameStatus(audioSensor(), lastTrainerMessageTime);
if (send(newsockfd, &(game_status.front()), game_status.size() * sizeof(int), 0) < 0){
perror("[Agent Server] ERROR sending game state from socket");
close(sockfd);
exit(1);
exit(1);
}
// Update and send the state features
const std::vector<float>& features =
feature_extractor->ExtractFeatures(this->world());
#ifdef ELOG
if (config().record()) {
elog.addText(Logger::WORLD, "GameStatus %d", game_status);
elog.addText(Logger::WORLD, "GameStatus %d", game_status[0]);
elog.flush();
feature_extractor->LogFeatures();
}
......
......@@ -43,7 +43,7 @@ public:
virtual FieldEvaluator::ConstPtr getFieldEvaluator() const;
// 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);
// Returns the feature extractor corresponding to the feature_set_t
......
......@@ -269,8 +269,9 @@ SamplePlayer::actionImpl()
hfo::LOW_LEVEL_FEATURE_SET, num_teammates, num_opponents, playing_offense);
}
} else {
hfo::status_t game_status = Agent::getGameStatus(
std::vector<int> full_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.flush();
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