package roart.evolution;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import roart.config.EvolutionConfig;
import roart.evaluation.Evaluation;

public class OrdinaryEvolution extends EvolutionAlgorithm {

    public OrdinaryEvolution(EvolutionConfig evolutionConfig) {
        super(evolutionConfig);
    }
    
    @Override
    public Individual getFittest(EvolutionConfig evolutionConfig, Evaluation evaluation) throws Exception {
        int selectionSize = getEvolutionConfig().getSelect();
        Population population = new Population(selectionSize, evolutionConfig, evaluation, false);
        calculate(population.getIndividuals());
        Collections.sort(population.getIndividuals());
        Individual parent = getBest(selectionSize, population, true, evaluation);
        evaluation.transformFromNode();
        return parent;
    }

    private Individual getBest(int selectionSize, Population population,
            boolean useMax, Evaluation evaluation) throws JsonParseException, JsonMappingException, IOException, InterruptedException, ExecutionException {
        //printmap(population.getFittest().getConf().getConfigValueMap());
        //printmap(population.getIndividuals().get(population.size() - 1).getConf().getConfigValueMap());
        
        for (int i = 0; i < getEvolutionConfig().getGenerations(); i++){
            population.truncate(Math.min(population.size(), getEvolutionConfig().getSelect()));
           
            List<Individual> children = crossover(getEvolutionConfig().getCrossover(), population.getIndividuals(), useMax, evaluation);
            
            mutateList(population.getIndividuals(), getEvolutionConfig().getElite(), population.size(), getEvolutionConfig().getMutate(), false, useMax);
            List<Individual> clonedmutated = clonedmutated(getEvolutionConfig().getElitecloneandmutate(), population.getIndividuals().get(0).getEvaluation());
            mutateList(children, 0, population.size(), getEvolutionConfig().getMutate(), true, useMax);
            
            population.getIndividuals().addAll(children);
            population.getIndividuals().addAll(clonedmutated);
            
            List<Individual> created = created(getEvolutionConfig().getGenerationcreate(), evaluation);
            population.getIndividuals().addAll(created);
            calculate(population.getIndividuals());
            Collections.sort(population.getIndividuals());
        }
        //printmap(population.getIndividuals().get(0).getConf().getConfigValueMap());
        //printmap(population.getIndividuals().get(population.size() - 1).getConf().getConfigValueMap());
        return population.getFittest();
    }

 }

@@ -789,16 +789,206 @@
 false);%0A
+        if (getEvolutionConfig().getUseoldelite() && !evaluation.isEmpty()) %7B%0A            population.getIndividuals().add(new Individual(evaluation).getNewWithValueCopyFactory());%0A        %7D%0A
         
@@ -1019,32 +1019,32 @@
 Individuals());%0A
-
         Collecti
@@ -1172,17 +1172,27 @@
         
-e
+parent.getE
 valuatio
@@ -1192,16 +1192,18 @@
 aluation
+()
 .transfo
@@ -2834,32 +2834,79 @@
 s());%0A        %7D%0A
+        printmap(population.getIndividuals());%0A
         //printm
@@ -3127,12 +3127,11 @@
 ;%0A    %7D%0A
-%0A
  %7D%0A

Add useoldelite. Check if old elite is empty/null.