diff options
Diffstat (limited to 'src/jcgp/function')
-rw-r--r-- | src/jcgp/function/Addition.java | 31 | ||||
-rw-r--r-- | src/jcgp/function/Arithmetic.java | 123 | ||||
-rw-r--r-- | src/jcgp/function/BitwiseLogic.java | 178 | ||||
-rw-r--r-- | src/jcgp/function/BooleanLogic.java | 178 | ||||
-rw-r--r-- | src/jcgp/function/Multiplication.java | 31 | ||||
-rw-r--r-- | src/jcgp/function/Subtraction.java | 31 |
6 files changed, 479 insertions, 93 deletions
diff --git a/src/jcgp/function/Addition.java b/src/jcgp/function/Addition.java deleted file mode 100644 index 3a8f123..0000000 --- a/src/jcgp/function/Addition.java +++ /dev/null @@ -1,31 +0,0 @@ -package jcgp.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class Addition 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 if (connections[0].getValue() instanceof Integer) { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 + arg2; - - System.out.println(arg1 + " + " + arg2 + " = " + result); - return result; - } else { - throw new InvalidArgumentsException("Wrong data type, this function takes Integer."); - } - } - - @Override - public int getArity() { - return arity; - } - -} diff --git a/src/jcgp/function/Arithmetic.java b/src/jcgp/function/Arithmetic.java new file mode 100644 index 0000000..b0bd5ca --- /dev/null +++ b/src/jcgp/function/Arithmetic.java @@ -0,0 +1,123 @@ +package jcgp.function; + +import jcgp.Parameters; +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; + + if (Parameters.getDebug()) { + System.out.println(arg1 + " + " + arg2 + " = " + result); + } + 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; + + if (Parameters.getDebug()) { + System.out.println(arg1 + " - " + arg2 + " = " + result); + } + 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; + + if (Parameters.getDebug()) { + System.out.println(arg1 + " * " + arg2 + " = " + result); + } + + + 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; + } + + + if (Parameters.getDebug()) { + System.out.println(arg1 + " / " + arg2 + " = " + result); + } + + + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + +} diff --git a/src/jcgp/function/BitwiseLogic.java b/src/jcgp/function/BitwiseLogic.java new file mode 100644 index 0000000..55f5df0 --- /dev/null +++ b/src/jcgp/function/BitwiseLogic.java @@ -0,0 +1,178 @@ +package jcgp.function; + +import jcgp.Parameters; +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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " AND " + arg2 + " = " + result); + } + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " OR " + arg2 + " = " + result); + } + 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; + if (Parameters.getDebug()) { + System.out.println("NOT " + arg1 + " = " + result); + } + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " XOR " + arg2 + " = " + result); + } + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " NAND " + arg2 + " = " + ~result); + } + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " NOR " + arg2 + " = " + ~result); + } + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " XNOR " + arg2 + " = " + ~result); + } + return ~result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + + +} diff --git a/src/jcgp/function/BooleanLogic.java b/src/jcgp/function/BooleanLogic.java new file mode 100644 index 0000000..713dcf8 --- /dev/null +++ b/src/jcgp/function/BooleanLogic.java @@ -0,0 +1,178 @@ +package jcgp.function; + +import jcgp.Parameters; +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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " AND " + arg2 + " = " + result); + } + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " OR " + arg2 + " = " + result); + } + 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; + if (Parameters.getDebug()) { + System.out.println("NOT " + arg1 + " = " + result); + } + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " XOR " + arg2 + " = " + result); + } + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " NAND " + arg2 + " = " + !result); + } + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " NOR " + arg2 + " = " + !result); + } + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " XNOR " + arg2 + " = " + !result); + } + return !result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + + +} diff --git a/src/jcgp/function/Multiplication.java b/src/jcgp/function/Multiplication.java deleted file mode 100644 index 986faa8..0000000 --- a/src/jcgp/function/Multiplication.java +++ /dev/null @@ -1,31 +0,0 @@ -package jcgp.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class Multiplication 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 if (connections[0].getValue() instanceof Integer) { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 * arg2; - - System.out.println(arg1 + " * " + arg2 + " = " + result); - return result; - } else { - throw new InvalidArgumentsException("Wrong data type, this function takes Integer."); - } - } - - @Override - public int getArity() { - return arity; - } - -} diff --git a/src/jcgp/function/Subtraction.java b/src/jcgp/function/Subtraction.java deleted file mode 100644 index cfbb907..0000000 --- a/src/jcgp/function/Subtraction.java +++ /dev/null @@ -1,31 +0,0 @@ -package jcgp.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class Subtraction 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 if (connections[0].getValue() instanceof Integer) { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 - arg2; - - System.out.println(arg1 + " - " + arg2 + " = " + result); - return result; - } else { - throw new InvalidArgumentsException("Wrong data type, this function takes Integer."); - } - } - - @Override - public int getArity() { - return arity; - } - -} |