Commit 231a5252 authored by Matthew Hausknecht's avatar Matthew Hausknecht

Updated HFO to be more permissive about calls to get state and act.

parent c9eb5ab2
...@@ -21,7 +21,7 @@ if __name__ == '__main__': ...@@ -21,7 +21,7 @@ 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 current game status # Take an action and get the current game status
status = hfo.act((HFO_Actions.KICK, 100, 12.3)) status = hfo.act((HFO_Actions.DASH, 100, 0))
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:
......
...@@ -25,6 +25,7 @@ class HFOEnvironment(object): ...@@ -25,6 +25,7 @@ class HFOEnvironment(object):
def __init__(self): def __init__(self):
self.socket = None # Socket connection to server self.socket = None # Socket connection to server
self.numFeatures = None # Given by the server in handshake self.numFeatures = None # Given by the server in handshake
self.features = None # The state features
def connectToAgentServer(self, server_port=6008): def connectToAgentServer(self, server_port=6008):
'''Connect to the server that controls the agent on the specified port. ''' '''Connect to the server that controls the agent on the specified port. '''
...@@ -40,6 +41,13 @@ class HFOEnvironment(object): ...@@ -40,6 +41,13 @@ class HFOEnvironment(object):
break break
print '[Agent Client] Connected' print '[Agent Client] Connected'
self.handshakeAgentServer() self.handshakeAgentServer()
# Get the initial state
state_data = self.socket.recv(struct.calcsize('f')*self.numFeatures)
if not state_data:
print '[Agent Client] ERROR Recieved bad data from Server. Perhaps server closed?'
self.cleanup()
exit(1)
self.features = struct.unpack('f'*self.numFeatures, state_data)
def handshakeAgentServer(self): def handshakeAgentServer(self):
'''Handshake with the agent's server. ''' '''Handshake with the agent's server. '''
...@@ -63,13 +71,7 @@ class HFOEnvironment(object): ...@@ -63,13 +71,7 @@ class HFOEnvironment(object):
def getState(self): def getState(self):
'''Get the current state of the world. Returns a list of floats with '''Get the current state of the world. Returns a list of floats with
size numFeatures. ''' size numFeatures. '''
data = self.socket.recv(struct.calcsize('f')*self.numFeatures) return self.features
if not data:
print '[Agent Client] ERROR Recieved bad data from Server. Perhaps server closed?'
self.cleanup()
exit(1)
features = struct.unpack('f'*self.numFeatures, data)
return features
def act(self, action): def act(self, action):
''' Send an action and recieve the game status.''' ''' Send an action and recieve the game status.'''
...@@ -77,6 +79,13 @@ class HFOEnvironment(object): ...@@ -77,6 +79,13 @@ class HFOEnvironment(object):
# Get the current game status # Get the current game status
data = self.socket.recv(struct.calcsize("i")) data = self.socket.recv(struct.calcsize("i"))
status = struct.unpack("i", data)[0] status = struct.unpack("i", data)[0]
# Get the next state features
state_data = self.socket.recv(struct.calcsize('f')*self.numFeatures)
if not state_data:
print '[Agent Client] ERROR Recieved bad data from Server. Perhaps server closed?'
self.cleanup()
exit(1)
self.features = struct.unpack('f'*self.numFeatures, state_data)
return status return status
def cleanup(self): def cleanup(self):
......
...@@ -50,6 +50,11 @@ void HFOEnvironment::connectToAgentServer(int server_port) { ...@@ -50,6 +50,11 @@ void HFOEnvironment::connectToAgentServer(int server_port) {
} }
std::cout << "[Agent Client] Connected" << std::endl; std::cout << "[Agent Client] Connected" << std::endl;
handshakeAgentServer(); handshakeAgentServer();
// Get the initial game state
feature_vec.resize(numFeatures);
if (recv(sockfd, &(feature_vec.front()), numFeatures*sizeof(float), 0) < 0) {
error("[Agent Client] ERROR recieving state features from socket");
}
} }
void HFOEnvironment::handshakeAgentServer() { void HFOEnvironment::handshakeAgentServer() {
...@@ -87,12 +92,6 @@ void HFOEnvironment::handshakeAgentServer() { ...@@ -87,12 +92,6 @@ void HFOEnvironment::handshakeAgentServer() {
} }
const std::vector<float>& HFOEnvironment::getState() { 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; return feature_vec;
} }
...@@ -106,5 +105,9 @@ hfo_status_t HFOEnvironment::act(Action action) { ...@@ -106,5 +105,9 @@ hfo_status_t HFOEnvironment::act(Action action) {
if (recv(sockfd, &game_status, sizeof(hfo_status_t), 0) < 0) { if (recv(sockfd, &game_status, sizeof(hfo_status_t), 0) < 0) {
error("[Agent Client] ERROR recieving from socket"); error("[Agent Client] ERROR recieving from socket");
} }
// Get the next game state
if (recv(sockfd, &(feature_vec.front()), numFeatures*sizeof(float), 0) < 0) {
error("[Agent Client] ERROR recieving state features from socket");
}
return game_status; return game_status;
} }
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