public double[][] generateDistanceMap(StateObservation state) {
    int x = (int) (state.getAvatarPosition().x / state.getBlockSize());
    int y = (int) (state.getAvatarPosition().y / state.getBlockSize());
    int typeId = StateObservationUtils.getAvatarType(state);

    return generateDistanceMap(state, x, y, typeId);
  }
  @Override
  protected double computeHeuristicValue(StateObservation actualState) {

    final Vector2d avPos = actualState.getAvatarPosition();

    final ArrayList<Observation>[] observables = this.getObservables(actualState);

    if (observables == null || observables.length == 0) {
      return 0;
    }

    double nearest = Double.MAX_VALUE;

    int counter = 0;

    double distSum = 0.0;

    for (ArrayList<Observation> observableList : observables) {
      for (Observation observable : observableList) {
        if (this.tabooMap.containsKey(observable.obsID)) {
          continue;
        }

        counter++;
        double xDist = (observable.position.x - avPos.x);
        double yDist = (observable.position.y - avPos.y);

        double mhDist = observable.position.dist(avPos); // Math.abs(xDist + yDist);

        distSum += mhDist;

        if (mhDist < nearest) {
          nearest = mhDist;
        }
      }
    }

    //		System.out.println(this.objectiveName + ": " + counter +  " - " + nearest);

    if (counter == 0) {
      return 0.0;
    }

    double height = actualState.getWorldDimension().getHeight() * actualState.getBlockSize();
    double width = actualState.getWorldDimension().getWidth() * actualState.getBlockSize();

    nearest = Utils.normalise(nearest, 0, height * height + width * width);

    switch (this.valType) {
      case MINIMIZE:
        return -counter;
      case MINIMIZE_NEAREST:
        return -(counter + nearest);
      case NEAREST:
        return -nearest;
      default:
        return -nearest;
    }
  }
  public boolean shouldCheck(int distance) {
    int factor = initialState.getBlockSize();
    Vector2d pos = initialState.getAvatarPosition();
    int pX = ((int) pos.x) / factor;
    int pY = ((int) pos.y) / factor;

    return this.areNPCNear(pX, pY, factor, distance);
  }
  @Override
  protected void updateHeuristic(StateObservation actualState) {

    final Vector2d avPos = actualState.getAvatarPosition();

    final ArrayList<Observation>[] observables = this.getObservables(actualState);

    if (observables == null || observables.length == 0) {
      return;
    }

    //		double nearest = Double.MAX_VALUE;
    //		int nearestID = Integer.MAX_VALUE;

    for (ArrayList<Observation> observableList : observables) {
      for (Observation observable : observableList) {
        //				double mhDist = observable.position.dist(avPos);//Math.abs(xDist + yDist);
        //
        //				if (mhDist < nearest && !(this.tabooMap.containsKey(observable.obsID)))
        //				{
        //					nearest = mhDist;
        //					nearestID = observable.obsID;
        //				}

        if (avPos.equals(observable.position)) {
          this.tabooMap.put(observable.obsID, actualState.getGameTick());
        }

        if (this.tabooMap.containsKey(observable.obsID)
            && this.tabooMap.get(observable.obsID).intValue() + this.tabooTime
                < actualState.getGameTick()) {
          this.tabooMap.remove(observable.obsID);
        }
      }
    }

    //		if (nearestID == this.nearestObsID)
    //		{
    //			sameObsIDCounter++;
    //
    //			if (sameObsIDCounter > sameObsIDCounterMax)
    //			{
    //				this.tabooMap.put(nearestID, actualState.getGameTick());
    //				this.nearestObsID = Integer.MAX_VALUE;
    //				this.sameObsIDCounter = 0;
    //			}
    //		}
    //		else
    //		{
    //			this.nearestObsID = nearestID;
    //			sameObsIDCounter = 0;
    //		}

  }
Example #5
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();
  }
  /** Computes the distance to the next target object. */
  @Override
  public double evaluateState(StateObservation stateObs) {
    if (stateObs.getGameWinner() == WINNER.PLAYER_WINS) return 0;
    else if (stateObs.getGameWinner() == WINNER.PLAYER_LOSES) return Double.POSITIVE_INFINITY;

    if (ObservationUtil.collisionLastStep(stateObs) == target.getItype()) return 0;

    Vector2d pos = stateObs.getAvatarPosition();

    // if the target does not exists anymore
    Vector2d targetPos = target.getPosition(stateObs);
    if (targetPos == null) return Double.POSITIVE_INFINITY;

    return Helper.distance(pos, targetPos);
  }
  @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;
  }
  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;
  }
  @Override
  public double evaluate(StateObservation oldState, StateObservation newState) {
    List<Observation> movables = StateObservationUtils.getMovablesOfType(newState, itypeOfMovable);
    List<Observation> sinks = StateObservationUtils.getImmovablesOfType(newState, itypeOfSink);

    double dist = calcDistBetweenMovablesAndSinks(movables, sinks, newState.getBlockSize());
    return dist;
  }
Example #10
0
  private void analyseVicinity() {
    urgency = WHISPER;
    bestAction = DEFAULT_ACTION;
    double bestScore = stateObs.getGameScore();

    List<StateObservation> nextStates = this.worldInformation.getImmediateStates();

    for (int action = 0; action < nextStates.size(); action++) {
      if (nextStates.get(action).getGameScore() < stateObs.getGameScore()
          || nextStates.get(action).getGameWinner() == Types.WINNER.PLAYER_LOSES) {
        urgency = SHOUT;
      } else if (nextStates.get(action).getGameScore() > bestScore) {
        bestScore = nextStates.get(action).getGameScore();
        bestAction = action;
      } else if (nextStates.get(action).getGameScore() == bestScore
          && nextStates.get(action).getGameWinner() != Types.WINNER.PLAYER_LOSES) {
        bestAction = action;
      }
    }
  }
  @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();
  }
  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;
  }
 private ArrayList<Observation>[] getObservables(StateObservation state) {
   switch (this.obsType) {
     case IMMOVABLE:
       return state.getImmovablePositions();
     case MOVABLE:
       return state.getMovablePositions();
     case NPC:
       return state.getNPCPositions();
     case PORTAL:
       return state.getPortalsPositions();
     case RESOURCE:
       return state.getResourcesPositions();
     default:
       return state.getPortalsPositions();
   }
 }
  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;
  }
Example #15
0
 public Agent(StateObservation so, ElapsedCpuTimer elapsedTimer) {
   random = new Random();
   actions = so.getAvailableActions().toArray(new ACTIONS[0]);
   MAX_DEPTH = 10;
 }