aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/function/FunctionSet.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/function/FunctionSet.java')
-rw-r--r--src/jcgp/backend/function/FunctionSet.java88
1 files changed, 79 insertions, 9 deletions
diff --git a/src/jcgp/backend/function/FunctionSet.java b/src/jcgp/backend/function/FunctionSet.java
index 1712b51..926ed68 100644
--- a/src/jcgp/backend/function/FunctionSet.java
+++ b/src/jcgp/backend/function/FunctionSet.java
@@ -6,6 +6,22 @@ import java.util.Iterator;
/**
+ * FunctionSet encapsulates a group of functions. This is usually done to
+ * simplify the implementation of problem types.
+ * <br><br>
+ * FunctionSet contains a variety of useful methods for acquiring general
+ * information, such as the maximum arity across all functions and the total
+ * number of functions.
+ * <br><br>
+ * In addition, FunctionSet offers the ability to enable and disable functions.
+ * Accessing the functions through {@code getAllowedFunction()} will return
+ * 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.
+ *
*
* @author Eduardo Pedroni
*
@@ -15,25 +31,55 @@ public abstract class FunctionSet {
protected ArrayList<Integer> allowedFunctions;
protected String name;
+ /**
+ * @return the number of currently allowed functions.
+ */
public int getAllowedFunctionCount() {
return allowedFunctions.size();
}
+ /**
+ * @return the total number of functions, including disabled ones.
+ */
public int getTotalFunctionCount() {
return functionList.length;
}
+ /**
+ * Returns an allowed function. This throws an
+ * ArrayIndexOutOfBoundsException if the supplied
+ * index is beyond the count of allowed functions.
+ *
+ * @param index the allowed function index.
+ * @return the allowed function object.
+ */
public Function getAllowedFunction(int index) {
return functionList[allowedFunctions.get(index)];
}
+ /**
+ * Returns a function from the complete collection,
+ * enabled or disabled alike. This throws an
+ * ArrayIndexOutOfBoundsException if the supplied
+ * index is beyond the count of allowed functions.
+ *
+ * @param index the function index.
+ * @return the function object.
+ */
public Function getFunction(int index) {
return functionList[index];
}
+ /**
+ * Computes and returns the maximum arity out of
+ * all the function, enabled or disabled.
+ *
+ * @return
+ */
public int getMaxArity(){
int arity = 0;
for (Function function : functionList) {
+ // if a higher arity is found, store it
if (function.getArity() > arity) {
arity = function.getArity();
}
@@ -41,25 +87,39 @@ public abstract class FunctionSet {
return arity;
}
- public String getName() {
- return name;
- }
-
+ /**
+ * Disables the indexed function. If the function
+ * is already disabled, this does nothing.
+ *
+ * @param index the function to disable.
+ */
public void disableFunction(int index) {
+ /*
+ * allowedFunctions is a list of the indices of all allowed functions,
+ * as addressed in functionList. This method iterates through the whole
+ * list of allowed functions and removes any elements which are equal
+ * to the specified index.
+ */
if (index < functionList.length) {
for (Iterator<Integer> iterator = allowedFunctions.iterator(); iterator.hasNext();) {
int function = iterator.next();
if (function == index) {
iterator.remove();
- break;
}
}
} else {
- throw new IndexOutOfBoundsException("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.length + " functions.");
}
}
+ /**
+ * Disables the indexed function. If the function is
+ * already enabled, this does nothing.
+ *
+ * @param index the function to disable.
+ */
public void enableFunction(int index) {
+ // add the specified index to the list of allowed indices
if (!allowedFunctions.contains(index)) {
allowedFunctions.add(index);
Collections.sort(allowedFunctions);
@@ -71,16 +131,26 @@ public abstract class FunctionSet {
return name;
}
- public boolean isEnabled(Function f) {
+ /**
+ * Checks if a specified function is enabled. If the function
+ * does not belong in the FunctionSet, this returns false.
+ *
+ * @param function the function to check.
+ * @return true if the function is enabled.
+ */
+ public boolean isEnabled(Function function) {
for (int i = 0; i < allowedFunctions.size(); i++) {
- if (functionList[allowedFunctions.get(i)] == f) {
+ if (functionList[allowedFunctions.get(i)] == function) {
return true;
}
}
return false;
}
- protected void enableAll() {
+ /**
+ * Enables all functions.
+ */
+ public void enableAll() {
allowedFunctions = new ArrayList<Integer>();
for (int i = 0; i < functionList.length; i++) {
allowedFunctions.add(i);