aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/modules/function/Arithmetic.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/modules/function/Arithmetic.java')
-rw-r--r--src/jcgp/modules/function/Arithmetic.java123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/jcgp/modules/function/Arithmetic.java b/src/jcgp/modules/function/Arithmetic.java
new file mode 100644
index 0000000..73debd2
--- /dev/null
+++ b/src/jcgp/modules/function/Arithmetic.java
@@ -0,0 +1,123 @@
+package jcgp.modules.function;
+
+import jcgp.exceptions.InvalidArgumentsException;
+import jcgp.parameters.Parameters;
+import jcgp.population.Connection;
+
+public class Arithmetic {
+
+ 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;
+
+ if (Parameters.getDebug()) {
+ System.out.println(arg1 + " + " + arg2 + " = " + result);
+ }
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+ }
+
+ 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;
+
+ if (Parameters.getDebug()) {
+ System.out.println(arg1 + " - " + arg2 + " = " + result);
+ }
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+ }
+
+ 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;
+
+ if (Parameters.getDebug()) {
+ System.out.println(arg1 + " * " + arg2 + " = " + result);
+ }
+
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+ }
+
+ 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;
+ }
+
+
+ if (Parameters.getDebug()) {
+ System.out.println(arg1 + " / " + arg2 + " = " + result);
+ }
+
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+ }
+
+}