@Override
  public ACTIONS act(StateObservation stateObs, ElapsedCpuTimer elapsedTimer) {
    Tuple current = new Tuple();

    if (stateObs.getAvailableActions().contains(ACTIONS.ACTION_USE)) {
      current.values.add(1.0);
    } else {
      current.values.add(0.0);
    }
    if (stateObs.getAvailableActions().contains(ACTIONS.ACTION_UP)
        || stateObs.getAvailableActions().contains(ACTIONS.ACTION_DOWN)) {
      current.values.add(1.0);
    } else {
      current.values.add(0.0);
    }

    Vector2d avatarPosition = stateObs.getAvatarPosition();

    analyzeData(stateObs.getResourcesPositions(), avatarPosition, current);
    analyzeData(stateObs.getNPCPositions(), avatarPosition, current);
    analyzeData(stateObs.getImmovablePositions(), avatarPosition, current);
    analyzeData(stateObs.getMovablePositions(), avatarPosition, current);
    analyzeData(stateObs.getPortalsPositions(), avatarPosition, current);

    current.normalize(maxValues);

    for (int i = 0; i < tuples.size(); i++) {
      tuples.get(i).getDistance(current);
    }
    Collections.sort(tuples);

    HashMap<Types.ACTIONS, Integer> output = new HashMap<Types.ACTIONS, Integer>();
    output.put(Types.ACTIONS.ACTION_LEFT, 0);
    output.put(Types.ACTIONS.ACTION_RIGHT, 0);
    output.put(Types.ACTIONS.ACTION_DOWN, 0);
    output.put(Types.ACTIONS.ACTION_UP, 0);
    output.put(Types.ACTIONS.ACTION_USE, 0);

    for (int i = 0; i < k; i++) {
      output.put(tuples.get(i).output, output.get(tuples.get(i).output) + 1);
    }

    Types.ACTIONS resultAction = Types.ACTIONS.ACTION_NIL;
    int maxValue = -1;
    for (Entry<Types.ACTIONS, Integer> e : output.entrySet()) {
      if (maxValue < e.getValue()) {
        resultAction = e.getKey();
        maxValue = e.getValue();
      }
    }

    return resultAction;
  }
Esempio n. 2
0
  @Override
  public ACTIONS act(StateObservation stateObs, ElapsedCpuTimer elapsedTimer) {

    float avgTime = 10;
    float worstTime = 10;
    float totalTime = 0;
    int numberOfIterations = 0;
    int limitExpansions = 1;

    // ArrayList<Node> queue = new ArrayList<Node>();
    Stack<Node> st = new Stack<Node>();

    Node currentNode = new Node(null, Types.ACTIONS.ACTION_NIL, stateObs);

    st.push(currentNode);

    ArrayList<Types.ACTIONS> possibleActions = stateObs.getAvailableActions();

    while (!st.isEmpty()
        && elapsedTimer.remainingTimeMillis() > 2 * avgTime
        && elapsedTimer.remainingTimeMillis() > worstTime) {
      // while(!st.isEmpty()){
      ElapsedCpuTimer methodTime = new ElapsedCpuTimer();

      currentNode = st.pop();

      if (currentNode.state.getGameWinner() == WINNER.PLAYER_WINS) {
        break;
      }
      if (currentNode.state.getGameWinner() == WINNER.PLAYER_LOSES) {
        continue;
      }

      if (deepFromRoot(currentNode) <= limitExpansions - 1) {
        possibleActions = currentNode.state.getAvailableActions();
        for (int i = 0; i < possibleActions.size(); i++) {
          StateObservation newState = currentNode.state.copy();
          newState.advance(possibleActions.get(i));
          Node newNode = new Node(currentNode, possibleActions.get(i), newState);
          st.push(newNode);
        }
      }
      if (st.isEmpty()) {
        limitExpansions += 1;
        st.push(new Node(null, Types.ACTIONS.ACTION_NIL, stateObs));
      }

      numberOfIterations += 1;
      totalTime += methodTime.elapsedMillis();
      avgTime = totalTime / numberOfIterations;
    }
    // System.out.println(numberOfIterations);
    return currentNode.getAction();
  }
  @Override
  public ACTIONS act(StateObservation stateObs, ElapsedCpuTimer elapsedTimer) {
    ArrayList<Node> queue = new ArrayList<Node>();
    queue.add(new Node(null, Types.ACTIONS.ACTION_NIL, stateObs));

    float avgTime = 10;
    float worstTime = 10;
    float totalTime = 0;
    int numberOfIterations = 0;
    Node currentNode = null;
    ArrayList<Types.ACTIONS> possibleActions = stateObs.getAvailableActions();

    while (!queue.isEmpty()
        && elapsedTimer.remainingTimeMillis() > 2 * avgTime
        && elapsedTimer.remainingTimeMillis() > worstTime) {
      ElapsedCpuTimer methodTime = new ElapsedCpuTimer();

      currentNode = queue.remove(0);
      if (currentNode.state.getGameWinner() == WINNER.PLAYER_WINS) {
        break;
      }
      if (currentNode.state.getGameWinner() == WINNER.PLAYER_LOSES) {
        continue;
      }
      for (int i = 0; i < possibleActions.size(); i++) {
        StateObservation newState = currentNode.state.copy();
        newState.advance(possibleActions.get(i));
        queue.add(new Node(currentNode, possibleActions.get(i), newState));
      }

      numberOfIterations += 1;
      totalTime += methodTime.elapsedMillis();
      avgTime = totalTime / numberOfIterations;
    }

    if (currentNode == null) {
      return Types.ACTIONS.ACTION_NIL;
    }

    return currentNode.getAction();
  }
Esempio n. 4
0
  private HashMap<Integer, Integer> getMapDeathActions(ElapsedCpuTimer elapsedTimer) {
    ArrayList<ACTIONS> actions = initialState.getAvailableActions();
    int numActions = actions.size();

    HashMap<Integer, Integer> mapActionDeath = new HashMap<Integer, Integer>(10);

    StateObservation nextState;
    int numTrials = 13;
    int i, max = 0;

    for (i = 0; i < numActions; i++) {
      mapActionDeath.put(i, 0);
    }

    for (int j = 0; j < numTrials; j++) {
      if (j > max) max = j;
      for (i = 0; i < numActions; i++) {
        int tlim = tlim1;
        if (checkOther) {
          tlim = tlim2;
          checkOther = false;
        }
        if (elapsedTimer.remainingTimeMillis() < tlim) {
          // System.out.println("-----" + max);
          return mapActionDeath;
        }
        nextState = initialState.copy();
        nextState.advance(actions.get(i));
        if (nextState.isGameOver()) {
          if (nextState.getGameWinner() == Types.WINNER.PLAYER_LOSES) {
            mapActionDeath.put(i, mapActionDeath.get(i) + 1);
          }
        }
      }
    }

    // System.out.println("-----" + max);
    return mapActionDeath;
  }
Esempio n. 5
0
 public Agent(StateObservation so, ElapsedCpuTimer elapsedTimer) {
   random = new Random();
   actions = so.getAvailableActions().toArray(new ACTIONS[0]);
   MAX_DEPTH = 10;
 }