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; // } }
@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; }
/** 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); }