high_level_random_agent.cpp 1.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <vector>
#include <HFO.hpp>
#include <cstdlib>

using namespace std;
using namespace hfo;

// Before running this program, first Start HFO server:
// $./bin/HFO --offense-agents 1

// Returns a random high-level action
13
action_t get_random_high_lv_action() {
14
  action_t action_indx = (action_t) ((rand() % 5) + MOVE);
15
  return action_indx;
16 17
}

18 19 20 21 22
int main(int argc, char** argv) {
  int port = 6000;
  if (argc > 1) {
    port = atoi(argv[1]);
  }
23 24 25 26
  // Create the HFO environment
  HFOEnvironment hfo;
  // Connect to the agent's server on port 6000 and request low-level
  // feature set. See manual for more information on feature sets.
27
  hfo.connectToAgentServer(port, HIGH_LEVEL_FEATURE_SET);
28
  // Play 5 episodes
29
  for (int episode=0; ; episode++) {
30 31 32 33
    status_t status = IN_GAME;
    while (status == IN_GAME) {
      // Get the vector of state features for the current state
      const vector<float>& feature_vec = hfo.getState();
34 35 36
      // Perform the action
      hfo.act(get_random_high_lv_action());
      // Advance the environment and get the game status
37
      status = hfo.step();
38 39 40 41 42 43
    }

    // Check what the outcome of the episode was
    cout << "Episode " << episode << " ended with status: ";
    switch (status) {
      case GOAL:
sanmit's avatar
sanmit committed
44
        cout << "goal " << hfo.playerOnBall().unum << endl;
45 46
        break;
      case CAPTURED_BY_DEFENSE:
sanmit's avatar
sanmit committed
47
        cout << "captured by defense " << hfo.playerOnBall().unum << endl;
48 49 50 51 52 53 54 55 56 57
        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);
58 59
    }
  }
60
  hfo.act(QUIT);
61
};