From 0c288cc1952809294c8d70d86b9f41b04878ac2e Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 23 Mar 2014 18:05:13 +0000 Subject: Majorly refactored, node grid is fully implemented. About to attempt active path locking. --- src/jcgp/modules/Module.java | 13 +- src/jcgp/modules/ea/EvolutionaryAlgorithm.java | 6 +- src/jcgp/modules/ea/StandardEA.java | 39 ++++-- src/jcgp/modules/fitness/FitnessFunction.java | 6 +- src/jcgp/modules/fitness/TestCase.java | 19 +-- src/jcgp/modules/fitness/TestCaseEvaluator.java | 34 +++++ src/jcgp/modules/fitness/TruthTableEvaluator.java | 30 ---- src/jcgp/modules/function/Arithmetic.java | 105 -------------- src/jcgp/modules/function/BitwiseLogic.java | 163 ---------------------- src/jcgp/modules/function/BooleanLogic.java | 163 ---------------------- src/jcgp/modules/function/Function.java | 12 -- src/jcgp/modules/function/FunctionSet.java | 45 ------ src/jcgp/modules/mutator/Mutator.java | 6 +- src/jcgp/modules/mutator/PointMutator.java | 39 ++++-- 14 files changed, 109 insertions(+), 571 deletions(-) create mode 100644 src/jcgp/modules/fitness/TestCaseEvaluator.java delete mode 100644 src/jcgp/modules/fitness/TruthTableEvaluator.java delete mode 100644 src/jcgp/modules/function/Arithmetic.java delete mode 100644 src/jcgp/modules/function/BitwiseLogic.java delete mode 100644 src/jcgp/modules/function/BooleanLogic.java delete mode 100644 src/jcgp/modules/function/Function.java delete mode 100644 src/jcgp/modules/function/FunctionSet.java (limited to 'src/jcgp/modules') diff --git a/src/jcgp/modules/Module.java b/src/jcgp/modules/Module.java index b8d44d2..5ce96b9 100644 --- a/src/jcgp/modules/Module.java +++ b/src/jcgp/modules/Module.java @@ -1,14 +1,11 @@ package jcgp.modules; +import java.util.HashMap; + +import jcgp.CGP.Resources; import jcgp.parameters.Parameter; -import jcgp.parameters.Parameters; -public abstract class Module { +public interface Module { - /** - * Register a new parameter - */ - protected final void registerParameter(String key, Parameter value) { - Parameters.add(key, value); - }; + public HashMap activate(Resources parameters); } diff --git a/src/jcgp/modules/ea/EvolutionaryAlgorithm.java b/src/jcgp/modules/ea/EvolutionaryAlgorithm.java index 8de8c87..d3fa709 100644 --- a/src/jcgp/modules/ea/EvolutionaryAlgorithm.java +++ b/src/jcgp/modules/ea/EvolutionaryAlgorithm.java @@ -1,12 +1,14 @@ package jcgp.modules.ea; +import jcgp.CGP.Resources; +import jcgp.modules.Module; import jcgp.modules.mutator.Mutator; import jcgp.population.Chromosome; import jcgp.population.Population; -public interface EvolutionaryAlgorithm { +public interface EvolutionaryAlgorithm extends Module { - public abstract void evolve(Population population, Mutator mutator); + public abstract void evolve(Population population, Mutator mutator, Resources parameters); public abstract Chromosome getFittestChromosome(); diff --git a/src/jcgp/modules/ea/StandardEA.java b/src/jcgp/modules/ea/StandardEA.java index 2db8776..f2473f5 100644 --- a/src/jcgp/modules/ea/StandardEA.java +++ b/src/jcgp/modules/ea/StandardEA.java @@ -1,14 +1,16 @@ package jcgp.modules.ea; +import java.util.HashMap; + +import jcgp.CGP.Resources; import jcgp.modules.mutator.Mutator; -import jcgp.parameters.Parameters; import jcgp.parameters.IntegerParameter; -import jcgp.parameters.BooleanParameter; +import jcgp.parameters.Parameter; import jcgp.population.Chromosome; import jcgp.population.Population; /** - * (1 + λ) EA. + * (μ + λ) EA. * * * @author Eduardo Pedroni @@ -17,28 +19,38 @@ import jcgp.population.Population; public class StandardEA implements EvolutionaryAlgorithm { private Chromosome fittestChromosome; + + private IntegerParameter parents, offspring; + private HashMap localParameters; + + public StandardEA() { + parents = new IntegerParameter(1, "Parents"); + offspring = new IntegerParameter(4, "Offspring"); + + localParameters = new HashMap(); + + localParameters.put("mu", parents); + localParameters.put("lambda", offspring); + } @Override - public void evolve(Population population, Mutator mutator) { + public void evolve(Population population, Mutator mutator, Resources parameters) { // select fittest chromosome int fittest = 0; - for (int i = 1; i < ((IntegerParameter) Parameters.get("population")).getValue(); i++) { + for (int i = 1; i < (int) parameters.get("popSize"); i++) { if (population.getChromosome(i).getFitness() >= population.getChromosome(fittest).getFitness()) { fittest = i; } } fittestChromosome = population.getChromosome(fittest); population.setBestIndividual(fittest); - if (((BooleanParameter) Parameters.get("debug")).getValue()) { - System.out.println("Best fitness: " + fittestChromosome.getFitness()); - } // create copies of fittest chromosome, mutate them Chromosome fc = population.getChromosome(fittest); - for (int i = 0; i < ((IntegerParameter) Parameters.get("population")).getValue(); i++) { + for (int i = 0; i < (int) parameters.get("popSize"); i++) { if (i != fittest) { population.getChromosome(i).copyConnections(fc); - mutator.mutate(population.getChromosome(i)); + mutator.mutate(population.getChromosome(i), parameters); } } } @@ -47,4 +59,11 @@ public class StandardEA implements EvolutionaryAlgorithm { public Chromosome getFittestChromosome() { return fittestChromosome; } + + @Override + public HashMap activate(Resources parameters) { + parameters.setManagedParameter("popSize", true); + + return localParameters; + } } diff --git a/src/jcgp/modules/fitness/FitnessFunction.java b/src/jcgp/modules/fitness/FitnessFunction.java index 8ed1b56..509c230 100644 --- a/src/jcgp/modules/fitness/FitnessFunction.java +++ b/src/jcgp/modules/fitness/FitnessFunction.java @@ -1,9 +1,11 @@ package jcgp.modules.fitness; +import jcgp.CGP.Resources; +import jcgp.modules.Module; import jcgp.population.Population; -public interface FitnessFunction { +public interface FitnessFunction extends Module { - public void evaluate(Population population); + public void evaluate(Population population, Resources resources); } diff --git a/src/jcgp/modules/fitness/TestCase.java b/src/jcgp/modules/fitness/TestCase.java index 0cb09f1..081a257 100644 --- a/src/jcgp/modules/fitness/TestCase.java +++ b/src/jcgp/modules/fitness/TestCase.java @@ -1,26 +1,13 @@ package jcgp.modules.fitness; -import jcgp.exceptions.ParameterMismatchException; -import jcgp.parameters.Parameters; - public class TestCase { private Object[] inputs; private Object[] outputs; - public TestCase(Object[] inputs, Object[] outputs) throws ParameterMismatchException { - if (inputs.length == Parameters.getInputs()) { - this.inputs = inputs; - } else { - throw new ParameterMismatchException(); - } - - if (outputs.length == Parameters.getOutputs()) { - this.outputs = outputs; - } else { - throw new ParameterMismatchException(); - } - + public TestCase(Object[] inputs, Object[] outputs) { + this.inputs = inputs; + this.outputs = outputs; } public Object getInput(int index) { diff --git a/src/jcgp/modules/fitness/TestCaseEvaluator.java b/src/jcgp/modules/fitness/TestCaseEvaluator.java new file mode 100644 index 0000000..1dade74 --- /dev/null +++ b/src/jcgp/modules/fitness/TestCaseEvaluator.java @@ -0,0 +1,34 @@ +package jcgp.modules.fitness; + +import java.util.HashMap; + +import jcgp.CGP.Resources; +import jcgp.parameters.Parameter; +import jcgp.population.Population; + +public class TestCaseEvaluator implements FitnessFunction { + + @Override + public void evaluate(Population population, Resources resources) { + // for every chromosome in the population + for (int i = 0; i < (int) resources.get("popSize"); i++) { + int fitness = 0; + // for every test case + for (int t = 0; t < resources.getTestCaseCount(); t++) { + population.getChromosome(i).setInputs(resources.getTestCase(t).getInputs()); + // check every output + for (int o = 0; o < (int) resources.get("outputs"); o++) { + if (population.getChromosome(i).getOutput(o).calculate() == resources.getTestCase(t).getOutput(o)) { + fitness++; + } + } + } + population.getChromosome(i).setFitness(fitness); + } + } + + @Override + public HashMap activate(Resources parameters) { + return new HashMap(); + } +} diff --git a/src/jcgp/modules/fitness/TruthTableEvaluator.java b/src/jcgp/modules/fitness/TruthTableEvaluator.java deleted file mode 100644 index a69de96..0000000 --- a/src/jcgp/modules/fitness/TruthTableEvaluator.java +++ /dev/null @@ -1,30 +0,0 @@ -package jcgp.modules.fitness; - -import jcgp.TruthTable; -import jcgp.parameters.Parameters; -import jcgp.population.Population; - -public class TruthTableEvaluator implements FitnessFunction { - - @Override - public void evaluate(Population population) { - // for every chromosome in the population - for (int i = 0; i < Parameters.getPopulationSize(); i++) { - int fitness = 0; - // for every test case - for (int t = 0; t < TruthTable.getTestCaseCount(); t++) { - population.getChromosome(i).setInputs(TruthTable.getTestCase(t).getInputs()); - // check every output - for (int o = 0; o < Parameters.getOutputs(); o++) { - if (population.getChromosome(i).getOutput(o).calculate() == TruthTable.getTestCase(t).getOutput(o)) { - fitness++; - } - } - } - population.getChromosome(i).setFitness(fitness); - if (Parameters.getDebug()) { - System.out.println("active nodes: " + population.getChromosome(i).getActiveNodes().size()); - } - } - } -} diff --git a/src/jcgp/modules/function/Arithmetic.java b/src/jcgp/modules/function/Arithmetic.java deleted file mode 100644 index aa5e9bf..0000000 --- a/src/jcgp/modules/function/Arithmetic.java +++ /dev/null @@ -1,105 +0,0 @@ -package jcgp.modules.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class Arithmetic { - - public static class Addition extends Function { - - private int arity = 2; - - @Override - public Integer run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 + arg2; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Subtraction extends Function { - - private int arity = 2; - - @Override - public Integer run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 - arg2; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Multiplication extends Function { - - private int arity = 2; - - @Override - public Integer run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 * arg2; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Division extends Function { - - private int arity = 2; - - @Override - public Integer run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result; - if (arg2 == 0) { - result = 0; - } else { - result = arg1 / arg2; - } - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - -} diff --git a/src/jcgp/modules/function/BitwiseLogic.java b/src/jcgp/modules/function/BitwiseLogic.java deleted file mode 100644 index c8452e6..0000000 --- a/src/jcgp/modules/function/BitwiseLogic.java +++ /dev/null @@ -1,163 +0,0 @@ -package jcgp.modules.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class BitwiseLogic { - - public static class And extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 & arg2; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Or extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 | arg2; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Not extends Function { - private int arity = 1; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int result = ~arg1; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Xor extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 ^ arg2; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Nand extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 & arg2; - - return ~result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Nor extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 | arg2; - - return ~result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Xnor extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 ^ arg2; - - return ~result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - - -} diff --git a/src/jcgp/modules/function/BooleanLogic.java b/src/jcgp/modules/function/BooleanLogic.java deleted file mode 100644 index f98d1db..0000000 --- a/src/jcgp/modules/function/BooleanLogic.java +++ /dev/null @@ -1,163 +0,0 @@ -package jcgp.modules.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class BooleanLogic { - - public static class And extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 && arg2; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Or extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 || arg2; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Not extends Function { - private int arity = 1; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean result = !arg1; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Xor extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 ^ arg2; - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Nand extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 && arg2; - - return !result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Nor extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 || arg2; - - return !result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Xnor extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 ^ arg2; - - return !result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - - -} diff --git a/src/jcgp/modules/function/Function.java b/src/jcgp/modules/function/Function.java deleted file mode 100644 index 3314c2f..0000000 --- a/src/jcgp/modules/function/Function.java +++ /dev/null @@ -1,12 +0,0 @@ -package jcgp.modules.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public abstract class Function { - - public abstract Object run(Connection ... connections) throws InvalidArgumentsException; - - public abstract int getArity(); - -} diff --git a/src/jcgp/modules/function/FunctionSet.java b/src/jcgp/modules/function/FunctionSet.java deleted file mode 100644 index fb3724f..0000000 --- a/src/jcgp/modules/function/FunctionSet.java +++ /dev/null @@ -1,45 +0,0 @@ -package jcgp.modules.function; - -/** - * - * TODO: if function set flexibility is desired (i.e. add more functions as the program runs) - * an add function method should be created - * this would lead to concurrency problems, so tread lightly! - * - * - * @author Eduardo Pedroni - * - */ -public class FunctionSet { - private Function[] functionList; - private int maxArity = 0; - private String name; - - public FunctionSet(String name, Function ... functions) { - functionList = functions; - for (Function function : functionList) { - if (function.getArity() > maxArity) { - maxArity = function.getArity(); - } - } - - this.name = name; - - } - - public int getFunctionCount() { - return functionList.length; - } - - public Function getFunction(int index) { - return functionList[index]; - } - - public int getMaxArity(){ - return maxArity; - } - - public String getName() { - return name; - } - } \ No newline at end of file diff --git a/src/jcgp/modules/mutator/Mutator.java b/src/jcgp/modules/mutator/Mutator.java index 10df8cd..5234f45 100644 --- a/src/jcgp/modules/mutator/Mutator.java +++ b/src/jcgp/modules/mutator/Mutator.java @@ -1,9 +1,11 @@ package jcgp.modules.mutator; +import jcgp.CGP.Resources; +import jcgp.modules.Module; import jcgp.population.Chromosome; -public interface Mutator { +public interface Mutator extends Module { - void mutate(Chromosome chromosome); + void mutate(Chromosome chromosome, Resources parameters); } diff --git a/src/jcgp/modules/mutator/PointMutator.java b/src/jcgp/modules/mutator/PointMutator.java index b06d678..ea1ed32 100644 --- a/src/jcgp/modules/mutator/PointMutator.java +++ b/src/jcgp/modules/mutator/PointMutator.java @@ -1,36 +1,49 @@ package jcgp.modules.mutator; -import jcgp.Utilities; -import jcgp.parameters.IntegerParameter; -import jcgp.parameters.Parameters; +import java.util.HashMap; + +import jcgp.parameters.DoubleParameter; +import jcgp.parameters.Parameter; +import jcgp.CGP.Resources; import jcgp.population.Chromosome; import jcgp.population.MutableElement; import jcgp.population.Node; import jcgp.population.Output; public class PointMutator implements Mutator { - + + private Parameter mutationRate; + private HashMap localParameters; + public PointMutator() { - Parameters.add("mutRate", new IntegerParameter(10, "Mutation rate")); + mutationRate = new DoubleParameter(0.5, "Percent mutation"); + + localParameters = new HashMap(); + localParameters.put("mutRate", mutationRate); } - + @Override - public void mutate(Chromosome chromosome) { - int mutations = (int) (((int) Parameters.get("mutRate").getValue()) * ((((double) Parameters.get("nodes").getValue()) + ((double) Parameters.get("outputs").getValue())) / 100)); - + public void mutate(Chromosome chromosome, Resources resources) { + int mutations = (int) Math.ceil((((double) mutationRate.getValue()) * (((((Integer) resources.get("nodes")).doubleValue() + ((Integer) resources.get("outputs")).doubleValue())) / (double) 100))); for (int i = 0; i < mutations; i++) { MutableElement m = chromosome.getRandomMutableElement(); if (m instanceof Output) { - m.setConnection(chromosome.getRandomConnection()); + m.setConnection(0, chromosome.getRandomConnection()); } else if (m instanceof Node) { - int geneType = Utilities.getRandomInt(1 + ((int) Parameters.get("Max arity").getValue())); + int geneType = resources.getRandomInt(1 + ((int) resources.get("arity"))); if (geneType < 1) { - ((Node) m).setFunction(Utilities.getRandomFunction()); + ((Node) m).setFunction(resources.getRandomFunction()); } else { - m.setConnection(chromosome.getRandomConnection(((Node) m).getColumn())); + m.setConnection(resources.getRandomInt((int) resources.get("arity")), chromosome.getRandomConnection(((Node) m).getColumn())); } } } } + + @Override + public HashMap activate(Resources parameters) { + return localParameters; + } + } -- cgit v1.2.3