HFO.cpp 4.04 KB
Newer Older
1 2 3 4 5 6 7 8
#include "HFO.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
9
#include <assert.h>
10 11
#include <netdb.h>
#include <iostream>
Matthew Hausknecht's avatar
Matthew Hausknecht committed
12
#include <stdarg.h>
13
#include <agent.h>
Matthew Hausknecht's avatar
Matthew Hausknecht committed
14 15 16
#include <rcsc/common/basic_client.h>
#include <rcsc/player/player_config.h>
#include <rcsc/player/world_model.h>
17

18 19
using namespace hfo;

20 21
#define MAX_STEPS 100

Matthew Hausknecht's avatar
Matthew Hausknecht committed
22 23 24 25 26 27 28 29 30 31
HFOEnvironment::HFOEnvironment() {
  client = new rcsc::BasicClient();
  agent = new Agent();
}

HFOEnvironment::~HFOEnvironment() {
  delete client;
  delete agent;
}

32 33 34 35 36
void HFOEnvironment::connectToServer(feature_set_t feature_set,
                                     std::string config_dir,
                                     int server_port,
                                     std::string server_addr,
                                     std::string team_name,
37 38
                                     bool play_goalie,
                                     std::string record_dir) {
Matthew Hausknecht's avatar
Matthew Hausknecht committed
39 40
  agent->setFeatureSet(feature_set);
  rcsc::PlayerConfig& config = agent->mutable_config();
41 42 43 44 45
  config.setConfigDir(config_dir);
  config.setPort(server_port);
  config.setHost(server_addr);
  config.setTeamName(team_name);
  config.setGoalie(play_goalie);
46 47 48 49
  if (!record_dir.empty()) {
    config.setLogDir(record_dir);
    config.setRecord(true);
  }
Matthew Hausknecht's avatar
Matthew Hausknecht committed
50
  if (!agent->init(client, 0, NULL)) {
51 52 53
    std::cerr << "Init failed" << std::endl;
    exit(1);
  }
Matthew Hausknecht's avatar
Matthew Hausknecht committed
54 55
  client->setClientMode(rcsc::BasicClient::ONLINE);
  if (!client->startAgent(agent)) {
56 57 58
    std::cerr << "Unable to start agent" << std::endl;
    exit(1);
  }
59
  // Do nothing until the agent begins getting state features
60
  act(NOOP);
Matthew Hausknecht's avatar
Matthew Hausknecht committed
61
  while (agent->getState().empty()) {
62
    if (!client->isServerAlive()) {
63
      std::cerr << "[ConnectToServer] Server Down!" << std::endl;
64 65
      exit(1);
    }
66 67 68 69
    ready_for_action = client->runStep(agent);
    if (ready_for_action) {
      agent->action();
    }
sanmit's avatar
sanmit committed
70
  }
71 72 73 74 75 76 77
  // Step until it is time to act
  do {
    ready_for_action = client->runStep(agent);
  } while (!ready_for_action);
  agent->ProcessTrainerMessages();
  agent->ProcessTeammateMessages();
  agent->UpdateFeatures();
78
  current_cycle = agent->currentTime().cycle();
79 80 81
}

const std::vector<float>& HFOEnvironment::getState() {
Matthew Hausknecht's avatar
Matthew Hausknecht committed
82
  return agent->getState();
83 84
}

85
void HFOEnvironment::act(action_t action, ...) {
Matthew Hausknecht's avatar
Matthew Hausknecht committed
86
  agent->setAction(action);
87
  int n_args = NumParams(action);
Matthew Hausknecht's avatar
Matthew Hausknecht committed
88
  std::vector<float> *params = agent->mutable_params();
89
  params->resize(n_args);
90 91 92
  va_list vl;
  va_start(vl, action);
  for (int i = 0; i < n_args; ++i) {
93
    (*params)[i] = va_arg(vl, double);
94 95 96 97
  }
  va_end(vl);
}

98
void HFOEnvironment::act(action_t action, const std::vector<float>& params) {
Matthew Hausknecht's avatar
Matthew Hausknecht committed
99
  agent->setAction(action);
100 101
  int n_args = NumParams(action);
  assert(n_args == params.size());
Matthew Hausknecht's avatar
Matthew Hausknecht committed
102
  std::vector<float> *agent_params = agent->mutable_params();
103 104 105
  (*agent_params) = params;
}

106
void HFOEnvironment::say(const std::string& message) {
Matthew Hausknecht's avatar
Matthew Hausknecht committed
107
  agent->setSayMsg(message);
108 109 110
}

std::string HFOEnvironment::hear() {
Matthew Hausknecht's avatar
Matthew Hausknecht committed
111
  return agent->getHearMsg();
112 113
}

114 115 116 117
int HFOEnvironment::getUnum() {
  return agent->getUnum();
}

118 119 120 121 122 123 124 125
int HFOEnvironment::getNumTeammates() {
  return agent->getNumTeammates();
}

int HFOEnvironment::getNumOpponents() {
  return agent->getNumOpponents();
}

126
Player HFOEnvironment::playerOnBall() {
Matthew Hausknecht's avatar
Matthew Hausknecht committed
127
  return agent->getPlayerOnBall();
128 129 130
}

status_t HFOEnvironment::step() {
131
  // Execute the action
132
  agent->executeAction();
133 134 135 136 137

  // Advance the environment by one step
  do {
    ready_for_action = client->runStep(agent);
    if (!client->isServerAlive() || agent->getGameStatus() == SERVER_DOWN) {
138
      return SERVER_DOWN;
139
    }
140 141 142 143
    agent->ProcessTrainerMessages();
  } while (agent->statusUpdateTime() <= current_cycle || !ready_for_action);

  // Update the state features
144
  agent->preAction();
145
  current_cycle = agent->currentTime().cycle();
146 147 148 149 150
  status_t status = agent->getGameStatus();
  // If the episode is over, take three NOOPs to refresh state features
  if (status != IN_GAME && status != SERVER_DOWN) {
    for (int i = 0; i < 3; ++i) {
      act(NOOP);
Matthew Hausknecht's avatar
Matthew Hausknecht committed
151
      step();
152 153 154
    }
  }
  return status;
155
}