icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Mon, 16 Jul 2012 15:20:48 +0400
branchCleverSearch
changeset 46 4197f736166a
parent 42 2bd032a701a7
child 47 b3d48e364f9e
permissions -rw-r--r--
Add history of searching
     1 package ru.bosony.solvers;
     2 
     3 import java.util.ArrayList;
     4 import java.util.HashMap;
     5 import java.util.HashSet;
     6 import java.util.List;
     7 import java.util.Map;
     8 import java.util.Set;
     9 
    10 import ru.bosony.graph.CellsLink;
    11 import ru.bosony.graph.CellsLinkTransformer;
    12 import ru.bosony.model.cellscontents.CellContent;
    13 import ru.bosony.model.game.Game;
    14 import ru.bosony.model.game.GameState;
    15 import ru.bosony.model.mine.Cell;
    16 import ru.bosony.model.mine.Mine;
    17 import ru.bosony.model.moving.Coordinate;
    18 import ru.bosony.model.moving.Movement;
    19 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
    20 import edu.uci.ics.jung.graph.DirectedSparseGraph;
    21 import edu.uci.ics.jung.graph.Graph;
    22 
    23 /**
    24  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    25  *         16.07.2012 11:05:26
    26  * 
    27  */
    28 public class DijkstraSolver extends AbstractSolver {
    29 
    30 	protected Map<Mine, Set<Cell>>	testedTargetCells	= new HashMap<Mine, Set<Cell>>();
    31 
    32 	public DijkstraSolver(Mine mine, SolverListener listener) {
    33 		super(mine, listener);
    34 	}
    35 
    36 	@Override
    37 	public void solve() {
    38 		while (true) {
    39 			Mine testMine = null;
    40 			try {
    41 				testMine = mine.clone();
    42 			} catch (CloneNotSupportedException e1) {
    43 				throw new RuntimeException(e1);
    44 			}
    45 			Game game = new Game(testMine);
    46 
    47 			while (game.getState() == GameState.Game) {
    48 				if (!testedTargetCells.containsKey(testMine)) {
    49 					testedTargetCells.put(testMine, new HashSet<Cell>());
    50 				}
    51 				Set<Cell> targets = testMine.findCells(CellContent.Lambda);
    52 				targets.addAll(testMine.findCells(CellContent.HighOrderRock));
    53 				if (targets.size() == 0)
    54 					targets = testMine.findCells(CellContent.OpenLambdaLift);
    55 				if (targets.size() == 0 || testedTargetCells.get(testMine).containsAll(targets)) {
    56 					// No targets. Walking around
    57 					Cell robotCell = testMine.getRobotCell();
    58 					for (Coordinate nextCoordinate : robotCell.getCoordinate().getAdjacentCoordinates()) {
    59 						Cell nextCell = testMine.getCell(nextCoordinate);
    60 						targets.add(nextCell);
    61 					}
    62 					if (testedTargetCells.get(testMine).containsAll(targets)) {
    63 						// Fail. All cases are tried.
    64 						game.move(Movement.ABORT);
    65 						break;
    66 					}
    67 				}
    68 				Graph<Cell, CellsLink> graph = getDirectedGraph(testMine);
    69 				DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
    70 						new CellsLinkTransformer<CellsLink, Double>());
    71 				Cell nextTarget = null;
    72 				List<CellsLink> routeToNextTarget = null;
    73 				double shortestDist = Double.MAX_VALUE;
    74 				for (Cell target : targets) {
    75 					if (target == null)
    76 						continue;
    77 					List<CellsLink> route = null;
    78 					Number dist = null;
    79 					try {
    80 						route = alg.getPath(testMine.getRobotCell(), target);
    81 						dist = alg.getDistance(testMine.getRobotCell(), target);
    82 					} catch (IllegalArgumentException e) {
    83 						// hmmm...
    84 						testedTargetCells.get(testMine).add(target);
    85 						continue;
    86 					}
    87 					if (dist != null && route != null && dist.doubleValue() < shortestDist
    88 							&& !testedTargetCells.get(testMine).contains(target)) {
    89 						shortestDist = dist.doubleValue();
    90 						nextTarget = target;
    91 						routeToNextTarget = route;
    92 						testedTargetCells.get(testMine).add(target);
    93 					}
    94 				}
    95 				if (nextTarget != null) {
    96 					for (CellsLink link : routeToNextTarget) {
    97 						Movement mov = link.getSource().getCoordinate()
    98 								.getNecessaryMovement(link.getTarget().getCoordinate());
    99 						game.move(mov);
   100 						break;
   101 					}
   102 				}
   103 			}
   104 			if (game.getState() == GameState.Win) {
   105 				addNewRoute(game.getStringRoute(), game.getScore());
   106 			}
   107 			attemptsCount++;
   108 		}
   109 	}
   110 
   111 	protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) {
   112 		Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
   113 		for (CellsLink link : getCellLinks(mine)) {
   114 			graph.addEdge(link, link.getSource(), link.getTarget());
   115 		}
   116 		return graph;
   117 	}
   118 
   119 	protected List<CellsLink> getCellLinks(Mine mine) {
   120 		List<CellsLink> links = new ArrayList<CellsLink>();
   121 		Cell robotCell = mine.getRobotCell();
   122 		findAllCellLinks(mine, robotCell, links, new HashSet<Cell>());
   123 		return links;
   124 	}
   125 
   126 	protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
   127 		if (processedCells.contains(cell))
   128 			return;
   129 		else
   130 			processedCells.add(cell);
   131 		for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
   132 			if (neighboringCell == null)
   133 				continue;
   134 			CellContent neighboringContent = neighboringCell.getContent();
   135 			if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
   136 					|| neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
   137 					|| neighboringContent == CellContent.OpenLambdaLift) {
   138 				CellsLink link = new CellsLink(mine, cell, neighboringCell);
   139 				links.add(link);
   140 				findAllCellLinks(mine, neighboringCell, links, processedCells);
   141 			} else if (!CellContent.getTargets().contains(cell.getContent())
   142 					&& CellContent.getTargets().contains(neighboringContent)) {
   143 				findAllCellLinks(mine, neighboringCell, links, processedCells);
   144 			}
   145 		}
   146 		if (CellContent.getTrampolines().contains(cell.getContent())) {
   147 			for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
   148 				if (target == null)
   149 					continue;
   150 				CellsLink link = new CellsLink(mine, cell, target);
   151 				links.add(link);
   152 				findAllCellLinks(mine, target, links, processedCells);
   153 			}
   154 		}
   155 	}
   156 }