icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java
changeset 42 2bd032a701a7
child 46 4197f736166a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java	Mon Jul 16 13:10:14 2012 +0400
     1.3 @@ -0,0 +1,123 @@
     1.4 +package ru.bosony.solvers;
     1.5 +
     1.6 +import java.util.ArrayList;
     1.7 +import java.util.HashSet;
     1.8 +import java.util.List;
     1.9 +import java.util.Set;
    1.10 +
    1.11 +import ru.bosony.graph.CellsLink;
    1.12 +import ru.bosony.graph.CellsLinkTransformer;
    1.13 +import ru.bosony.model.cellscontents.CellContent;
    1.14 +import ru.bosony.model.game.Game;
    1.15 +import ru.bosony.model.game.GameState;
    1.16 +import ru.bosony.model.mine.Cell;
    1.17 +import ru.bosony.model.mine.Mine;
    1.18 +import ru.bosony.model.moving.Movement;
    1.19 +import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
    1.20 +import edu.uci.ics.jung.graph.DirectedSparseGraph;
    1.21 +import edu.uci.ics.jung.graph.Graph;
    1.22 +
    1.23 +/**
    1.24 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    1.25 + *         16.07.2012 11:05:26
    1.26 + * 
    1.27 + */
    1.28 +public class DijkstraSolver extends AbstractSolver {
    1.29 +
    1.30 +	public DijkstraSolver(Mine mine, SolverListener listener) {
    1.31 +		super(mine, listener);
    1.32 +	}
    1.33 +
    1.34 +	@Override
    1.35 +	protected void solve() {
    1.36 +		while (true) {
    1.37 +			Game game = new Game(mine);
    1.38 +
    1.39 +			while (game.getState() == GameState.Game) {
    1.40 +				try {
    1.41 +					Set<Cell> targets = mine.findCells(CellContent.Lambda);
    1.42 +					targets.addAll(mine.findCells(CellContent.HighOrderRock));
    1.43 +					if (targets.size() == 0)
    1.44 +						targets = mine.findCells(CellContent.OpenLambdaLift);
    1.45 +					if (targets.size() == 0)
    1.46 +						break;
    1.47 +					Graph<Cell, CellsLink> graph = getDirectedGraph();
    1.48 +					DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
    1.49 +							new CellsLinkTransformer<CellsLink, Double>());
    1.50 +					Cell nextTarget = null;
    1.51 +					List<CellsLink> routeToNextTarget = null;
    1.52 +					double shortestDist = Double.MAX_VALUE;
    1.53 +					for (Cell target : targets) {
    1.54 +						List<CellsLink> route = alg.getPath(mine.getRobotCell(), target);
    1.55 +						Number dist = alg.getDistance(mine.getRobotCell(), target);
    1.56 +						if (dist != null && dist.doubleValue() < shortestDist) {
    1.57 +							shortestDist = dist.doubleValue();
    1.58 +							nextTarget = target;
    1.59 +							routeToNextTarget = route;
    1.60 +						}
    1.61 +					}
    1.62 +					if (nextTarget != null) {
    1.63 +						for (CellsLink link : routeToNextTarget) {
    1.64 +							Movement mov = link.getSource().getCoordinate()
    1.65 +									.getNecessaryMovement(link.getTarget().getCoordinate());
    1.66 +							game.move(mov);
    1.67 +							break;
    1.68 +						}
    1.69 +					}
    1.70 +				} catch (IllegalArgumentException e) {
    1.71 +					// hmmm...
    1.72 +				}
    1.73 +			}
    1.74 +			if (game.getState() == GameState.Win) {
    1.75 +				addNewRoute(game.getStringRoute(), game.getScore());
    1.76 +			}
    1.77 +			attemptsCount++;
    1.78 +		}
    1.79 +	}
    1.80 +
    1.81 +	protected Graph<Cell, CellsLink> getDirectedGraph() {
    1.82 +		Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
    1.83 +		for (CellsLink link : getCellLinks()) {
    1.84 +			graph.addEdge(link, link.getSource(), link.getTarget());
    1.85 +		}
    1.86 +		return graph;
    1.87 +	}
    1.88 +
    1.89 +	protected List<CellsLink> getCellLinks() {
    1.90 +		List<CellsLink> links = new ArrayList<CellsLink>();
    1.91 +		Cell robotCell = mine.getRobotCell();
    1.92 +		findAllCellLinks(robotCell, links, new HashSet<Cell>());
    1.93 +		return links;
    1.94 +	}
    1.95 +
    1.96 +	protected void findAllCellLinks(Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
    1.97 +		if (processedCells.contains(cell))
    1.98 +			return;
    1.99 +		else
   1.100 +			processedCells.add(cell);
   1.101 +		for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
   1.102 +			if (neighboringCell == null)
   1.103 +				continue;
   1.104 +			CellContent neighboringContent = neighboringCell.getContent();
   1.105 +			if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
   1.106 +					|| neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
   1.107 +					|| neighboringContent == CellContent.OpenLambdaLift) {
   1.108 +				CellsLink link = new CellsLink(mine, cell, neighboringCell);
   1.109 +				links.add(link);
   1.110 +				findAllCellLinks(neighboringCell, links, processedCells);
   1.111 +			} else if (!CellContent.getTargets().contains(cell.getContent())
   1.112 +					&& CellContent.getTargets().contains(neighboringContent)) {
   1.113 +				findAllCellLinks(neighboringCell, links, processedCells);
   1.114 +			}
   1.115 +		}
   1.116 +		if (CellContent.getTrampolines().contains(cell.getContent())) {
   1.117 +			for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
   1.118 +				if (target == null)
   1.119 +					continue;
   1.120 +				CellsLink link = new CellsLink(mine, cell, target);
   1.121 +				links.add(link);
   1.122 +				findAllCellLinks(target, links, processedCells);
   1.123 +			}
   1.124 +		}
   1.125 +	}
   1.126 +}