aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/function
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/function')
-rw-r--r--src/jcgp/function/Arithmetic.java140
-rw-r--r--src/jcgp/function/BitwiseLogic.java216
-rw-r--r--src/jcgp/function/BooleanLogic.java216
-rw-r--r--src/jcgp/function/Function.java13
-rw-r--r--src/jcgp/function/FunctionSet.java54
5 files changed, 639 insertions, 0 deletions
diff --git a/src/jcgp/function/Arithmetic.java b/src/jcgp/function/Arithmetic.java
new file mode 100644
index 0000000..7ec1366
--- /dev/null
+++ b/src/jcgp/function/Arithmetic.java
@@ -0,0 +1,140 @@
+package jcgp.function;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import jcgp.exceptions.InvalidArgumentsException;
+import jcgp.population.Connection;
+
+public class Arithmetic extends FunctionSet {
+
+ public Arithmetic() {
+ maxArity = 2;
+ 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/function/BitwiseLogic.java b/src/jcgp/function/BitwiseLogic.java
new file mode 100644
index 0000000..033534d
--- /dev/null
+++ b/src/jcgp/function/BitwiseLogic.java
@@ -0,0 +1,216 @@
+package jcgp.function;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import jcgp.exceptions.InvalidArgumentsException;
+import jcgp.population.Connection;
+
+public class BitwiseLogic extends FunctionSet {
+
+ public BitwiseLogic() {
+ maxArity = 2;
+ 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/function/BooleanLogic.java b/src/jcgp/function/BooleanLogic.java
new file mode 100644
index 0000000..d31b798
--- /dev/null
+++ b/src/jcgp/function/BooleanLogic.java
@@ -0,0 +1,216 @@
+package jcgp.function;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import jcgp.exceptions.InvalidArgumentsException;
+import jcgp.population.Connection;
+
+public class BooleanLogic extends FunctionSet {
+
+ public BooleanLogic() {
+ maxArity = 2;
+ 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/function/Function.java b/src/jcgp/function/Function.java
new file mode 100644
index 0000000..64dd206
--- /dev/null
+++ b/src/jcgp/function/Function.java
@@ -0,0 +1,13 @@
+package jcgp.function;
+
+import jcgp.exceptions.InvalidArgumentsException;
+import jcgp.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/function/FunctionSet.java b/src/jcgp/function/FunctionSet.java
new file mode 100644
index 0000000..07b4ea4
--- /dev/null
+++ b/src/jcgp/function/FunctionSet.java
@@ -0,0 +1,54 @@
+package jcgp.function;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+
+/**
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+public abstract class FunctionSet {
+ protected Function[] functionList;
+ protected ArrayList<Function> allowedFunctions;
+ protected int maxArity;
+ protected String name;
+
+// public int getTotalFunctionCount() {
+// return functionList.length;
+// }
+
+ public int getFunctionCount() {
+ return allowedFunctions.size();
+ }
+
+ public Function getFunction(int index) {
+ return allowedFunctions.get(index);
+ }
+
+ public int getMaxArity(){
+ return maxArity;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void disableFunction(int index) {
+ for (Iterator<Function> iterator = allowedFunctions.iterator(); iterator.hasNext();) {
+ if (index < functionList.length) {
+ Function function = (Function) iterator.next();
+ if (function == functionList[index]) {
+ iterator.remove();
+ }
+ }
+
+ }
+ }
+
+ public void enableFunction(int index) {
+ if (!allowedFunctions.contains(functionList[index])) {
+ allowedFunctions.add(functionList[index]);
+ }
+ }
+ } \ No newline at end of file