From 88314e71f908efcfc38da3b800319c171a6ccceb Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Fri, 18 Apr 2014 09:08:41 +0100 Subject: Added parsers, did a bit of testing, switched to java8 --- src/jcgp/backend/function/BitwiseLogic.java | 337 ++++++++++--- src/jcgp/backend/function/DoubleArithmetic.java | 589 +++++++++++++++++++++++ src/jcgp/backend/function/FunctionSet.java | 1 + src/jcgp/backend/function/IntegerArithmetic.java | 122 ----- 4 files changed, 859 insertions(+), 190 deletions(-) create mode 100644 src/jcgp/backend/function/DoubleArithmetic.java delete mode 100644 src/jcgp/backend/function/IntegerArithmetic.java (limited to 'src/jcgp/backend/function') diff --git a/src/jcgp/backend/function/BitwiseLogic.java b/src/jcgp/backend/function/BitwiseLogic.java index 5d47ff7..14f4488 100644 --- a/src/jcgp/backend/function/BitwiseLogic.java +++ b/src/jcgp/backend/function/BitwiseLogic.java @@ -1,6 +1,5 @@ package jcgp.backend.function; -import jcgp.backend.exceptions.InvalidArgumentsException; import jcgp.backend.population.Connection; public class BitwiseLogic extends FunctionSet { @@ -8,26 +7,155 @@ public class BitwiseLogic extends FunctionSet { public BitwiseLogic() { name = "32-bit Logic"; functionList = new Function[]{ + new ConstantZero(), + new ConstantOne(), + new WireA(), + new WireB(), + new NotA(), + new NotB(), new And(), - new Or(), - new Not(), - new Xor(), - new Nand(), + new AndNotA(), + new AndNotB(), new Nor(), - new Xnor()}; + new Xor(), + new Xnor(), + new Or(), + new OrNotA(), + new OrNotB(), + new Nand()}; + + // TODO muxes enableAll(); } + + public static class ConstantZero extends Function { + @Override + public Long run(Connection... connections) { + return new Long(0); + } + + @Override + public int getArity() { + return 0; + } + + @Override + public String getName() { + return "0"; + } + } + + public static class ConstantOne extends Function { + @Override + public Long run(Connection... connections) { + return new Long(Long.MAX_VALUE); + } + + @Override + public int getArity() { + return 0; + } + + @Override + public String getName() { + return "1"; + } + } + + public static class WireA extends Function { + @Override + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + return ((Long) connections[0].getValue()).longValue(); + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Wire A"; + } + } + + public static class WireB extends Function { + @Override + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + return ((Long) connections[1].getValue()).longValue(); + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Wire B"; + } + } + + public static class NotA extends Function { + @Override + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + return ~((Long) connections[0].getValue()).longValue(); + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Not A"; + } + } + + public static class NotB extends Function { + @Override + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + return ~((Long) connections[1].getValue()).longValue(); + } + } + + @Override + public int getArity() { + return 2; + } + @Override + public String getName() { + return "Not B"; + } + } + public static class And extends Function { @Override - public Object run(Connection... connections) { - if (connections.length < 2) { - throw new InvalidArgumentsException("Not enough connections were given."); + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 & arg2; + Long arg1 = ((Long) connections[0].getValue()); + Long arg2 = ((Long) connections[1].getValue()); + Long result = arg1 & arg2; return result; } @@ -40,20 +168,20 @@ public class BitwiseLogic extends FunctionSet { @Override public String getName() { - return "AND"; + return "And"; } } - public static class Or extends Function { + public static class AndNotA extends Function { @Override - public Object run(Connection... connections) { - if (connections.length < 2) { - throw new InvalidArgumentsException("Not enough connections were given."); + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 | arg2; - + Long arg1 = ((Long) connections[0].getValue()); + Long arg2 = ((Long) connections[1].getValue()); + Long result = ~arg1 & arg2; + return result; } } @@ -65,43 +193,69 @@ public class BitwiseLogic extends FunctionSet { @Override public String getName() { - return "OR"; + return "And !A"; } } - - public static class Not extends Function { + + public static class AndNotB extends Function { @Override - public Object run(Connection... connections) { - if (connections.length < 1) { - throw new InvalidArgumentsException("Not enough connections were given."); + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); } else { - int arg1 = ((int) connections[0].getValue()); - int result = ~arg1; - + Long arg1 = ((Long) connections[0].getValue()); + Long arg2 = ((Long) connections[1].getValue()); + Long result = arg1 & ~arg2; + return result; } } @Override public int getArity() { - return 1; + return 2; } @Override public String getName() { - return "NOT"; + return "And !B"; } } + + public static class Nor extends Function { + @Override + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Long arg1 = ((Long) connections[0].getValue()); + Long arg2 = ((Long) connections[1].getValue()); + Long result = arg1 | arg2; + return ~result; + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Nor"; + } + } + public static class Xor extends Function { @Override - public Object run(Connection... connections) { - if (connections.length < 2) { - throw new InvalidArgumentsException("Not enough connections were given."); + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 ^ arg2; + Long arg1 = ((Long) connections[0].getValue()); + Long arg2 = ((Long) connections[1].getValue()); + Long result = arg1 ^ arg2; return result; } @@ -114,19 +268,19 @@ public class BitwiseLogic extends FunctionSet { @Override public String getName() { - return "XOR"; + return "Xor"; } } - - public static class Nand extends Function { + + public static class Xnor extends Function { @Override - public Object run(Connection... connections) { - if (connections.length < 2) { - throw new InvalidArgumentsException("Not enough connections were given."); + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 & arg2; + Long arg1 = ((Long) connections[0].getValue()); + Long arg2 = ((Long) connections[1].getValue()); + Long result = arg1 ^ arg2; return ~result; } @@ -139,21 +293,71 @@ public class BitwiseLogic extends FunctionSet { @Override public String getName() { - return "NAND"; + return "Xnor"; } } + + public static class Or extends Function { + @Override + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Long arg1 = ((Long) connections[0].getValue()); + Long arg2 = ((Long) connections[1].getValue()); + Long result = arg1 | arg2; - public static class Nor extends Function { + return result; + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Or"; + } + } + + public static class OrNotA extends Function { @Override - public Object run(Connection... connections) { - if (connections.length < 2) { - throw new InvalidArgumentsException("Not enough connections were given."); + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 | arg2; + Long arg1 = ((Long) connections[0].getValue()); + Long arg2 = ((Long) connections[1].getValue()); + Long result = ~arg1 | arg2; - return ~result; + return result; + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Or !A"; + } + } + + public static class OrNotB extends Function { + @Override + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Long arg1 = ((Long) connections[0].getValue()); + Long arg2 = ((Long) connections[1].getValue()); + Long result = arg1 | ~arg2; + + return result; } } @@ -164,19 +368,19 @@ public class BitwiseLogic extends FunctionSet { @Override public String getName() { - return "NOR"; + return "Or !B"; } } - public static class Xnor extends Function { + public static class Nand extends Function { @Override - public Object run(Connection... connections) { - if (connections.length < 2) { - throw new InvalidArgumentsException("Not enough connections were given."); + public Long run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 ^ arg2; + Long arg1 = ((Long) connections[0].getValue()); + Long arg2 = ((Long) connections[1].getValue()); + Long result = arg1 & arg2; return ~result; } @@ -189,10 +393,7 @@ public class BitwiseLogic extends FunctionSet { @Override public String getName() { - return "XNOR"; + return "Nand"; } } - - - } diff --git a/src/jcgp/backend/function/DoubleArithmetic.java b/src/jcgp/backend/function/DoubleArithmetic.java new file mode 100644 index 0000000..ce4826f --- /dev/null +++ b/src/jcgp/backend/function/DoubleArithmetic.java @@ -0,0 +1,589 @@ +package jcgp.backend.function; + +import jcgp.backend.population.Connection; + +public class DoubleArithmetic extends FunctionSet { + + public final static double DIVISION_LIMIT = 0.0001; + + public DoubleArithmetic() { + name = "Double Arithmetic"; + functionList = new Function[]{ + new Absolute(), + new SquareRoot(), + new Reciprocal(), + new Sine(), + new Cosine(), + new Tangent(), + new Exponential(), + new HyperbolicSine(), + new HyperbolicCosine(), + new HyperbolicTangent(), + new NaturalLog(), + new LogBaseTen(), + new SineAB(), + new CosineAB(), + new Hypotenuse(), + new Power(), + new Addition(), + new Subtraction(), + new Multiplication(), + new Division()}; + + enableAll(); + } + + /** + * Absolute returns the positive value of input 0. + * + * @see Math.abs() + */ + public static class Absolute extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return Math.abs(in0); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Absolute"; + } + } + + /** + * Protected square root function, returns the square root of the absolute + * value of input 0. + * + * @see Math.abs(), Math.sqrt() + */ + public static class SquareRoot extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return Math.sqrt(Math.abs(in0)); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Square root"; + } + } + + /** + * Protected reciprocal function, returns (1 / input 0). If input 0 is less than + * {@link DoubleArithmetic.}DIVISION_LIMIT, this returns it unchanged. + * + */ + public static class Reciprocal extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return in0 < DIVISION_LIMIT ? in0 : (1 / in0); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Reciprocal"; + } + } + + /** + * Sine function, in radians. + * + * @see Math.sin() + */ + public static class Sine extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return Math.sin(in0); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Sin"; + } + } + + /** + * Cosine function, in radians. + * + * @see Math.cos() + */ + public static class Cosine extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return Math.cos(in0); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Cos"; + } + } + + /** + * Protected tangent function, in radians. Returns the tangent of input 0. + * If input 0 is less than {@link DoubleArithmetic.}DIVISION_LIMIT, + * this returns it unchanged. + * + * @see Math.tan() + */ + public static class Tangent extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return in0 < DIVISION_LIMIT ? in0 : Math.tan(in0); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Tan"; + } + } + + /** + * Exponential function. Returns the exponential of input 0. + * + * @see Math.exp() + */ + public static class Exponential extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return Math.exp(in0); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Exp"; + } + } + + /** + * Returns the hyperbolic sine of input 0. + * + * @see Math.sinh() + */ + public static class HyperbolicSine extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return Math.sinh(in0); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Sinh"; + } + } + + /** + * Returns the hyperbolic cosine of input 0. + * + * @see Math.cosh() + */ + public static class HyperbolicCosine extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return Math.cosh(in0); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Cosh"; + } + } + + /** + * Returns the hyperbolic tangent of input 0. + * + * @see Math.tanh() + */ + public static class HyperbolicTangent extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return Math.tanh(in0); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Tanh"; + } + } + + /** + * Protected natural log function. Returns the natural log of the absolute + * value of input 0. If input 0 is less than {@link DoubleArithmetic.}DIVISION_LIMIT, + * this returns it unchanged. + * + * @see Math.log(), Math.abs() + */ + public static class NaturalLog extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return in0 < DIVISION_LIMIT ? in0 : Math.log(Math.abs(in0)); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Ln"; + } + } + + /** + * Protected log base 10 function. Returns the log to base 10 the absolute + * value of input 0. If input 0 is less than {@link DoubleArithmetic.}DIVISION_LIMIT, + * this returns it unchanged. + * + * @see Math.log10(), Math.abs() + */ + public static class LogBaseTen extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + return in0 < DIVISION_LIMIT ? in0 : Math.log10(Math.abs(in0)); + } + } + + @Override + public int getArity() { + return 1; + } + + @Override + public String getName() { + return "Log"; + } + } + + /** + * Sine of sum. Returns the sine of the sum of inputs 0 and 1. + * + * @see Math.sin() + */ + public static class SineAB extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + Double in1 = ((Double) connections[1].getValue()); + return Math.sin(in0 + in1); + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Sin(a+b)"; + } + } + + /** + * Cosine of sum. Returns the cosine of the sum of inputs 0 and 1. + * + * @see Math.cos() + */ + public static class CosineAB extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + Double in1 = ((Double) connections[1].getValue()); + return Math.cos(in0 + in1); + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Cos(a+b)"; + } + } + + /** + * Hypotenuse function. Returns the square root of input 0 squared + * plus input 1 squared. + * + * @see Math.hypot() + */ + public static class Hypotenuse extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + Double in1 = ((Double) connections[1].getValue()); + return Math.hypot(in0, in1); + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Hypotenuse"; + } + } + + /** + * Power function. Returns the absolute value of input 0 to the power of input 1. + * + * @see Math.abs(), Math.pow + */ + public static class Power extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + Double in1 = ((Double) connections[1].getValue()); + return Math.pow(Math.abs(in0), in1); + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Power"; + } + } + + /** + * Addition returns the sum of inputs 0 and 1. + * + */ + public static class Addition extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + Double in1 = ((Double) connections[1].getValue()); + return in0 + in1; + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Addition"; + } + } + + /** + * Subtraction returns the difference between inputs 0 and 1. + * + */ + public static class Subtraction extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + Double in1 = ((Double) connections[1].getValue()); + return in0 - in1; + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Subtraction"; + } + } + + /** + * Multiplication returns the product of inputs 0 and 1. + * + */ + public static class Multiplication extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + Double in1 = ((Double) connections[1].getValue()); + return in0 * in1; + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Multiplication"; + } + } + + /** + * Protected division, returns the quotient of input 0 (the dividend) and input 1 (the divisor). + * If the divisor is less than {@link DoubleArithmetic.}DIVISION_LIMIT, this returns the it unchanged. + * + */ + public static class Division extends Function { + @Override + public Double run(Connection... connections) { + if (connections.length < getArity()) { + throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + "."); + } else { + Double in0 = ((Double) connections[0].getValue()); + Double in1 = ((Double) connections[1].getValue()); + + return in1 < DIVISION_LIMIT ? in0 : (in0 / in1); + } + } + + @Override + public int getArity() { + return 2; + } + + @Override + public String getName() { + return "Division"; + } + } +} diff --git a/src/jcgp/backend/function/FunctionSet.java b/src/jcgp/backend/function/FunctionSet.java index 78801fc..1712b51 100644 --- a/src/jcgp/backend/function/FunctionSet.java +++ b/src/jcgp/backend/function/FunctionSet.java @@ -51,6 +51,7 @@ public abstract class FunctionSet { int function = iterator.next(); if (function == index) { iterator.remove(); + break; } } } else { diff --git a/src/jcgp/backend/function/IntegerArithmetic.java b/src/jcgp/backend/function/IntegerArithmetic.java deleted file mode 100644 index 9bb02b5..0000000 --- a/src/jcgp/backend/function/IntegerArithmetic.java +++ /dev/null @@ -1,122 +0,0 @@ -package jcgp.backend.function; - -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 IllegalArgumentException("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 IllegalArgumentException("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 IllegalArgumentException("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 IllegalArgumentException("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