From e6dd7711c7dad5e000445208eb5845801f4ccffc Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 6 Apr 2014 21:58:53 +0100 Subject: About to make big changes to the way fitness works, committing just in case --- src/jcgp/backend/function/Arithmetic.java | 139 ----------------------- src/jcgp/backend/function/BitwiseLogic.java | 61 ++++------ src/jcgp/backend/function/BooleanLogic.java | 60 ++++------ src/jcgp/backend/function/FunctionSet.java | 32 ++++-- src/jcgp/backend/function/IntegerArithmetic.java | 124 ++++++++++++++++++++ 5 files changed, 191 insertions(+), 225 deletions(-) delete mode 100644 src/jcgp/backend/function/Arithmetic.java create mode 100644 src/jcgp/backend/function/IntegerArithmetic.java (limited to 'src/jcgp/backend/function') diff --git a/src/jcgp/backend/function/Arithmetic.java b/src/jcgp/backend/function/Arithmetic.java deleted file mode 100644 index 6971663..0000000 --- a/src/jcgp/backend/function/Arithmetic.java +++ /dev/null @@ -1,139 +0,0 @@ -package jcgp.backend.function; - -import java.util.ArrayList; -import java.util.Arrays; - -import jcgp.backend.exceptions.InvalidArgumentsException; -import jcgp.backend.population.Connection; - -public class Arithmetic extends FunctionSet { - - public Arithmetic() { - name = "Arithmetic"; - functionList = new Function[]{ - new Addition(), - new Subtraction(), - new Multiplication(), - new Division()}; - - allowedFunctions = new ArrayList(Arrays.asList(functionList)); - } - - 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; - } - - @Override - public String getName() { - return "Addition"; - } - } - - 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; - } - - @Override - public String getName() { - return "Subtraction"; - } - } - - 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; - } - - @Override - public String getName() { - return "Multiplication"; - } - } - - 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; - } - - @Override - public String getName() { - return "Division"; - } - } - -} diff --git a/src/jcgp/backend/function/BitwiseLogic.java b/src/jcgp/backend/function/BitwiseLogic.java index bfe361f..5d47ff7 100644 --- a/src/jcgp/backend/function/BitwiseLogic.java +++ b/src/jcgp/backend/function/BitwiseLogic.java @@ -1,8 +1,5 @@ package jcgp.backend.function; -import java.util.ArrayList; -import java.util.Arrays; - import jcgp.backend.exceptions.InvalidArgumentsException; import jcgp.backend.population.Connection; @@ -19,15 +16,13 @@ public class BitwiseLogic extends FunctionSet { new Nor(), new Xnor()}; - allowedFunctions = new ArrayList(Arrays.asList(functionList)); + enableAll(); } - public static class And extends Function { - private int arity = 2; - + public static class And extends Function { @Override public Object run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { int arg1 = ((int) connections[0].getValue()); @@ -40,7 +35,7 @@ public class BitwiseLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override @@ -49,12 +44,10 @@ public class BitwiseLogic extends FunctionSet { } } - public static class Or extends Function { - private int arity = 2; - + public static class Or extends Function { @Override public Object run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { int arg1 = ((int) connections[0].getValue()); @@ -67,7 +60,7 @@ public class BitwiseLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override @@ -76,12 +69,10 @@ public class BitwiseLogic extends FunctionSet { } } - public static class Not extends Function { - private int arity = 1; - + public static class Not extends Function { @Override public Object run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 1) { throw new InvalidArgumentsException("Not enough connections were given."); } else { int arg1 = ((int) connections[0].getValue()); @@ -93,7 +84,7 @@ public class BitwiseLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 1; } @Override @@ -102,12 +93,10 @@ public class BitwiseLogic extends FunctionSet { } } - public static class Xor extends Function { - private int arity = 2; - + public static class Xor extends Function { @Override public Object run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { int arg1 = ((int) connections[0].getValue()); @@ -120,7 +109,7 @@ public class BitwiseLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override @@ -129,12 +118,10 @@ public class BitwiseLogic extends FunctionSet { } } - public static class Nand extends Function { - private int arity = 2; - + public static class Nand extends Function { @Override public Object run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { int arg1 = ((int) connections[0].getValue()); @@ -147,7 +134,7 @@ public class BitwiseLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override @@ -156,12 +143,10 @@ public class BitwiseLogic extends FunctionSet { } } - public static class Nor extends Function { - private int arity = 2; - + public static class Nor extends Function { @Override public Object run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { int arg1 = ((int) connections[0].getValue()); @@ -174,7 +159,7 @@ public class BitwiseLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override @@ -183,12 +168,10 @@ public class BitwiseLogic extends FunctionSet { } } - public static class Xnor extends Function { - private int arity = 2; - + public static class Xnor extends Function { @Override public Object run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { int arg1 = ((int) connections[0].getValue()); @@ -201,7 +184,7 @@ public class BitwiseLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override diff --git a/src/jcgp/backend/function/BooleanLogic.java b/src/jcgp/backend/function/BooleanLogic.java index e0b5c9c..9e7d1ff 100644 --- a/src/jcgp/backend/function/BooleanLogic.java +++ b/src/jcgp/backend/function/BooleanLogic.java @@ -1,7 +1,5 @@ package jcgp.backend.function; -import java.util.ArrayList; -import java.util.Arrays; import jcgp.backend.exceptions.InvalidArgumentsException; import jcgp.backend.population.Connection; @@ -19,15 +17,13 @@ public class BooleanLogic extends FunctionSet { new Nor(), new Xnor()}; - allowedFunctions = new ArrayList(Arrays.asList(functionList)); + enableAll(); } - public static class And extends Function { - private int arity = 2; - + public static class And extends Function { @Override public Boolean run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); @@ -40,7 +36,7 @@ public class BooleanLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override @@ -49,12 +45,10 @@ public class BooleanLogic extends FunctionSet { } } - public static class Or extends Function { - private int arity = 2; - + public static class Or extends Function { @Override public Boolean run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); @@ -67,7 +61,7 @@ public class BooleanLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override @@ -76,12 +70,10 @@ public class BooleanLogic extends FunctionSet { } } - public static class Not extends Function { - private int arity = 1; - + public static class Not extends Function { @Override public Boolean run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 1) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); @@ -93,7 +85,7 @@ public class BooleanLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 1; } @Override @@ -102,12 +94,10 @@ public class BooleanLogic extends FunctionSet { } } - public static class Xor extends Function { - private int arity = 2; - + public static class Xor extends Function { @Override public Boolean run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); @@ -120,7 +110,7 @@ public class BooleanLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override @@ -129,12 +119,10 @@ public class BooleanLogic extends FunctionSet { } } - public static class Nand extends Function { - private int arity = 2; - + public static class Nand extends Function { @Override public Boolean run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); @@ -147,7 +135,7 @@ public class BooleanLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override @@ -156,12 +144,10 @@ public class BooleanLogic extends FunctionSet { } } - public static class Nor extends Function { - private int arity = 2; - + public static class Nor extends Function { @Override public Boolean run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); @@ -174,7 +160,7 @@ public class BooleanLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override @@ -183,12 +169,10 @@ public class BooleanLogic extends FunctionSet { } } - public static class Xnor extends Function { - private int arity = 2; - + public static class Xnor extends Function { @Override public Boolean run(Connection... connections) { - if (connections.length < arity) { + if (connections.length < 2) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); @@ -201,7 +185,7 @@ public class BooleanLogic extends FunctionSet { @Override public int getArity() { - return arity; + return 2; } @Override diff --git a/src/jcgp/backend/function/FunctionSet.java b/src/jcgp/backend/function/FunctionSet.java index 4470ac8..78801fc 100644 --- a/src/jcgp/backend/function/FunctionSet.java +++ b/src/jcgp/backend/function/FunctionSet.java @@ -1,6 +1,7 @@ package jcgp.backend.function; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; @@ -11,7 +12,7 @@ import java.util.Iterator; */ public abstract class FunctionSet { protected Function[] functionList; - protected ArrayList allowedFunctions; + protected ArrayList allowedFunctions; protected String name; public int getAllowedFunctionCount() { @@ -23,7 +24,7 @@ public abstract class FunctionSet { } public Function getAllowedFunction(int index) { - return allowedFunctions.get(index); + return functionList[allowedFunctions.get(index)]; } public Function getFunction(int index) { @@ -32,7 +33,7 @@ public abstract class FunctionSet { public int getMaxArity(){ int arity = 0; - for (Function function : allowedFunctions) { + for (Function function : functionList) { if (function.getArity() > arity) { arity = function.getArity(); } @@ -46,9 +47,9 @@ public abstract class FunctionSet { public void disableFunction(int index) { if (index < functionList.length) { - for (Iterator iterator = allowedFunctions.iterator(); iterator.hasNext();) { - Function function = (Function) iterator.next(); - if (function == functionList[index]) { + for (Iterator iterator = allowedFunctions.iterator(); iterator.hasNext();) { + int function = iterator.next(); + if (function == index) { iterator.remove(); } } @@ -58,8 +59,9 @@ public abstract class FunctionSet { } public void enableFunction(int index) { - if (!allowedFunctions.contains(functionList[index])) { - allowedFunctions.add(functionList[index]); + if (!allowedFunctions.contains(index)) { + allowedFunctions.add(index); + Collections.sort(allowedFunctions); } } @@ -69,6 +71,18 @@ public abstract class FunctionSet { } public boolean isEnabled(Function f) { - return allowedFunctions.contains(f); + for (int i = 0; i < allowedFunctions.size(); i++) { + if (functionList[allowedFunctions.get(i)] == f) { + return true; + } + } + return false; + } + + protected void enableAll() { + allowedFunctions = new ArrayList(); + for (int i = 0; i < functionList.length; i++) { + allowedFunctions.add(i); + } } } \ No newline at end of file diff --git a/src/jcgp/backend/function/IntegerArithmetic.java b/src/jcgp/backend/function/IntegerArithmetic.java new file mode 100644 index 0000000..c08a72a --- /dev/null +++ b/src/jcgp/backend/function/IntegerArithmetic.java @@ -0,0 +1,124 @@ +package jcgp.backend.function; + +import jcgp.backend.exceptions.InvalidArgumentsException; +import jcgp.backend.population.Connection; + +public class IntegerArithmetic extends FunctionSet { + + public IntegerArithmetic() { + name = "Integer Arithmetic"; + functionList = new Function[]{ + new Addition(), + new Subtraction(), + new Multiplication(), + new Division()}; + + enableAll(); + } + + public static class Addition extends Function { + @Override + public Integer run(Connection... connections) { + if (connections.length < 2) { + 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 2; + } + + @Override + public String getName() { + return "Addition"; + } + } + + public static class Subtraction extends Function { + @Override + public Integer run(Connection... connections) { + if (connections.length < 2) { + 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 2; + } + + @Override + public String getName() { + return "Subtraction"; + } + } + + public static class Multiplication extends Function { + @Override + public Integer run(Connection... connections) { + if (connections.length < 2) { + 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 2; + } + + @Override + public String getName() { + return "Multiplication"; + } + } + + public static class Division extends Function { + @Override + public Integer run(Connection... connections) { + if (connections.length < 2) { + 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 2; + } + + @Override + public String getName() { + return "Division"; + } + } + +} -- cgit v1.2.3