Example #1
0
/** Parses the given exit policy string. */
void
Policy::fromString(QString policy)
{
  /* Separate the action and the address/mask/port info */
  QStringList ruleParts = policy.split(" ");
  _action = toAction(ruleParts.at(0));

  /* If some address/mask/port stuff was specified, parse it. */
  if (ruleParts.size() > 1) {
    QStringList addrParts = ruleParts.at(1).split(":");

    /* Parse the address and mask (if specified) */
    QString addr = addrParts.at(0);
    _address.setAddress(addr.mid(0, addr.indexOf("/")));
    if (_address.isNull()) {
      _address = QHostAddress::Any;
    }
    if (addr.contains("/")) {
      _mask = addr.mid(addr.indexOf("/")+1).toUInt();
    }

    /* Parse the specified port range (if specified) */
    if (addrParts.size() > 1) {
      QString ports = addrParts.at(1);
      _fromPort = ports.mid(0, ports.indexOf("-")).toUInt();
      if (ports.contains("-")) {
        _toPort = ports.mid(ports.indexOf("-")+1).toUInt();
      } else {
        _toPort = _fromPort;
      }
    }
  }
}
Example #2
0
void offenseAgent(int port, int numTMates, int numEpi, double learnR,
                  int suffix, bool oppPres, double eps) {

  // Number of features
  int numF = oppPres ? (4 + 4 * numTMates) : (3 + 3 * numTMates);
  // Number of actions
  int numA = 2 + numTMates;

  double discFac = 1;

  // Tile coding parameter
  double resolution = 0.1;

  double range[numF];
  double min[numF];
  double res[numF];
  for(int i = 0; i < numF; i++) {
      min[i] = -1;
      range[i] = 2;
      res[i] = resolution;
  }

  // Weights file
  char *wtFile;
  std::string s = "weights_" + std::to_string(port) +
                  "_" + std::to_string(numTMates + 1) +
                  "_" + std::to_string(suffix);
  wtFile = &s[0u];
  double lambda = 0;
  CMAC *fa = new CMAC(numF, numA, range, min, res);
  SarsaAgent *sa = new SarsaAgent(numF, numA, learnR, eps, lambda, fa, wtFile, wtFile);

  hfo::HFOEnvironment hfo;
  hfo::status_t status;
  hfo::action_t a;
  double state[numF];
  int action = -1;
  double reward;
  hfo.connectToServer(hfo::HIGH_LEVEL_FEATURE_SET,"../../bin/teams/base/config/formations-dt",6000,"localhost","base_left",false,"");
  for (int episode=0; episode < numEpi; episode++) {
    int count = 0;
    status = hfo::IN_GAME;
    action = -1;
    while (status == hfo::IN_GAME) {
      const std::vector<float>& state_vec = hfo.getState();
      // If has ball
      if(state_vec[5] == 1) {
        if(action != -1) {
          reward = getReward(status);
          sa->update(state, action, reward, discFac);
        }

        // Fill up state array
        purgeFeatures(state, state_vec, numTMates, oppPres);

	// Get raw action
        action = sa->selectAction(state);

        // Get hfo::Action
        a = toAction(action, state_vec);

      } else {
            a = hfo::MOVE;
      }
      if (a== hfo::PASS) {
           hfo.act(a,state_vec[(9+6*numTMates) - (action-2)*3]);
           //std::cout<<(9+6*numTMates) - (action-2)*3;
        } else {
           hfo.act(a);
        }
      status = hfo.step();
    }
    // End of episode
    if(action != -1) {
      reward = getReward(status);
      sa->update(state, action, reward, discFac);
      sa->endEpisode();
    }
  }

  delete sa;
  delete fa;
}