From e6dd7711c7dad5e000445208eb5845801f4ccffc Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 6 Apr 2014 21:58:53 +0100 Subject: About to make big changes to the way fitness works, committing just in case --- src/jcgp/backend/function/IntegerArithmetic.java | 124 +++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/jcgp/backend/function/IntegerArithmetic.java (limited to 'src/jcgp/backend/function/IntegerArithmetic.java') diff --git a/src/jcgp/backend/function/IntegerArithmetic.java b/src/jcgp/backend/function/IntegerArithmetic.java new file mode 100644 index 0000000..c08a72a --- /dev/null +++ b/src/jcgp/backend/function/IntegerArithmetic.java @@ -0,0 +1,124 @@ +package jcgp.backend.function; + +import jcgp.backend.exceptions.InvalidArgumentsException; +import jcgp.backend.population.Connection; + +public class IntegerArithmetic extends FunctionSet { + + public IntegerArithmetic() { + name = "Integer Arithmetic"; + functionList = new Function[]{ + new Addition(), + new Subtraction(), + new Multiplication(), + new Division()}; + + enableAll(); + } + + public static class Addition extends Function { + @Override + public Integer run(Connection... connections) { + if (connections.length < 2) { + 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 2; + } + + @Override + public String getName() { + return "Addition"; + } + } + + public static class Subtraction extends Function { + @Override + public Integer run(Connection... connections) { + if (connections.length < 2) { + 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 2; + } + + @Override + public String getName() { + return "Subtraction"; + } + } + + public static class Multiplication extends Function { + @Override + public Integer run(Connection... connections) { + if (connections.length < 2) { + 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 2; + } + + @Override + public String getName() { + return "Multiplication"; + } + } + + public static class Division extends Function { + @Override + public Integer run(Connection... connections) { + if (connections.length < 2) { + 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 2; + } + + @Override + public String getName() { + return "Division"; + } + } + +} -- cgit v1.2.3