package jcgp; import java.util.Random; import jcgp.modules.ea.EvolutionaryAlgorithm; import jcgp.modules.ea.StandardEA; import jcgp.modules.fitness.FitnessFunction; import jcgp.modules.fitness.TestCase; import jcgp.modules.fitness.TruthTableEvaluator; import jcgp.modules.function.Arithmetic; import jcgp.modules.function.FunctionSet; import jcgp.modules.mutator.Mutator; import jcgp.modules.mutator.StandardMutator; import jcgp.parameters.BooleanParameter; import jcgp.parameters.IntegerParameter; import jcgp.parameters.Parameters; import jcgp.population.Population; public class CGP { // CGP components private EvolutionaryAlgorithm evolutionaryAlgorithm; private Mutator mutator; private Population population; private FitnessFunction fitnessFunction; public CGP() { initialise(); for (int i = 0; i < Parameters.getTotalGenerations(); i++) { Parameters.setCurrentGeneration(i); fitnessFunction.evaluate(population); evolutionaryAlgorithm.evolve(population, mutator); if (evolutionaryAlgorithm.getFittestChromosome().getFitness() >= 3) { if (Parameters.getDebug()) { evolutionaryAlgorithm.getFittestChromosome().printNodes(); } break; } } } /** * */ private void initialise() { // make fundamental parameters Parameters.add("Rows", new IntegerParameter(3)); Parameters.add("Columns", new IntegerParameter(3)); Parameters.add("Inputs", new IntegerParameter(3)); Parameters.add("Outputs", new IntegerParameter(3)); Parameters.add("population", new IntegerParameter(5)); Parameters.add("generations", new IntegerParameter(100)); Parameters.add("runs", new IntegerParameter(5)); Parameters.add("debug", new BooleanParameter(false)); // initialise function set FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction(), new Arithmetic.Multiplication()); // initialise utilities Utilities.setResources(new Random(1234), functionSet); // initialise fitness function and truth table TruthTable.setTestCases(new TestCase(new Object[] {2, 5, 4}, new Object[] {1, 10, 15})); fitnessFunction = new TruthTableEvaluator(); // initialise EA evolutionaryAlgorithm = new StandardEA(); mutator = new StandardMutator(); // initialise population population = new Population(); } }