icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Mon, 16 Jul 2012 13:17:06 +0400
changeset 44 b87d85a52577
parent 42 2bd032a701a7
child 46 4197f736166a
permissions -rw-r--r--
Print to standard output is deprecated
     1 package ru.bosony.solvers;
     2 
     3 import java.util.ArrayList;
     4 import java.util.HashSet;
     5 import java.util.List;
     6 import java.util.Set;
     7 
     8 import ru.bosony.graph.CellsLink;
     9 import ru.bosony.graph.CellsLinkTransformer;
    10 import ru.bosony.model.cellscontents.CellContent;
    11 import ru.bosony.model.game.Game;
    12 import ru.bosony.model.game.GameState;
    13 import ru.bosony.model.mine.Cell;
    14 import ru.bosony.model.mine.Mine;
    15 import ru.bosony.model.moving.Movement;
    16 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
    17 import edu.uci.ics.jung.graph.DirectedSparseGraph;
    18 import edu.uci.ics.jung.graph.Graph;
    19 
    20 /**
    21  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    22  *         16.07.2012 11:05:26
    23  * 
    24  */
    25 public class DijkstraSolver extends AbstractSolver {
    26 
    27 	public DijkstraSolver(Mine mine, SolverListener listener) {
    28 		super(mine, listener);
    29 	}
    30 
    31 	@Override
    32 	protected void solve() {
    33 		while (true) {
    34 			Game game = new Game(mine);
    35 
    36 			while (game.getState() == GameState.Game) {
    37 				try {
    38 					Set<Cell> targets = mine.findCells(CellContent.Lambda);
    39 					targets.addAll(mine.findCells(CellContent.HighOrderRock));
    40 					if (targets.size() == 0)
    41 						targets = mine.findCells(CellContent.OpenLambdaLift);
    42 					if (targets.size() == 0)
    43 						break;
    44 					Graph<Cell, CellsLink> graph = getDirectedGraph();
    45 					DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
    46 							new CellsLinkTransformer<CellsLink, Double>());
    47 					Cell nextTarget = null;
    48 					List<CellsLink> routeToNextTarget = null;
    49 					double shortestDist = Double.MAX_VALUE;
    50 					for (Cell target : targets) {
    51 						List<CellsLink> route = alg.getPath(mine.getRobotCell(), target);
    52 						Number dist = alg.getDistance(mine.getRobotCell(), target);
    53 						if (dist != null && dist.doubleValue() < shortestDist) {
    54 							shortestDist = dist.doubleValue();
    55 							nextTarget = target;
    56 							routeToNextTarget = route;
    57 						}
    58 					}
    59 					if (nextTarget != null) {
    60 						for (CellsLink link : routeToNextTarget) {
    61 							Movement mov = link.getSource().getCoordinate()
    62 									.getNecessaryMovement(link.getTarget().getCoordinate());
    63 							game.move(mov);
    64 							break;
    65 						}
    66 					}
    67 				} catch (IllegalArgumentException e) {
    68 					// hmmm...
    69 				}
    70 			}
    71 			if (game.getState() == GameState.Win) {
    72 				addNewRoute(game.getStringRoute(), game.getScore());
    73 			}
    74 			attemptsCount++;
    75 		}
    76 	}
    77 
    78 	protected Graph<Cell, CellsLink> getDirectedGraph() {
    79 		Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
    80 		for (CellsLink link : getCellLinks()) {
    81 			graph.addEdge(link, link.getSource(), link.getTarget());
    82 		}
    83 		return graph;
    84 	}
    85 
    86 	protected List<CellsLink> getCellLinks() {
    87 		List<CellsLink> links = new ArrayList<CellsLink>();
    88 		Cell robotCell = mine.getRobotCell();
    89 		findAllCellLinks(robotCell, links, new HashSet<Cell>());
    90 		return links;
    91 	}
    92 
    93 	protected void findAllCellLinks(Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
    94 		if (processedCells.contains(cell))
    95 			return;
    96 		else
    97 			processedCells.add(cell);
    98 		for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
    99 			if (neighboringCell == null)
   100 				continue;
   101 			CellContent neighboringContent = neighboringCell.getContent();
   102 			if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
   103 					|| neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
   104 					|| neighboringContent == CellContent.OpenLambdaLift) {
   105 				CellsLink link = new CellsLink(mine, cell, neighboringCell);
   106 				links.add(link);
   107 				findAllCellLinks(neighboringCell, links, processedCells);
   108 			} else if (!CellContent.getTargets().contains(cell.getContent())
   109 					&& CellContent.getTargets().contains(neighboringContent)) {
   110 				findAllCellLinks(neighboringCell, links, processedCells);
   111 			}
   112 		}
   113 		if (CellContent.getTrampolines().contains(cell.getContent())) {
   114 			for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
   115 				if (target == null)
   116 					continue;
   117 				CellsLink link = new CellsLink(mine, cell, target);
   118 				links.add(link);
   119 				findAllCellLinks(target, links, processedCells);
   120 			}
   121 		}
   122 	}
   123 }