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
No related merge requests found
......@@ -21,7 +21,7 @@ if __name__ == '__main__':
# Grab the state features from the environment
features = hfo.getState()
# 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',
# Check what the outcome of the episode was
if status == HFO_Status.GOAL:
......
......@@ -25,6 +25,7 @@ class HFOEnvironment(object):
def __init__(self):
self.socket = None # Socket connection to server
self.numFeatures = None # Given by the server in handshake
self.features = None # The state features
def connectToAgentServer(self, server_port=6008):
'''Connect to the server that controls the agent on the specified port. '''
......@@ -40,6 +41,13 @@ class HFOEnvironment(object):
break
print '[Agent Client] Connected'
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):
'''Handshake with the agent's server. '''
......@@ -63,13 +71,7 @@ class HFOEnvironment(object):
def getState(self):
'''Get the current state of the world. Returns a list of floats with
size numFeatures. '''
data = self.socket.recv(struct.calcsize('f')*self.numFeatures)
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
return self.features
def act(self, action):
''' Send an action and recieve the game status.'''
......@@ -77,6 +79,13 @@ class HFOEnvironment(object):
# Get the current game status
data = self.socket.recv(struct.calcsize("i"))
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
def cleanup(self):
......
......@@ -50,6 +50,11 @@ void HFOEnvironment::connectToAgentServer(int server_port) {
}
std::cout << "[Agent Client] Connected" << std::endl;
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() {
......@@ -87,12 +92,6 @@ void HFOEnvironment::handshakeAgentServer() {
}
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;
}
......@@ -106,5 +105,9 @@ hfo_status_t HFOEnvironment::act(Action action) {
if (recv(sockfd, &game_status, sizeof(hfo_status_t), 0) < 0) {
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;
}
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