From c0269683bcc7fde0d437ae84cd89a93d9d8fd81b Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 9 Mar 2014 23:32:05 +0000 Subject: Started refactoring backend in preparation for integration with the GUI --- src/jcgp/modules/Module.java | 1 - src/jcgp/modules/function/Arithmetic.java | 18 -------------- src/jcgp/modules/function/BitwiseLogic.java | 29 ++++++--------------- src/jcgp/modules/function/BooleanLogic.java | 29 ++++++--------------- src/jcgp/modules/function/FunctionSet.java | 10 ++++++-- src/jcgp/modules/mutator/PointMutator.java | 36 +++++++++++++++++++++++++++ src/jcgp/modules/mutator/StandardMutator.java | 31 ----------------------- 7 files changed, 58 insertions(+), 96 deletions(-) create mode 100644 src/jcgp/modules/mutator/PointMutator.java delete mode 100644 src/jcgp/modules/mutator/StandardMutator.java (limited to 'src/jcgp/modules') diff --git a/src/jcgp/modules/Module.java b/src/jcgp/modules/Module.java index a0da50a..b8d44d2 100644 --- a/src/jcgp/modules/Module.java +++ b/src/jcgp/modules/Module.java @@ -11,5 +11,4 @@ public abstract class Module { protected final void registerParameter(String key, Parameter value) { Parameters.add(key, value); }; - } diff --git a/src/jcgp/modules/function/Arithmetic.java b/src/jcgp/modules/function/Arithmetic.java index 73debd2..aa5e9bf 100644 --- a/src/jcgp/modules/function/Arithmetic.java +++ b/src/jcgp/modules/function/Arithmetic.java @@ -1,7 +1,6 @@ package jcgp.modules.function; import jcgp.exceptions.InvalidArgumentsException; -import jcgp.parameters.Parameters; import jcgp.population.Connection; public class Arithmetic { @@ -19,9 +18,6 @@ public class Arithmetic { Integer arg2 = ((Integer) connections[1].getValue()); Integer result = arg1 + arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " + " + arg2 + " = " + result); - } return result; } } @@ -45,9 +41,6 @@ public class Arithmetic { Integer arg2 = ((Integer) connections[1].getValue()); Integer result = arg1 - arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " - " + arg2 + " = " + result); - } return result; } } @@ -71,11 +64,6 @@ public class Arithmetic { Integer arg2 = ((Integer) connections[1].getValue()); Integer result = arg1 * arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " * " + arg2 + " = " + result); - } - - return result; } } @@ -103,12 +91,6 @@ public class Arithmetic { } else { result = arg1 / arg2; } - - - if (Parameters.getDebug()) { - System.out.println(arg1 + " / " + arg2 + " = " + result); - } - return result; } diff --git a/src/jcgp/modules/function/BitwiseLogic.java b/src/jcgp/modules/function/BitwiseLogic.java index a260715..c8452e6 100644 --- a/src/jcgp/modules/function/BitwiseLogic.java +++ b/src/jcgp/modules/function/BitwiseLogic.java @@ -1,7 +1,6 @@ package jcgp.modules.function; import jcgp.exceptions.InvalidArgumentsException; -import jcgp.parameters.Parameters; import jcgp.population.Connection; public class BitwiseLogic { @@ -17,9 +16,7 @@ public class BitwiseLogic { int arg1 = ((int) connections[0].getValue()); int arg2 = ((int) connections[1].getValue()); int result = arg1 & arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " AND " + arg2 + " = " + result); - } + return result; } } @@ -41,9 +38,7 @@ public class BitwiseLogic { int arg1 = ((int) connections[0].getValue()); int arg2 = ((int) connections[1].getValue()); int result = arg1 | arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " OR " + arg2 + " = " + result); - } + return result; } } @@ -64,9 +59,7 @@ public class BitwiseLogic { } else { int arg1 = ((int) connections[0].getValue()); int result = ~arg1; - if (Parameters.getDebug()) { - System.out.println("NOT " + arg1 + " = " + result); - } + return result; } } @@ -88,9 +81,7 @@ public class BitwiseLogic { int arg1 = ((int) connections[0].getValue()); int arg2 = ((int) connections[1].getValue()); int result = arg1 ^ arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " XOR " + arg2 + " = " + result); - } + return result; } } @@ -112,9 +103,7 @@ public class BitwiseLogic { int arg1 = ((int) connections[0].getValue()); int arg2 = ((int) connections[1].getValue()); int result = arg1 & arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " NAND " + arg2 + " = " + ~result); - } + return ~result; } } @@ -136,9 +125,7 @@ public class BitwiseLogic { int arg1 = ((int) connections[0].getValue()); int arg2 = ((int) connections[1].getValue()); int result = arg1 | arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " NOR " + arg2 + " = " + ~result); - } + return ~result; } } @@ -160,9 +147,7 @@ public class BitwiseLogic { int arg1 = ((int) connections[0].getValue()); int arg2 = ((int) connections[1].getValue()); int result = arg1 ^ arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " XNOR " + arg2 + " = " + ~result); - } + return ~result; } } diff --git a/src/jcgp/modules/function/BooleanLogic.java b/src/jcgp/modules/function/BooleanLogic.java index 887e0e6..f98d1db 100644 --- a/src/jcgp/modules/function/BooleanLogic.java +++ b/src/jcgp/modules/function/BooleanLogic.java @@ -1,7 +1,6 @@ package jcgp.modules.function; import jcgp.exceptions.InvalidArgumentsException; -import jcgp.parameters.Parameters; import jcgp.population.Connection; public class BooleanLogic { @@ -17,9 +16,7 @@ public class BooleanLogic { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 && arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " AND " + arg2 + " = " + result); - } + return result; } } @@ -41,9 +38,7 @@ public class BooleanLogic { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 || arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " OR " + arg2 + " = " + result); - } + return result; } } @@ -64,9 +59,7 @@ public class BooleanLogic { } else { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean result = !arg1; - if (Parameters.getDebug()) { - System.out.println("NOT " + arg1 + " = " + result); - } + return result; } } @@ -88,9 +81,7 @@ public class BooleanLogic { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 ^ arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " XOR " + arg2 + " = " + result); - } + return result; } } @@ -112,9 +103,7 @@ public class BooleanLogic { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 && arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " NAND " + arg2 + " = " + !result); - } + return !result; } } @@ -136,9 +125,7 @@ public class BooleanLogic { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 || arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " NOR " + arg2 + " = " + !result); - } + return !result; } } @@ -160,9 +147,7 @@ public class BooleanLogic { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 ^ arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " XNOR " + arg2 + " = " + !result); - } + return !result; } } diff --git a/src/jcgp/modules/function/FunctionSet.java b/src/jcgp/modules/function/FunctionSet.java index 8a2190a..fb3724f 100644 --- a/src/jcgp/modules/function/FunctionSet.java +++ b/src/jcgp/modules/function/FunctionSet.java @@ -13,16 +13,18 @@ package jcgp.modules.function; public class FunctionSet { private Function[] functionList; private int maxArity = 0; + private String name; - public FunctionSet(Function ... functions) { + 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() { @@ -36,4 +38,8 @@ public class FunctionSet { public int getMaxArity(){ return maxArity; } + + public String getName() { + return name; + } } \ No newline at end of file diff --git a/src/jcgp/modules/mutator/PointMutator.java b/src/jcgp/modules/mutator/PointMutator.java new file mode 100644 index 0000000..b06d678 --- /dev/null +++ b/src/jcgp/modules/mutator/PointMutator.java @@ -0,0 +1,36 @@ +package jcgp.modules.mutator; + +import jcgp.Utilities; +import jcgp.parameters.IntegerParameter; +import jcgp.parameters.Parameters; +import jcgp.population.Chromosome; +import jcgp.population.MutableElement; +import jcgp.population.Node; +import jcgp.population.Output; + +public class PointMutator implements Mutator { + + public PointMutator() { + Parameters.add("mutRate", new IntegerParameter(10, "Mutation rate")); + } + + @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)); + + for (int i = 0; i < mutations; i++) { + MutableElement m = chromosome.getRandomMutableElement(); + + if (m instanceof Output) { + m.setConnection(chromosome.getRandomConnection()); + } else if (m instanceof Node) { + int geneType = Utilities.getRandomInt(1 + ((int) Parameters.get("Max arity").getValue())); + if (geneType < 1) { + ((Node) m).setFunction(Utilities.getRandomFunction()); + } else { + m.setConnection(chromosome.getRandomConnection(((Node) m).getColumn())); + } + } + } + } +} diff --git a/src/jcgp/modules/mutator/StandardMutator.java b/src/jcgp/modules/mutator/StandardMutator.java deleted file mode 100644 index 17bd0be..0000000 --- a/src/jcgp/modules/mutator/StandardMutator.java +++ /dev/null @@ -1,31 +0,0 @@ -package jcgp.modules.mutator; - -import jcgp.Utilities; -import jcgp.parameters.Parameters; -import jcgp.population.Chromosome; -import jcgp.population.MutableElement; -import jcgp.population.Node; -import jcgp.population.Output; - -public class StandardMutator implements Mutator { - - @Override - public void mutate(Chromosome chromosome) { - int mutations = (int) (Parameters.getMutationRate() * (((double) Parameters.getNodeCount() + Parameters.getOutputs()) / 100)); - - for (int i = 0; i < mutations; i++) { - MutableElement m = chromosome.getRandomMutableElement(); - - if (m instanceof Output) { - m.setConnection(chromosome.getRandomConnection()); - } else if (m instanceof Node) { - int geneType = Utilities.getRandomInt(1 + Parameters.getMaxArity()); - if (geneType < 1) { - ((Node) m).setFunction(Utilities.getRandomFunction()); - } else { - m.setConnection(chromosome.getRandomConnection(((Node) m).getColumn())); - } - } - } - } -} -- cgit v1.2.3