diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-15 21:43:35 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-15 21:45:21 +0000 |
commit | e9290a500b228e2561a8537adbc13e86f1356747 (patch) | |
tree | 5e76280ddefc5bf9dcf9cdb55e80ca4831998115 /src/jcgp/function | |
parent | afa484021ba94d12e98da682a9ff69c3837d5dbb (diff) |
Population and Chromosome classes refactored so that EAs are easier to implement.
Diffstat (limited to 'src/jcgp/function')
-rw-r--r-- | src/jcgp/function/Addition.java | 8 | ||||
-rw-r--r-- | src/jcgp/function/Function.java | 4 | ||||
-rw-r--r-- | src/jcgp/function/InvalidArgumentsException.java | 20 | ||||
-rw-r--r-- | src/jcgp/function/Multiplication.java | 31 | ||||
-rw-r--r-- | src/jcgp/function/Subtraction.java | 8 |
5 files changed, 47 insertions, 24 deletions
diff --git a/src/jcgp/function/Addition.java b/src/jcgp/function/Addition.java index f40bc24..3a8f123 100644 --- a/src/jcgp/function/Addition.java +++ b/src/jcgp/function/Addition.java @@ -1,5 +1,6 @@ package jcgp.function; +import jcgp.exceptions.InvalidArgumentsException; import jcgp.population.Connection; public class Addition extends Function { @@ -11,7 +12,12 @@ public class Addition extends Function { if (connections.length < arity) { throw new InvalidArgumentsException("Not enough connections were given."); } else if (connections[0].getValue() instanceof Integer) { - return ((Integer) connections[0].getValue()) + ((Integer) connections[1].getValue()); + 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."); } diff --git a/src/jcgp/function/Function.java b/src/jcgp/function/Function.java index 118131a..584421a 100644 --- a/src/jcgp/function/Function.java +++ b/src/jcgp/function/Function.java @@ -1,11 +1,11 @@ package jcgp.function; +import jcgp.exceptions.InvalidArgumentsException; import jcgp.population.Connection; -import jcgp.population.InsufficientConnectionsException; public abstract class Function { - public abstract Object run(Connection ... connections) throws InsufficientConnectionsException; + public abstract Object run(Connection ... connections) throws InvalidArgumentsException; public abstract int getArity(); diff --git a/src/jcgp/function/InvalidArgumentsException.java b/src/jcgp/function/InvalidArgumentsException.java deleted file mode 100644 index cf55937..0000000 --- a/src/jcgp/function/InvalidArgumentsException.java +++ /dev/null @@ -1,20 +0,0 @@ -package jcgp.function; - -public class InvalidArgumentsException extends RuntimeException { - - /** - * - */ - private static final long serialVersionUID = 2675108124600817777L; - private String reason; - - public InvalidArgumentsException(String reason) { - super(); - this.reason = reason; - } - - public String getReason() { - return reason; - } - -} diff --git a/src/jcgp/function/Multiplication.java b/src/jcgp/function/Multiplication.java new file mode 100644 index 0000000..986faa8 --- /dev/null +++ b/src/jcgp/function/Multiplication.java @@ -0,0 +1,31 @@ +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 index d785614..cfbb907 100644 --- a/src/jcgp/function/Subtraction.java +++ b/src/jcgp/function/Subtraction.java @@ -1,5 +1,6 @@ package jcgp.function; +import jcgp.exceptions.InvalidArgumentsException; import jcgp.population.Connection; public class Subtraction extends Function { @@ -11,7 +12,12 @@ public class Subtraction extends Function { if (connections.length < arity) { throw new InvalidArgumentsException("Not enough connections were given."); } else if (connections[0].getValue() instanceof Integer) { - return ((Integer) connections[0].getValue()) - ((Integer) connections[1].getValue()); + 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."); } |