aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/function
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/function')
-rw-r--r--src/jcgp/backend/function/BitwiseLogic.java61
-rw-r--r--src/jcgp/backend/function/BooleanLogic.java60
-rw-r--r--src/jcgp/backend/function/FunctionSet.java32
-rw-r--r--src/jcgp/backend/function/IntegerArithmetic.java (renamed from src/jcgp/backend/function/Arithmetic.java)39
4 files changed, 79 insertions, 113 deletions
diff --git a/src/jcgp/backend/function/BitwiseLogic.java b/src/jcgp/backend/function/BitwiseLogic.java
index bfe361f..5d47ff7 100644
--- a/src/jcgp/backend/function/BitwiseLogic.java
+++ b/src/jcgp/backend/function/BitwiseLogic.java
@@ -1,8 +1,5 @@
package jcgp.backend.function;
-import java.util.ArrayList;
-import java.util.Arrays;
-
import jcgp.backend.exceptions.InvalidArgumentsException;
import jcgp.backend.population.Connection;
@@ -19,15 +16,13 @@ public class BitwiseLogic extends FunctionSet {
new Nor(),
new Xnor()};
- allowedFunctions = new ArrayList<Function>(Arrays.asList(functionList));
+ enableAll();
}
- public static class And extends Function {
- private int arity = 2;
-
+ public static class And extends Function {
@Override
public Object run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
int arg1 = ((int) connections[0].getValue());
@@ -40,7 +35,7 @@ public class BitwiseLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -49,12 +44,10 @@ public class BitwiseLogic extends FunctionSet {
}
}
- public static class Or extends Function {
- private int arity = 2;
-
+ public static class Or extends Function {
@Override
public Object run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
int arg1 = ((int) connections[0].getValue());
@@ -67,7 +60,7 @@ public class BitwiseLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -76,12 +69,10 @@ public class BitwiseLogic extends FunctionSet {
}
}
- public static class Not extends Function {
- private int arity = 1;
-
+ public static class Not extends Function {
@Override
public Object run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 1) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
int arg1 = ((int) connections[0].getValue());
@@ -93,7 +84,7 @@ public class BitwiseLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 1;
}
@Override
@@ -102,12 +93,10 @@ public class BitwiseLogic extends FunctionSet {
}
}
- public static class Xor extends Function {
- private int arity = 2;
-
+ public static class Xor extends Function {
@Override
public Object run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
int arg1 = ((int) connections[0].getValue());
@@ -120,7 +109,7 @@ public class BitwiseLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -129,12 +118,10 @@ public class BitwiseLogic extends FunctionSet {
}
}
- public static class Nand extends Function {
- private int arity = 2;
-
+ public static class Nand extends Function {
@Override
public Object run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
int arg1 = ((int) connections[0].getValue());
@@ -147,7 +134,7 @@ public class BitwiseLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -156,12 +143,10 @@ public class BitwiseLogic extends FunctionSet {
}
}
- public static class Nor extends Function {
- private int arity = 2;
-
+ public static class Nor extends Function {
@Override
public Object run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
int arg1 = ((int) connections[0].getValue());
@@ -174,7 +159,7 @@ public class BitwiseLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -183,12 +168,10 @@ public class BitwiseLogic extends FunctionSet {
}
}
- public static class Xnor extends Function {
- private int arity = 2;
-
+ public static class Xnor extends Function {
@Override
public Object run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
int arg1 = ((int) connections[0].getValue());
@@ -201,7 +184,7 @@ public class BitwiseLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
diff --git a/src/jcgp/backend/function/BooleanLogic.java b/src/jcgp/backend/function/BooleanLogic.java
index e0b5c9c..9e7d1ff 100644
--- a/src/jcgp/backend/function/BooleanLogic.java
+++ b/src/jcgp/backend/function/BooleanLogic.java
@@ -1,7 +1,5 @@
package jcgp.backend.function;
-import java.util.ArrayList;
-import java.util.Arrays;
import jcgp.backend.exceptions.InvalidArgumentsException;
import jcgp.backend.population.Connection;
@@ -19,15 +17,13 @@ public class BooleanLogic extends FunctionSet {
new Nor(),
new Xnor()};
- allowedFunctions = new ArrayList<Function>(Arrays.asList(functionList));
+ enableAll();
}
- public static class And extends Function {
- private int arity = 2;
-
+ public static class And extends Function {
@Override
public Boolean run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Boolean arg1 = ((Boolean) connections[0].getValue());
@@ -40,7 +36,7 @@ public class BooleanLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -49,12 +45,10 @@ public class BooleanLogic extends FunctionSet {
}
}
- public static class Or extends Function {
- private int arity = 2;
-
+ public static class Or extends Function {
@Override
public Boolean run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Boolean arg1 = ((Boolean) connections[0].getValue());
@@ -67,7 +61,7 @@ public class BooleanLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -76,12 +70,10 @@ public class BooleanLogic extends FunctionSet {
}
}
- public static class Not extends Function {
- private int arity = 1;
-
+ public static class Not extends Function {
@Override
public Boolean run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 1) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Boolean arg1 = ((Boolean) connections[0].getValue());
@@ -93,7 +85,7 @@ public class BooleanLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 1;
}
@Override
@@ -102,12 +94,10 @@ public class BooleanLogic extends FunctionSet {
}
}
- public static class Xor extends Function {
- private int arity = 2;
-
+ public static class Xor extends Function {
@Override
public Boolean run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Boolean arg1 = ((Boolean) connections[0].getValue());
@@ -120,7 +110,7 @@ public class BooleanLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -129,12 +119,10 @@ public class BooleanLogic extends FunctionSet {
}
}
- public static class Nand extends Function {
- private int arity = 2;
-
+ public static class Nand extends Function {
@Override
public Boolean run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Boolean arg1 = ((Boolean) connections[0].getValue());
@@ -147,7 +135,7 @@ public class BooleanLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -156,12 +144,10 @@ public class BooleanLogic extends FunctionSet {
}
}
- public static class Nor extends Function {
- private int arity = 2;
-
+ public static class Nor extends Function {
@Override
public Boolean run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Boolean arg1 = ((Boolean) connections[0].getValue());
@@ -174,7 +160,7 @@ public class BooleanLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -183,12 +169,10 @@ public class BooleanLogic extends FunctionSet {
}
}
- public static class Xnor extends Function {
- private int arity = 2;
-
+ public static class Xnor extends Function {
@Override
public Boolean run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Boolean arg1 = ((Boolean) connections[0].getValue());
@@ -201,7 +185,7 @@ public class BooleanLogic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
diff --git a/src/jcgp/backend/function/FunctionSet.java b/src/jcgp/backend/function/FunctionSet.java
index 4470ac8..78801fc 100644
--- a/src/jcgp/backend/function/FunctionSet.java
+++ b/src/jcgp/backend/function/FunctionSet.java
@@ -1,6 +1,7 @@
package jcgp.backend.function;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Iterator;
@@ -11,7 +12,7 @@ import java.util.Iterator;
*/
public abstract class FunctionSet {
protected Function[] functionList;
- protected ArrayList<Function> allowedFunctions;
+ protected ArrayList<Integer> allowedFunctions;
protected String name;
public int getAllowedFunctionCount() {
@@ -23,7 +24,7 @@ public abstract class FunctionSet {
}
public Function getAllowedFunction(int index) {
- return allowedFunctions.get(index);
+ return functionList[allowedFunctions.get(index)];
}
public Function getFunction(int index) {
@@ -32,7 +33,7 @@ public abstract class FunctionSet {
public int getMaxArity(){
int arity = 0;
- for (Function function : allowedFunctions) {
+ for (Function function : functionList) {
if (function.getArity() > arity) {
arity = function.getArity();
}
@@ -46,9 +47,9 @@ public abstract class FunctionSet {
public void disableFunction(int index) {
if (index < functionList.length) {
- for (Iterator<Function> iterator = allowedFunctions.iterator(); iterator.hasNext();) {
- Function function = (Function) iterator.next();
- if (function == functionList[index]) {
+ for (Iterator<Integer> iterator = allowedFunctions.iterator(); iterator.hasNext();) {
+ int function = iterator.next();
+ if (function == index) {
iterator.remove();
}
}
@@ -58,8 +59,9 @@ public abstract class FunctionSet {
}
public void enableFunction(int index) {
- if (!allowedFunctions.contains(functionList[index])) {
- allowedFunctions.add(functionList[index]);
+ if (!allowedFunctions.contains(index)) {
+ allowedFunctions.add(index);
+ Collections.sort(allowedFunctions);
}
}
@@ -69,6 +71,18 @@ public abstract class FunctionSet {
}
public boolean isEnabled(Function f) {
- return allowedFunctions.contains(f);
+ for (int i = 0; i < allowedFunctions.size(); i++) {
+ if (functionList[allowedFunctions.get(i)] == f) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ protected void enableAll() {
+ allowedFunctions = new ArrayList<Integer>();
+ for (int i = 0; i < functionList.length; i++) {
+ allowedFunctions.add(i);
+ }
}
} \ No newline at end of file
diff --git a/src/jcgp/backend/function/Arithmetic.java b/src/jcgp/backend/function/IntegerArithmetic.java
index 6971663..c08a72a 100644
--- a/src/jcgp/backend/function/Arithmetic.java
+++ b/src/jcgp/backend/function/IntegerArithmetic.java
@@ -1,31 +1,25 @@
package jcgp.backend.function;
-import java.util.ArrayList;
-import java.util.Arrays;
-
import jcgp.backend.exceptions.InvalidArgumentsException;
import jcgp.backend.population.Connection;
-public class Arithmetic extends FunctionSet {
+public class IntegerArithmetic extends FunctionSet {
- public Arithmetic() {
- name = "Arithmetic";
+ public IntegerArithmetic() {
+ name = "Integer Arithmetic";
functionList = new Function[]{
new Addition(),
new Subtraction(),
new Multiplication(),
new Division()};
- allowedFunctions = new ArrayList<Function>(Arrays.asList(functionList));
+ enableAll();
}
public static class Addition extends Function {
-
- private int arity = 2;
-
@Override
public Integer run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Integer arg1 = ((Integer) connections[0].getValue());
@@ -38,7 +32,7 @@ public class Arithmetic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -48,12 +42,9 @@ public class Arithmetic extends FunctionSet {
}
public static class Subtraction extends Function {
-
- private int arity = 2;
-
@Override
public Integer run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Integer arg1 = ((Integer) connections[0].getValue());
@@ -66,7 +57,7 @@ public class Arithmetic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -76,12 +67,9 @@ public class Arithmetic extends FunctionSet {
}
public static class Multiplication extends Function {
-
- private int arity = 2;
-
@Override
public Integer run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Integer arg1 = ((Integer) connections[0].getValue());
@@ -94,7 +82,7 @@ public class Arithmetic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override
@@ -104,12 +92,9 @@ public class Arithmetic extends FunctionSet {
}
public static class Division extends Function {
-
- private int arity = 2;
-
@Override
public Integer run(Connection... connections) {
- if (connections.length < arity) {
+ if (connections.length < 2) {
throw new InvalidArgumentsException("Not enough connections were given.");
} else {
Integer arg1 = ((Integer) connections[0].getValue());
@@ -127,7 +112,7 @@ public class Arithmetic extends FunctionSet {
@Override
public int getArity() {
- return arity;
+ return 2;
}
@Override