From 02fd2bc7059da416937beb1abe67e5ca60379030 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Tue, 1 Apr 2014 23:00:53 +0100 Subject: Settings pane now actually controls the parameters, not much left to do. --- src/jcgp/backend/function/Arithmetic.java | 139 ++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/jcgp/backend/function/Arithmetic.java (limited to 'src/jcgp/backend/function/Arithmetic.java') 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(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"; + } + } + +} -- cgit v1.2.3