Moar CleverSearch
authorindvdum (gotoindvdum[at]gmail[dot]com)
Tue, 17 Jul 2012 21:38:39 +0400
branchCleverSearch
changeset 509530f55e9639
parent 49 70cfa8a066df
child 51 b431cec961d7
Moar
icfp-model/src/main/java/ru/bosony/model/game/Game.java
icfp-model/src/main/java/ru/bosony/model/mine/Cell.java
icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java
icfp-model/src/main/java/ru/bosony/model/moving/Movement.java
icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java
icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java
icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java
     1.1 --- a/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Mon Jul 16 18:59:13 2012 +0400
     1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Tue Jul 17 21:38:39 2012 +0400
     1.3 @@ -72,11 +72,7 @@
     1.4  
     1.5  		// Moving and scoring
     1.6  		route.add(mov);
     1.7 -		try {
     1.8 -			history.add(mine.clone());
     1.9 -		} catch (CloneNotSupportedException e) {
    1.10 -			throw new RuntimeException(e);
    1.11 -		}
    1.12 +		history.add(mine.clone());
    1.13  		Cell robotCell = mine.getRobotCell();
    1.14  		Coordinate robotCoord = robotCell.getCoordinate();
    1.15  		Coordinate nextRobotCoord = null;
    1.16 @@ -371,4 +367,10 @@
    1.17  		}
    1.18  		return false;
    1.19  	}
    1.20 +
    1.21 +	@Override
    1.22 +	public String toString() {
    1.23 +		return state.toString() + ", score = " + score + ", " + route.size() + " steps: "
    1.24 +				+ (route.size() > 100 ? getStringRoute().substring(0, 100) + "..." : getStringRoute()) + "\n\n" + mine;
    1.25 +	}
    1.26  }
     2.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java	Mon Jul 16 18:59:13 2012 +0400
     2.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java	Tue Jul 17 21:38:39 2012 +0400
     2.3 @@ -36,4 +36,9 @@
     2.4  		return clone;
     2.5  	}
     2.6  
     2.7 +	@Override
     2.8 +	public String toString() {
     2.9 +		return "Cell [coordinate=" + coordinate + ", content=" + content + "]";
    2.10 +	}
    2.11 +
    2.12  }
     3.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Mon Jul 16 18:59:13 2012 +0400
     3.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Tue Jul 17 21:38:39 2012 +0400
     3.3 @@ -48,13 +48,17 @@
     3.4  	protected static String		firstTrampolineRegexp				= "(?s).*?Trampoline ([A-I]) targets ([1-9])";
     3.5  
     3.6  	public Mine(String text) {
     3.7 -		fromText(text);
     3.8 +		this(text, null);
     3.9  		if (findCells(OpenLambdaLift).size() != 0)
    3.10  			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    3.11  		if (findCells(MiningRobot).size() != 1)
    3.12  			throw new RuntimeException("New map does not contain a robot!");
    3.13  		if (findCells(ClosedLambdaLift).size() != 1)
    3.14  			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
    3.15 +	}
    3.16 +
    3.17 +	protected Mine(String text, Object obj) {
    3.18 +		fromText(text);
    3.19  		lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size();
    3.20  	}
    3.21  
    3.22 @@ -310,8 +314,8 @@
    3.23  	}
    3.24  
    3.25  	@Override
    3.26 -	public Mine clone() throws CloneNotSupportedException {
    3.27 -		return new Mine(toText());
    3.28 +	public Mine clone() {
    3.29 +		return new Mine(toText(), null);
    3.30  	}
    3.31  
    3.32  }
     4.1 --- a/icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java	Mon Jul 16 18:59:13 2012 +0400
     4.2 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java	Tue Jul 17 21:38:39 2012 +0400
     4.3 @@ -96,4 +96,9 @@
     4.4  		return super.equals(obj);
     4.5  	}
     4.6  
     4.7 +	@Override
     4.8 +	public String toString() {
     4.9 +		return "Coordinate [x=" + x + ", y=" + y + "]";
    4.10 +	}
    4.11 +
    4.12  }
     5.1 --- a/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java	Mon Jul 16 18:59:13 2012 +0400
     5.2 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java	Tue Jul 17 21:38:39 2012 +0400
     5.3 @@ -9,12 +9,12 @@
     5.4   */
     5.5  public enum Movement implements TextRepresentable {
     5.6  	
     5.7 +	WAIT("W"),
     5.8 +	RAZOR("S"),
     5.9  	LEFT("L"),
    5.10  	UP("U"),
    5.11  	RIGHT("R"),
    5.12  	DOWN("D"),
    5.13 -	WAIT("W"),
    5.14 -	RAZOR("S"),
    5.15  	ABORT("A");
    5.16  
    5.17  	private String	text;
     6.1 --- a/icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java	Mon Jul 16 18:59:13 2012 +0400
     6.2 +++ b/icfp-solver/src/main/java/ru/bosony/graph/CellsLink.java	Tue Jul 17 21:38:39 2012 +0400
     6.3 @@ -56,4 +56,9 @@
     6.4  		return super.equals(obj);
     6.5  	}
     6.6  
     6.7 +	@Override
     6.8 +	public String toString() {
     6.9 +		return "CellsLink [source=" + source + ", target=" + target + ", weight=" + weight + "]";
    6.10 +	}
    6.11 +
    6.12  }
     7.1 --- a/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java	Mon Jul 16 18:59:13 2012 +0400
     7.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java	Tue Jul 17 21:38:39 2012 +0400
     7.3 @@ -1,8 +1,11 @@
     7.4  package ru.bosony.solvers;
     7.5  
     7.6 -import java.util.HashMap;
     7.7 -import java.util.Map;
     7.8 +import java.text.DecimalFormat;
     7.9 +import java.util.Date;
    7.10 +import java.util.HashSet;
    7.11 +import java.util.Set;
    7.12  
    7.13 +import ru.bosony.model.game.Game;
    7.14  import ru.bosony.model.mine.Mine;
    7.15  
    7.16  /**
    7.17 @@ -12,30 +15,38 @@
    7.18   */
    7.19  public abstract class AbstractSolver {
    7.20  
    7.21 -	protected Mine					mine			= null;
    7.22 -	protected SolverListener		listener		= null;
    7.23 -	protected Map<String, Integer>	routesWithScore	= new HashMap<String, Integer>();
    7.24 -	protected long					attemptsCount	= 0;
    7.25 +	protected Mine				mine			= null;
    7.26 +	protected SolverListener	listener		= null;
    7.27 +	protected Set<Game>			games			= new HashSet<Game>();
    7.28 +	protected long				attemptsCount	= 0;
    7.29 +	protected long				startTime		= System.currentTimeMillis();
    7.30  
    7.31  	public AbstractSolver(Mine mine, SolverListener listener) {
    7.32  		this.mine = mine;
    7.33  		this.listener = listener;
    7.34  	}
    7.35  
    7.36 -	protected void addNewRoute(String route, int score) {
    7.37 -		if (!routesWithScore.containsKey(route) && score > getMaxFoundScore()) {
    7.38 -			routesWithScore.put(route, score);
    7.39 -			listener.foundNextRoute(route);
    7.40 +	protected void addNewRoute(Game game) {
    7.41 +		Game maxScoreGame = getMaxFoundScoreGame();
    7.42 +		if (maxScoreGame == null || game.getScore() > maxScoreGame.getScore()
    7.43 +				|| game.getScore() == maxScoreGame.getScore()
    7.44 +				&& game.getRoute().size() < maxScoreGame.getRoute().size()) {
    7.45 +			listener.foundNextRoute(game.getStringRoute());
    7.46 +			// TODO delete
    7.47 +			System.out.println(new DecimalFormat("0.00").format((((System.currentTimeMillis() - startTime) / 1000d)))
    7.48 +					+ " seconds, State = " + game.getState() + ", Score = " + game.getScore() + ", Route = "
    7.49 +					+ game.getStringRoute());
    7.50  		}
    7.51 +		games.add(game);
    7.52  	}
    7.53  
    7.54 -	protected int getMaxFoundScore() {
    7.55 -		int maxScore = 0;
    7.56 -		for (int foundScore : routesWithScore.values()) {
    7.57 -			if (foundScore > maxScore)
    7.58 -				maxScore = foundScore;
    7.59 +	protected Game getMaxFoundScoreGame() {
    7.60 +		Game game = null;
    7.61 +		for (Game foundGame : games) {
    7.62 +			if (game == null || foundGame.getScore() > game.getScore())
    7.63 +				game = foundGame;
    7.64  		}
    7.65 -		return maxScore;
    7.66 +		return game;
    7.67  	}
    7.68  
    7.69  	public abstract void solve();
     8.1 --- a/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java	Mon Jul 16 18:59:13 2012 +0400
     8.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java	Tue Jul 17 21:38:39 2012 +0400
     8.3 @@ -37,11 +37,7 @@
     8.4  	public void solve() {
     8.5  		while (true) {
     8.6  			Mine testMine = null;
     8.7 -			try {
     8.8 -				testMine = mine.clone();
     8.9 -			} catch (CloneNotSupportedException e1) {
    8.10 -				throw new RuntimeException(e1);
    8.11 -			}
    8.12 +			testMine = mine.clone();
    8.13  			Game game = new Game(testMine);
    8.14  
    8.15  			while (game.getState() == GameState.Game) {
    8.16 @@ -90,7 +86,7 @@
    8.17  						if (isSafeMovement(testMine, source, target)) {
    8.18  							Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate());
    8.19  							GameState state = game.move(mov);
    8.20 -							if (state == GameState.Lose || state == GameState.Abort || game.hasViciousCircle()) {
    8.21 +							if (state == GameState.Lose || state == GameState.Abort) {
    8.22  								loseMovs.get(map).add(mov);
    8.23  								loseLinks.add(link);
    8.24  							}
    8.25 @@ -101,17 +97,17 @@
    8.26  					for (Movement mov : Movement.values()) {
    8.27  						if (!loseMovs.get(map).contains(mov)) {
    8.28  							GameState state = game.move(mov);
    8.29 -							if (state == GameState.Lose || state == GameState.Abort || map.equals(testMine.toText())
    8.30 -									|| game.hasViciousCircle())
    8.31 -								loseMovs.get(map).add(mov);
    8.32 +							if (state == GameState.Game && map.equals(testMine.toText())) {
    8.33 +								if (mov != Movement.WAIT)
    8.34 +									loseMovs.get(map).add(mov);
    8.35 +								game.move(Movement.ABORT);
    8.36 +							}
    8.37  							break;
    8.38  						}
    8.39  					}
    8.40  				}
    8.41  			}
    8.42 -			if (game.getState() == GameState.Win) {
    8.43 -				addNewRoute(game.getStringRoute(), game.getScore());
    8.44 -			}
    8.45 +			addNewRoute(game);
    8.46  			attemptsCount++;
    8.47  		}
    8.48  	}