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