icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java
changeset 53 80f5fc954cff
parent 51 b431cec961d7
child 54 8a248d789ec7
     1.1 --- a/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java	Mon Jul 16 13:19:10 2012 +0400
     1.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java	Wed Jul 18 09:42:52 2012 +0400
     1.3 @@ -1,8 +1,10 @@
     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.HashSet;
    1.10 +import java.util.Set;
    1.11  
    1.12 +import ru.bosony.model.game.Game;
    1.13  import ru.bosony.model.mine.Mine;
    1.14  
    1.15  /**
    1.16 @@ -12,32 +14,39 @@
    1.17   */
    1.18  public abstract class AbstractSolver {
    1.19  
    1.20 -	protected Mine					mine			= null;
    1.21 -	protected SolverListener		listener		= null;
    1.22 -	protected Map<String, Integer>	routesWithScore	= new HashMap<String, Integer>();
    1.23 -	protected long					attemptsCount	= 0;
    1.24 +	protected Mine				initialMine		= null;
    1.25 +	protected SolverListener	listener		= null;
    1.26 +	protected Set<Game>			games			= new HashSet<Game>();
    1.27 +	protected long				attemptsCount	= 0;
    1.28 +	protected long				startTime		= System.currentTimeMillis();
    1.29  
    1.30  	public AbstractSolver(Mine mine, SolverListener listener) {
    1.31 -		this.mine = mine;
    1.32 +		this.initialMine = mine;
    1.33  		this.listener = listener;
    1.34 -		solve();
    1.35  	}
    1.36  
    1.37 -	protected void addNewRoute(String route, int score) {
    1.38 -		if (!routesWithScore.containsKey(route) && score > getMaxFoundScore()) {
    1.39 -			routesWithScore.put(route, score);
    1.40 -			listener.foundNextRoute(route);
    1.41 +	protected void addNewRoute(Game game) {
    1.42 +		Game maxScoreGame = getMaxFoundScoreGame();
    1.43 +		if (maxScoreGame == null || game.getScore() > maxScoreGame.getScore()
    1.44 +				|| game.getScore() == maxScoreGame.getScore()
    1.45 +				&& game.getRoute().size() < maxScoreGame.getRoute().size()) {
    1.46 +			listener.foundNextRoute(game.getStringRoute());
    1.47 +			// TODO delete
    1.48 +			System.out.println(new DecimalFormat("0.00").format((((System.currentTimeMillis() - startTime) / 1000d)))
    1.49 +					+ " seconds, State = " + game.getState() + ", Score = " + game.getScore() + ", Route("
    1.50 +					+ game.getRoute().size() + ") = " + game.getStringRoute());
    1.51  		}
    1.52 +		games.add(game);
    1.53  	}
    1.54  
    1.55 -	protected int getMaxFoundScore() {
    1.56 -		int maxScore = 0;
    1.57 -		for (int foundScore : routesWithScore.values()) {
    1.58 -			if (foundScore > maxScore)
    1.59 -				maxScore = foundScore;
    1.60 +	protected Game getMaxFoundScoreGame() {
    1.61 +		Game game = null;
    1.62 +		for (Game foundGame : games) {
    1.63 +			if (game == null || foundGame.getScore() > game.getScore())
    1.64 +				game = foundGame;
    1.65  		}
    1.66 -		return maxScore;
    1.67 +		return game;
    1.68  	}
    1.69  
    1.70 -	protected abstract void solve();
    1.71 +	public abstract void solve();
    1.72  }