# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342429814 -14400 # Node ID 2bd032a701a7bf139db2fe27f37ced22b3fb9ed5 # Parent ce8e85452c3b05977d020517c62810cba58fd9d7 Add solver based on Dijkstra algorithm diff -r ce8e85452c3b -r 2bd032a701a7 icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java Mon Jul 16 13:10:14 2012 +0400 @@ -0,0 +1,59 @@ +package ru.bosony.graph; + +import ru.bosony.model.mine.Cell; +import ru.bosony.model.mine.Mine; + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 16.07.2012 11:09:35 + * + */ +public class CellsLink { + Mine mine; + Cell source; + Cell target; + double weight = 1d; + + public CellsLink(Mine mine, Cell source, Cell target) { + this.mine = mine; + this.source = source; + this.target = target; + calculateWeight(); + } + + protected void calculateWeight() { + + } + + public Mine getMine() { + return mine; + } + + public Cell getSource() { + return source; + } + + public Cell getTarget() { + return target; + } + + public double getWeight() { + return weight; + } + + @Override + public int hashCode() { + // TODO Auto-generated method stub + return super.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof CellsLink) { + CellsLink otherLink = (CellsLink) obj; + return source.equals(otherLink.source) && target.equals(otherLink.target); + } + return super.equals(obj); + } + +} diff -r ce8e85452c3b -r 2bd032a701a7 icfp-solver/src/main/java/ru/bosony/graph/CellsLinkTransformer.groovy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-solver/src/main/java/ru/bosony/graph/CellsLinkTransformer.groovy Mon Jul 16 13:10:14 2012 +0400 @@ -0,0 +1,18 @@ +package ru.bosony.graph; + +import org.apache.commons.collections15.Transformer; + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 16.07.2012 12:05:51 + * + * @param + * @param + */ +public class CellsLinkTransformer implements Transformer { + + @Override + public Double transform(CellsLink input) { + return input.getWeight(); + } +} diff -r ce8e85452c3b -r 2bd032a701a7 icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java --- a/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java Mon Jul 16 13:09:16 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java Mon Jul 16 13:10:14 2012 +0400 @@ -1,16 +1,21 @@ package ru.bosony.solvers; +import java.util.HashMap; +import java.util.Map; + import ru.bosony.model.mine.Mine; /** - * @author indvdum (gotoindvdum[at]gmail[dot]com)
- * 16.07.2012 4:30:47 - * + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 16.07.2012 4:30:47 + * */ public abstract class AbstractSolver { - protected Mine mine = null; - SolverListener listener = null; + protected Mine mine = null; + protected SolverListener listener = null; + protected Map routesWithScore = new HashMap(); + protected long attemptsCount = 0; public AbstractSolver(Mine mine, SolverListener listener) { this.mine = mine; @@ -18,5 +23,21 @@ solve(); } + protected void addNewRoute(String route, int score) { + if (!routesWithScore.containsKey(route) && score > getMaxFoundScore()) { + routesWithScore.put(route, score); + listener.foundNextRoute(route); + } + } + + protected int getMaxFoundScore() { + int maxScore = 0; + for (int foundScore : routesWithScore.values()) { + if (foundScore > maxScore) + maxScore = foundScore; + } + return maxScore; + } + protected abstract void solve(); } diff -r ce8e85452c3b -r 2bd032a701a7 icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Mon Jul 16 13:10:14 2012 +0400 @@ -0,0 +1,123 @@ +package ru.bosony.solvers; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import ru.bosony.graph.CellsLink; +import ru.bosony.graph.CellsLinkTransformer; +import ru.bosony.model.cellscontents.CellContent; +import ru.bosony.model.game.Game; +import ru.bosony.model.game.GameState; +import ru.bosony.model.mine.Cell; +import ru.bosony.model.mine.Mine; +import ru.bosony.model.moving.Movement; +import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath; +import edu.uci.ics.jung.graph.DirectedSparseGraph; +import edu.uci.ics.jung.graph.Graph; + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 16.07.2012 11:05:26 + * + */ +public class DijkstraSolver extends AbstractSolver { + + public DijkstraSolver(Mine mine, SolverListener listener) { + super(mine, listener); + } + + @Override + protected void solve() { + while (true) { + Game game = new Game(mine); + + while (game.getState() == GameState.Game) { + try { + Set targets = mine.findCells(CellContent.Lambda); + targets.addAll(mine.findCells(CellContent.HighOrderRock)); + if (targets.size() == 0) + targets = mine.findCells(CellContent.OpenLambdaLift); + if (targets.size() == 0) + break; + Graph graph = getDirectedGraph(); + DijkstraShortestPath alg = new DijkstraShortestPath(graph, + new CellsLinkTransformer()); + Cell nextTarget = null; + List routeToNextTarget = null; + double shortestDist = Double.MAX_VALUE; + for (Cell target : targets) { + List route = alg.getPath(mine.getRobotCell(), target); + Number dist = alg.getDistance(mine.getRobotCell(), target); + if (dist != null && dist.doubleValue() < shortestDist) { + shortestDist = dist.doubleValue(); + nextTarget = target; + routeToNextTarget = route; + } + } + if (nextTarget != null) { + for (CellsLink link : routeToNextTarget) { + Movement mov = link.getSource().getCoordinate() + .getNecessaryMovement(link.getTarget().getCoordinate()); + game.move(mov); + break; + } + } + } catch (IllegalArgumentException e) { + // hmmm... + } + } + if (game.getState() == GameState.Win) { + addNewRoute(game.getStringRoute(), game.getScore()); + } + attemptsCount++; + } + } + + protected Graph getDirectedGraph() { + Graph graph = new DirectedSparseGraph(); + for (CellsLink link : getCellLinks()) { + graph.addEdge(link, link.getSource(), link.getTarget()); + } + return graph; + } + + protected List getCellLinks() { + List links = new ArrayList(); + Cell robotCell = mine.getRobotCell(); + findAllCellLinks(robotCell, links, new HashSet()); + return links; + } + + protected void findAllCellLinks(Cell cell, List links, Set processedCells) { + if (processedCells.contains(cell)) + return; + else + processedCells.add(cell); + for (Cell neighboringCell : mine.getAdjacentCells(cell)) { + if (neighboringCell == null) + continue; + CellContent neighboringContent = neighboringCell.getContent(); + if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty + || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda + || neighboringContent == CellContent.OpenLambdaLift) { + CellsLink link = new CellsLink(mine, cell, neighboringCell); + links.add(link); + findAllCellLinks(neighboringCell, links, processedCells); + } else if (!CellContent.getTargets().contains(cell.getContent()) + && CellContent.getTargets().contains(neighboringContent)) { + findAllCellLinks(neighboringCell, links, processedCells); + } + } + if (CellContent.getTrampolines().contains(cell.getContent())) { + for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) { + if (target == null) + continue; + CellsLink link = new CellsLink(mine, cell, target); + links.add(link); + findAllCellLinks(target, links, processedCells); + } + } + } +} diff -r ce8e85452c3b -r 2bd032a701a7 icfp-solver/src/main/java/ru/bosony/solvers/Solver.java --- a/icfp-solver/src/main/java/ru/bosony/solvers/Solver.java Mon Jul 16 13:09:16 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/Solver.java Mon Jul 16 13:10:14 2012 +0400 @@ -25,8 +25,8 @@ System.out.println(route); } }); - AbstractSolver solver = new SimpleSolver(mine, new SolverListener() { - + AbstractSolver solver = new DijkstraSolver(mine, new SolverListener() { + @Override public void foundNextRoute(String newRoute) { route = newRoute;