icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java
branchCleverSearch
changeset 47 b3d48e364f9e
parent 46 4197f736166a
child 50 9530f55e9639
     1.1 --- a/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java	Mon Jul 16 15:20:48 2012 +0400
     1.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java	Mon Jul 16 18:58:08 2012 +0400
     1.3 @@ -14,7 +14,6 @@
     1.4  import ru.bosony.model.game.GameState;
     1.5  import ru.bosony.model.mine.Cell;
     1.6  import ru.bosony.model.mine.Mine;
     1.7 -import ru.bosony.model.moving.Coordinate;
     1.8  import ru.bosony.model.moving.Movement;
     1.9  import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
    1.10  import edu.uci.ics.jung.graph.DirectedSparseGraph;
    1.11 @@ -27,7 +26,8 @@
    1.12   */
    1.13  public class DijkstraSolver extends AbstractSolver {
    1.14  
    1.15 -	protected Map<Mine, Set<Cell>>	testedTargetCells	= new HashMap<Mine, Set<Cell>>();
    1.16 +	protected Map<String, Set<Movement>>	loseMovs	= new HashMap<String, Set<Movement>>();
    1.17 +	protected Set<CellsLink>				loseLinks	= new HashSet<CellsLink>();
    1.18  
    1.19  	public DijkstraSolver(Mine mine, SolverListener listener) {
    1.20  		super(mine, listener);
    1.21 @@ -45,26 +45,14 @@
    1.22  			Game game = new Game(testMine);
    1.23  
    1.24  			while (game.getState() == GameState.Game) {
    1.25 -				if (!testedTargetCells.containsKey(testMine)) {
    1.26 -					testedTargetCells.put(testMine, new HashSet<Cell>());
    1.27 +				String map = testMine.toText();
    1.28 +				if (!loseMovs.containsKey(map)) {
    1.29 +					loseMovs.put(map, new HashSet<Movement>());
    1.30  				}
    1.31  				Set<Cell> targets = testMine.findCells(CellContent.Lambda);
    1.32  				targets.addAll(testMine.findCells(CellContent.HighOrderRock));
    1.33  				if (targets.size() == 0)
    1.34  					targets = testMine.findCells(CellContent.OpenLambdaLift);
    1.35 -				if (targets.size() == 0 || testedTargetCells.get(testMine).containsAll(targets)) {
    1.36 -					// No targets. Walking around
    1.37 -					Cell robotCell = testMine.getRobotCell();
    1.38 -					for (Coordinate nextCoordinate : robotCell.getCoordinate().getAdjacentCoordinates()) {
    1.39 -						Cell nextCell = testMine.getCell(nextCoordinate);
    1.40 -						targets.add(nextCell);
    1.41 -					}
    1.42 -					if (testedTargetCells.get(testMine).containsAll(targets)) {
    1.43 -						// Fail. All cases are tried.
    1.44 -						game.move(Movement.ABORT);
    1.45 -						break;
    1.46 -					}
    1.47 -				}
    1.48  				Graph<Cell, CellsLink> graph = getDirectedGraph(testMine);
    1.49  				DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
    1.50  						new CellsLinkTransformer<CellsLink, Double>());
    1.51 @@ -81,24 +69,44 @@
    1.52  						dist = alg.getDistance(testMine.getRobotCell(), target);
    1.53  					} catch (IllegalArgumentException e) {
    1.54  						// hmmm...
    1.55 -						testedTargetCells.get(testMine).add(target);
    1.56  						continue;
    1.57  					}
    1.58 -					if (dist != null && route != null && dist.doubleValue() < shortestDist
    1.59 -							&& !testedTargetCells.get(testMine).contains(target)) {
    1.60 -						shortestDist = dist.doubleValue();
    1.61 -						nextTarget = target;
    1.62 -						routeToNextTarget = route;
    1.63 -						testedTargetCells.get(testMine).add(target);
    1.64 +					if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) {
    1.65 +						CellsLink firstLink = route.get(0);
    1.66 +						Movement mov = firstLink.getSource().getCoordinate()
    1.67 +								.getNecessaryMovement(firstLink.getTarget().getCoordinate());
    1.68 +						if (!loseMovs.get(map).contains(mov)) {
    1.69 +							shortestDist = dist.doubleValue();
    1.70 +							nextTarget = target;
    1.71 +							routeToNextTarget = route;
    1.72 +							break;
    1.73 +						}
    1.74  					}
    1.75  				}
    1.76  				if (nextTarget != null) {
    1.77  					for (CellsLink link : routeToNextTarget) {
    1.78 -						Movement mov = link.getSource().getCoordinate()
    1.79 -								.getNecessaryMovement(link.getTarget().getCoordinate());
    1.80 -						game.move(mov);
    1.81 +						Cell source = link.getSource();
    1.82 +						Cell target = link.getTarget();
    1.83 +						if (isSafeMovement(testMine, source, target)) {
    1.84 +							Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate());
    1.85 +							GameState state = game.move(mov);
    1.86 +							if (state == GameState.Lose || state == GameState.Abort || game.hasViciousCircle()) {
    1.87 +								loseMovs.get(map).add(mov);
    1.88 +								loseLinks.add(link);
    1.89 +							}
    1.90 +						}
    1.91  						break;
    1.92  					}
    1.93 +				} else {
    1.94 +					for (Movement mov : Movement.values()) {
    1.95 +						if (!loseMovs.get(map).contains(mov)) {
    1.96 +							GameState state = game.move(mov);
    1.97 +							if (state == GameState.Lose || state == GameState.Abort || map.equals(testMine.toText())
    1.98 +									|| game.hasViciousCircle())
    1.99 +								loseMovs.get(map).add(mov);
   1.100 +							break;
   1.101 +						}
   1.102 +					}
   1.103  				}
   1.104  			}
   1.105  			if (game.getState() == GameState.Win) {
   1.106 @@ -108,6 +116,28 @@
   1.107  		}
   1.108  	}
   1.109  
   1.110 +	protected boolean isSafeMovement(Mine mine, Cell source, Cell target) {
   1.111 +		if (source == null || target == null)
   1.112 +			return false;
   1.113 +		if (source.getContent().getTrampolineTarget() == target.getContent())
   1.114 +			return true;
   1.115 +		Cell upCell = mine.getCell(target.getCoordinate().up());
   1.116 +		if (upCell == null)
   1.117 +			return true;
   1.118 +		Cell upUpCell = mine.getCell(target.getCoordinate().up().up());
   1.119 +		Cell upUpLeftCell = mine.getCell(target.getCoordinate().up().up().left());
   1.120 +		Cell upUpRightCell = mine.getCell(target.getCoordinate().up().up().right());
   1.121 +		if (upCell.getContent() == CellContent.Empty
   1.122 +				&& (upUpCell != null
   1.123 +						&& (upUpCell.getContent() == CellContent.Rock || upUpCell.getContent() == CellContent.HighOrderRock)
   1.124 +						|| upUpLeftCell != null
   1.125 +						&& (upUpLeftCell.getContent() == CellContent.Rock || upUpLeftCell.getContent() == CellContent.HighOrderRock) || upUpRightCell != null
   1.126 +						&& (upUpRightCell.getContent() == CellContent.Rock || upUpRightCell.getContent() == CellContent.HighOrderRock))) {
   1.127 +			return false;
   1.128 +		}
   1.129 +		return true;
   1.130 +	}
   1.131 +
   1.132  	protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) {
   1.133  		Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
   1.134  		for (CellsLink link : getCellLinks(mine)) {
   1.135 @@ -135,9 +165,13 @@
   1.136  			if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
   1.137  					|| neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
   1.138  					|| neighboringContent == CellContent.OpenLambdaLift) {
   1.139 -				CellsLink link = new CellsLink(mine, cell, neighboringCell);
   1.140 -				links.add(link);
   1.141 -				findAllCellLinks(mine, neighboringCell, links, processedCells);
   1.142 +				if (isSafeMovement(mine, cell, neighboringCell)) {
   1.143 +					CellsLink link = new CellsLink(mine, cell, neighboringCell);
   1.144 +					if (!loseLinks.contains(link)) {
   1.145 +						links.add(link);
   1.146 +						findAllCellLinks(mine, neighboringCell, links, processedCells);
   1.147 +					}
   1.148 +				}
   1.149  			} else if (!CellContent.getTargets().contains(cell.getContent())
   1.150  					&& CellContent.getTargets().contains(neighboringContent)) {
   1.151  				findAllCellLinks(mine, neighboringCell, links, processedCells);
   1.152 @@ -147,9 +181,13 @@
   1.153  			for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
   1.154  				if (target == null)
   1.155  					continue;
   1.156 -				CellsLink link = new CellsLink(mine, cell, target);
   1.157 -				links.add(link);
   1.158 -				findAllCellLinks(mine, target, links, processedCells);
   1.159 +				if (isSafeMovement(mine, cell, target)) {
   1.160 +					CellsLink link = new CellsLink(mine, cell, target);
   1.161 +					if (!loseLinks.contains(link)) {
   1.162 +						links.add(link);
   1.163 +						findAllCellLinks(mine, target, links, processedCells);
   1.164 +					}
   1.165 +				}
   1.166  			}
   1.167  		}
   1.168  	}