# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342546719 -14400 # Node ID 9530f55e9639ddb1106382f844063f654d790a8c # Parent 70cfa8a066df90b2b69475c372d0ff3b6f8042c7 Moar diff -r 70cfa8a066df -r 9530f55e9639 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 18:59:13 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java Tue Jul 17 21:38:39 2012 +0400 @@ -72,11 +72,7 @@ // Moving and scoring route.add(mov); - try { - history.add(mine.clone()); - } catch (CloneNotSupportedException e) { - throw new RuntimeException(e); - } + history.add(mine.clone()); Cell robotCell = mine.getRobotCell(); Coordinate robotCoord = robotCell.getCoordinate(); Coordinate nextRobotCoord = null; @@ -371,4 +367,10 @@ } 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 70cfa8a066df -r 9530f55e9639 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 18:59:13 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java Tue Jul 17 21:38:39 2012 +0400 @@ -36,4 +36,9 @@ return clone; } + @Override + public String toString() { + return "Cell [coordinate=" + coordinate + ", content=" + content + "]"; + } + } diff -r 70cfa8a066df -r 9530f55e9639 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 18:59:13 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Tue Jul 17 21:38:39 2012 +0400 @@ -48,13 +48,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(); } @@ -310,8 +314,8 @@ } @Override - public Mine clone() throws CloneNotSupportedException { - return new Mine(toText()); + public Mine clone() { + return new Mine(toText(), null); } } diff -r 70cfa8a066df -r 9530f55e9639 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 18:59:13 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java Tue Jul 17 21:38:39 2012 +0400 @@ -96,4 +96,9 @@ return super.equals(obj); } + @Override + public String toString() { + return "Coordinate [x=" + x + ", y=" + y + "]"; + } + } diff -r 70cfa8a066df -r 9530f55e9639 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 18:59:13 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java Tue Jul 17 21:38:39 2012 +0400 @@ -9,12 +9,12 @@ */ public enum Movement implements TextRepresentable { + WAIT("W"), + RAZOR("S"), LEFT("L"), UP("U"), RIGHT("R"), DOWN("D"), - WAIT("W"), - RAZOR("S"), ABORT("A"); private String text; diff -r 70cfa8a066df -r 9530f55e9639 icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java --- a/icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java Mon Jul 16 18:59:13 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java Tue Jul 17 21:38:39 2012 +0400 @@ -56,4 +56,9 @@ return super.equals(obj); } + @Override + public String toString() { + return "CellsLink [source=" + source + ", target=" + target + ", weight=" + weight + "]"; + } + } diff -r 70cfa8a066df -r 9530f55e9639 icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java --- a/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java Mon Jul 16 18:59:13 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java Tue Jul 17 21:38:39 2012 +0400 @@ -1,8 +1,11 @@ package ru.bosony.solvers; -import java.util.HashMap; -import java.util.Map; +import java.text.DecimalFormat; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; +import ru.bosony.model.game.Game; import ru.bosony.model.mine.Mine; /** @@ -12,30 +15,38 @@ */ public abstract class AbstractSolver { - protected Mine mine = null; - protected SolverListener listener = null; - protected Map routesWithScore = new HashMap(); - protected long attemptsCount = 0; + protected Mine mine = 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.listener = listener; } - 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.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; } public abstract void solve(); diff -r 70cfa8a066df -r 9530f55e9639 icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java --- a/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Mon Jul 16 18:59:13 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Tue Jul 17 21:38:39 2012 +0400 @@ -37,11 +37,7 @@ public void solve() { while (true) { Mine testMine = null; - try { - testMine = mine.clone(); - } catch (CloneNotSupportedException e1) { - throw new RuntimeException(e1); - } + testMine = mine.clone(); Game game = new Game(testMine); while (game.getState() == GameState.Game) { @@ -90,7 +86,7 @@ if (isSafeMovement(testMine, source, target)) { Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate()); GameState state = game.move(mov); - if (state == GameState.Lose || state == GameState.Abort || game.hasViciousCircle()) { + if (state == GameState.Lose || state == GameState.Abort) { loseMovs.get(map).add(mov); loseLinks.add(link); } @@ -101,17 +97,17 @@ for (Movement mov : Movement.values()) { if (!loseMovs.get(map).contains(mov)) { GameState state = game.move(mov); - if (state == GameState.Lose || state == GameState.Abort || map.equals(testMine.toText()) - || game.hasViciousCircle()) - loseMovs.get(map).add(mov); + if (state == GameState.Game && map.equals(testMine.toText())) { + if (mov != Movement.WAIT) + loseMovs.get(map).add(mov); + game.move(Movement.ABORT); + } break; } } } } - if (game.getState() == GameState.Win) { - addNewRoute(game.getStringRoute(), game.getScore()); - } + addNewRoute(game); attemptsCount++; } }