aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/function/Arithmetic.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/function/Arithmetic.java')
-rw-r--r--src/jcgp/function/Arithmetic.java140
1 files changed, 140 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";
+ }
+ }
+
+}