high_level_random_agent.cpp 1.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#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

12 13 14
// Server Connection Options. See printouts from bin/HFO.
feature_set_t features = HIGH_LEVEL_FEATURE_SET;
string config_dir = "bin/teams/base/config/formations-dt";
15
int port = 6000;
16 17 18 19
string server_addr = "localhost";
string team_name = "base_left";
bool goalie = false;

20 21
// We omit PASS & CATCH & MOVE actions here
action_t HIGH_LEVEL_ACTIONS[2] = { SHOOT, DRIBBLE };
22

23
int main(int argc, char** argv) {
24 25
  // Create the HFO environment
  HFOEnvironment hfo;
26 27
  // Connect to the server and request high-level feature set. See
  // manual for more information on feature sets.
28
  hfo.connectToServer(features, config_dir, port, server_addr,
29 30 31 32
                      team_name, goalie);
  status_t status = IN_GAME;
  for (int episode = 0; status != SERVER_DOWN; episode++) {
    status = IN_GAME;
33 34 35
    while (status == IN_GAME) {
      // Get the vector of state features for the current state
      const vector<float>& feature_vec = hfo.getState();
36
      // Perform the action
37 38 39 40 41
      if (feature_vec[5] == 1) { // Feature 5 is 1 when the player can kick the ball
        hfo.act(HIGH_LEVEL_ACTIONS[rand() % 2]);
      } else {
        hfo.act(MOVE);
      }
42
      // Advance the environment and get the game status
43
      status = hfo.step();
44 45
    }
    // Check what the outcome of the episode was
46
    cout << "Episode " << episode << " ended with status: "
47
         << StatusToString(status) << endl;
48
  }
49
  hfo.act(QUIT);
50
};