icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java
branchCleverSearch
changeset 51 b431cec961d7
parent 50 9530f55e9639
equal deleted inserted replaced
50:9530f55e9639 51:b431cec961d7
    12 import ru.bosony.model.cellscontents.CellContent;
    12 import ru.bosony.model.cellscontents.CellContent;
    13 import ru.bosony.model.game.Game;
    13 import ru.bosony.model.game.Game;
    14 import ru.bosony.model.game.GameState;
    14 import ru.bosony.model.game.GameState;
    15 import ru.bosony.model.mine.Cell;
    15 import ru.bosony.model.mine.Cell;
    16 import ru.bosony.model.mine.Mine;
    16 import ru.bosony.model.mine.Mine;
       
    17 import ru.bosony.model.moving.Coordinate;
    17 import ru.bosony.model.moving.Movement;
    18 import ru.bosony.model.moving.Movement;
    18 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
    19 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
    19 import edu.uci.ics.jung.graph.DirectedSparseGraph;
    20 import edu.uci.ics.jung.graph.DirectedSparseGraph;
    20 import edu.uci.ics.jung.graph.Graph;
    21 import edu.uci.ics.jung.graph.Graph;
    21 
    22 
    24  *         16.07.2012 11:05:26
    25  *         16.07.2012 11:05:26
    25  * 
    26  * 
    26  */
    27  */
    27 public class DijkstraSolver extends AbstractSolver {
    28 public class DijkstraSolver extends AbstractSolver {
    28 
    29 
    29 	protected Map<String, Set<Movement>>	loseMovs	= new HashMap<String, Set<Movement>>();
    30 	protected Map<String, Set<Movement>>	loseMovs							= new HashMap<String, Set<Movement>>();
    30 	protected Set<CellsLink>				loseLinks	= new HashSet<CellsLink>();
    31 	protected Map<String, Set<CellsLink>>	loseLinks							= new HashMap<String, Set<CellsLink>>();
       
    32 	protected Map<String, Set<Coordinate>>	attendedWalkingAroundCoordinates	= new HashMap<String, Set<Coordinate>>();
       
    33 	protected Map<String, Set<Coordinate>>	noWay								= new HashMap<String, Set<Coordinate>>();
    31 
    34 
    32 	public DijkstraSolver(Mine mine, SolverListener listener) {
    35 	public DijkstraSolver(Mine mine, SolverListener listener) {
    33 		super(mine, listener);
    36 		super(mine, listener);
    34 	}
    37 	}
    35 
    38 
    36 	@Override
    39 	@Override
    37 	public void solve() {
    40 	public void solve() {
    38 		while (true) {
    41 		while (true) {
    39 			Mine testMine = null;
    42 			Mine mine = null;
    40 			testMine = mine.clone();
    43 			mine = initialMine.clone();
    41 			Game game = new Game(testMine);
    44 			Game game = new Game(mine);
    42 
    45 
    43 			while (game.getState() == GameState.Game) {
    46 			while (game.getState() == GameState.Game) {
    44 				String map = testMine.toText();
    47 				Cell robotCell = mine.getRobotCell();
       
    48 				String map = mine.toText();
       
    49 				String mapWithoutRobot = mine.toTextWithoutRobot();
    45 				if (!loseMovs.containsKey(map)) {
    50 				if (!loseMovs.containsKey(map)) {
    46 					loseMovs.put(map, new HashSet<Movement>());
    51 					loseMovs.put(map, new HashSet<Movement>());
    47 				}
    52 				}
    48 				Set<Cell> targets = testMine.findCells(CellContent.Lambda);
    53 				if (!loseLinks.containsKey(map)) {
    49 				targets.addAll(testMine.findCells(CellContent.HighOrderRock));
    54 					loseLinks.put(map, new HashSet<CellsLink>());
    50 				if (targets.size() == 0)
    55 				}
    51 					targets = testMine.findCells(CellContent.OpenLambdaLift);
    56 				if (!noWay.containsKey(map)) {
    52 				Graph<Cell, CellsLink> graph = getDirectedGraph(testMine);
    57 					noWay.put(map, new HashSet<Coordinate>());
       
    58 				}
       
    59 				if (!attendedWalkingAroundCoordinates.containsKey(mapWithoutRobot)) {
       
    60 					attendedWalkingAroundCoordinates.put(mapWithoutRobot, new HashSet<Coordinate>());
       
    61 				}
       
    62 				attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(robotCell.getCoordinate());
       
    63 				Set<Cell> targets = new HashSet<Cell>();
       
    64 				boolean isFreeWay = false;
       
    65 				for (Cell cell : mine.getAdjacentCells(robotCell)) {
       
    66 					Movement mov = robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate());
       
    67 					isFreeWay |= !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(cell.getCoordinate())
       
    68 							&& !loseMovs.get(map).contains(mov);
       
    69 				}
       
    70 				if (isFreeWay) {
       
    71 					targets.addAll(mine.findCells(CellContent.Lambda));
       
    72 					targets.addAll(mine.findCells(CellContent.HighOrderRock));
       
    73 					if (targets.size() == 0)
       
    74 						targets.addAll(mine.findCells(CellContent.OpenLambdaLift));
       
    75 					targets.removeAll(mine.getCells(noWay.get(map)));
       
    76 				} else {
       
    77 					targets.addAll(mine.findCells(CellContent.Empty));
       
    78 					targets.addAll(mine.findCells(CellContent.Earth));
       
    79 					targets.addAll(mine.findCells(CellContent.HuttonsRazor));
       
    80 					for (CellContent content : CellContent.getTrampolines()) {
       
    81 						targets.addAll(mine.findCells(content));
       
    82 					}
       
    83 					targets.removeAll(mine.getCells(attendedWalkingAroundCoordinates.get(mapWithoutRobot)));
       
    84 					targets.removeAll(mine.getCells(noWay.get(map)));
       
    85 					if (targets.size() == 0) {
       
    86 						game.move(Movement.ABORT);
       
    87 						continue;
       
    88 					}
       
    89 				}
       
    90 				Graph<Cell, CellsLink> graph = getDirectedGraph(mine);
    53 				DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
    91 				DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
    54 						new CellsLinkTransformer<CellsLink, Double>());
    92 						new CellsLinkTransformer<CellsLink, Double>());
    55 				Cell nextTarget = null;
    93 				Cell nextTarget = null;
    56 				List<CellsLink> routeToNextTarget = null;
    94 				List<CellsLink> routeToNextTarget = null;
    57 				double shortestDist = Double.MAX_VALUE;
    95 				double shortestDist = Double.MAX_VALUE;
    58 				for (Cell target : targets) {
    96 				for (Cell target : targets) {
    59 					if (target == null)
       
    60 						continue;
       
    61 					List<CellsLink> route = null;
    97 					List<CellsLink> route = null;
    62 					Number dist = null;
    98 					Number dist = null;
    63 					try {
    99 					try {
    64 						route = alg.getPath(testMine.getRobotCell(), target);
   100 						route = alg.getPath(mine.getRobotCell(), target);
    65 						dist = alg.getDistance(testMine.getRobotCell(), target);
   101 						dist = alg.getDistance(mine.getRobotCell(), target);
    66 					} catch (IllegalArgumentException e) {
   102 					} catch (IllegalArgumentException e) {
    67 						// hmmm...
   103 						noWay.get(map).add(target.getCoordinate());
    68 						continue;
   104 						continue;
    69 					}
   105 					}
    70 					if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) {
   106 					if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) {
    71 						CellsLink firstLink = route.get(0);
   107 						CellsLink firstLink = route.get(0);
    72 						Movement mov = firstLink.getSource().getCoordinate()
   108 						Movement mov = firstLink.getSource().getCoordinate()
    73 								.getNecessaryMovement(firstLink.getTarget().getCoordinate());
   109 								.getNecessaryMovement(firstLink.getTarget().getCoordinate());
    74 						if (!loseMovs.get(map).contains(mov)) {
   110 						if (!loseMovs.get(map).contains(mov)
       
   111 								&& !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(
       
   112 										target.getCoordinate())) {
    75 							shortestDist = dist.doubleValue();
   113 							shortestDist = dist.doubleValue();
    76 							nextTarget = target;
   114 							nextTarget = target;
    77 							routeToNextTarget = route;
   115 							routeToNextTarget = route;
    78 							break;
       
    79 						}
   116 						}
    80 					}
   117 					}
    81 				}
   118 				}
    82 				if (nextTarget != null) {
   119 				if (nextTarget != null) {
    83 					for (CellsLink link : routeToNextTarget) {
   120 					for (CellsLink link : routeToNextTarget) {
    84 						Cell source = link.getSource();
   121 						Cell source = link.getSource();
    85 						Cell target = link.getTarget();
   122 						Cell target = link.getTarget();
    86 						if (isSafeMovement(testMine, source, target)) {
   123 						if (isSafeMovement(mine, source, target)) {
    87 							Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate());
   124 							Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate());
    88 							GameState state = game.move(mov);
   125 							GameState state = game.move(mov);
    89 							if (state == GameState.Lose || state == GameState.Abort) {
   126 							if (state == GameState.Lose || state == GameState.Abort) {
    90 								loseMovs.get(map).add(mov);
   127 								loseMovs.get(map).add(mov);
    91 								loseLinks.add(link);
   128 								loseLinks.get(map).add(link);
    92 							}
   129 							}
    93 						}
   130 						}
    94 						break;
   131 						break;
    95 					}
   132 					}
    96 				} else {
   133 				} else {
       
   134 					// Walking around
    97 					for (Movement mov : Movement.values()) {
   135 					for (Movement mov : Movement.values()) {
    98 						if (!loseMovs.get(map).contains(mov)) {
   136 						if (!loseMovs.get(map).contains(mov)) {
    99 							GameState state = game.move(mov);
   137 							if (mov == Movement.RAZOR && mine.getRazorsCount() > 0) {
   100 							if (state == GameState.Game && map.equals(testMine.toText())) {
   138 								boolean isRazorApplied = false;
   101 								if (mov != Movement.WAIT)
   139 								for (Cell cell : mine.getNeighboringCells(robotCell)) {
       
   140 									isRazorApplied |= cell.getContent() == CellContent.WadlersBeard;
       
   141 								}
       
   142 								if (!isRazorApplied)
       
   143 									continue;
       
   144 							}
       
   145 							Cell movCell = null;
       
   146 							for (Cell cell : mine.getAdjacentCells(robotCell)) {
       
   147 								if (robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate()) == mov) {
       
   148 									if (!attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(
       
   149 											cell.getCoordinate())) {
       
   150 										movCell = cell;
       
   151 										break;
       
   152 									}
       
   153 								}
       
   154 
       
   155 							}
       
   156 							if (mov == Movement.WAIT
       
   157 									|| mov == Movement.RAZOR
       
   158 									|| movCell != null
       
   159 									&& !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(
       
   160 											movCell.getCoordinate())) {
       
   161 								GameState state = game.move(mov);
       
   162 								if (movCell != null && mapWithoutRobot.equals(mine.toTextWithoutRobot()))
       
   163 									attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(movCell.getCoordinate());
       
   164 								if (state == GameState.Game && map.equals(mine.toText())) {
   102 									loseMovs.get(map).add(mov);
   165 									loseMovs.get(map).add(mov);
   103 								game.move(Movement.ABORT);
   166 								}
   104 							}
   167 								break;
   105 							break;
   168 							}
   106 						}
   169 						}
   107 					}
   170 					}
   108 				}
   171 				}
   109 			}
   172 			}
   110 			addNewRoute(game);
   173 			addNewRoute(game);
   152 	protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
   215 	protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
   153 		if (processedCells.contains(cell))
   216 		if (processedCells.contains(cell))
   154 			return;
   217 			return;
   155 		else
   218 		else
   156 			processedCells.add(cell);
   219 			processedCells.add(cell);
       
   220 		String map = mine.toText();
   157 		for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
   221 		for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
   158 			if (neighboringCell == null)
   222 			if (neighboringCell == null)
   159 				continue;
   223 				continue;
   160 			CellContent neighboringContent = neighboringCell.getContent();
   224 			CellContent neighboringContent = neighboringCell.getContent();
   161 			if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
   225 			if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
   162 					|| neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
   226 					|| neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
   163 					|| neighboringContent == CellContent.OpenLambdaLift) {
   227 					|| neighboringContent == CellContent.OpenLambdaLift) {
   164 				if (isSafeMovement(mine, cell, neighboringCell)) {
   228 				if (isSafeMovement(mine, cell, neighboringCell)) {
   165 					CellsLink link = new CellsLink(mine, cell, neighboringCell);
   229 					CellsLink link = new CellsLink(mine, cell, neighboringCell);
   166 					if (!loseLinks.contains(link)) {
   230 					if (!loseLinks.get(map).contains(link)) {
   167 						links.add(link);
   231 						links.add(link);
   168 						findAllCellLinks(mine, neighboringCell, links, processedCells);
   232 						findAllCellLinks(mine, neighboringCell, links, processedCells);
   169 					}
   233 					}
   170 				}
   234 				}
   171 			} else if (!CellContent.getTargets().contains(cell.getContent())
   235 			} else if (!CellContent.getTargets().contains(cell.getContent())
   177 			for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
   241 			for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
   178 				if (target == null)
   242 				if (target == null)
   179 					continue;
   243 					continue;
   180 				if (isSafeMovement(mine, cell, target)) {
   244 				if (isSafeMovement(mine, cell, target)) {
   181 					CellsLink link = new CellsLink(mine, cell, target);
   245 					CellsLink link = new CellsLink(mine, cell, target);
   182 					if (!loseLinks.contains(link)) {
   246 					if (!loseLinks.get(map).contains(link)) {
   183 						links.add(link);
   247 						links.add(link);
   184 						findAllCellLinks(mine, target, links, processedCells);
   248 						findAllCellLinks(mine, target, links, processedCells);
   185 					}
   249 					}
   186 				}
   250 				}
   187 			}
   251 			}