aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/function
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-02-14 18:13:21 +0000
committerEduardo Pedroni <ep625@york.ac.uk>2014-02-14 18:13:21 +0000
commitafa484021ba94d12e98da682a9ff69c3837d5dbb (patch)
tree2150c437cb74ccf141f8cfde228ebf5dd442314a /src/jcgp/function
parent6419b69faeb4736db1ccb50cfa5a030f9aa818aa (diff)
Generic data type functionality implemented. All tests were refactored to reflect this, and some chromosome tests were rewritten with more rigorous assertions.
Diffstat (limited to 'src/jcgp/function')
-rw-r--r--src/jcgp/function/Addition.java14
-rw-r--r--src/jcgp/function/Function.java4
-rw-r--r--src/jcgp/function/InsufficientArgumentsException.java10
-rw-r--r--src/jcgp/function/InvalidArgumentsException.java20
-rw-r--r--src/jcgp/function/Subtraction.java14
5 files changed, 40 insertions, 22 deletions
diff --git a/src/jcgp/function/Addition.java b/src/jcgp/function/Addition.java
index 45a8d35..f40bc24 100644
--- a/src/jcgp/function/Addition.java
+++ b/src/jcgp/function/Addition.java
@@ -3,19 +3,23 @@ package jcgp.function;
import jcgp.population.Connection;
public class Addition extends Function {
+
+ private int arity = 2;
@Override
- public int run(Connection... connections) {
- if (connections.length > 0) {
- return connections[0].getValue() + connections[1].getValue();
+ public Object run(Connection... connections) {
+ 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());
} else {
- throw new InsufficientArgumentsException();
+ throw new InvalidArgumentsException("Wrong data type, this function takes Integer.");
}
}
@Override
public int getArity() {
- return 2;
+ return arity;
}
}
diff --git a/src/jcgp/function/Function.java b/src/jcgp/function/Function.java
index 4900c68..118131a 100644
--- a/src/jcgp/function/Function.java
+++ b/src/jcgp/function/Function.java
@@ -4,8 +4,8 @@ import jcgp.population.Connection;
import jcgp.population.InsufficientConnectionsException;
public abstract class Function {
-
- public abstract int run(Connection ... connections) throws InsufficientConnectionsException;
+
+ public abstract Object run(Connection ... connections) throws InsufficientConnectionsException;
public abstract int getArity();
diff --git a/src/jcgp/function/InsufficientArgumentsException.java b/src/jcgp/function/InsufficientArgumentsException.java
deleted file mode 100644
index a591b20..0000000
--- a/src/jcgp/function/InsufficientArgumentsException.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package jcgp.function;
-
-public class InsufficientArgumentsException extends RuntimeException {
-
- /**
- *
- */
- private static final long serialVersionUID = 2675108124600817777L;
-
-}
diff --git a/src/jcgp/function/InvalidArgumentsException.java b/src/jcgp/function/InvalidArgumentsException.java
new file mode 100644
index 0000000..cf55937
--- /dev/null
+++ b/src/jcgp/function/InvalidArgumentsException.java
@@ -0,0 +1,20 @@
+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/Subtraction.java b/src/jcgp/function/Subtraction.java
index 8f107b1..d785614 100644
--- a/src/jcgp/function/Subtraction.java
+++ b/src/jcgp/function/Subtraction.java
@@ -4,18 +4,22 @@ import jcgp.population.Connection;
public class Subtraction extends Function {
+ private int arity = 2;
+
@Override
- public int run(Connection... connections) {
- if (connections.length > 1) {
- return connections[0].getValue() - connections[1].getValue();
+ public Object run(Connection... connections) {
+ 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());
} else {
- throw new InsufficientArgumentsException();
+ throw new InvalidArgumentsException("Wrong data type, this function takes Integer.");
}
}
@Override
public int getArity() {
- return 2;
+ return arity;
}
}