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 Mon Jul 16 15:20:48 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,48 +27,78 @@
1.23 */
1.24 public class DijkstraSolver extends AbstractSolver {
1.25
1.26 + protected Map<Mine, Set<Cell>> testedTargetCells = new HashMap<Mine, Set<Cell>>();
1.27 +
1.28 public DijkstraSolver(Mine mine, SolverListener listener) {
1.29 super(mine, listener);
1.30 }
1.31
1.32 @Override
1.33 - protected void solve() {
1.34 + public void solve() {
1.35 while (true) {
1.36 - Game game = new Game(mine);
1.37 + Mine testMine = null;
1.38 + try {
1.39 + testMine = mine.clone();
1.40 + } catch (CloneNotSupportedException e1) {
1.41 + throw new RuntimeException(e1);
1.42 + }
1.43 + Game game = new Game(testMine);
1.44
1.45 while (game.getState() == GameState.Game) {
1.46 - try {
1.47 - Set<Cell> targets = mine.findCells(CellContent.Lambda);
1.48 - targets.addAll(mine.findCells(CellContent.HighOrderRock));
1.49 - if (targets.size() == 0)
1.50 - targets = mine.findCells(CellContent.OpenLambdaLift);
1.51 - if (targets.size() == 0)
1.52 + if (!testedTargetCells.containsKey(testMine)) {
1.53 + testedTargetCells.put(testMine, new HashSet<Cell>());
1.54 + }
1.55 + Set<Cell> targets = testMine.findCells(CellContent.Lambda);
1.56 + targets.addAll(testMine.findCells(CellContent.HighOrderRock));
1.57 + if (targets.size() == 0)
1.58 + targets = testMine.findCells(CellContent.OpenLambdaLift);
1.59 + if (targets.size() == 0 || testedTargetCells.get(testMine).containsAll(targets)) {
1.60 + // No targets. Walking around
1.61 + Cell robotCell = testMine.getRobotCell();
1.62 + for (Coordinate nextCoordinate : robotCell.getCoordinate().getAdjacentCoordinates()) {
1.63 + Cell nextCell = testMine.getCell(nextCoordinate);
1.64 + targets.add(nextCell);
1.65 + }
1.66 + if (testedTargetCells.get(testMine).containsAll(targets)) {
1.67 + // Fail. All cases are tried.
1.68 + game.move(Movement.ABORT);
1.69 break;
1.70 - Graph<Cell, CellsLink> graph = getDirectedGraph();
1.71 - DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
1.72 - new CellsLinkTransformer<CellsLink, Double>());
1.73 - Cell nextTarget = null;
1.74 - List<CellsLink> routeToNextTarget = null;
1.75 - double shortestDist = Double.MAX_VALUE;
1.76 - for (Cell target : targets) {
1.77 - List<CellsLink> route = alg.getPath(mine.getRobotCell(), target);
1.78 - Number dist = alg.getDistance(mine.getRobotCell(), target);
1.79 - if (dist != null && dist.doubleValue() < shortestDist) {
1.80 - shortestDist = dist.doubleValue();
1.81 - nextTarget = target;
1.82 - routeToNextTarget = route;
1.83 - }
1.84 }
1.85 - if (nextTarget != null) {
1.86 - for (CellsLink link : routeToNextTarget) {
1.87 - Movement mov = link.getSource().getCoordinate()
1.88 - .getNecessaryMovement(link.getTarget().getCoordinate());
1.89 - game.move(mov);
1.90 - break;
1.91 - }
1.92 + }
1.93 + Graph<Cell, CellsLink> graph = getDirectedGraph(testMine);
1.94 + DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
1.95 + new CellsLinkTransformer<CellsLink, Double>());
1.96 + Cell nextTarget = null;
1.97 + List<CellsLink> routeToNextTarget = null;
1.98 + double shortestDist = Double.MAX_VALUE;
1.99 + for (Cell target : targets) {
1.100 + if (target == null)
1.101 + continue;
1.102 + List<CellsLink> route = null;
1.103 + Number dist = null;
1.104 + try {
1.105 + route = alg.getPath(testMine.getRobotCell(), target);
1.106 + dist = alg.getDistance(testMine.getRobotCell(), target);
1.107 + } catch (IllegalArgumentException e) {
1.108 + // hmmm...
1.109 + testedTargetCells.get(testMine).add(target);
1.110 + continue;
1.111 }
1.112 - } catch (IllegalArgumentException e) {
1.113 - // hmmm...
1.114 + if (dist != null && route != null && dist.doubleValue() < shortestDist
1.115 + && !testedTargetCells.get(testMine).contains(target)) {
1.116 + shortestDist = dist.doubleValue();
1.117 + nextTarget = target;
1.118 + routeToNextTarget = route;
1.119 + testedTargetCells.get(testMine).add(target);
1.120 + }
1.121 + }
1.122 + if (nextTarget != null) {
1.123 + for (CellsLink link : routeToNextTarget) {
1.124 + Movement mov = link.getSource().getCoordinate()
1.125 + .getNecessaryMovement(link.getTarget().getCoordinate());
1.126 + game.move(mov);
1.127 + break;
1.128 + }
1.129 }
1.130 }
1.131 if (game.getState() == GameState.Win) {
1.132 @@ -75,22 +108,22 @@
1.133 }
1.134 }
1.135
1.136 - protected Graph<Cell, CellsLink> getDirectedGraph() {
1.137 + protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) {
1.138 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
1.139 - for (CellsLink link : getCellLinks()) {
1.140 + for (CellsLink link : getCellLinks(mine)) {
1.141 graph.addEdge(link, link.getSource(), link.getTarget());
1.142 }
1.143 return graph;
1.144 }
1.145
1.146 - protected List<CellsLink> getCellLinks() {
1.147 + protected List<CellsLink> getCellLinks(Mine mine) {
1.148 List<CellsLink> links = new ArrayList<CellsLink>();
1.149 Cell robotCell = mine.getRobotCell();
1.150 - findAllCellLinks(robotCell, links, new HashSet<Cell>());
1.151 + findAllCellLinks(mine, robotCell, links, new HashSet<Cell>());
1.152 return links;
1.153 }
1.154
1.155 - protected void findAllCellLinks(Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
1.156 + protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
1.157 if (processedCells.contains(cell))
1.158 return;
1.159 else
1.160 @@ -104,10 +137,10 @@
1.161 || neighboringContent == CellContent.OpenLambdaLift) {
1.162 CellsLink link = new CellsLink(mine, cell, neighboringCell);
1.163 links.add(link);
1.164 - findAllCellLinks(neighboringCell, links, processedCells);
1.165 + findAllCellLinks(mine, neighboringCell, links, processedCells);
1.166 } else if (!CellContent.getTargets().contains(cell.getContent())
1.167 && CellContent.getTargets().contains(neighboringContent)) {
1.168 - findAllCellLinks(neighboringCell, links, processedCells);
1.169 + findAllCellLinks(mine, neighboringCell, links, processedCells);
1.170 }
1.171 }
1.172 if (CellContent.getTrampolines().contains(cell.getContent())) {
1.173 @@ -116,7 +149,7 @@
1.174 continue;
1.175 CellsLink link = new CellsLink(mine, cell, target);
1.176 links.add(link);
1.177 - findAllCellLinks(target, links, processedCells);
1.178 + findAllCellLinks(mine, target, links, processedCells);
1.179 }
1.180 }
1.181 }