icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java
branchCleverSearch
changeset 50 9530f55e9639
parent 46 4197f736166a
child 51 b431cec961d7
     1.1 --- a/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java	Mon Jul 16 18:59:13 2012 +0400
     1.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java	Tue Jul 17 21:38:39 2012 +0400
     1.3 @@ -1,8 +1,11 @@
     1.4  package ru.bosony.solvers;
     1.5  
     1.6 -import java.util.HashMap;
     1.7 -import java.util.Map;
     1.8 +import java.text.DecimalFormat;
     1.9 +import java.util.Date;
    1.10 +import java.util.HashSet;
    1.11 +import java.util.Set;
    1.12  
    1.13 +import ru.bosony.model.game.Game;
    1.14  import ru.bosony.model.mine.Mine;
    1.15  
    1.16  /**
    1.17 @@ -12,30 +15,38 @@
    1.18   */
    1.19  public abstract class AbstractSolver {
    1.20  
    1.21 -	protected Mine					mine			= null;
    1.22 -	protected SolverListener		listener		= null;
    1.23 -	protected Map<String, Integer>	routesWithScore	= new HashMap<String, Integer>();
    1.24 -	protected long					attemptsCount	= 0;
    1.25 +	protected Mine				mine			= null;
    1.26 +	protected SolverListener	listener		= null;
    1.27 +	protected Set<Game>			games			= new HashSet<Game>();
    1.28 +	protected long				attemptsCount	= 0;
    1.29 +	protected long				startTime		= System.currentTimeMillis();
    1.30  
    1.31  	public AbstractSolver(Mine mine, SolverListener listener) {
    1.32  		this.mine = mine;
    1.33  		this.listener = listener;
    1.34  	}
    1.35  
    1.36 -	protected void addNewRoute(String route, int score) {
    1.37 -		if (!routesWithScore.containsKey(route) && score > getMaxFoundScore()) {
    1.38 -			routesWithScore.put(route, score);
    1.39 -			listener.foundNextRoute(route);
    1.40 +	protected void addNewRoute(Game game) {
    1.41 +		Game maxScoreGame = getMaxFoundScoreGame();
    1.42 +		if (maxScoreGame == null || game.getScore() > maxScoreGame.getScore()
    1.43 +				|| game.getScore() == maxScoreGame.getScore()
    1.44 +				&& game.getRoute().size() < maxScoreGame.getRoute().size()) {
    1.45 +			listener.foundNextRoute(game.getStringRoute());
    1.46 +			// TODO delete
    1.47 +			System.out.println(new DecimalFormat("0.00").format((((System.currentTimeMillis() - startTime) / 1000d)))
    1.48 +					+ " seconds, State = " + game.getState() + ", Score = " + game.getScore() + ", Route = "
    1.49 +					+ game.getStringRoute());
    1.50  		}
    1.51 +		games.add(game);
    1.52  	}
    1.53  
    1.54 -	protected int getMaxFoundScore() {
    1.55 -		int maxScore = 0;
    1.56 -		for (int foundScore : routesWithScore.values()) {
    1.57 -			if (foundScore > maxScore)
    1.58 -				maxScore = foundScore;
    1.59 +	protected Game getMaxFoundScoreGame() {
    1.60 +		Game game = null;
    1.61 +		for (Game foundGame : games) {
    1.62 +			if (game == null || foundGame.getScore() > game.getScore())
    1.63 +				game = foundGame;
    1.64  		}
    1.65 -		return maxScore;
    1.66 +		return game;
    1.67  	}
    1.68  
    1.69  	public abstract void solve();