aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/function
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-01-31 16:45:45 +0000
committerEduardo Pedroni <ep625@york.ac.uk>2014-01-31 16:45:45 +0000
commit2343cc0e456e0306711c0a7218d3027f17cffee7 (patch)
treeefb26576ff4a283a87bd4b56deb9aba175924cfe /src/jcgp/function
parenta02f1fff03ab58416da812597e67a0c7e21fdbd5 (diff)
Added lots of utility methods for initialisation and mutation; the foundation is laid down and probably works, now it's time to test it and implement the standard CGP modules.
Diffstat (limited to 'src/jcgp/function')
-rw-r--r--src/jcgp/function/Addition.java13
-rw-r--r--src/jcgp/function/Function.java4
-rw-r--r--src/jcgp/function/FunctionSet.java22
-rw-r--r--src/jcgp/function/Subtraction.java11
4 files changed, 33 insertions, 17 deletions
diff --git a/src/jcgp/function/Addition.java b/src/jcgp/function/Addition.java
index 7dc17e2..8c1e0b5 100644
--- a/src/jcgp/function/Addition.java
+++ b/src/jcgp/function/Addition.java
@@ -6,13 +6,16 @@ public class Addition extends Function {
@Override
public int run(Connection... connections) {
- int sum = 0;
if (connections.length > 0) {
- for (int i = 0; i < connections.length; i++) {
- sum += connections[i].evaluate();
- }
+ return connections[0].evaluate() + connections[1].evaluate();
+ } else {
+ return 0;
}
- return sum;
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
}
}
diff --git a/src/jcgp/function/Function.java b/src/jcgp/function/Function.java
index 0f0d8a3..f2d1125 100644
--- a/src/jcgp/function/Function.java
+++ b/src/jcgp/function/Function.java
@@ -3,7 +3,9 @@ package jcgp.function;
import jcgp.population.Connection;
public abstract class Function {
-
+
public abstract int run(Connection ... connections);
+ public abstract int getArity();
+
}
diff --git a/src/jcgp/function/FunctionSet.java b/src/jcgp/function/FunctionSet.java
index e2d04a0..30e1067 100644
--- a/src/jcgp/function/FunctionSet.java
+++ b/src/jcgp/function/FunctionSet.java
@@ -6,7 +6,7 @@ import java.util.ArrayList;
public class FunctionSet {
private ArrayList<Function> functionList;
- public void setFunctions(Function ... functions) {
+ public FunctionSet(Function ... functions) {
functionList = new ArrayList<Function>(functions.length);
for (int i = 0; i < functions.length; i++) {
@@ -14,13 +14,6 @@ public class FunctionSet {
}
}
- public void addFunction(Function newFunction) {
- if (functionList == null) {
- functionList = new ArrayList<Function>();
- }
- functionList.add(newFunction);
- }
-
public int getFunctionCount() {
return functionList.size();
}
@@ -28,4 +21,17 @@ public class FunctionSet {
public Function getFunction(int index) {
return functionList.get(index);
}
+
+ public int getMaxArity(){
+
+ int maxArity = 0;
+
+ for (Function function : functionList) {
+ if (function.getArity() > maxArity) {
+ maxArity = function.getArity();
+ }
+ }
+
+ return maxArity;
+ }
} \ No newline at end of file
diff --git a/src/jcgp/function/Subtraction.java b/src/jcgp/function/Subtraction.java
index 70297c3..169f88c 100644
--- a/src/jcgp/function/Subtraction.java
+++ b/src/jcgp/function/Subtraction.java
@@ -6,11 +6,16 @@ public class Subtraction extends Function {
@Override
public int run(Connection... connections) {
- int subtraction = 0;
if (connections.length > 1) {
- subtraction = connections[0].evaluate() - connections[1].evaluate();
+ return connections[0].evaluate() - connections[1].evaluate();
+ } else {
+ return 0;
}
- return subtraction;
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
}
}