コード例 #1
0
ファイル: Agent.java プロジェクト: Tiago-NYU/Perceptron-GVGAI
  @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();
  }
コード例 #2
0
  public boolean isActionSafe(ACTIONS action, ElapsedCpuTimer elapsedTimer) {
    checkOther = true;
    int numTries = 10;
    if (!shouldCheck(2)) numTries = 1;

    StateObservation stateNext;

    for (int i = 0; i < numTries; i++) {
      if (elapsedTimer.remainingTimeMillis() < tlim1) break;
      stateNext = initialState.copy();
      stateNext.advance(action);
      if (stateNext.getGameWinner() == Types.WINNER.PLAYER_LOSES) return false;
    }

    return true;
  }
コード例 #3
0
  @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();
  }
コード例 #4
0
  public boolean isNilSafe(ElapsedCpuTimer elapsedTimer) {
    checkOther = true;
    int numTries = 10;
    if (!shouldCheck(2)) numTries = 1;
    ACTIONS actNil = this.getActionNil();

    StateObservation stateNext;

    for (int i = 0; i < numTries; i++) {
      if (elapsedTimer.remainingTimeMillis() < tlim1) {
        // System.out.println("-----" + i);
        break;
      }
      stateNext = initialState.copy();
      stateNext.advance(actNil);
      if (stateNext.getGameWinner() == Types.WINNER.PLAYER_LOSES) return false;
    }

    // System.out.println("-----" + 9);
    return true;
  }
コード例 #5
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;
  }