aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/function
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/function')
-rw-r--r--src/jcgp/backend/function/BitwiseLogic.java506
-rw-r--r--src/jcgp/backend/function/DigitalCircuitFunctions.java504
-rw-r--r--src/jcgp/backend/function/DoubleArithmetic.java589
-rw-r--r--src/jcgp/backend/function/Function.java29
-rw-r--r--src/jcgp/backend/function/FunctionSet.java88
-rw-r--r--src/jcgp/backend/function/SymbolicRegressionFunctions.java587
-rw-r--r--src/jcgp/backend/function/TravellingSalesmanFunctions.java396
7 files changed, 1588 insertions, 1111 deletions
diff --git a/src/jcgp/backend/function/BitwiseLogic.java b/src/jcgp/backend/function/BitwiseLogic.java
deleted file mode 100644
index 2466f36..0000000
--- a/src/jcgp/backend/function/BitwiseLogic.java
+++ /dev/null
@@ -1,506 +0,0 @@
-package jcgp.backend.function;
-
-import jcgp.backend.population.Connection;
-
-public class BitwiseLogic extends FunctionSet {
-
- public BitwiseLogic() {
- name = "32-bit Logic";
- functionList = new Function[]{
- new ConstantZero(),
- new ConstantOne(),
- new WireA(),
- new WireB(),
- new NotA(),
- new NotB(),
- new And(),
- new AndNotA(),
- new AndNotB(),
- new Nor(),
- new Xor(),
- new Xnor(),
- new Or(),
- new OrNotA(),
- new OrNotB(),
- new Nand()
-// new Mux1(),
-// new Mux2(),
-// new Mux3(),
-// new Mux4()
- };
-
- enableAll();
- }
-
- public static class ConstantZero extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- return new UnsignedInteger(0);
- }
-
- @Override
- public int getArity() {
- return 0;
- }
-
- @Override
- public String getName() {
- return "0";
- }
- }
-
- public static class ConstantOne extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- return new UnsignedInteger(0xFFFF);
- }
-
- @Override
- public int getArity() {
- return 0;
- }
-
- @Override
- public String getName() {
- return "1";
- }
- }
-
- public static class WireA extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- return ((UnsignedInteger) connections[0].getValue());
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Wire A";
- }
- }
-
- public static class WireB extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- return ((UnsignedInteger) connections[1].getValue());
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Wire B";
- }
- }
-
- public static class NotA extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- return new UnsignedInteger(~((UnsignedInteger) connections[0].getValue()).get());
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Not A";
- }
- }
-
- public static class NotB extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- return new UnsignedInteger(~((UnsignedInteger) connections[1].getValue()).get());
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Not B";
- }
- }
-
- public static class And extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- Integer result = in0.get() & in1.get();
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "And";
- }
- }
-
- public static class AndNotA extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- Integer result = ~(in0.get()) & in1.get();
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "And !A";
- }
- }
-
- public static class AndNotB extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- Integer result = in0.get() & ~(in1.get());
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "And !B";
- }
- }
-
- public static class Nor extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- Integer result = in0.get() | in1.get();
-
- return new UnsignedInteger(~result);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Nor";
- }
- }
-
- public static class Xor extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- Integer result = in0.get() ^ in1.get();
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Xor";
- }
- }
-
- public static class Xnor extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- Integer result = in0.get() ^ in1.get();
-
- return new UnsignedInteger(~result);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Xnor";
- }
- }
-
- public static class Or extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- Integer result = in0.get() | in1.get();
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Or";
- }
- }
-
- public static class OrNotA extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- Integer result = ~in0.get() | in1.get();
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Or !A";
- }
- }
-
- public static class OrNotB extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- Integer result = in0.get() | ~in1.get();
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Or !B";
- }
- }
-
- public static class Nand extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- Integer result = in0.get() & in1.get();
-
- return new UnsignedInteger(~result);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Nand";
- }
- }
-
- public static class Mux1 extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- UnsignedInteger in2 = ((UnsignedInteger) connections[2].getValue());
- Integer result = ((in0.get() & ~in2.get()) | (in1.get() & in2.get()));
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 3;
- }
-
- @Override
- public String getName() {
- return "Mux1";
- }
- }
-
- public static class Mux2 extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- UnsignedInteger in2 = ((UnsignedInteger) connections[2].getValue());
- Integer result = ((in0.get() & ~in2.get()) | (~in1.get() & in2.get()));
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 3;
- }
-
- @Override
- public String getName() {
- return "Mux2";
- }
- }
-
- public static class Mux3 extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- UnsignedInteger in2 = ((UnsignedInteger) connections[2].getValue());
- Integer result = ((~in0.get() & ~in2.get()) | (in1.get() & in2.get()));
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 3;
- }
-
- @Override
- public String getName() {
- return "Mux3";
- }
- }
-
- public static class Mux4 extends Function {
- @Override
- public UnsignedInteger run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- UnsignedInteger in0 = ((UnsignedInteger) connections[0].getValue());
- UnsignedInteger in1 = ((UnsignedInteger) connections[1].getValue());
- UnsignedInteger in2 = ((UnsignedInteger) connections[2].getValue());
- Integer result = ((~in0.get() & ~in2.get()) | (~in1.get() & in2.get()));
-
- return new UnsignedInteger(result);
- }
- }
-
- @Override
- public int getArity() {
- return 3;
- }
-
- @Override
- public String getName() {
- return "Mux4";
- }
- }
-}
diff --git a/src/jcgp/backend/function/DigitalCircuitFunctions.java b/src/jcgp/backend/function/DigitalCircuitFunctions.java
new file mode 100644
index 0000000..31cdf17
--- /dev/null
+++ b/src/jcgp/backend/function/DigitalCircuitFunctions.java
@@ -0,0 +1,504 @@
+package jcgp.backend.function;
+
+public class DigitalCircuitFunctions extends FunctionSet {
+
+ public DigitalCircuitFunctions() {
+ name = "32-bit Logic";
+ functionList = new Function[]{
+ new ConstantZero(),
+ new ConstantOne(),
+ new WireA(),
+ new WireB(),
+ new NotA(),
+ new NotB(),
+ new And(),
+ new AndNotA(),
+ new AndNotB(),
+ new Nor(),
+ new Xor(),
+ new Xnor(),
+ new Or(),
+ new OrNotA(),
+ new OrNotB(),
+ new Nand(),
+ new Mux1(),
+ new Mux2(),
+ new Mux3(),
+ new Mux4()
+ };
+
+ enableAll();
+ }
+
+ public static class ConstantZero extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ return new UnsignedInteger(0);
+ }
+
+ @Override
+ public int getArity() {
+ return 0;
+ }
+
+ @Override
+ public String toString() {
+ return "0";
+ }
+ }
+
+ public static class ConstantOne extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ return new UnsignedInteger(0xFFFF);
+ }
+
+ @Override
+ public int getArity() {
+ return 0;
+ }
+
+ @Override
+ public String toString() {
+ return "1";
+ }
+ }
+
+ public static class WireA extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ return ((UnsignedInteger) args[0]);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Wire A";
+ }
+ }
+
+ public static class WireB extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ return ((UnsignedInteger) args[1]);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Wire B";
+ }
+ }
+
+ public static class NotA extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ return new UnsignedInteger(~((UnsignedInteger) args[0]).get());
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Not A";
+ }
+ }
+
+ public static class NotB extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ return new UnsignedInteger(~((UnsignedInteger) args[1]).get());
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Not B";
+ }
+ }
+
+ public static class And extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ Integer result = in0.get() & in1.get();
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "And";
+ }
+ }
+
+ public static class AndNotA extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ Integer result = ~(in0.get()) & in1.get();
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "And !A";
+ }
+ }
+
+ public static class AndNotB extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ Integer result = in0.get() & ~(in1.get());
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "And !B";
+ }
+ }
+
+ public static class Nor extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ Integer result = in0.get() | in1.get();
+
+ return new UnsignedInteger(~result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Nor";
+ }
+ }
+
+ public static class Xor extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ Integer result = in0.get() ^ in1.get();
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Xor";
+ }
+ }
+
+ public static class Xnor extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ Integer result = in0.get() ^ in1.get();
+
+ return new UnsignedInteger(~result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Xnor";
+ }
+ }
+
+ public static class Or extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ Integer result = in0.get() | in1.get();
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Or";
+ }
+ }
+
+ public static class OrNotA extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ Integer result = ~in0.get() | in1.get();
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Or !A";
+ }
+ }
+
+ public static class OrNotB extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ Integer result = in0.get() | ~in1.get();
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Or !B";
+ }
+ }
+
+ public static class Nand extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ Integer result = in0.get() & in1.get();
+
+ return new UnsignedInteger(~result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Nand";
+ }
+ }
+
+ public static class Mux1 extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ UnsignedInteger in2 = ((UnsignedInteger) args[2]);
+ Integer result = ((in0.get() & ~in2.get()) | (in1.get() & in2.get()));
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 3;
+ }
+
+ @Override
+ public String toString() {
+ return "Mux1";
+ }
+ }
+
+ public static class Mux2 extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ UnsignedInteger in2 = ((UnsignedInteger) args[2]);
+ Integer result = ((in0.get() & ~in2.get()) | (~in1.get() & in2.get()));
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 3;
+ }
+
+ @Override
+ public String toString() {
+ return "Mux2";
+ }
+ }
+
+ public static class Mux3 extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ UnsignedInteger in2 = ((UnsignedInteger) args[2]);
+ Integer result = ((~in0.get() & ~in2.get()) | (in1.get() & in2.get()));
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 3;
+ }
+
+ @Override
+ public String toString() {
+ return "Mux3";
+ }
+ }
+
+ public static class Mux4 extends Function {
+ @Override
+ public UnsignedInteger run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " in but arity is " + getArity() + ".");
+ } else {
+ UnsignedInteger in0 = ((UnsignedInteger) args[0]);
+ UnsignedInteger in1 = ((UnsignedInteger) args[1]);
+ UnsignedInteger in2 = ((UnsignedInteger) args[2]);
+ Integer result = ((~in0.get() & ~in2.get()) | (~in1.get() & in2.get()));
+
+ return new UnsignedInteger(result);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 3;
+ }
+
+ @Override
+ public String toString() {
+ return "Mux4";
+ }
+ }
+}
diff --git a/src/jcgp/backend/function/DoubleArithmetic.java b/src/jcgp/backend/function/DoubleArithmetic.java
deleted file mode 100644
index ce4826f..0000000
--- a/src/jcgp/backend/function/DoubleArithmetic.java
+++ /dev/null
@@ -1,589 +0,0 @@
-package jcgp.backend.function;
-
-import jcgp.backend.population.Connection;
-
-public class DoubleArithmetic extends FunctionSet {
-
- public final static double DIVISION_LIMIT = 0.0001;
-
- public DoubleArithmetic() {
- name = "Double Arithmetic";
- functionList = new Function[]{
- new Absolute(),
- new SquareRoot(),
- new Reciprocal(),
- new Sine(),
- new Cosine(),
- new Tangent(),
- new Exponential(),
- new HyperbolicSine(),
- new HyperbolicCosine(),
- new HyperbolicTangent(),
- new NaturalLog(),
- new LogBaseTen(),
- new SineAB(),
- new CosineAB(),
- new Hypotenuse(),
- new Power(),
- new Addition(),
- new Subtraction(),
- new Multiplication(),
- new Division()};
-
- enableAll();
- }
-
- /**
- * Absolute returns the positive value of input 0.
- *
- * @see Math.abs()
- */
- public static class Absolute extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return Math.abs(in0);
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Absolute";
- }
- }
-
- /**
- * Protected square root function, returns the square root of the absolute
- * value of input 0.
- *
- * @see Math.abs(), Math.sqrt()
- */
- public static class SquareRoot extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return Math.sqrt(Math.abs(in0));
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Square root";
- }
- }
-
- /**
- * Protected reciprocal function, returns (1 / input 0). If input 0 is less than
- * {@link DoubleArithmetic.}DIVISION_LIMIT, this returns it unchanged.
- *
- */
- public static class Reciprocal extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return in0 < DIVISION_LIMIT ? in0 : (1 / in0);
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Reciprocal";
- }
- }
-
- /**
- * Sine function, in radians.
- *
- * @see Math.sin()
- */
- public static class Sine extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return Math.sin(in0);
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Sin";
- }
- }
-
- /**
- * Cosine function, in radians.
- *
- * @see Math.cos()
- */
- public static class Cosine extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return Math.cos(in0);
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Cos";
- }
- }
-
- /**
- * Protected tangent function, in radians. Returns the tangent of input 0.
- * If input 0 is less than {@link DoubleArithmetic.}DIVISION_LIMIT,
- * this returns it unchanged.
- *
- * @see Math.tan()
- */
- public static class Tangent extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return in0 < DIVISION_LIMIT ? in0 : Math.tan(in0);
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Tan";
- }
- }
-
- /**
- * Exponential function. Returns the exponential of input 0.
- *
- * @see Math.exp()
- */
- public static class Exponential extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return Math.exp(in0);
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Exp";
- }
- }
-
- /**
- * Returns the hyperbolic sine of input 0.
- *
- * @see Math.sinh()
- */
- public static class HyperbolicSine extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return Math.sinh(in0);
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Sinh";
- }
- }
-
- /**
- * Returns the hyperbolic cosine of input 0.
- *
- * @see Math.cosh()
- */
- public static class HyperbolicCosine extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return Math.cosh(in0);
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Cosh";
- }
- }
-
- /**
- * Returns the hyperbolic tangent of input 0.
- *
- * @see Math.tanh()
- */
- public static class HyperbolicTangent extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return Math.tanh(in0);
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Tanh";
- }
- }
-
- /**
- * Protected natural log function. Returns the natural log of the absolute
- * value of input 0. If input 0 is less than {@link DoubleArithmetic.}DIVISION_LIMIT,
- * this returns it unchanged.
- *
- * @see Math.log(), Math.abs()
- */
- public static class NaturalLog extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return in0 < DIVISION_LIMIT ? in0 : Math.log(Math.abs(in0));
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Ln";
- }
- }
-
- /**
- * Protected log base 10 function. Returns the log to base 10 the absolute
- * value of input 0. If input 0 is less than {@link DoubleArithmetic.}DIVISION_LIMIT,
- * this returns it unchanged.
- *
- * @see Math.log10(), Math.abs()
- */
- public static class LogBaseTen extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- return in0 < DIVISION_LIMIT ? in0 : Math.log10(Math.abs(in0));
- }
- }
-
- @Override
- public int getArity() {
- return 1;
- }
-
- @Override
- public String getName() {
- return "Log";
- }
- }
-
- /**
- * Sine of sum. Returns the sine of the sum of inputs 0 and 1.
- *
- * @see Math.sin()
- */
- public static class SineAB extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- Double in1 = ((Double) connections[1].getValue());
- return Math.sin(in0 + in1);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Sin(a+b)";
- }
- }
-
- /**
- * Cosine of sum. Returns the cosine of the sum of inputs 0 and 1.
- *
- * @see Math.cos()
- */
- public static class CosineAB extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- Double in1 = ((Double) connections[1].getValue());
- return Math.cos(in0 + in1);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Cos(a+b)";
- }
- }
-
- /**
- * Hypotenuse function. Returns the square root of input 0 squared
- * plus input 1 squared.
- *
- * @see Math.hypot()
- */
- public static class Hypotenuse extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- Double in1 = ((Double) connections[1].getValue());
- return Math.hypot(in0, in1);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Hypotenuse";
- }
- }
-
- /**
- * Power function. Returns the absolute value of input 0 to the power of input 1.
- *
- * @see Math.abs(), Math.pow
- */
- public static class Power extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- Double in1 = ((Double) connections[1].getValue());
- return Math.pow(Math.abs(in0), in1);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Power";
- }
- }
-
- /**
- * Addition returns the sum of inputs 0 and 1.
- *
- */
- public static class Addition extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- Double in1 = ((Double) connections[1].getValue());
- return in0 + in1;
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Addition";
- }
- }
-
- /**
- * Subtraction returns the difference between inputs 0 and 1.
- *
- */
- public static class Subtraction extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- Double in1 = ((Double) connections[1].getValue());
- return in0 - in1;
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Subtraction";
- }
- }
-
- /**
- * Multiplication returns the product of inputs 0 and 1.
- *
- */
- public static class Multiplication extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- Double in1 = ((Double) connections[1].getValue());
- return in0 * in1;
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Multiplication";
- }
- }
-
- /**
- * Protected division, returns the quotient of input 0 (the dividend) and input 1 (the divisor).
- * If the divisor is less than {@link DoubleArithmetic.}DIVISION_LIMIT, this returns the it unchanged.
- *
- */
- public static class Division extends Function {
- @Override
- public Double run(Connection... connections) {
- if (connections.length < getArity()) {
- throw new IllegalArgumentException(getName() + " received " + connections.length + " connections but arity is " + getArity() + ".");
- } else {
- Double in0 = ((Double) connections[0].getValue());
- Double in1 = ((Double) connections[1].getValue());
-
- return in1 < DIVISION_LIMIT ? in0 : (in0 / in1);
- }
- }
-
- @Override
- public int getArity() {
- return 2;
- }
-
- @Override
- public String getName() {
- return "Division";
- }
- }
-}
diff --git a/src/jcgp/backend/function/Function.java b/src/jcgp/backend/function/Function.java
index 30bbcf0..fdacac0 100644
--- a/src/jcgp/backend/function/Function.java
+++ b/src/jcgp/backend/function/Function.java
@@ -1,13 +1,28 @@
package jcgp.backend.function;
-import jcgp.backend.exceptions.InvalidArgumentsException;
-import jcgp.backend.population.Connection;
-
+/**
+ * Function is a callback wrapper.
+ * <br><br>
+ * A concrete implementation of Function overrides {@code run()} to perform
+ * any arbitrary operation on the arguments specified. It must also override
+ * {@code getArity()} to return the function arity.
+ *
+ *
+ * @author Eduardo Pedroni
+ *
+ */
public abstract class Function {
-
- public abstract Object run(Connection ... connections) throws InvalidArgumentsException;
- public abstract int getArity();
+ /**
+ * Executes the function.
+ *
+ * @param args the function arguments
+ * @return the function result
+ */
+ public abstract Object run(Object... args);
- public abstract String getName();
+ /**
+ * @return the arity of the function.
+ */
+ public abstract int getArity();
}
diff --git a/src/jcgp/backend/function/FunctionSet.java b/src/jcgp/backend/function/FunctionSet.java
index 1712b51..926ed68 100644
--- a/src/jcgp/backend/function/FunctionSet.java
+++ b/src/jcgp/backend/function/FunctionSet.java
@@ -6,6 +6,22 @@ import java.util.Iterator;
/**
+ * FunctionSet encapsulates a group of functions. This is usually done to
+ * simplify the implementation of problem types.
+ * <br><br>
+ * FunctionSet contains a variety of useful methods for acquiring general
+ * information, such as the maximum arity across all functions and the total
+ * number of functions.
+ * <br><br>
+ * In addition, FunctionSet offers the ability to enable and disable functions.
+ * Accessing the functions through {@code getAllowedFunction()} will return
+ * allowed functions only, providing an easy way to control which functions
+ * can be used in mutations.
+ * <br><br>
+ * An implementation of FunctionSet must simply set the name field and initialise
+ * the functionList array with all of the functions. It is advisable to call
+ * {@code enableAll()} to enable all functions once the array is initialised.
+ *
*
* @author Eduardo Pedroni
*
@@ -15,25 +31,55 @@ public abstract class FunctionSet {
protected ArrayList<Integer> allowedFunctions;
protected String name;
+ /**
+ * @return the number of currently allowed functions.
+ */
public int getAllowedFunctionCount() {
return allowedFunctions.size();
}
+ /**
+ * @return the total number of functions, including disabled ones.
+ */
public int getTotalFunctionCount() {
return functionList.length;
}
+ /**
+ * Returns an allowed function. This throws an
+ * ArrayIndexOutOfBoundsException if the supplied
+ * index is beyond the count of allowed functions.
+ *
+ * @param index the allowed function index.
+ * @return the allowed function object.
+ */
public Function getAllowedFunction(int index) {
return functionList[allowedFunctions.get(index)];
}
+ /**
+ * Returns a function from the complete collection,
+ * enabled or disabled alike. This throws an
+ * ArrayIndexOutOfBoundsException if the supplied
+ * index is beyond the count of allowed functions.
+ *
+ * @param index the function index.
+ * @return the function object.
+ */
public Function getFunction(int index) {
return functionList[index];
}
+ /**
+ * Computes and returns the maximum arity out of
+ * all the function, enabled or disabled.
+ *
+ * @return
+ */
public int getMaxArity(){
int arity = 0;
for (Function function : functionList) {
+ // if a higher arity is found, store it
if (function.getArity() > arity) {
arity = function.getArity();
}
@@ -41,25 +87,39 @@ public abstract class FunctionSet {
return arity;
}
- public String getName() {
- return name;
- }
-
+ /**
+ * Disables the indexed function. If the function
+ * is already disabled, this does nothing.
+ *
+ * @param index the function to disable.
+ */
public void disableFunction(int index) {
+ /*
+ * allowedFunctions is a list of the indices of all allowed functions,
+ * as addressed in functionList. This method iterates through the whole
+ * list of allowed functions and removes any elements which are equal
+ * to the specified index.
+ */
if (index < functionList.length) {
for (Iterator<Integer> iterator = allowedFunctions.iterator(); iterator.hasNext();) {
int function = iterator.next();
if (function == index) {
iterator.remove();
- break;
}
}
} else {
- throw new IndexOutOfBoundsException("Function " + index + " does not exist, the set only has " + functionList.length + " functions.");
+ throw new ArrayIndexOutOfBoundsException("Function " + index + " does not exist, the set only has " + functionList.length + " functions.");
}
}
+ /**
+ * Disables the indexed function. If the function is
+ * already enabled, this does nothing.
+ *
+ * @param index the function to disable.
+ */
public void enableFunction(int index) {
+ // add the specified index to the list of allowed indices
if (!allowedFunctions.contains(index)) {
allowedFunctions.add(index);
Collections.sort(allowedFunctions);
@@ -71,16 +131,26 @@ public abstract class FunctionSet {
return name;
}
- public boolean isEnabled(Function f) {
+ /**
+ * Checks if a specified function is enabled. If the function
+ * does not belong in the FunctionSet, this returns false.
+ *
+ * @param function the function to check.
+ * @return true if the function is enabled.
+ */
+ public boolean isEnabled(Function function) {
for (int i = 0; i < allowedFunctions.size(); i++) {
- if (functionList[allowedFunctions.get(i)] == f) {
+ if (functionList[allowedFunctions.get(i)] == function) {
return true;
}
}
return false;
}
- protected void enableAll() {
+ /**
+ * Enables all functions.
+ */
+ public void enableAll() {
allowedFunctions = new ArrayList<Integer>();
for (int i = 0; i < functionList.length; i++) {
allowedFunctions.add(i);
diff --git a/src/jcgp/backend/function/SymbolicRegressionFunctions.java b/src/jcgp/backend/function/SymbolicRegressionFunctions.java
new file mode 100644
index 0000000..a35f258
--- /dev/null
+++ b/src/jcgp/backend/function/SymbolicRegressionFunctions.java
@@ -0,0 +1,587 @@
+package jcgp.backend.function;
+
+public class SymbolicRegressionFunctions extends FunctionSet {
+
+ public final static double DIVISION_LIMIT = 0.0001;
+
+ public SymbolicRegressionFunctions() {
+ name = "Symbolic regression functions";
+ functionList = new Function[] {
+ new Absolute(),
+ new SquareRoot(),
+ new Reciprocal(),
+ new Sine(),
+ new Cosine(),
+ new Tangent(),
+ new Exponential(),
+ new HyperbolicSine(),
+ new HyperbolicCosine(),
+ new HyperbolicTangent(),
+ new NaturalLog(),
+ new LogBaseTen(),
+ new SineAB(),
+ new CosineAB(),
+ new Hypotenuse(),
+ new Power(),
+ new Addition(),
+ new Subtraction(),
+ new Multiplication(),
+ new Division()};
+
+ enableAll();
+ }
+
+ /**
+ * Absolute returns the positive value of input 0.
+ *
+ * @see Math
+ */
+ public static class Absolute extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return Math.abs(in0);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Absolute";
+ }
+ }
+
+ /**
+ * Protected square root function, returns the square root of the absolute
+ * value of input 0.
+ *
+ * @see Math
+ */
+ public static class SquareRoot extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return Math.sqrt(Math.abs(in0));
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Square root";
+ }
+ }
+
+ /**
+ * Protected reciprocal function, returns (1 / input 0). If input 0 is less than
+ * {@code DoubleArithmetic.DIVISION_LIMIT}, this returns it unchanged.
+ *
+ */
+ public static class Reciprocal extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return in0 < DIVISION_LIMIT ? in0 : (1 / in0);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Reciprocal";
+ }
+ }
+
+ /**
+ * Sine function, in radians.
+ *
+ * @see Math
+ */
+ public static class Sine extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return Math.sin(in0);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Sin";
+ }
+ }
+
+ /**
+ * Cosine function, in radians.
+ *
+ * @see Math
+ */
+ public static class Cosine extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return Math.cos(in0);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Cos";
+ }
+ }
+
+ /**
+ * Protected tangent function, in radians. Returns the tangent of input 0.
+ * If input 0 is less than {@link DoubleArithmetic.}DIVISION_LIMIT,
+ * this returns it unchanged.
+ *
+ * @see Math
+ */
+ public static class Tangent extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return in0 < DIVISION_LIMIT ? in0 : Math.tan(in0);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Tan";
+ }
+ }
+
+ /**
+ * Exponential function. Returns the exponential of input 0.
+ *
+ * @see Math
+ */
+ public static class Exponential extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return Math.exp(in0);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Exp";
+ }
+ }
+
+ /**
+ * Returns the hyperbolic sine of input 0.
+ *
+ * @see Math
+ */
+ public static class HyperbolicSine extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return Math.sinh(in0);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Sinh";
+ }
+ }
+
+ /**
+ * Returns the hyperbolic cosine of input 0.
+ *
+ * @see Math
+ */
+ public static class HyperbolicCosine extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return Math.cosh(in0);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Cosh";
+ }
+ }
+
+ /**
+ * Returns the hyperbolic tangent of input 0.
+ *
+ * @see Math
+ */
+ public static class HyperbolicTangent extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return Math.tanh(in0);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Tanh";
+ }
+ }
+
+ /**
+ * Protected natural log function. Returns the natural log of the absolute
+ * value of input 0. If input 0 is less than {@link DoubleArithmetic.}DIVISION_LIMIT,
+ * this returns it unchanged.
+ *
+ * @see Math
+ */
+ public static class NaturalLog extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return in0 < DIVISION_LIMIT ? in0 : Math.log(Math.abs(in0));
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Ln";
+ }
+ }
+
+ /**
+ * Protected log base 10 function. Returns the log to base 10 the absolute
+ * value of input 0. If input 0 is less than {@link DoubleArithmetic.}DIVISION_LIMIT,
+ * this returns it unchanged.
+ *
+ * @see Math
+ */
+ public static class LogBaseTen extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return in0 < DIVISION_LIMIT ? in0 : Math.log10(Math.abs(in0));
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Log";
+ }
+ }
+
+ /**
+ * Sine of sum. Returns the sine of the sum of inputs 0 and 1.
+ *
+ * @see Math
+ */
+ public static class SineAB extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return Math.sin(in0 + in1);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Sin(a+b)";
+ }
+ }
+
+ /**
+ * Cosine of sum. Returns the cosine of the sum of inputs 0 and 1.
+ *
+ * @see Math
+ */
+ public static class CosineAB extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return Math.cos(in0 + in1);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Cos(a+b)";
+ }
+ }
+
+ /**
+ * Hypotenuse function. Returns the square root of input 0 squared
+ * plus input 1 squared.
+ *
+ * @see Math
+ */
+ public static class Hypotenuse extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return Math.hypot(in0, in1);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Hypotenuse";
+ }
+ }
+
+ /**
+ * Power function. Returns the absolute value of input 0 to the power of input 1.
+ *
+ * @see Math
+ */
+ public static class Power extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return Math.pow(Math.abs(in0), in1);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Power";
+ }
+ }
+
+ /**
+ * Addition returns the sum of inputs 0 and 1.
+ *
+ */
+ public static class Addition extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return in0 + in1;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Addition";
+ }
+ }
+
+ /**
+ * Subtraction returns the difference between inputs 0 and 1.
+ *
+ */
+ public static class Subtraction extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return in0 - in1;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Subtraction";
+ }
+ }
+
+ /**
+ * Multiplication returns the product of inputs 0 and 1.
+ *
+ */
+ public static class Multiplication extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return in0 * in1;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Multiplication";
+ }
+ }
+
+ /**
+ * Protected division, returns the quotient of input 0 (the dividend) and input 1 (the divisor).
+ * If the divisor is less than {@code DoubleArithmetic.DIVISION_LIMIT}, this returns it unchanged.
+ *
+ */
+ public static class Division extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+
+ return in1 < DIVISION_LIMIT ? in0 : (in0 / in1);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Division";
+ }
+ }
+}
diff --git a/src/jcgp/backend/function/TravellingSalesmanFunctions.java b/src/jcgp/backend/function/TravellingSalesmanFunctions.java
new file mode 100644
index 0000000..472b7ad
--- /dev/null
+++ b/src/jcgp/backend/function/TravellingSalesmanFunctions.java
@@ -0,0 +1,396 @@
+package jcgp.backend.function;
+
+public class TravellingSalesmanFunctions extends FunctionSet {
+
+ public TravellingSalesmanFunctions() {
+ name = "Travelling salesman functions";
+ functionList = new Function[]{
+ new SquareRoot(),
+ new Square(),
+ new Cube(),
+ new ScaledExponential(),
+ new AbsoluteSineAB(),
+ new AbsoluteCosineAB(),
+ new AbsoluteHyperbolicTangentAB(),
+ new ScaledHypotenuse(),
+ new ScaledAddition(),
+ new SymmetricSubtraction(),
+ new Multiplication(),
+ new BoundedDivision() };
+
+ enableAll();
+ }
+
+ /**
+ * Protected square root function, returns the square root of the absolute
+ * value of input 0.
+ *
+ * @see Math
+ */
+ public static class SquareRoot extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return Math.sqrt(Math.abs(in0));
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Square root";
+ }
+ }
+
+ /**
+ * Square function, returns the square of the
+ * value of input 0.
+ *
+ */
+ public static class Square extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return in0 * in0;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Square";
+ }
+ }
+
+ /**
+ * Cube function, returns the value of input 0 cubed.
+ *
+ */
+ public static class Cube extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return in0 * in0 * in0;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Cube";
+ }
+ }
+
+ /**
+ * Scaled exponential function. Returns the exponential of input 0
+ * scaled to the range 0 < x < 1.
+ *
+ * @see Math
+ */
+ public static class ScaledExponential extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ return (Math.exp(in0) - 1) / (Math.exp(-1) - 1);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 1;
+ }
+
+ @Override
+ public String toString() {
+ return "Scaled Exp";
+ }
+ }
+
+ /**
+ * Sine of sum. Returns the absolute value of the sine
+ * of the sum of inputs 0 and 1, in radians.
+ *
+ * @see Math
+ */
+ public static class AbsoluteSineAB extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return Math.abs(Math.sin(in0 + in1));
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Abs Sin(a+b)";
+ }
+ }
+
+ /**
+ * Cosine of sum. Returns the absolute value of the cosine
+ * of the sum of inputs 0 and 1, in radians.
+ *
+ * @see Math
+ */
+ public static class AbsoluteCosineAB extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return Math.abs(Math.cos(in0 + in1));
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Abs Cos(a+b)";
+ }
+ }
+
+ /**
+ * Hyperbolic tangent of sum. Returns the absolute value of the sine
+ * of the sum of inputs 0 and 1, in radians.
+ *
+ * @see Math
+ */
+ public static class AbsoluteHyperbolicTangentAB extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return Math.abs(Math.tanh(in0 + in1));
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Abs Tanh(a+b)";
+ }
+ }
+
+ /**
+ * Scaled hypotenuse function. Returns the square root of input 0 squared
+ * plus input 1 squared, scaled to the range 0 < x < 1.
+ *
+ * @see Math
+ */
+ public static class ScaledHypotenuse extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return Math.hypot(in0, in1) / Math.sqrt(2);
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Scaled Hypotenuse";
+ }
+ }
+
+ /**
+ * Scaled addition returns the sum of inputs 0 and 1 scaled
+ * to the range 0 < x < 1.
+ *
+ */
+ public static class ScaledAddition extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return (in0 + in1) / 2.0;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Scaled Addition";
+ }
+ }
+
+ /**
+ * Symmetric subtraction returns the absolute difference between inputs 0 and 1,
+ * scaled to the range 0 < x < 1.
+ *
+ */
+ public static class SymmetricSubtraction extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return Math.abs(in0 - in1) / 2.0;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Symmetric Subtraction";
+ }
+ }
+
+ /**
+ * Multiplication returns the product of inputs 0 and 1.
+ *
+ */
+ public static class Multiplication extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return in0 * in1;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Multiplication";
+ }
+ }
+
+ /**
+ * Multiplication returns the product of inputs 0 and 1, squared.
+ *
+ */
+ public static class SquaredMultiplication extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = ((Double) args[0]);
+ Double in1 = ((Double) args[1]);
+ return in0 * in1 * in0 * in1;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Squared Multiplication";
+ }
+ }
+
+ /**
+ * Bounded division, returns the quotient of the two inputs where the larger
+ * is the denominator.
+ *
+ */
+ public static class BoundedDivision extends Function {
+ @Override
+ public Double run(Object... args) {
+ if (args.length < getArity()) {
+ throw new IllegalArgumentException(toString() + " received " + args.length + " arguments but arity is " + getArity() + ".");
+ } else {
+ Double in0 = Math.abs((Double) args[0]);
+ Double in1 = Math.abs((Double) args[1]);
+ Double result;
+
+ if (in1 < 1e-10) {
+ result = 1.0;
+ } else if (in1 > in0) {
+ result = in0 / in1;
+ } else {
+ result = in1 / in0;
+ }
+
+ return result;
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public String toString() {
+ return "Bounded Division";
+ }
+ }
+}