From ef7a850a8f2f81ccaa07f25d9b7ad602e84d88c9 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 16 Feb 2014 22:41:46 +0000 Subject: Light refactoring, a good amount of testing done, added some new functions. --- src/jcgp/function/BitwiseLogic.java | 178 ++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 src/jcgp/function/BitwiseLogic.java (limited to 'src/jcgp/function/BitwiseLogic.java') diff --git a/src/jcgp/function/BitwiseLogic.java b/src/jcgp/function/BitwiseLogic.java new file mode 100644 index 0000000..55f5df0 --- /dev/null +++ b/src/jcgp/function/BitwiseLogic.java @@ -0,0 +1,178 @@ +package jcgp.function; + +import jcgp.Parameters; +import jcgp.exceptions.InvalidArgumentsException; +import jcgp.population.Connection; + +public class BitwiseLogic { + + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " AND " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " OR " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + 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; + if (Parameters.getDebug()) { + System.out.println("NOT " + arg1 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " XOR " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " NAND " + arg2 + " = " + ~result); + } + return ~result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " NOR " + arg2 + " = " + ~result); + } + return ~result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + 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; + if (Parameters.getDebug()) { + System.out.println(arg1 + " XNOR " + arg2 + " = " + ~result); + } + return ~result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + + +} -- cgit v1.2.3