Parameters.java
1 package pep; 2 3 import genetics.*; 4 import hlt.*; 5 6 import java.util.HashMap; 7 8 public class Parameters { 9 private static HashMap<String, Double> parameters = new HashMap<>(); 10 private static Population population; 11 private static int currentId; // the ID of the current individual we are using the genes of 12 private static int initialHalite; 13 14 public static void evolvedDefaults(){ 15 parameters.put("depositorAmount", 0.88); // amount of halite at which depositor should return 16 parameters.put("depositorLateAmount", 0.612); // amount of halite in late game at which depositor should return 17 parameters.put("depositorLateBuffer", 12.81); // buffer for turn at which a depositor should return late game 18 // parameters.put("percentBH", 0.11); // percentage of harvesters to make BestHarvester's 19 parameters.put("scanDistanceBH", 6.566); // the distance scanned by a BestHarvester 20 parameters.put("scanDistanceTH", 17.94); // the distance scanned by a ThresholdHarvester 21 parameters.put("thresholdTH", 0.179); // the halite threshold a TH seeks 22 parameters.put("scarceMult", 0.449); // multiplier - amount of halite below which is considered scarce/not worth harvesting 23 parameters.put("scarceLocalDist", 2.42); // local distance to scan to determine scarce halite 24 parameters.put("scarceLocalMult", 0.437); // multiplier - average local halite -> scarce threshold 25 } 26 27 // ===== Methods for instantiating parameters from an individual ===== 28 29 public static void populateFromXML(){ 30 population = PopulationXML.readXML("population.xml"); 31 currentId = PopulationXML.currentIndividual; 32 parameters.putAll(population.getIndividual(currentId).getChromosome()); 33 } 34 35 public static void saveInitialState(Game game){ 36 initialHalite = game.gameMap.getTotalHalite(); 37 } 38 39 public static void writeFitness(Game game){ 40 double gameScore = ((double)game.me.halite) / initialHalite; 41 double prevFitness = population.getIndividual(currentId).getFitness(); 42 double fitness = prevFitness + gameScore; 43 fitness = Math.floor(fitness * 1000) / 1000; 44 population.getIndividual(currentId).setFitness(fitness); 45 currentId = (currentId + 1) % GA.POPULATION_SIZE; 46 PopulationXML.writeToXML("population.xml", population, currentId); 47 } 48 49 /** 50 * Writes to XML the fitness of this individual, fitness based on early game performance 51 */ 52 public static void writeFitnessEarly(Game game){ 53 double averageScore = 0.0; // average score across other players 54 for (Player player : game.players){ 55 if (!player.id.equals(game.me.id)){ 56 averageScore += (double)(player.halite + player.ships.size() * Constants.SHIP_COST); 57 } 58 } 59 averageScore = averageScore / (game.players.size() - 1); 60 61 // calculate increase in fitness from this game 62 double myScore = (double)(game.me.halite + game.me.ships.size() * Constants.SHIP_COST); 63 double diff = myScore - averageScore; 64 double gameFitness = diff > 0 ? 1.0 : -1.0; 65 66 // increment fitness and write to XML 67 double prevFitness = population.getIndividual(currentId).getFitness(); 68 double fitness = prevFitness + gameFitness; 69 fitness = Math.floor(fitness * 1000) / 1000; 70 population.getIndividual(currentId).setFitness(fitness); 71 currentId = (currentId + 1) % GA.POPULATION_SIZE; 72 PopulationXML.writeToXML("population.xml", population, currentId); 73 } 74 75 // ===== Methods for accessing parameters ===== 76 77 /** 78 * @return Amount of halite below which is considered scarce/not worth harvesting 79 */ 80 static int getScarceHalite(Game game, Ship ship){ 81 int globalScarce = (int)(parameters.get("scarceMult") * game.gameMap.getAverageHalite()); 82 int localDistance = parameters.get("scarceLocalDist").intValue(); 83 int localCells = Ripple.positionsWithinDist(localDistance); 84 double localAvgHalite = game.gameMap.haliteWithinDistance(ship.position, localDistance) / (double) localCells; 85 int localScarce = (int)(parameters.get("scarceLocalMult") * localAvgHalite); 86 return Math.max(globalScarce, localScarce); 87 } 88 89 static double getPercentBH(){ 90 return parameters.get("percentBH"); 91 } 92 93 /** 94 * @return The number of turns this ship needs to deposit late game 95 */ 96 static int getDepositorLateTurns(Game game, Ship ship){ 97 GameMap gameMap = game.gameMap; 98 int dist = gameMap.calculateDistance(ship.position, gameMap.closestDropoff(game.me, ship.position)); 99 return parameters.get("depositorLateBuffer").intValue() + dist; 100 } 101 102 static int getDepositorLateAmount(){ 103 return (int)(parameters.get("depositorLateAmount") * Constants.MAX_HALITE); 104 } 105 106 static int getDepositorAmount(){ 107 return (int)(parameters.get("depositorAmount") * Constants.MAX_HALITE); 108 } 109 110 static int getScanDistanceBH(){ 111 return parameters.get("scanDistanceBH").intValue(); 112 } 113 114 static int getScanDistanceTH(){ 115 return parameters.get("scanDistanceTH").intValue(); 116 } 117 118 static int getThresholdTH(Game game, Ship ship){ 119 return getScarceHalite(game, ship) + (int)(parameters.get("thresholdTH") * Constants.MAX_HALITE); 120 } 121 }