diff options
Diffstat (limited to 'src/jcgp/function')
-rw-r--r-- | src/jcgp/function/Arithmetic.java | 139 | ||||
-rw-r--r-- | src/jcgp/function/BitwiseLogic.java | 215 | ||||
-rw-r--r-- | src/jcgp/function/BooleanLogic.java | 215 | ||||
-rw-r--r-- | src/jcgp/function/Function.java | 13 | ||||
-rw-r--r-- | src/jcgp/function/FunctionSet.java | 74 |
5 files changed, 0 insertions, 656 deletions
diff --git a/src/jcgp/function/Arithmetic.java b/src/jcgp/function/Arithmetic.java deleted file mode 100644 index b61ee65..0000000 --- a/src/jcgp/function/Arithmetic.java +++ /dev/null @@ -1,139 +0,0 @@ -package jcgp.function; - -import java.util.ArrayList; -import java.util.Arrays; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.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<Function>(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/function/BitwiseLogic.java b/src/jcgp/function/BitwiseLogic.java deleted file mode 100644 index 7ced8e8..0000000 --- a/src/jcgp/function/BitwiseLogic.java +++ /dev/null @@ -1,215 +0,0 @@ -package jcgp.function; - -import java.util.ArrayList; -import java.util.Arrays; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class BitwiseLogic extends FunctionSet { - - public BitwiseLogic() { - name = "32-bit Logic"; - functionList = new Function[]{ - new And(), - new Or(), - new Not(), - new Xor(), - new Nand(), - new Nor(), - new Xnor()}; - - allowedFunctions = new ArrayList<Function>(Arrays.asList(functionList)); - } - - 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; - } - - @Override - public String getName() { - return "AND"; - } - } - - 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; - } - - @Override - public String getName() { - return "OR"; - } - } - - 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; - } - - @Override - public String getName() { - return "NOT"; - } - } - - 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; - } - - @Override - public String getName() { - return "XOR"; - } - } - - 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; - } - - @Override - public String getName() { - return "NAND"; - } - } - - 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; - } - - @Override - public String getName() { - return "NOR"; - } - } - - 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; - } - - @Override - public String getName() { - return "XNOR"; - } - } - - - -} diff --git a/src/jcgp/function/BooleanLogic.java b/src/jcgp/function/BooleanLogic.java deleted file mode 100644 index 7e441f2..0000000 --- a/src/jcgp/function/BooleanLogic.java +++ /dev/null @@ -1,215 +0,0 @@ -package jcgp.function; - -import java.util.ArrayList; -import java.util.Arrays; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class BooleanLogic extends FunctionSet { - - public BooleanLogic() { - name = "1-bit Logic"; - functionList = new Function[]{ - new And(), - new Or(), - new Not(), - new Xor(), - new Nand(), - new Nor(), - new Xnor()}; - - allowedFunctions = new ArrayList<Function>(Arrays.asList(functionList)); - } - - 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; - } - - @Override - public String getName() { - return "AND"; - } - } - - 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; - } - - @Override - public String getName() { - return "OR"; - } - } - - 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; - } - - @Override - public String getName() { - return "NOT"; - } - } - - 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; - } - - @Override - public String getName() { - return "XOR"; - } - } - - 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; - } - - @Override - public String getName() { - return "NAND"; - } - } - - 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; - } - - @Override - public String getName() { - return "NOR"; - } - } - - 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; - } - - @Override - public String getName() { - return "XNOR"; - } - } - - - -} diff --git a/src/jcgp/function/Function.java b/src/jcgp/function/Function.java deleted file mode 100644 index 64dd206..0000000 --- a/src/jcgp/function/Function.java +++ /dev/null @@ -1,13 +0,0 @@ -package jcgp.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(); - - public abstract String getName(); -} diff --git a/src/jcgp/function/FunctionSet.java b/src/jcgp/function/FunctionSet.java deleted file mode 100644 index fef8f88..0000000 --- a/src/jcgp/function/FunctionSet.java +++ /dev/null @@ -1,74 +0,0 @@ -package jcgp.function; - -import java.util.ArrayList; -import java.util.Iterator; - - -/** - * - * @author Eduardo Pedroni - * - */ -public abstract class FunctionSet { - protected Function[] functionList; - protected ArrayList<Function> allowedFunctions; - protected String name; - - public int getAllowedFunctionCount() { - return allowedFunctions.size(); - } - - public int getTotalFunctionCount() { - return functionList.length; - } - - public Function getAllowedFunction(int index) { - return allowedFunctions.get(index); - } - - public Function getFunction(int index) { - return functionList[index]; - } - - public int getMaxArity(){ - int arity = 0; - for (Function function : allowedFunctions) { - if (function.getArity() > arity) { - arity = function.getArity(); - } - } - return arity; - } - - public String getName() { - return name; - } - - public void disableFunction(int index) { - if (index < functionList.length) { - for (Iterator<Function> iterator = allowedFunctions.iterator(); iterator.hasNext();) { - Function function = (Function) iterator.next(); - if (function == functionList[index]) { - iterator.remove(); - } - } - } else { - throw new IndexOutOfBoundsException("Function " + index + " does not exist, the set only has " + functionList.length + " functions."); - } - } - - public void enableFunction(int index) { - if (!allowedFunctions.contains(functionList[index])) { - allowedFunctions.add(functionList[index]); - } - } - - @Override - public String toString() { - return name; - } - - public boolean isEnabled(Function f) { - return allowedFunctions.contains(f); - } - }
\ No newline at end of file |