diff options
Diffstat (limited to 'src/jcgp/function')
-rw-r--r-- | src/jcgp/function/Addition.java | 13 | ||||
-rw-r--r-- | src/jcgp/function/Function.java | 4 | ||||
-rw-r--r-- | src/jcgp/function/FunctionSet.java | 22 | ||||
-rw-r--r-- | src/jcgp/function/Subtraction.java | 11 |
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; } } |