diff options
Diffstat (limited to 'src/jcgp/backend/function/FunctionSet.java')
-rw-r--r-- | src/jcgp/backend/function/FunctionSet.java | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/src/jcgp/backend/function/FunctionSet.java b/src/jcgp/backend/function/FunctionSet.java index 926ed68..052183a 100644 --- a/src/jcgp/backend/function/FunctionSet.java +++ b/src/jcgp/backend/function/FunctionSet.java @@ -4,9 +4,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; - /** - * FunctionSet encapsulates a group of functions. This is usually done to + * FunctionSet encapsulates a group of functions. This is done to * simplify the implementation of problem types. * <br><br> * FunctionSet contains a variety of useful methods for acquiring general @@ -18,18 +17,16 @@ import java.util.Iterator; * allowed functions only, providing an easy way to control which functions * can be used in mutations. * <br><br> - * An implementation of FunctionSet must simply set the name field and initialise - * the functionList array with all of the functions. It is advisable to call - * {@code enableAll()} to enable all functions once the array is initialised. - * + * An implementation of FunctionSet must simply use its constructor to set + * the name field and use {@code registerFunctions()} to add the required + * functions. * * @author Eduardo Pedroni * */ public abstract class FunctionSet { - protected Function[] functionList; - protected ArrayList<Integer> allowedFunctions; - protected String name; + private ArrayList<Function> functionList = new ArrayList<Function>(); + private ArrayList<Integer> allowedFunctions = new ArrayList<Integer>(); /** * @return the number of currently allowed functions. @@ -42,7 +39,7 @@ public abstract class FunctionSet { * @return the total number of functions, including disabled ones. */ public int getTotalFunctionCount() { - return functionList.length; + return functionList.size(); } /** @@ -54,7 +51,7 @@ public abstract class FunctionSet { * @return the allowed function object. */ public Function getAllowedFunction(int index) { - return functionList[allowedFunctions.get(index)]; + return functionList.get(allowedFunctions.get(index)); } /** @@ -67,7 +64,7 @@ public abstract class FunctionSet { * @return the function object. */ public Function getFunction(int index) { - return functionList[index]; + return functionList.get(index); } /** @@ -100,7 +97,7 @@ public abstract class FunctionSet { * list of allowed functions and removes any elements which are equal * to the specified index. */ - if (index < functionList.length) { + if (index < functionList.size()) { for (Iterator<Integer> iterator = allowedFunctions.iterator(); iterator.hasNext();) { int function = iterator.next(); if (function == index) { @@ -108,7 +105,7 @@ public abstract class FunctionSet { } } } else { - throw new ArrayIndexOutOfBoundsException("Function " + index + " does not exist, the set only has " + functionList.length + " functions."); + throw new ArrayIndexOutOfBoundsException("Function " + index + " does not exist, the set only has " + functionList.size() + " functions."); } } @@ -126,11 +123,6 @@ public abstract class FunctionSet { } } - @Override - public String toString() { - return name; - } - /** * Checks if a specified function is enabled. If the function * does not belong in the FunctionSet, this returns false. @@ -140,7 +132,7 @@ public abstract class FunctionSet { */ public boolean isEnabled(Function function) { for (int i = 0; i < allowedFunctions.size(); i++) { - if (functionList[allowedFunctions.get(i)] == function) { + if (functionList.get(allowedFunctions.get(i)) == function) { return true; } } @@ -148,12 +140,34 @@ public abstract class FunctionSet { } /** - * Enables all functions. + * For internal use in subclass constructors. This method + * adds the specified functions and enables them. The same + * function cannot be added more than once. + * + * @param functions the functions to register in the function set. */ - public void enableAll() { - allowedFunctions = new ArrayList<Integer>(); - for (int i = 0; i < functionList.length; i++) { - allowedFunctions.add(i); + protected void registerFunctions(Function... functions) { + for (int i = 0; i < functions.length; i++) { + if (!alreadyHave(functions[i])) { + functionList.add(functions[i]); + enableFunction(functionList.size() - 1); + } } } + + /** + * For internal use only, this checks whether a function + * is already present in the function set. + * + * @param function the function to look for. + * @return true if the function is already in the function set. + */ + private boolean alreadyHave(Function function) { + for (int i = 0; i < functionList.size(); i++) { + if (functionList.get(i).getClass() == function.getClass()) { + return true; + } + } + return false; + } }
\ No newline at end of file |