GA.java
1 package pep; 2 3 import genetics.*; 4 import java.util.HashMap; 5 6 /** 7 * The Genetic Algorithm generates the offspring of the current population 8 * It saves the old generation to XML, and writes the new generation to XML 9 * 10 * GA also defines the genes that will be used for this bot 11 */ 12 public class GA { 13 static int POPULATION_SIZE = 20; 14 static HashMap<String, Gene> genes = new HashMap<>(); 15 16 /** 17 * Initializes genes with the proper names and min/max values 18 */ 19 static void initializeGenes(){ 20 genes.put("depositorAmount", new Gene("depositorAmount", 0.5, 1.0)); 21 genes.put("depositorLateAmount", new Gene("depositorLateAmount", 0.01, 0.7)); 22 genes.put("depositorLateBuffer", new Gene("depositorLateBuffer", 9.0, 15.0)); 23 // genes.put("percentBH", new Gene("percentBH", 0.1, 0.9)); 24 genes.put("scarceMult", new Gene("scarceMult", 0.3, 1.5)); 25 genes.put("scanDistanceBH", new Gene("scanDistanceBH", 1.0, 10.0)); 26 genes.put("scanDistanceTH", new Gene("scanDistanceTH", 1.0, 20.0)); 27 genes.put("thresholdTH", new Gene("thresholdTH", 0.0, 0.2)); 28 genes.put("scarceLocalDist", new Gene("scarceLocalDist", 1.0, 5.1)); 29 genes.put("scarceLocalMult", new Gene("scarceLocalMult", 0.3, 1.5)); 30 } 31 32 /** 33 * Reads the current population from XML, reproduces, then writes offspring to XML 34 */ 35 public static void main(String[] args) { 36 initializeGenes(); 37 Population population = PopulationXML.readXML("population.xml"); 38 int oldGen = population.getGeneration(); 39 PopulationXML.writeToXML("population" + oldGen + ".xml", population, 0); 40 Population newPopulation = Reproduction.reproduce(population, genes); 41 PopulationXML.writeToXML("population.xml", newPopulation, 0); 42 } 43 }