// @Override public void execute2(SensorData data, ModifiableAction action) { double absMeasure = Math.abs(situation.getMeasure()); double trackPos = data.getTrackPosition(); double trackAngle = data.getAngleToTrackAxis(); boolean mirror = situation.isRight(); if (mirror) { trackPos *= -1.0; trackAngle *= -1.0; } // 0.976807459 0,008231851 -0,209828013 -0,634093073 0,046558592 -0,079286572 1,05025865 // 0,517923684 -0,158864381 1,460560156 0,944967234 0,836506662 0,99226593 1,01798939 double result = 0.002708663 * absMeasure * absMeasure + 0.983090667 * trackPos * trackPos + 0.995064624 * trackAngle * trackAngle + 0.0 * absMeasure * trackPos + 1.695369918 * absMeasure * trackAngle + 1.010313665 * trackPos * trackAngle + 1.020039262; if (mirror) { result *= -1.0; } result = Math.min(45.0, Math.max(-45.0, result)); action.setSteering(result / 45.0); action.limitValues(); }
public Action policy(State observation, float reward) { Map<Float, Action> options = new HashMap<Float, Action>(); scala.collection.Iterator<Action> it = actions().iterator(); while (it.hasNext()) { Action action = it.next(); options.put(expectedReward(observation, action), action); } Action bestGuess = options.get(new TreeSet<Float>(options.keySet()).last()); Action decision = bestGuess; if (random.nextDouble() < randomFactor) { decision = randomAction(); } Situation currentSA = new Situation(observation, decision); float currentExpected = expectedReward(observation, decision); float lastExpected = expectedReward(lastSA.getKey(), lastSA.getValue()); float newQValue = lastExpected + alpha * (reward + gamma * currentExpected - lastExpected); qTable.put(lastSA, newQValue); // update the Q-Table lastSA = currentSA; return decision; }
private int tickExplore() { Situation s = this.getSituation(); if (s.getExitCount() > 2) { this.addToGraph(new JunctionNode(this.robot)); } switch (s) { case CORRIDOR: int dir = this.corridor(); if (dir != IRobot.AHEAD) { // Corner // Don't really want to do this but it massively simplifies // NavigatorController this.addToGraph(new Node(this.robot.getLocation())); } return dir; case CROSSROAD: return this.crossroads(); case DEAD_END: if (!this.firstRun) { this.isBacktracking = true; } return this.deadEnd(); case JUNCTION: return this.junction(); default: return 0; } }
protected final Situation getSituation() { int count = 0; for (int i = IRobot.AHEAD; i <= IRobot.LEFT; i++) { if (this.robot.look(i) != IRobot.WALL) { count++; } } for (Situation s : Situation.values()) { if (s.getExitCount() == count) { return s; } } throw new IllegalStateException("Cannot handle number of exits: " + count); }
@Override public String getNextAction(int[] grid, int _playerNumber) { String ret = "X"; // as fallback, make a random move playerNumber = _playerNumber; // now try to come up with something more clever long startTime = new Date().getTime(); situations = 0; IAICallback logCallBack = new IAICallback() { @Override public void callback(String message) { System.out.println(message); } }; int opponentNumber = playerNumber - 1; if (opponentNumber == 0) { opponentNumber = 2; } Situation rootSituation = new Situation(-1, opponentNumber, grid, 0, null); bestSituation = rootSituation; // may never be null! rootSituation.callback = logCallBack; rootSituation.computeNextSituations(TREE_DEPTH); long timePassed = new Date().getTime() - startTime; double seconds = (double) timePassed / 1000.0; double sps = (double) situations / seconds; System.out.println( "computed " + situations + " situation in " + timePassed + "ms: " + sps + " situations per second."); // found something! if (bestSituation._parentSituation != null && bestSituation._parentSituation._followingSituationsAreWins != 0) { ret = getNextStepToSituation(bestSituation); } else { System.out.println("Couldn't find any good moves... spamming random move."); ret = computeRandomAction(); } return ret; }
public void evaluatePostAction( LazyObjectAccessor source, LazyObjectAccessor target, Action action, boolean sourceAction) throws SynchronizationException { if (postAction != null) { Map<String, Object> scope = new HashMap<String, Object>(); scope.put("sourceAction", sourceAction); scope.put("action", action.name()); scope.put("situation", situation.name()); if (source != null) { scope.put("source", source.asMap()); } if (target != null) { scope.put("target", target.asMap()); } try { postAction.exec(scope); } catch (ScriptException se) { LOGGER.debug("action script encountered exception", se); throw new SynchronizationException(se); } } }
@Override public void execute(SensorData data, ModifiableAction action) { // steering based on the "biggest sensor value" heuristic double[] rawSensors = data.getRawTrackEdgeSensors(); int index = 0; double longest = rawSensors[0]; for (int i = 1; i < rawSensors.length; ++i) { if (rawSensors[i] > longest) { index = i; longest = rawSensors[i]; } } double angleD = -angles[index]; if (TEXT_DEBUG) { System.out.println( Utils.timeToExactString(data.getCurrentLapTime()) + " @ " + data.getDistanceRacedS() + "- track: " + data.getTrackPositionS() + ", index: " + index + ", angleD: " + Utils.dTS(angleD)); } double absMeasure = Math.abs(situation.getMeasure()); if (absMeasure >= 20.0 && absMeasure < 35.0) { angleD *= 1.0 + ((absMeasure - 20.0) / (35.0 - 20.0)); } else if (absMeasure >= 35.0 && absMeasure < 60.0) { angleD *= 2.0 + ((absMeasure - 35.0) / (60.0 - 35.0)); } else if (absMeasure >= 60.0) { angleD *= 3.0; } if (TEXT_DEBUG) { System.out.println("absMeasure: " + Utils.dTS(absMeasure) + ", angleD: " + Utils.dTS(angleD)); } double trackPos = data.getTrackPosition(); double trackAngle = data.getAngleToTrackAxis(); if (situation.isCorner()) { boolean mirror = situation.isRight(); if (mirror) { trackPos *= -1.0; trackAngle *= -1.0; angleD *= -1.0; index = 18 - index; } double absTrackPos = SensorData.calcAbsoluteTrackPosition(trackPos, trackWidth); // inside if (absTrackPos - (CAR_WIDTH * 0.5) < INSIDE_MARGIN && trackAngle < 0.0) { double beta = (INSIDE_MARGIN - (absTrackPos - (CAR_WIDTH * 0.5))) / INSIDE_MARGIN; if (TEXT_DEBUG) { System.out.println("Inside"); } // not so close -> open steering if (beta < 0.5) { // System.out.println("IN - Opening@"+Utils.dTS(data.getDistanceFromStartLine())); if (TEXT_DEBUG) { System.out.print("Beta: " + Utils.dTS(beta) + ", old: " + Utils.dTS(angleD)); } double alpha = (beta - 0.5) * -(1 / 0.5); angleD *= alpha; if (TEXT_DEBUG) { System.out.println(", alpha: " + Utils.dTS(alpha) + ", new: " + Utils.dTS(angleD)); } } else { // System.out.println("IN - Other@"+Utils.dTS(data.getDistanceFromStartLine())); beta = Math.min(beta, 1.0); // very close, steer in the other direction if (TEXT_DEBUG) { System.out.print("Beta: " + Utils.dTS(beta) + ", old: " + Utils.dTS(angleD)); } double alpha = (beta - 0.5) * (1 / 0.5); angleD = Math.toDegrees(trackAngle) * alpha; // angleD = 2.0*alpha; if (TEXT_DEBUG) { System.out.println(", alpha: " + Utils.dTS(alpha) + ", new: " + Utils.dTS(angleD)); } } } // outside if (absTrackPos + (CAR_WIDTH * 0.5) > trackWidth - OUTSIDE_MARGIN) { // System.out.println("OUT@"+Utils.dTS(data.getDistanceFromStartLine())); if (TEXT_DEBUG) { System.out.println("Outside"); } // increase steering angle double alpha = Math.min(absMeasure, OUTSIDE_DOUBLE) / OUTSIDE_DOUBLE; double beta = ((absTrackPos + (CAR_WIDTH * 0.5)) - (trackWidth - OUTSIDE_MARGIN)) / OUTSIDE_MARGIN; if (TEXT_DEBUG) { System.out.print("Beta: " + Utils.dTS(beta) + ", old: " + Utils.dTS(angleD)); } beta = Math.min(beta, 1.0); angleD *= 1.0 + (alpha * beta); if (TEXT_DEBUG) { System.out.println( ", 1+alpha: " + Utils.dTS(1.0 + alpha) + ", new: " + Utils.dTS(angleD)); } } // if(Math.abs(Math.toDegrees(trackAngle)) < 10.0){ // System.out.println(absMeasure + ";" + trackPos + ";" + Math.toDegrees(trackAngle) + ";" // + index + ";" + angleD); // } if (mirror) { angleD *= -1.0; } } angleD = Math.min(45.0, Math.max(-45.0, angleD)); action.setSteering(angleD / 45.0); action.limitValues(); if (TEXT_DEBUG) { System.out.println("Final value: " + action.getSteeringS()); } }
private boolean withinRightCorner() { return (segment != null && !segment.isUnknown() && segment.isRight()) || situation.isRight(); }
public void computeNextSituations(int maxOrder_) { if (_order >= maxOrder_) { return; } int nextPlayer = _playerNumberMadeThisMove == 1 ? 2 : 1; // check all possible actions for (int col = 0; col < AITools.GRID_COLUMNS; col++) { if (callback != null) { callback.callback("beginning col " + col); } // next free row for this column int nextFreeIndex = AITools.coordsToIndex(col, AITools.GRID_ROWS - 1); while (_grid[nextFreeIndex] != 0) { nextFreeIndex = AITools.getGridIndexOnTopOf(nextFreeIndex); if (nextFreeIndex < 0) // can't throw in this row. no possible situation { break; } } if (nextFreeIndex < 0) // can't throw in this row. no possible situation { continue; } // copy grid and apply new action int[] newGrid = _grid.clone(); newGrid[nextFreeIndex] = nextPlayer; if (_nextSituations == null) { _nextSituations = new ArrayList<Situation>(); } Situation newSituation = new Situation(col, nextPlayer, newGrid, _order + 1, this); _nextSituations.add(newSituation); newSituation._score = 0; int gameStatus = AITools.getGameStatus(newGrid); // compute follow Situations only if not a an end point if (gameStatus == -1) { newSituation.computeNextSituations(maxOrder_); } else if (gameStatus == playerNumber) { this._followingSituationsAreWins++; // check: if this situation has the order 1 (next move) and is a win, then take it! if (newSituation._order == 1) { bestSituation = newSituation; System.out.println("TAKE THE WIN: next move will finish match!"); AITools.visualizeGrid(newGrid); break; } if (bestSituation._parentSituation == null || this._followingSituationsAreWins > bestSituation._parentSituation._followingSituationsAreWins) { // safety check: up to that situation, there may be no situation where the opponent has // a winning chance! if (!thereIsASafePathToWin(newSituation)) { continue; } bestSituation = newSituation; System.out.println( "new best Situation with " + this._followingSituationsAreWins + " win chances with one piece."); AITools.visualizeGrid(newGrid); } } else if (gameStatus != 0) // not draw game -> opponent wins { this._followingSituationsAreLosses++; // System.out.println("found situation with score: " + newSituation._score); // AITools.visualizeGrid(newGrid); } } }