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/Arithmetic.java139
-rw-r--r--src/jcgp/backend/function/BitwiseLogic.java215
-rw-r--r--src/jcgp/backend/function/BooleanLogic.java215
-rw-r--r--src/jcgp/backend/function/Function.java13
-rw-r--r--src/jcgp/backend/function/FunctionSet.java74
5 files changed, 656 insertions, 0 deletions
diff --git a/src/jcgp/backend/function/Arithmetic.java b/src/jcgp/backend/function/Arithmetic.java
new file mode 100644
index 0000000..6971663
--- /dev/null
+++ b/src/jcgp/backend/function/Arithmetic.java
@@ -0,0 +1,139 @@
+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 Arithmetic() {
+ name = "Arithmetic";
+ functionList = new Function[]{
+ new Addition(),
+ new Subtraction(),
+ new Multiplication(),
+ new Division()};
+
+ allowedFunctions = new ArrayList<Function>(Arrays.asList(functionList));
+ }
+
+ public static class Addition extends Function {
+
+ private int arity = 2;
+
+ @Override
+ public Integer run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Integer arg1 = ((Integer) connections[0].getValue());
+ Integer arg2 = ((Integer) connections[1].getValue());
+ Integer result = arg1 + arg2;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "Addition";
+ }
+ }
+
+ public static class Subtraction extends Function {
+
+ private int arity = 2;
+
+ @Override
+ public Integer run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Integer arg1 = ((Integer) connections[0].getValue());
+ Integer arg2 = ((Integer) connections[1].getValue());
+ Integer result = arg1 - arg2;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "Subtraction";
+ }
+ }
+
+ public static class Multiplication extends Function {
+
+ private int arity = 2;
+
+ @Override
+ public Integer run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Integer arg1 = ((Integer) connections[0].getValue());
+ Integer arg2 = ((Integer) connections[1].getValue());
+ Integer result = arg1 * arg2;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "Multiplication";
+ }
+ }
+
+ public static class Division extends Function {
+
+ private int arity = 2;
+
+ @Override
+ public Integer run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Integer arg1 = ((Integer) connections[0].getValue());
+ Integer arg2 = ((Integer) connections[1].getValue());
+ Integer result;
+ if (arg2 == 0) {
+ result = 0;
+ } else {
+ result = arg1 / arg2;
+ }
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "Division";
+ }
+ }
+
+}
diff --git a/src/jcgp/backend/function/BitwiseLogic.java b/src/jcgp/backend/function/BitwiseLogic.java
new file mode 100644
index 0000000..bfe361f
--- /dev/null
+++ b/src/jcgp/backend/function/BitwiseLogic.java
@@ -0,0 +1,215 @@
+package jcgp.backend.function;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import jcgp.backend.exceptions.InvalidArgumentsException;
+import jcgp.backend.population.Connection;
+
+public class BitwiseLogic extends FunctionSet {
+
+ public BitwiseLogic() {
+ name = "32-bit Logic";
+ functionList = new Function[]{
+ new And(),
+ new Or(),
+ new Not(),
+ new Xor(),
+ new Nand(),
+ new Nor(),
+ new Xnor()};
+
+ allowedFunctions = new ArrayList<Function>(Arrays.asList(functionList));
+ }
+
+ public static class And extends Function {
+ private int arity = 2;
+
+ @Override
+ public Object run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ int arg1 = ((int) connections[0].getValue());
+ int arg2 = ((int) connections[1].getValue());
+ int result = arg1 & arg2;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "AND";
+ }
+ }
+
+ public static class Or extends Function {
+ private int arity = 2;
+
+ @Override
+ public Object run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ int arg1 = ((int) connections[0].getValue());
+ int arg2 = ((int) connections[1].getValue());
+ int result = arg1 | arg2;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "OR";
+ }
+ }
+
+ public static class Not extends Function {
+ private int arity = 1;
+
+ @Override
+ public Object run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ int arg1 = ((int) connections[0].getValue());
+ int result = ~arg1;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "NOT";
+ }
+ }
+
+ public static class Xor extends Function {
+ private int arity = 2;
+
+ @Override
+ public Object run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ int arg1 = ((int) connections[0].getValue());
+ int arg2 = ((int) connections[1].getValue());
+ int result = arg1 ^ arg2;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "XOR";
+ }
+ }
+
+ public static class Nand extends Function {
+ private int arity = 2;
+
+ @Override
+ public Object run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ int arg1 = ((int) connections[0].getValue());
+ int arg2 = ((int) connections[1].getValue());
+ int result = arg1 & arg2;
+
+ return ~result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "NAND";
+ }
+ }
+
+ public static class Nor extends Function {
+ private int arity = 2;
+
+ @Override
+ public Object run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ int arg1 = ((int) connections[0].getValue());
+ int arg2 = ((int) connections[1].getValue());
+ int result = arg1 | arg2;
+
+ return ~result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "NOR";
+ }
+ }
+
+ public static class Xnor extends Function {
+ private int arity = 2;
+
+ @Override
+ public Object run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ int arg1 = ((int) connections[0].getValue());
+ int arg2 = ((int) connections[1].getValue());
+ int result = arg1 ^ arg2;
+
+ return ~result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "XNOR";
+ }
+ }
+
+
+
+}
diff --git a/src/jcgp/backend/function/BooleanLogic.java b/src/jcgp/backend/function/BooleanLogic.java
new file mode 100644
index 0000000..e0b5c9c
--- /dev/null
+++ b/src/jcgp/backend/function/BooleanLogic.java
@@ -0,0 +1,215 @@
+package jcgp.backend.function;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import jcgp.backend.exceptions.InvalidArgumentsException;
+import jcgp.backend.population.Connection;
+
+public class BooleanLogic extends FunctionSet {
+
+ public BooleanLogic() {
+ name = "1-bit Logic";
+ functionList = new Function[]{
+ new And(),
+ new Or(),
+ new Not(),
+ new Xor(),
+ new Nand(),
+ new Nor(),
+ new Xnor()};
+
+ allowedFunctions = new ArrayList<Function>(Arrays.asList(functionList));
+ }
+
+ public static class And extends Function {
+ private int arity = 2;
+
+ @Override
+ public Boolean run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Boolean arg1 = ((Boolean) connections[0].getValue());
+ Boolean arg2 = ((Boolean) connections[1].getValue());
+ Boolean result = arg1 && arg2;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "AND";
+ }
+ }
+
+ public static class Or extends Function {
+ private int arity = 2;
+
+ @Override
+ public Boolean run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Boolean arg1 = ((Boolean) connections[0].getValue());
+ Boolean arg2 = ((Boolean) connections[1].getValue());
+ Boolean result = arg1 || arg2;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "OR";
+ }
+ }
+
+ public static class Not extends Function {
+ private int arity = 1;
+
+ @Override
+ public Boolean run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Boolean arg1 = ((Boolean) connections[0].getValue());
+ Boolean result = !arg1;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "NOT";
+ }
+ }
+
+ public static class Xor extends Function {
+ private int arity = 2;
+
+ @Override
+ public Boolean run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Boolean arg1 = ((Boolean) connections[0].getValue());
+ Boolean arg2 = ((Boolean) connections[1].getValue());
+ Boolean result = arg1 ^ arg2;
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "XOR";
+ }
+ }
+
+ public static class Nand extends Function {
+ private int arity = 2;
+
+ @Override
+ public Boolean run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Boolean arg1 = ((Boolean) connections[0].getValue());
+ Boolean arg2 = ((Boolean) connections[1].getValue());
+ Boolean result = arg1 && arg2;
+
+ return !result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "NAND";
+ }
+ }
+
+ public static class Nor extends Function {
+ private int arity = 2;
+
+ @Override
+ public Boolean run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Boolean arg1 = ((Boolean) connections[0].getValue());
+ Boolean arg2 = ((Boolean) connections[1].getValue());
+ Boolean result = arg1 || arg2;
+
+ return !result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "NOR";
+ }
+ }
+
+ public static class Xnor extends Function {
+ private int arity = 2;
+
+ @Override
+ public Boolean run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else {
+ Boolean arg1 = ((Boolean) connections[0].getValue());
+ Boolean arg2 = ((Boolean) connections[1].getValue());
+ Boolean result = arg1 ^ arg2;
+
+ return !result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+ @Override
+ public String getName() {
+ return "XNOR";
+ }
+ }
+
+
+
+}
diff --git a/src/jcgp/backend/function/Function.java b/src/jcgp/backend/function/Function.java
new file mode 100644
index 0000000..30bbcf0
--- /dev/null
+++ b/src/jcgp/backend/function/Function.java
@@ -0,0 +1,13 @@
+package jcgp.backend.function;
+
+import jcgp.backend.exceptions.InvalidArgumentsException;
+import jcgp.backend.population.Connection;
+
+public abstract class Function {
+
+ public abstract Object run(Connection ... connections) throws InvalidArgumentsException;
+
+ public abstract int getArity();
+
+ public abstract String getName();
+}
diff --git a/src/jcgp/backend/function/FunctionSet.java b/src/jcgp/backend/function/FunctionSet.java
new file mode 100644
index 0000000..4470ac8
--- /dev/null
+++ b/src/jcgp/backend/function/FunctionSet.java
@@ -0,0 +1,74 @@
+package jcgp.backend.function;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+
+
+/**
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+public abstract class FunctionSet {
+ protected Function[] functionList;
+ protected ArrayList<Function> allowedFunctions;
+ protected String name;
+
+ public int getAllowedFunctionCount() {
+ return allowedFunctions.size();
+ }
+
+ public int getTotalFunctionCount() {
+ return functionList.length;
+ }
+
+ public Function getAllowedFunction(int index) {
+ return allowedFunctions.get(index);
+ }
+
+ public Function getFunction(int index) {
+ return functionList[index];
+ }
+
+ public int getMaxArity(){
+ int arity = 0;
+ for (Function function : allowedFunctions) {
+ if (function.getArity() > arity) {
+ arity = function.getArity();
+ }
+ }
+ return arity;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ 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]) {
+ iterator.remove();
+ }
+ }
+ } else {
+ throw new IndexOutOfBoundsException("Function " + index + " does not exist, the set only has " + functionList.length + " functions.");
+ }
+ }
+
+ public void enableFunction(int index) {
+ if (!allowedFunctions.contains(functionList[index])) {
+ allowedFunctions.add(functionList[index]);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+
+ public boolean isEnabled(Function f) {
+ return allowedFunctions.contains(f);
+ }
+ } \ No newline at end of file