test_class.file: java/src/test/java/net/auxesia/PopulationTest.java
test_class.identifier: PopulationTest
test_case.body: @Test
  	public void testEvolve() {
  		Population pop = new Population(1024, 0.8f, 0.1f, 0.05f);
  		Chromosome[] oldArr = pop.getPopulation();
  		
  		// Evolve and get the new population
  		pop.evolve();
  		Chromosome[] newArr = pop.getPopulation();
  		
  		// Check the details on the resulting evolved population.
  		assertEquals(80, (int) (pop.getCrossover() * 100));
  		assertEquals(10, (int) (pop.getElitism() * 100));
  		assertEquals(5, (int) (pop.getMutation() * 100));
  		
  		// Check to ensure that the elitism took.
  		final int elitismCount = Math.round(1024 * 0.1f);
  		int counter = 0;
  		for (int i = 0; i < oldArr.length; i++) {
  			if (Arrays.binarySearch(newArr, oldArr[i]) >= 0) {
  				++counter;
  			}
  		}
  		// Account for the fact that mating/mutation may cause more than
  		// just the fixed number of chromosomes to be identical.
  		assertTrue(counter >= elitismCount);
  		assertTrue(counter < oldArr.length);
  	}
test_case.class_method_signature: PopulationTest.testEvolve()
test_case.full_signature: @Test public void testEvolve()
test_case.identifier: testEvolve
test_case.invocations: getPopulation evolve getPopulation assertEquals getCrossover assertEquals getElitism assertEquals getMutation round binarySearch assertTrue assertTrue
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testEvolve()
focal_class.declarator: TOURNAMENT_SIZE = 3
focal_class.modifier: private static final
focal_class.original_string: private static final int TOURNAMENT_SIZE = 3;
focal_class.type: int
focal_class.var_name: TOURNAMENT_SIZE
focal_class.declarator: rand = new Random(System.currentTimeMillis())
focal_class.modifier: private static final
focal_class.original_string: private static final Random rand = new Random(System.currentTimeMillis());
focal_class.type: Random
focal_class.var_name: rand
focal_class.declarator: elitism
focal_class.modifier: private
focal_class.original_string: private float elitism;
focal_class.type: float
focal_class.var_name: elitism
focal_class.declarator: mutation
focal_class.modifier: private
focal_class.original_string: private float mutation;
focal_class.type: float
focal_class.var_name: mutation
focal_class.declarator: crossover
focal_class.modifier: private
focal_class.original_string: private float crossover;
focal_class.type: float
focal_class.var_name: crossover
focal_class.declarator: popArr
focal_class.modifier: private
focal_class.original_string: private Chromosome[] popArr;
focal_class.type: Chromosome[]
focal_class.var_name: popArr
focal_class.file: java/src/main/java/net/auxesia/Population.java
focal_class.identifier: Population
focal_class.class_method_signature: Population.Population(int size, float crossoverRatio, float elitismRatio, 
  			float mutationRatio)
focal_class.full_signature: public  Population(int size, float crossoverRatio, float elitismRatio, 
  			float mutationRatio)
focal_class.identifier: Population
focal_class.modifiers: public
focal_class.parameters: (int size, float crossoverRatio, float elitismRatio, 
  			float mutationRatio)
focal_class.signature:  Population(int size, float crossoverRatio, float elitismRatio, 
  			float mutationRatio)
focal_class.class_method_signature: Population.evolve()
focal_class.full_signature: public void evolve()
focal_class.identifier: evolve
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void evolve()
focal_class.class_method_signature: Population.getPopulation()
focal_class.full_signature: public Chromosome[] getPopulation()
focal_class.identifier: getPopulation
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: Chromosome[]
focal_class.signature: Chromosome[] getPopulation()
focal_class.class_method_signature: Population.getElitism()
focal_class.full_signature: public float getElitism()
focal_class.identifier: getElitism
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: float
focal_class.signature: float getElitism()
focal_class.class_method_signature: Population.getCrossover()
focal_class.full_signature: public float getCrossover()
focal_class.identifier: getCrossover
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: float
focal_class.signature: float getCrossover()
focal_class.class_method_signature: Population.getMutation()
focal_class.full_signature: public float getMutation()
focal_class.identifier: getMutation
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: float
focal_class.signature: float getMutation()
focal_class.class_method_signature: Population.selectParents()
focal_class.full_signature: private Chromosome[] selectParents()
focal_class.identifier: selectParents
focal_class.modifiers: private
focal_class.parameters: ()
focal_class.return: Chromosome[]
focal_class.signature: Chromosome[] selectParents()
focal_method.body: public void evolve() {
  		// Create a buffer for the new generation
  		Chromosome[] buffer = new Chromosome[popArr.length];
  		
  		// Copy over a portion of the population unchanged, based on 
  		// the elitism ratio.
  		int idx = Math.round(popArr.length * elitism);
  		System.arraycopy(popArr, 0, buffer, 0, idx);
  
  		// Iterate over the remainder of the population and evolve as 
  		// appropriate.
  		while (idx < buffer.length) {
  			// Check to see if we should perform a crossover. 
  			if (rand.nextFloat() <= crossover) {
  				
  				// Select the parents and mate to get their children
  				Chromosome[] parents = selectParents();
  				Chromosome[] children = parents[0].mate(parents[1]);
  				
  				// Check to see if the first child should be mutated.
  				if (rand.nextFloat() <= mutation) {
  					buffer[idx++] = children[0].mutate();
  				} else {
  					buffer[idx++] = children[0];
  				}
  				
  				// Repeat for the second child, if there is room.
  				if (idx < buffer.length) {
  					if (rand.nextFloat() <= mutation) {
  						buffer[idx] = children[1].mutate();
  					} else {
  						buffer[idx] = children[1];
  					}
  				}
  			} else { // No crossover, so copy verbatium.
  				// Determine if mutation should occur.
  				if (rand.nextFloat() <= mutation) {
  					buffer[idx] = popArr[idx].mutate();
  				} else {
  					buffer[idx] = popArr[idx];
  				}
  			}
  			
  			// Increase our counter
  			++idx;
  		}
  
  		// Sort the buffer based on fitness.
  		Arrays.sort(buffer);
  		
  		// Reset the population
  		popArr = buffer;
  	}
focal_method.class_method_signature: Population.evolve()
focal_method.full_signature: public void evolve()
focal_method.identifier: evolve
focal_method.invocations: round arraycopy nextFloat selectParents mate nextFloat mutate nextFloat mutate nextFloat mutate sort
focal_method.modifiers: public
focal_method.parameters: ()
focal_method.return: void
focal_method.signature: void evolve()