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