Exemplo n.º 1
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();
  }
Exemplo n.º 3
0
  @Override
  public ACTIONS act(StateObservation stateObs, ElapsedCpuTimer elapsedTimer) {

    double worstCase = 10;
    double avgTime = 10;
    double totalTime = 0;
    double iteration = 0;
    int bestAction = -1;
    TreeNode root = new TreeNode(stateObs, null);

    while (elapsedTimer.remainingTimeMillis() > 2 * avgTime
        && elapsedTimer.remainingTimeMillis() > worstCase) {
      ElapsedCpuTimer temp = new ElapsedCpuTimer();
      // treeSelect
      TreeNode node = root.SelectNode();

      // Simulate
      double value = node.ExploreNode();

      // RollBack
      node.UpdateNode(value);

      // Get the best action
      bestAction = root.GetBestChild();

      totalTime += temp.elapsedMillis();
      iteration += 1;
      avgTime = totalTime / iteration;
    }

    if (bestAction == -1) {
      System.out.println("Out of time choosing random action");
      bestAction = random.nextInt(actions.length);
    }

    return actions[bestAction];
  }
Exemplo n.º 4
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;
  }
Exemplo n.º 5
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;
  }
Exemplo n.º 6
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;
  }
Exemplo n.º 7
0
  private boolean winConditionIsStillValid(YoloState yoloState, ElapsedCpuTimer elapsedTimer) {
    if (targetSolution == null || targetSolution.actions.isEmpty()) return false;

    if (true) return true;

    double avgTimeTaken = 0;
    double acumTimeTaken = 0;
    long remaining = elapsedTimer.remainingTimeMillis();
    int remainingLimit = 5;
    int numIters = 0;

    int startAction = 0;

    int successfullWins = 0;
    int successfullWinsMax = 20;

    // check First-Game-Tick bug:
    int x = yoloState.getAvatarX();
    int y = yoloState.getAvatarY();

    yoloState.advance(targetSolution.actions.get(0));

    boolean error = true;
    error &= yoloState.getGameTick() == 1;
    error &= yoloState.getAvatarOrientation().equals(YoloKnowledge.ORIENTATION_NULL);
    error &= yoloState.getAvatarX() == x;
    error &= yoloState.getAvatarY() == y;
    if (!error) {
      startAction = 1;
    } else {
      if (!Agent.UPLOAD_VERSION) System.out.println("First-Game-Tick Error!");
    }

    while (remaining > 2 * avgTimeTaken
        && remaining > remainingLimit
        && successfullWins < successfullWinsMax) {
      ElapsedCpuTimer elapsedTimerIteration = new ElapsedCpuTimer();

      // Do win simulation:
      YoloState currentState = yoloState.copy();
      //        	currentState.setNewSeed((int)(Math.random()*10000));

      for (int i = startAction; i < targetSolution.actions.size(); i++) {
        ACTIONS todo = targetSolution.actions.get(i);
        // Do action:
        currentState.advance(todo);

        if (currentState.isGameOver()) {
          if (currentState.getGameWinner() == WINNER.PLAYER_WINS) {
            // Erfolgreich durchsimuliert!
            break;
          } else {
            // Den Tod gefunden!
            return false;
          }
        }
      }
      if (currentState.getGameWinner() != WINNER.PLAYER_WINS) {
        // Durchsimuliert, nicht gestorben aber auch nicht gewonnen:
        if (!Agent.UPLOAD_VERSION) System.out.println("Simulation nicht beendet!");
        // return false;
      }

      successfullWins++;

      numIters++;
      acumTimeTaken += (elapsedTimerIteration.elapsedMillis());

      avgTimeTaken = acumTimeTaken / numIters;
      remaining = elapsedTimer.remainingTimeMillis();
    }

    if (!Agent.UPLOAD_VERSION)
      System.out.println("WinState has been recreated " + successfullWins + " times!");

    return true;
  }
Exemplo n.º 8
0
  private void doBreitensuche(ElapsedCpuTimer elapsedTimer) {
    OwnHistoryLight h, h2;

    double avgTimeTaken = 0;
    double acumTimeTaken = 0;
    long remaining = elapsedTimer.remainingTimeMillis();
    int remainingLimit = 5;
    int numIters = 0;

    while (!queue.isEmpty()) {
      ElapsedCpuTimer elapsedTimerIteration = new ElapsedCpuTimer();

      // System.out.println(avgTime);
      // Ist zeit uebrig?
      if (!(remaining > 2 * avgTimeTaken && remaining > remainingLimit)) break;

      // Queue

      h = queue.poll();
      expandedCount++;

      if (h.tick > deepestState.getGameTick()
          && YoloKnowledge.instance.playerItypeIsWellKnown(h.state)
          && YoloKnowledge.instance.agentHasControlOfMovement(h.state)) deepestState = h.state;

      for (ontology.Types.ACTIONS action : h.state.getAvailableActions(true)) {
        boolean forceExpand = false;
        if (YoloKnowledge.instance.playerItypeIsWellKnown(h.state)
            && !YoloKnowledge.instance.agentHasControlOfMovement(h.state))
          if (!action.equals(ACTIONS.ACTION_NIL)) continue;
          else forceExpand = true;
        //        		boolean guessWillCancel = YoloKnowledge.instance.moveWillCancel(h.state,
        // action); // Was tippt Knowledge?
        Long probabilHash = YoloKnowledge.instance.getPropablyHash(h.state, action, true);
        boolean guessWillCancel = probabilHash != null && visited.contains(probabilHash);
        if (guessWillCancel && !forceExpand) {
          cancelMoves++;
          continue;
        } else {
          // expandSteps++;
        }
        h2 = new OwnHistoryLight(h, action);

        if (!sawNPC
            && h2.state.getNpcPositions() != null
            && h2.state.getNpcPositions().length > 0) {
          sawNPC = true;
        }

        long hash = h2.state.getHash(true);

        if (!Agent.UPLOAD_VERSION && guessWillCancel && !visited.contains(hash)) {
          // System.out.println("Is at" + h2.state.getAvatarX() + "|" + h2.state.getAvatarY());
          System.out.println("\t\t\tKRITISCHER FEHLER DER WISSENSDATENBANK!");
        }

        if (!forceExpand
            && visited.contains(hash)
            && h.tick != 0
            && h2.timeSinceAvatarChange
                != 1 /* && (YoloKnowledge.instance.playerItypeIsWellKnown(h2.state) && YoloKnowledge.instance.agentHasControlOfMovement(h2.state)) */) {
          //        			System.out.println("Jump");
          cancelMoves++;
          continue;
        } else {
          expandSteps++;
          //        			System.out.println("\t NoJump");
        }
        visited.add(hash);
        branchedCount++;

        if (!h2.state.isGameOver()) {
          if ((h2.score > bestScore.score
                  || !YoloKnowledge.instance.agentHasControlOfMovement(bestScore.state))
              && YoloKnowledge.instance.agentHasControlOfMovement(h2.state)) {
            bestScore = h2;
            //						System.out.println("bester Score"+ bestScore.score);
            //						System.out.println("bester tick"+ bestScore.tick);
          }
          // fifo.add(h2);
          if (!h2.toPrune()) queue.add(h2);
        } else if (h2.state.getGameWinner() == WINNER.PLAYER_WINS) {
          if (firstSecond
              || YoloKnowledge.instance.canIncreaseScoreWithoutWinning(h.state)
                  && tick + h2.tick < maybeEndOfTick) {
            if (winSolution == null || h2.score >= winSolution.score) {
              winSolution = h2;
              //							System.out.println("win Score"+ winSolution.score);
              //							System.out.println("win tick"+ winSolution.tick);
            }
          } else {
            targetSolution = h2;
            return;
          }
        }
        if (fastForward) break;
      }

      numIters++;
      acumTimeTaken += (elapsedTimerIteration.elapsedMillis());

      avgTimeTaken = acumTimeTaken / numIters;
      remaining = elapsedTimer.remainingTimeMillis();
    }

    currentBranchingFactor = (double) branchedCount / (double) expandedCount;
  }