icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Tue, 17 Jul 2012 21:38:39 +0400
branchCleverSearch
changeset 50 9530f55e9639
parent 47 b3d48e364f9e
child 51 b431cec961d7
permissions -rw-r--r--
Moar
     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.Movement;
    18 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
    19 import edu.uci.ics.jung.graph.DirectedSparseGraph;
    20 import edu.uci.ics.jung.graph.Graph;
    21 
    22 /**
    23  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    24  *         16.07.2012 11:05:26
    25  * 
    26  */
    27 public class DijkstraSolver extends AbstractSolver {
    28 
    29 	protected Map<String, Set<Movement>>	loseMovs	= new HashMap<String, Set<Movement>>();
    30 	protected Set<CellsLink>				loseLinks	= new HashSet<CellsLink>();
    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 			testMine = mine.clone();
    41 			Game game = new Game(testMine);
    42 
    43 			while (game.getState() == GameState.Game) {
    44 				String map = testMine.toText();
    45 				if (!loseMovs.containsKey(map)) {
    46 					loseMovs.put(map, new HashSet<Movement>());
    47 				}
    48 				Set<Cell> targets = testMine.findCells(CellContent.Lambda);
    49 				targets.addAll(testMine.findCells(CellContent.HighOrderRock));
    50 				if (targets.size() == 0)
    51 					targets = testMine.findCells(CellContent.OpenLambdaLift);
    52 				Graph<Cell, CellsLink> graph = getDirectedGraph(testMine);
    53 				DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
    54 						new CellsLinkTransformer<CellsLink, Double>());
    55 				Cell nextTarget = null;
    56 				List<CellsLink> routeToNextTarget = null;
    57 				double shortestDist = Double.MAX_VALUE;
    58 				for (Cell target : targets) {
    59 					if (target == null)
    60 						continue;
    61 					List<CellsLink> route = null;
    62 					Number dist = null;
    63 					try {
    64 						route = alg.getPath(testMine.getRobotCell(), target);
    65 						dist = alg.getDistance(testMine.getRobotCell(), target);
    66 					} catch (IllegalArgumentException e) {
    67 						// hmmm...
    68 						continue;
    69 					}
    70 					if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) {
    71 						CellsLink firstLink = route.get(0);
    72 						Movement mov = firstLink.getSource().getCoordinate()
    73 								.getNecessaryMovement(firstLink.getTarget().getCoordinate());
    74 						if (!loseMovs.get(map).contains(mov)) {
    75 							shortestDist = dist.doubleValue();
    76 							nextTarget = target;
    77 							routeToNextTarget = route;
    78 							break;
    79 						}
    80 					}
    81 				}
    82 				if (nextTarget != null) {
    83 					for (CellsLink link : routeToNextTarget) {
    84 						Cell source = link.getSource();
    85 						Cell target = link.getTarget();
    86 						if (isSafeMovement(testMine, source, target)) {
    87 							Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate());
    88 							GameState state = game.move(mov);
    89 							if (state == GameState.Lose || state == GameState.Abort) {
    90 								loseMovs.get(map).add(mov);
    91 								loseLinks.add(link);
    92 							}
    93 						}
    94 						break;
    95 					}
    96 				} else {
    97 					for (Movement mov : Movement.values()) {
    98 						if (!loseMovs.get(map).contains(mov)) {
    99 							GameState state = game.move(mov);
   100 							if (state == GameState.Game && map.equals(testMine.toText())) {
   101 								if (mov != Movement.WAIT)
   102 									loseMovs.get(map).add(mov);
   103 								game.move(Movement.ABORT);
   104 							}
   105 							break;
   106 						}
   107 					}
   108 				}
   109 			}
   110 			addNewRoute(game);
   111 			attemptsCount++;
   112 		}
   113 	}
   114 
   115 	protected boolean isSafeMovement(Mine mine, Cell source, Cell target) {
   116 		if (source == null || target == null)
   117 			return false;
   118 		if (source.getContent().getTrampolineTarget() == target.getContent())
   119 			return true;
   120 		Cell upCell = mine.getCell(target.getCoordinate().up());
   121 		if (upCell == null)
   122 			return true;
   123 		Cell upUpCell = mine.getCell(target.getCoordinate().up().up());
   124 		Cell upUpLeftCell = mine.getCell(target.getCoordinate().up().up().left());
   125 		Cell upUpRightCell = mine.getCell(target.getCoordinate().up().up().right());
   126 		if (upCell.getContent() == CellContent.Empty
   127 				&& (upUpCell != null
   128 						&& (upUpCell.getContent() == CellContent.Rock || upUpCell.getContent() == CellContent.HighOrderRock)
   129 						|| upUpLeftCell != null
   130 						&& (upUpLeftCell.getContent() == CellContent.Rock || upUpLeftCell.getContent() == CellContent.HighOrderRock) || upUpRightCell != null
   131 						&& (upUpRightCell.getContent() == CellContent.Rock || upUpRightCell.getContent() == CellContent.HighOrderRock))) {
   132 			return false;
   133 		}
   134 		return true;
   135 	}
   136 
   137 	protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) {
   138 		Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
   139 		for (CellsLink link : getCellLinks(mine)) {
   140 			graph.addEdge(link, link.getSource(), link.getTarget());
   141 		}
   142 		return graph;
   143 	}
   144 
   145 	protected List<CellsLink> getCellLinks(Mine mine) {
   146 		List<CellsLink> links = new ArrayList<CellsLink>();
   147 		Cell robotCell = mine.getRobotCell();
   148 		findAllCellLinks(mine, robotCell, links, new HashSet<Cell>());
   149 		return links;
   150 	}
   151 
   152 	protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
   153 		if (processedCells.contains(cell))
   154 			return;
   155 		else
   156 			processedCells.add(cell);
   157 		for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
   158 			if (neighboringCell == null)
   159 				continue;
   160 			CellContent neighboringContent = neighboringCell.getContent();
   161 			if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
   162 					|| neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
   163 					|| neighboringContent == CellContent.OpenLambdaLift) {
   164 				if (isSafeMovement(mine, cell, neighboringCell)) {
   165 					CellsLink link = new CellsLink(mine, cell, neighboringCell);
   166 					if (!loseLinks.contains(link)) {
   167 						links.add(link);
   168 						findAllCellLinks(mine, neighboringCell, links, processedCells);
   169 					}
   170 				}
   171 			} else if (!CellContent.getTargets().contains(cell.getContent())
   172 					&& CellContent.getTargets().contains(neighboringContent)) {
   173 				findAllCellLinks(mine, neighboringCell, links, processedCells);
   174 			}
   175 		}
   176 		if (CellContent.getTrampolines().contains(cell.getContent())) {
   177 			for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
   178 				if (target == null)
   179 					continue;
   180 				if (isSafeMovement(mine, cell, target)) {
   181 					CellsLink link = new CellsLink(mine, cell, target);
   182 					if (!loseLinks.contains(link)) {
   183 						links.add(link);
   184 						findAllCellLinks(mine, target, links, processedCells);
   185 					}
   186 				}
   187 			}
   188 		}
   189 	}
   190 }