# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342590172 -14400 # Node ID 80f5fc954cffb0b8c1b0f82d051d74be7836d3d3 # Parent d357a16ee62d642becedf7d55d3196322a7e65a6# Parent db49d3e519213c0515ad9c8a2f4756339d9f59b0 Merge diff -r d357a16ee62d -r 80f5fc954cff icfp-model/src/main/java/ru/bosony/model/game/Game.java --- a/icfp-model/src/main/java/ru/bosony/model/game/Game.java Mon Jul 16 13:19:10 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java Wed Jul 18 09:42:52 2012 +0400 @@ -12,7 +12,9 @@ import static ru.bosony.model.cellscontents.CellContent.WadlersBeard; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import ru.bosony.model.cellscontents.CellContent; import ru.bosony.model.mine.Cell; @@ -32,6 +34,7 @@ protected int lastScoreChange = 0; protected GameState state = GameState.Game; protected int lambdaCollectedCount = 0; + protected List history = new ArrayList(); protected List route = new ArrayList(); protected int underwater = 0; @@ -69,6 +72,7 @@ // Moving and scoring route.add(mov); + history.add(mine.clone()); Cell robotCell = mine.getRobotCell(); Coordinate robotCoord = robotCell.getCoordinate(); Coordinate nextRobotCoord = null; @@ -349,4 +353,24 @@ public int getUnderwater() { return underwater; } + + public boolean hasViciousCircle() { + if (route.size() != history.size()) + throw new RuntimeException("Desynchronized history"); + Map state = new HashMap(); + for (int i = route.size() - 1; i >= 0; i--) { + Mine mine = history.get(i); + Movement mov = route.get(i); + if (state.get(mine) == mov) + return true; + state.put(mine, mov); + } + return false; + } + + @Override + public String toString() { + return state.toString() + ", score = " + score + ", " + route.size() + " steps: " + + (route.size() > 100 ? getStringRoute().substring(0, 100) + "..." : getStringRoute()) + "\n\n" + mine; + } } diff -r d357a16ee62d -r 80f5fc954cff icfp-model/src/main/java/ru/bosony/model/mine/Cell.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java Mon Jul 16 13:19:10 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java Wed Jul 18 09:42:52 2012 +0400 @@ -36,4 +36,9 @@ return clone; } + @Override + public String toString() { + return "Cell [coordinate=" + coordinate + ", content=" + content + "]"; + } + } diff -r d357a16ee62d -r 80f5fc954cff icfp-model/src/main/java/ru/bosony/model/mine/Mine.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Mon Jul 16 13:19:10 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Wed Jul 18 09:42:52 2012 +0400 @@ -8,6 +8,7 @@ import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift; import static ru.bosony.model.cellscontents.CellContent.Rock; +import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; @@ -23,7 +24,7 @@ * 14.07.2012 2:02:18 * */ -public class Mine implements TextRepresentable { +public class Mine implements TextRepresentable, Cloneable { protected Cell[][] cells; protected int sizeX; @@ -48,13 +49,17 @@ protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])"; public Mine(String text) { - fromText(text); + this(text, null); if (findCells(OpenLambdaLift).size() != 0) throw new RuntimeException("Detected OpenLambdaLift in new map!"); if (findCells(MiningRobot).size() != 1) throw new RuntimeException("New map does not contain a robot!"); if (findCells(ClosedLambdaLift).size() != 1) throw new RuntimeException("New map does not contain a ClosedLambdaLift!"); + } + + protected Mine(String text, Object obj) { + fromText(text); lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size(); } @@ -74,6 +79,10 @@ return text; } + public String toTextWithoutRobot() { + return toText().replaceAll(CellContent.MiningRobot.toText(), CellContent.Empty.toText()); + } + protected void fromText(String str) { Matcher matcher = minePattern.matcher(str); if (!matcher.matches()) @@ -174,6 +183,18 @@ return cells[x - 1][sizeY - y]; } + public Set getCells(Collection coordinates) { + Set cells = new HashSet(); + if (coordinates == null) + return cells; + for (Coordinate coordinate : coordinates) { + Cell cell = getCell(coordinate); + if (cell != null) + cells.add(cell); + } + return cells; + } + @Override public String toString() { return toText(); @@ -215,15 +236,21 @@ public Set getNeighboringCells(Cell cell) { Set cells = new HashSet(); - for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) - cells.add(getCell(coordinate)); + for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) { + Cell nextCell = getCell(coordinate); + if (nextCell != null) + cells.add(nextCell); + } return cells; } public Set getAdjacentCells(Cell cell) { Set cells = new HashSet(); - for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) - cells.add(getCell(coordinate)); + for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) { + Cell nextCell = getCell(coordinate); + if (nextCell != null) + cells.add(nextCell); + } return cells; } @@ -309,4 +336,9 @@ return toText().hashCode(); } + @Override + public Mine clone() { + return new Mine(toText(), null); + } + } diff -r d357a16ee62d -r 80f5fc954cff icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java --- a/icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java Mon Jul 16 13:19:10 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java Wed Jul 18 09:42:52 2012 +0400 @@ -96,4 +96,9 @@ return super.equals(obj); } + @Override + public String toString() { + return "Coordinate [x=" + x + ", y=" + y + "]"; + } + } diff -r d357a16ee62d -r 80f5fc954cff icfp-model/src/main/java/ru/bosony/model/moving/Movement.java --- a/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java Mon Jul 16 13:19:10 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java Wed Jul 18 09:42:52 2012 +0400 @@ -9,13 +9,13 @@ */ public enum Movement implements TextRepresentable { + WAIT("W"), + RAZOR("S"), LEFT("L"), + UP("U"), RIGHT("R"), - UP("U"), DOWN("D"), - WAIT("W"), - ABORT("A"), - RAZOR("S"); + ABORT("A"); private String text; diff -r d357a16ee62d -r 80f5fc954cff icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java --- a/icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java Mon Jul 16 13:19:10 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java Wed Jul 18 09:42:52 2012 +0400 @@ -56,4 +56,9 @@ return super.equals(obj); } + @Override + public String toString() { + return "CellsLink [source=" + source + ", target=" + target + ", weight=" + weight + "]"; + } + } diff -r d357a16ee62d -r 80f5fc954cff 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:19:10 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java Wed Jul 18 09:42:52 2012 +0400 @@ -1,8 +1,10 @@ package ru.bosony.solvers; -import java.util.HashMap; -import java.util.Map; +import java.text.DecimalFormat; +import java.util.HashSet; +import java.util.Set; +import ru.bosony.model.game.Game; import ru.bosony.model.mine.Mine; /** @@ -12,32 +14,39 @@ */ public abstract class AbstractSolver { - protected Mine mine = null; - protected SolverListener listener = null; - protected Map routesWithScore = new HashMap(); - protected long attemptsCount = 0; + protected Mine initialMine = null; + protected SolverListener listener = null; + protected Set games = new HashSet(); + protected long attemptsCount = 0; + protected long startTime = System.currentTimeMillis(); public AbstractSolver(Mine mine, SolverListener listener) { - this.mine = mine; + this.initialMine = mine; this.listener = listener; - solve(); } - protected void addNewRoute(String route, int score) { - if (!routesWithScore.containsKey(route) && score > getMaxFoundScore()) { - routesWithScore.put(route, score); - listener.foundNextRoute(route); + protected void addNewRoute(Game game) { + Game maxScoreGame = getMaxFoundScoreGame(); + if (maxScoreGame == null || game.getScore() > maxScoreGame.getScore() + || game.getScore() == maxScoreGame.getScore() + && game.getRoute().size() < maxScoreGame.getRoute().size()) { + listener.foundNextRoute(game.getStringRoute()); + // TODO delete + System.out.println(new DecimalFormat("0.00").format((((System.currentTimeMillis() - startTime) / 1000d))) + + " seconds, State = " + game.getState() + ", Score = " + game.getScore() + ", Route(" + + game.getRoute().size() + ") = " + game.getStringRoute()); } + games.add(game); } - protected int getMaxFoundScore() { - int maxScore = 0; - for (int foundScore : routesWithScore.values()) { - if (foundScore > maxScore) - maxScore = foundScore; + protected Game getMaxFoundScoreGame() { + Game game = null; + for (Game foundGame : games) { + if (game == null || foundGame.getScore() > game.getScore()) + game = foundGame; } - return maxScore; + return game; } - protected abstract void solve(); + public abstract void solve(); } diff -r d357a16ee62d -r 80f5fc954cff icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java --- a/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Mon Jul 16 13:19:10 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Wed Jul 18 09:42:52 2012 +0400 @@ -1,8 +1,10 @@ package ru.bosony.solvers; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import ru.bosony.graph.CellsLink; @@ -12,6 +14,7 @@ import ru.bosony.model.game.GameState; import ru.bosony.model.mine.Cell; import ru.bosony.model.mine.Mine; +import ru.bosony.model.moving.Coordinate; import ru.bosony.model.moving.Movement; import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath; import edu.uci.ics.jung.graph.DirectedSparseGraph; @@ -24,77 +27,197 @@ */ public class DijkstraSolver extends AbstractSolver { + protected Map> loseMovs = new HashMap>(); + protected Map> loseLinks = new HashMap>(); + protected Map> attendedWalkingAroundCoordinates = new HashMap>(); + protected Map> noWay = new HashMap>(); + public DijkstraSolver(Mine mine, SolverListener listener) { super(mine, listener); } @Override - protected void solve() { + public void solve() { while (true) { + Mine mine = null; + mine = initialMine.clone(); Game game = new Game(mine); while (game.getState() == GameState.Game) { - try { - Set targets = mine.findCells(CellContent.Lambda); + Cell robotCell = mine.getRobotCell(); + String map = mine.toText(); + String mapWithoutRobot = mine.toTextWithoutRobot(); + if (!loseMovs.containsKey(map)) { + loseMovs.put(map, new HashSet()); + } + if (!loseLinks.containsKey(map)) { + loseLinks.put(map, new HashSet()); + } + if (!noWay.containsKey(map)) { + noWay.put(map, new HashSet()); + } + if (!attendedWalkingAroundCoordinates.containsKey(mapWithoutRobot)) { + attendedWalkingAroundCoordinates.put(mapWithoutRobot, new HashSet()); + } + attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(robotCell.getCoordinate()); + Set targets = new HashSet(); + boolean isFreeWay = false; + for (Cell cell : mine.getAdjacentCells(robotCell)) { + Movement mov = robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate()); + isFreeWay |= !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(cell.getCoordinate()) + && !loseMovs.get(map).contains(mov); + } + if (isFreeWay) { + targets.addAll(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) { + targets.addAll(mine.findCells(CellContent.OpenLambdaLift)); + targets.removeAll(mine.getCells(noWay.get(map))); + } else { + targets.addAll(mine.findCells(CellContent.Empty)); + targets.addAll(mine.findCells(CellContent.Earth)); + targets.addAll(mine.findCells(CellContent.HuttonsRazor)); + for (CellContent content : CellContent.getTrampolines()) { + targets.addAll(mine.findCells(content)); + } + targets.removeAll(mine.getCells(attendedWalkingAroundCoordinates.get(mapWithoutRobot))); + targets.removeAll(mine.getCells(noWay.get(map))); + if (targets.size() == 0) { + game.move(Movement.ABORT); + continue; + } + } + Graph graph = getDirectedGraph(mine); + DijkstraShortestPath alg = new DijkstraShortestPath(graph, + new CellsLinkTransformer()); + Cell nextTarget = null; + List routeToNextTarget = null; + double shortestDist = Double.MAX_VALUE; + for (Cell target : targets) { + List route = null; + Number dist = null; + try { + route = alg.getPath(mine.getRobotCell(), target); + dist = alg.getDistance(mine.getRobotCell(), target); + } catch (IllegalArgumentException e) { + noWay.get(map).add(target.getCoordinate()); + continue; + } + if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) { + CellsLink firstLink = route.get(0); + Movement mov = firstLink.getSource().getCoordinate() + .getNecessaryMovement(firstLink.getTarget().getCoordinate()); + if (!loseMovs.get(map).contains(mov) + && !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains( + target.getCoordinate())) { 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; + } + if (nextTarget != null) { + for (CellsLink link : routeToNextTarget) { + Cell source = link.getSource(); + Cell target = link.getTarget(); + if (isSafeMovement(mine, source, target)) { + Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate()); + GameState state = game.move(mov); + if (state == GameState.Lose || state == GameState.Abort) { + loseMovs.get(map).add(mov); + loseLinks.get(map).add(link); + } + } + break; + } + } else { + // Walking around + for (Movement mov : Movement.values()) { + if (!loseMovs.get(map).contains(mov)) { + if (mov == Movement.RAZOR && mine.getRazorsCount() > 0) { + boolean isRazorApplied = false; + for (Cell cell : mine.getNeighboringCells(robotCell)) { + isRazorApplied |= cell.getContent() == CellContent.WadlersBeard; + } + if (!isRazorApplied) + continue; + } + Cell movCell = null; + for (Cell cell : mine.getAdjacentCells(robotCell)) { + if (robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate()) == mov) { + if (!attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains( + cell.getCoordinate())) { + movCell = cell; + break; + } + } + + } + if (mov == Movement.WAIT + || mov == Movement.RAZOR + || movCell != null + && !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains( + movCell.getCoordinate())) { + GameState state = game.move(mov); + if (movCell != null && mapWithoutRobot.equals(mine.toTextWithoutRobot())) + attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(movCell.getCoordinate()); + if (state == GameState.Game && map.equals(mine.toText())) { + loseMovs.get(map).add(mov); + } + break; + } } } - } catch (IllegalArgumentException e) { - // hmmm... } } - if (game.getState() == GameState.Win) { - addNewRoute(game.getStringRoute(), game.getScore()); - } + addNewRoute(game); attemptsCount++; } } - protected Graph getDirectedGraph() { + protected boolean isSafeMovement(Mine mine, Cell source, Cell target) { + if (source == null || target == null) + return false; + if (source.getContent().getTrampolineTarget() == target.getContent()) + return true; + Cell upCell = mine.getCell(target.getCoordinate().up()); + if (upCell == null) + return true; + Cell upUpCell = mine.getCell(target.getCoordinate().up().up()); + Cell upUpLeftCell = mine.getCell(target.getCoordinate().up().up().left()); + Cell upUpRightCell = mine.getCell(target.getCoordinate().up().up().right()); + if (upCell.getContent() == CellContent.Empty + && (upUpCell != null + && (upUpCell.getContent() == CellContent.Rock || upUpCell.getContent() == CellContent.HighOrderRock) + || upUpLeftCell != null + && (upUpLeftCell.getContent() == CellContent.Rock || upUpLeftCell.getContent() == CellContent.HighOrderRock) || upUpRightCell != null + && (upUpRightCell.getContent() == CellContent.Rock || upUpRightCell.getContent() == CellContent.HighOrderRock))) { + return false; + } + return true; + } + + protected Graph getDirectedGraph(Mine mine) { Graph graph = new DirectedSparseGraph(); - for (CellsLink link : getCellLinks()) { + for (CellsLink link : getCellLinks(mine)) { graph.addEdge(link, link.getSource(), link.getTarget()); } return graph; } - protected List getCellLinks() { + protected List getCellLinks(Mine mine) { List links = new ArrayList(); Cell robotCell = mine.getRobotCell(); - findAllCellLinks(robotCell, links, new HashSet()); + findAllCellLinks(mine, robotCell, links, new HashSet()); return links; } - protected void findAllCellLinks(Cell cell, List links, Set processedCells) { + protected void findAllCellLinks(Mine mine, Cell cell, List links, Set processedCells) { if (processedCells.contains(cell)) return; else processedCells.add(cell); + String map = mine.toText(); for (Cell neighboringCell : mine.getAdjacentCells(cell)) { if (neighboringCell == null) continue; @@ -102,21 +225,29 @@ 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); + if (isSafeMovement(mine, cell, neighboringCell)) { + CellsLink link = new CellsLink(mine, cell, neighboringCell); + if (!loseLinks.get(map).contains(link)) { + links.add(link); + findAllCellLinks(mine, neighboringCell, links, processedCells); + } + } } else if (!CellContent.getTargets().contains(cell.getContent()) && CellContent.getTargets().contains(neighboringContent)) { - findAllCellLinks(neighboringCell, links, processedCells); + findAllCellLinks(mine, 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); + if (isSafeMovement(mine, cell, target)) { + CellsLink link = new CellsLink(mine, cell, target); + if (!loseLinks.get(map).contains(link)) { + links.add(link); + findAllCellLinks(mine, target, links, processedCells); + } + } } } } diff -r d357a16ee62d -r 80f5fc954cff icfp-solver/src/main/java/ru/bosony/solvers/SimpleSolver.java --- a/icfp-solver/src/main/java/ru/bosony/solvers/SimpleSolver.java Mon Jul 16 13:19:10 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/SimpleSolver.java Wed Jul 18 09:42:52 2012 +0400 @@ -14,7 +14,7 @@ } @Override - protected void solve() { + public void solve() { String route = ""; while (true) { try { diff -r d357a16ee62d -r 80f5fc954cff 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:19:10 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/Solver.java Wed Jul 18 09:42:52 2012 +0400 @@ -32,6 +32,7 @@ route = newRoute; } }); + solver.solve(); } public static void main(String[] args) throws IOException, InterruptedException { diff -r d357a16ee62d -r 80f5fc954cff icfp-submit/pom.xml --- a/icfp-submit/pom.xml Mon Jul 16 13:19:10 2012 +0400 +++ b/icfp-submit/pom.xml Wed Jul 18 09:42:52 2012 +0400 @@ -19,7 +19,7 @@ src/main/assembly/assembly.xml - icfp-xxxxxxxx + icfp-96518158