1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java Mon Jul 16 13:10:14 2012 +0400
1.3 @@ -0,0 +1,59 @@
1.4 +package ru.bosony.graph;
1.5 +
1.6 +import ru.bosony.model.mine.Cell;
1.7 +import ru.bosony.model.mine.Mine;
1.8 +
1.9 +/**
1.10 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
1.11 + * 16.07.2012 11:09:35
1.12 + *
1.13 + */
1.14 +public class CellsLink {
1.15 + Mine mine;
1.16 + Cell source;
1.17 + Cell target;
1.18 + double weight = 1d;
1.19 +
1.20 + public CellsLink(Mine mine, Cell source, Cell target) {
1.21 + this.mine = mine;
1.22 + this.source = source;
1.23 + this.target = target;
1.24 + calculateWeight();
1.25 + }
1.26 +
1.27 + protected void calculateWeight() {
1.28 +
1.29 + }
1.30 +
1.31 + public Mine getMine() {
1.32 + return mine;
1.33 + }
1.34 +
1.35 + public Cell getSource() {
1.36 + return source;
1.37 + }
1.38 +
1.39 + public Cell getTarget() {
1.40 + return target;
1.41 + }
1.42 +
1.43 + public double getWeight() {
1.44 + return weight;
1.45 + }
1.46 +
1.47 + @Override
1.48 + public int hashCode() {
1.49 + // TODO Auto-generated method stub
1.50 + return super.hashCode();
1.51 + }
1.52 +
1.53 + @Override
1.54 + public boolean equals(Object obj) {
1.55 + if (obj instanceof CellsLink) {
1.56 + CellsLink otherLink = (CellsLink) obj;
1.57 + return source.equals(otherLink.source) && target.equals(otherLink.target);
1.58 + }
1.59 + return super.equals(obj);
1.60 + }
1.61 +
1.62 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/icfp-solver/src/main/java/ru/bosony/graph/CellsLinkTransformer.groovy Mon Jul 16 13:10:14 2012 +0400
2.3 @@ -0,0 +1,18 @@
2.4 +package ru.bosony.graph;
2.5 +
2.6 +import org.apache.commons.collections15.Transformer;
2.7 +
2.8 +/**
2.9 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
2.10 + * 16.07.2012 12:05:51
2.11 + *
2.12 + * @param <CellsLink>
2.13 + * @param <Double>
2.14 + */
2.15 +public class CellsLinkTransformer<CellsLink, Double> implements Transformer<CellsLink, Double> {
2.16 +
2.17 + @Override
2.18 + public Double transform(CellsLink input) {
2.19 + return input.getWeight();
2.20 + }
2.21 +}
3.1 --- a/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java Mon Jul 16 13:09:16 2012 +0400
3.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java Mon Jul 16 13:10:14 2012 +0400
3.3 @@ -1,16 +1,21 @@
3.4 package ru.bosony.solvers;
3.5
3.6 +import java.util.HashMap;
3.7 +import java.util.Map;
3.8 +
3.9 import ru.bosony.model.mine.Mine;
3.10
3.11 /**
3.12 - * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
3.13 - * 16.07.2012 4:30:47
3.14 - *
3.15 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
3.16 + * 16.07.2012 4:30:47
3.17 + *
3.18 */
3.19 public abstract class AbstractSolver {
3.20
3.21 - protected Mine mine = null;
3.22 - SolverListener listener = null;
3.23 + protected Mine mine = null;
3.24 + protected SolverListener listener = null;
3.25 + protected Map<String, Integer> routesWithScore = new HashMap<String, Integer>();
3.26 + protected long attemptsCount = 0;
3.27
3.28 public AbstractSolver(Mine mine, SolverListener listener) {
3.29 this.mine = mine;
3.30 @@ -18,5 +23,21 @@
3.31 solve();
3.32 }
3.33
3.34 + protected void addNewRoute(String route, int score) {
3.35 + if (!routesWithScore.containsKey(route) && score > getMaxFoundScore()) {
3.36 + routesWithScore.put(route, score);
3.37 + listener.foundNextRoute(route);
3.38 + }
3.39 + }
3.40 +
3.41 + protected int getMaxFoundScore() {
3.42 + int maxScore = 0;
3.43 + for (int foundScore : routesWithScore.values()) {
3.44 + if (foundScore > maxScore)
3.45 + maxScore = foundScore;
3.46 + }
3.47 + return maxScore;
3.48 + }
3.49 +
3.50 protected abstract void solve();
3.51 }
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Mon Jul 16 13:10:14 2012 +0400
4.3 @@ -0,0 +1,123 @@
4.4 +package ru.bosony.solvers;
4.5 +
4.6 +import java.util.ArrayList;
4.7 +import java.util.HashSet;
4.8 +import java.util.List;
4.9 +import java.util.Set;
4.10 +
4.11 +import ru.bosony.graph.CellsLink;
4.12 +import ru.bosony.graph.CellsLinkTransformer;
4.13 +import ru.bosony.model.cellscontents.CellContent;
4.14 +import ru.bosony.model.game.Game;
4.15 +import ru.bosony.model.game.GameState;
4.16 +import ru.bosony.model.mine.Cell;
4.17 +import ru.bosony.model.mine.Mine;
4.18 +import ru.bosony.model.moving.Movement;
4.19 +import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
4.20 +import edu.uci.ics.jung.graph.DirectedSparseGraph;
4.21 +import edu.uci.ics.jung.graph.Graph;
4.22 +
4.23 +/**
4.24 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
4.25 + * 16.07.2012 11:05:26
4.26 + *
4.27 + */
4.28 +public class DijkstraSolver extends AbstractSolver {
4.29 +
4.30 + public DijkstraSolver(Mine mine, SolverListener listener) {
4.31 + super(mine, listener);
4.32 + }
4.33 +
4.34 + @Override
4.35 + protected void solve() {
4.36 + while (true) {
4.37 + Game game = new Game(mine);
4.38 +
4.39 + while (game.getState() == GameState.Game) {
4.40 + try {
4.41 + Set<Cell> targets = mine.findCells(CellContent.Lambda);
4.42 + targets.addAll(mine.findCells(CellContent.HighOrderRock));
4.43 + if (targets.size() == 0)
4.44 + targets = mine.findCells(CellContent.OpenLambdaLift);
4.45 + if (targets.size() == 0)
4.46 + break;
4.47 + Graph<Cell, CellsLink> graph = getDirectedGraph();
4.48 + DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
4.49 + new CellsLinkTransformer<CellsLink, Double>());
4.50 + Cell nextTarget = null;
4.51 + List<CellsLink> routeToNextTarget = null;
4.52 + double shortestDist = Double.MAX_VALUE;
4.53 + for (Cell target : targets) {
4.54 + List<CellsLink> route = alg.getPath(mine.getRobotCell(), target);
4.55 + Number dist = alg.getDistance(mine.getRobotCell(), target);
4.56 + if (dist != null && dist.doubleValue() < shortestDist) {
4.57 + shortestDist = dist.doubleValue();
4.58 + nextTarget = target;
4.59 + routeToNextTarget = route;
4.60 + }
4.61 + }
4.62 + if (nextTarget != null) {
4.63 + for (CellsLink link : routeToNextTarget) {
4.64 + Movement mov = link.getSource().getCoordinate()
4.65 + .getNecessaryMovement(link.getTarget().getCoordinate());
4.66 + game.move(mov);
4.67 + break;
4.68 + }
4.69 + }
4.70 + } catch (IllegalArgumentException e) {
4.71 + // hmmm...
4.72 + }
4.73 + }
4.74 + if (game.getState() == GameState.Win) {
4.75 + addNewRoute(game.getStringRoute(), game.getScore());
4.76 + }
4.77 + attemptsCount++;
4.78 + }
4.79 + }
4.80 +
4.81 + protected Graph<Cell, CellsLink> getDirectedGraph() {
4.82 + Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
4.83 + for (CellsLink link : getCellLinks()) {
4.84 + graph.addEdge(link, link.getSource(), link.getTarget());
4.85 + }
4.86 + return graph;
4.87 + }
4.88 +
4.89 + protected List<CellsLink> getCellLinks() {
4.90 + List<CellsLink> links = new ArrayList<CellsLink>();
4.91 + Cell robotCell = mine.getRobotCell();
4.92 + findAllCellLinks(robotCell, links, new HashSet<Cell>());
4.93 + return links;
4.94 + }
4.95 +
4.96 + protected void findAllCellLinks(Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
4.97 + if (processedCells.contains(cell))
4.98 + return;
4.99 + else
4.100 + processedCells.add(cell);
4.101 + for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
4.102 + if (neighboringCell == null)
4.103 + continue;
4.104 + CellContent neighboringContent = neighboringCell.getContent();
4.105 + if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
4.106 + || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
4.107 + || neighboringContent == CellContent.OpenLambdaLift) {
4.108 + CellsLink link = new CellsLink(mine, cell, neighboringCell);
4.109 + links.add(link);
4.110 + findAllCellLinks(neighboringCell, links, processedCells);
4.111 + } else if (!CellContent.getTargets().contains(cell.getContent())
4.112 + && CellContent.getTargets().contains(neighboringContent)) {
4.113 + findAllCellLinks(neighboringCell, links, processedCells);
4.114 + }
4.115 + }
4.116 + if (CellContent.getTrampolines().contains(cell.getContent())) {
4.117 + for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
4.118 + if (target == null)
4.119 + continue;
4.120 + CellsLink link = new CellsLink(mine, cell, target);
4.121 + links.add(link);
4.122 + findAllCellLinks(target, links, processedCells);
4.123 + }
4.124 + }
4.125 + }
4.126 +}
5.1 --- a/icfp-solver/src/main/java/ru/bosony/solvers/Solver.java Mon Jul 16 13:09:16 2012 +0400
5.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/Solver.java Mon Jul 16 13:10:14 2012 +0400
5.3 @@ -25,8 +25,8 @@
5.4 System.out.println(route);
5.5 }
5.6 });
5.7 - AbstractSolver solver = new SimpleSolver(mine, new SolverListener() {
5.8 -
5.9 + AbstractSolver solver = new DijkstraSolver(mine, new SolverListener() {
5.10 +
5.11 @Override
5.12 public void foundNextRoute(String newRoute) {
5.13 route = newRoute;