icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java
changeset 53 80f5fc954cff
parent 51 b431cec961d7
child 54 8a248d789ec7
equal deleted inserted replaced
45:d357a16ee62d 53:80f5fc954cff
     1 package ru.bosony.solvers;
     1 package ru.bosony.solvers;
     2 
     2 
     3 import java.util.HashMap;
     3 import java.text.DecimalFormat;
     4 import java.util.Map;
     4 import java.util.HashSet;
       
     5 import java.util.Set;
     5 
     6 
       
     7 import ru.bosony.model.game.Game;
     6 import ru.bosony.model.mine.Mine;
     8 import ru.bosony.model.mine.Mine;
     7 
     9 
     8 /**
    10 /**
     9  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    11  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    10  *         16.07.2012 4:30:47
    12  *         16.07.2012 4:30:47
    11  * 
    13  * 
    12  */
    14  */
    13 public abstract class AbstractSolver {
    15 public abstract class AbstractSolver {
    14 
    16 
    15 	protected Mine					mine			= null;
    17 	protected Mine				initialMine		= null;
    16 	protected SolverListener		listener		= null;
    18 	protected SolverListener	listener		= null;
    17 	protected Map<String, Integer>	routesWithScore	= new HashMap<String, Integer>();
    19 	protected Set<Game>			games			= new HashSet<Game>();
    18 	protected long					attemptsCount	= 0;
    20 	protected long				attemptsCount	= 0;
       
    21 	protected long				startTime		= System.currentTimeMillis();
    19 
    22 
    20 	public AbstractSolver(Mine mine, SolverListener listener) {
    23 	public AbstractSolver(Mine mine, SolverListener listener) {
    21 		this.mine = mine;
    24 		this.initialMine = mine;
    22 		this.listener = listener;
    25 		this.listener = listener;
    23 		solve();
       
    24 	}
    26 	}
    25 
    27 
    26 	protected void addNewRoute(String route, int score) {
    28 	protected void addNewRoute(Game game) {
    27 		if (!routesWithScore.containsKey(route) && score > getMaxFoundScore()) {
    29 		Game maxScoreGame = getMaxFoundScoreGame();
    28 			routesWithScore.put(route, score);
    30 		if (maxScoreGame == null || game.getScore() > maxScoreGame.getScore()
    29 			listener.foundNextRoute(route);
    31 				|| game.getScore() == maxScoreGame.getScore()
       
    32 				&& game.getRoute().size() < maxScoreGame.getRoute().size()) {
       
    33 			listener.foundNextRoute(game.getStringRoute());
       
    34 			// TODO delete
       
    35 			System.out.println(new DecimalFormat("0.00").format((((System.currentTimeMillis() - startTime) / 1000d)))
       
    36 					+ " seconds, State = " + game.getState() + ", Score = " + game.getScore() + ", Route("
       
    37 					+ game.getRoute().size() + ") = " + game.getStringRoute());
    30 		}
    38 		}
       
    39 		games.add(game);
    31 	}
    40 	}
    32 
    41 
    33 	protected int getMaxFoundScore() {
    42 	protected Game getMaxFoundScoreGame() {
    34 		int maxScore = 0;
    43 		Game game = null;
    35 		for (int foundScore : routesWithScore.values()) {
    44 		for (Game foundGame : games) {
    36 			if (foundScore > maxScore)
    45 			if (game == null || foundGame.getScore() > game.getScore())
    37 				maxScore = foundScore;
    46 				game = foundGame;
    38 		}
    47 		}
    39 		return maxScore;
    48 		return game;
    40 	}
    49 	}
    41 
    50 
    42 	protected abstract void solve();
    51 	public abstract void solve();
    43 }
    52 }