1.1 --- a/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Mon Jul 16 13:19:10 2012 +0400
1.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Wed Jul 18 09:42:52 2012 +0400
1.3 @@ -1,8 +1,10 @@
1.4 package ru.bosony.solvers;
1.5
1.6 import java.util.ArrayList;
1.7 +import java.util.HashMap;
1.8 import java.util.HashSet;
1.9 import java.util.List;
1.10 +import java.util.Map;
1.11 import java.util.Set;
1.12
1.13 import ru.bosony.graph.CellsLink;
1.14 @@ -12,6 +14,7 @@
1.15 import ru.bosony.model.game.GameState;
1.16 import ru.bosony.model.mine.Cell;
1.17 import ru.bosony.model.mine.Mine;
1.18 +import ru.bosony.model.moving.Coordinate;
1.19 import ru.bosony.model.moving.Movement;
1.20 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
1.21 import edu.uci.ics.jung.graph.DirectedSparseGraph;
1.22 @@ -24,77 +27,197 @@
1.23 */
1.24 public class DijkstraSolver extends AbstractSolver {
1.25
1.26 + protected Map<String, Set<Movement>> loseMovs = new HashMap<String, Set<Movement>>();
1.27 + protected Map<String, Set<CellsLink>> loseLinks = new HashMap<String, Set<CellsLink>>();
1.28 + protected Map<String, Set<Coordinate>> attendedWalkingAroundCoordinates = new HashMap<String, Set<Coordinate>>();
1.29 + protected Map<String, Set<Coordinate>> noWay = new HashMap<String, Set<Coordinate>>();
1.30 +
1.31 public DijkstraSolver(Mine mine, SolverListener listener) {
1.32 super(mine, listener);
1.33 }
1.34
1.35 @Override
1.36 - protected void solve() {
1.37 + public void solve() {
1.38 while (true) {
1.39 + Mine mine = null;
1.40 + mine = initialMine.clone();
1.41 Game game = new Game(mine);
1.42
1.43 while (game.getState() == GameState.Game) {
1.44 - try {
1.45 - Set<Cell> targets = mine.findCells(CellContent.Lambda);
1.46 + Cell robotCell = mine.getRobotCell();
1.47 + String map = mine.toText();
1.48 + String mapWithoutRobot = mine.toTextWithoutRobot();
1.49 + if (!loseMovs.containsKey(map)) {
1.50 + loseMovs.put(map, new HashSet<Movement>());
1.51 + }
1.52 + if (!loseLinks.containsKey(map)) {
1.53 + loseLinks.put(map, new HashSet<CellsLink>());
1.54 + }
1.55 + if (!noWay.containsKey(map)) {
1.56 + noWay.put(map, new HashSet<Coordinate>());
1.57 + }
1.58 + if (!attendedWalkingAroundCoordinates.containsKey(mapWithoutRobot)) {
1.59 + attendedWalkingAroundCoordinates.put(mapWithoutRobot, new HashSet<Coordinate>());
1.60 + }
1.61 + attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(robotCell.getCoordinate());
1.62 + Set<Cell> targets = new HashSet<Cell>();
1.63 + boolean isFreeWay = false;
1.64 + for (Cell cell : mine.getAdjacentCells(robotCell)) {
1.65 + Movement mov = robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate());
1.66 + isFreeWay |= !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(cell.getCoordinate())
1.67 + && !loseMovs.get(map).contains(mov);
1.68 + }
1.69 + if (isFreeWay) {
1.70 + targets.addAll(mine.findCells(CellContent.Lambda));
1.71 targets.addAll(mine.findCells(CellContent.HighOrderRock));
1.72 if (targets.size() == 0)
1.73 - targets = mine.findCells(CellContent.OpenLambdaLift);
1.74 - if (targets.size() == 0)
1.75 - break;
1.76 - Graph<Cell, CellsLink> graph = getDirectedGraph();
1.77 - DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
1.78 - new CellsLinkTransformer<CellsLink, Double>());
1.79 - Cell nextTarget = null;
1.80 - List<CellsLink> routeToNextTarget = null;
1.81 - double shortestDist = Double.MAX_VALUE;
1.82 - for (Cell target : targets) {
1.83 - List<CellsLink> route = alg.getPath(mine.getRobotCell(), target);
1.84 - Number dist = alg.getDistance(mine.getRobotCell(), target);
1.85 - if (dist != null && dist.doubleValue() < shortestDist) {
1.86 + targets.addAll(mine.findCells(CellContent.OpenLambdaLift));
1.87 + targets.removeAll(mine.getCells(noWay.get(map)));
1.88 + } else {
1.89 + targets.addAll(mine.findCells(CellContent.Empty));
1.90 + targets.addAll(mine.findCells(CellContent.Earth));
1.91 + targets.addAll(mine.findCells(CellContent.HuttonsRazor));
1.92 + for (CellContent content : CellContent.getTrampolines()) {
1.93 + targets.addAll(mine.findCells(content));
1.94 + }
1.95 + targets.removeAll(mine.getCells(attendedWalkingAroundCoordinates.get(mapWithoutRobot)));
1.96 + targets.removeAll(mine.getCells(noWay.get(map)));
1.97 + if (targets.size() == 0) {
1.98 + game.move(Movement.ABORT);
1.99 + continue;
1.100 + }
1.101 + }
1.102 + Graph<Cell, CellsLink> graph = getDirectedGraph(mine);
1.103 + DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
1.104 + new CellsLinkTransformer<CellsLink, Double>());
1.105 + Cell nextTarget = null;
1.106 + List<CellsLink> routeToNextTarget = null;
1.107 + double shortestDist = Double.MAX_VALUE;
1.108 + for (Cell target : targets) {
1.109 + List<CellsLink> route = null;
1.110 + Number dist = null;
1.111 + try {
1.112 + route = alg.getPath(mine.getRobotCell(), target);
1.113 + dist = alg.getDistance(mine.getRobotCell(), target);
1.114 + } catch (IllegalArgumentException e) {
1.115 + noWay.get(map).add(target.getCoordinate());
1.116 + continue;
1.117 + }
1.118 + if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) {
1.119 + CellsLink firstLink = route.get(0);
1.120 + Movement mov = firstLink.getSource().getCoordinate()
1.121 + .getNecessaryMovement(firstLink.getTarget().getCoordinate());
1.122 + if (!loseMovs.get(map).contains(mov)
1.123 + && !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(
1.124 + target.getCoordinate())) {
1.125 shortestDist = dist.doubleValue();
1.126 nextTarget = target;
1.127 routeToNextTarget = route;
1.128 }
1.129 }
1.130 - if (nextTarget != null) {
1.131 - for (CellsLink link : routeToNextTarget) {
1.132 - Movement mov = link.getSource().getCoordinate()
1.133 - .getNecessaryMovement(link.getTarget().getCoordinate());
1.134 - game.move(mov);
1.135 - break;
1.136 + }
1.137 + if (nextTarget != null) {
1.138 + for (CellsLink link : routeToNextTarget) {
1.139 + Cell source = link.getSource();
1.140 + Cell target = link.getTarget();
1.141 + if (isSafeMovement(mine, source, target)) {
1.142 + Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate());
1.143 + GameState state = game.move(mov);
1.144 + if (state == GameState.Lose || state == GameState.Abort) {
1.145 + loseMovs.get(map).add(mov);
1.146 + loseLinks.get(map).add(link);
1.147 + }
1.148 + }
1.149 + break;
1.150 + }
1.151 + } else {
1.152 + // Walking around
1.153 + for (Movement mov : Movement.values()) {
1.154 + if (!loseMovs.get(map).contains(mov)) {
1.155 + if (mov == Movement.RAZOR && mine.getRazorsCount() > 0) {
1.156 + boolean isRazorApplied = false;
1.157 + for (Cell cell : mine.getNeighboringCells(robotCell)) {
1.158 + isRazorApplied |= cell.getContent() == CellContent.WadlersBeard;
1.159 + }
1.160 + if (!isRazorApplied)
1.161 + continue;
1.162 + }
1.163 + Cell movCell = null;
1.164 + for (Cell cell : mine.getAdjacentCells(robotCell)) {
1.165 + if (robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate()) == mov) {
1.166 + if (!attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(
1.167 + cell.getCoordinate())) {
1.168 + movCell = cell;
1.169 + break;
1.170 + }
1.171 + }
1.172 +
1.173 + }
1.174 + if (mov == Movement.WAIT
1.175 + || mov == Movement.RAZOR
1.176 + || movCell != null
1.177 + && !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(
1.178 + movCell.getCoordinate())) {
1.179 + GameState state = game.move(mov);
1.180 + if (movCell != null && mapWithoutRobot.equals(mine.toTextWithoutRobot()))
1.181 + attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(movCell.getCoordinate());
1.182 + if (state == GameState.Game && map.equals(mine.toText())) {
1.183 + loseMovs.get(map).add(mov);
1.184 + }
1.185 + break;
1.186 + }
1.187 }
1.188 }
1.189 - } catch (IllegalArgumentException e) {
1.190 - // hmmm...
1.191 }
1.192 }
1.193 - if (game.getState() == GameState.Win) {
1.194 - addNewRoute(game.getStringRoute(), game.getScore());
1.195 - }
1.196 + addNewRoute(game);
1.197 attemptsCount++;
1.198 }
1.199 }
1.200
1.201 - protected Graph<Cell, CellsLink> getDirectedGraph() {
1.202 + protected boolean isSafeMovement(Mine mine, Cell source, Cell target) {
1.203 + if (source == null || target == null)
1.204 + return false;
1.205 + if (source.getContent().getTrampolineTarget() == target.getContent())
1.206 + return true;
1.207 + Cell upCell = mine.getCell(target.getCoordinate().up());
1.208 + if (upCell == null)
1.209 + return true;
1.210 + Cell upUpCell = mine.getCell(target.getCoordinate().up().up());
1.211 + Cell upUpLeftCell = mine.getCell(target.getCoordinate().up().up().left());
1.212 + Cell upUpRightCell = mine.getCell(target.getCoordinate().up().up().right());
1.213 + if (upCell.getContent() == CellContent.Empty
1.214 + && (upUpCell != null
1.215 + && (upUpCell.getContent() == CellContent.Rock || upUpCell.getContent() == CellContent.HighOrderRock)
1.216 + || upUpLeftCell != null
1.217 + && (upUpLeftCell.getContent() == CellContent.Rock || upUpLeftCell.getContent() == CellContent.HighOrderRock) || upUpRightCell != null
1.218 + && (upUpRightCell.getContent() == CellContent.Rock || upUpRightCell.getContent() == CellContent.HighOrderRock))) {
1.219 + return false;
1.220 + }
1.221 + return true;
1.222 + }
1.223 +
1.224 + protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) {
1.225 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
1.226 - for (CellsLink link : getCellLinks()) {
1.227 + for (CellsLink link : getCellLinks(mine)) {
1.228 graph.addEdge(link, link.getSource(), link.getTarget());
1.229 }
1.230 return graph;
1.231 }
1.232
1.233 - protected List<CellsLink> getCellLinks() {
1.234 + protected List<CellsLink> getCellLinks(Mine mine) {
1.235 List<CellsLink> links = new ArrayList<CellsLink>();
1.236 Cell robotCell = mine.getRobotCell();
1.237 - findAllCellLinks(robotCell, links, new HashSet<Cell>());
1.238 + findAllCellLinks(mine, robotCell, links, new HashSet<Cell>());
1.239 return links;
1.240 }
1.241
1.242 - protected void findAllCellLinks(Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
1.243 + protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
1.244 if (processedCells.contains(cell))
1.245 return;
1.246 else
1.247 processedCells.add(cell);
1.248 + String map = mine.toText();
1.249 for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
1.250 if (neighboringCell == null)
1.251 continue;
1.252 @@ -102,21 +225,29 @@
1.253 if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
1.254 || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
1.255 || neighboringContent == CellContent.OpenLambdaLift) {
1.256 - CellsLink link = new CellsLink(mine, cell, neighboringCell);
1.257 - links.add(link);
1.258 - findAllCellLinks(neighboringCell, links, processedCells);
1.259 + if (isSafeMovement(mine, cell, neighboringCell)) {
1.260 + CellsLink link = new CellsLink(mine, cell, neighboringCell);
1.261 + if (!loseLinks.get(map).contains(link)) {
1.262 + links.add(link);
1.263 + findAllCellLinks(mine, neighboringCell, links, processedCells);
1.264 + }
1.265 + }
1.266 } else if (!CellContent.getTargets().contains(cell.getContent())
1.267 && CellContent.getTargets().contains(neighboringContent)) {
1.268 - findAllCellLinks(neighboringCell, links, processedCells);
1.269 + findAllCellLinks(mine, neighboringCell, links, processedCells);
1.270 }
1.271 }
1.272 if (CellContent.getTrampolines().contains(cell.getContent())) {
1.273 for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
1.274 if (target == null)
1.275 continue;
1.276 - CellsLink link = new CellsLink(mine, cell, target);
1.277 - links.add(link);
1.278 - findAllCellLinks(target, links, processedCells);
1.279 + if (isSafeMovement(mine, cell, target)) {
1.280 + CellsLink link = new CellsLink(mine, cell, target);
1.281 + if (!loseLinks.get(map).contains(link)) {
1.282 + links.add(link);
1.283 + findAllCellLinks(mine, target, links, processedCells);
1.284 + }
1.285 + }
1.286 }
1.287 }
1.288 }