From f01258fff1c9bac02296943cc38cab90a0457823 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Mon, 1 Dec 2014 19:22:30 +0100 Subject: [PATCH] Fix Step function and add test for known optimum. --- src/eva2/problems/F3Problem.java | 8 +++++--- test/eva2/problems/F3ProblemTest.java | 24 ++++++++++++++++++++++++ test/eva2/tools/SerializerTest.java | 2 -- 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 test/eva2/problems/F3ProblemTest.java diff --git a/src/eva2/problems/F3Problem.java b/src/eva2/problems/F3Problem.java index 9ebab745..696f01f3 100644 --- a/src/eva2/problems/F3Problem.java +++ b/src/eva2/problems/F3Problem.java @@ -28,7 +28,9 @@ public class F3Problem extends AbstractProblemDoubleOffset implements java.io.Se } /** - * Ths method allows you to evaluate a double[] to determine the fitness + * Evaluates a double vector according to the Step function. + * + * fitness = sum(floor(x_i + 0.5)^2) * * @param x The n-dimensional input vector * @return The m-dimensional output vector. @@ -37,9 +39,9 @@ public class F3Problem extends AbstractProblemDoubleOffset implements java.io.Se public double[] evaluate(double[] x) { x = rotateMaybe(x); double[] result = new double[1]; - result[0] = yOffset + 6 * x.length; + result[0] = yOffset; for (int i = 0; i < x.length - 1; i++) { - result[0] += Math.floor(x[i] - this.xOffset); + result[0] += Math.pow(Math.floor(x[i] + 0.5 - this.xOffset), 2); } return result; } diff --git a/test/eva2/problems/F3ProblemTest.java b/test/eva2/problems/F3ProblemTest.java new file mode 100644 index 00000000..05b84524 --- /dev/null +++ b/test/eva2/problems/F3ProblemTest.java @@ -0,0 +1,24 @@ +package eva2.problems; + +import eva2.tools.math.Mathematics; +import junit.framework.TestCase; + +public class F3ProblemTest extends TestCase { + + /** + * The Step function has its minimum + * at x_{i} = 0 + * + * @throws Exception + */ + public void testEvaluate() throws Exception { + F3Problem problem = new F3Problem(); + + for (int i = 1; i < 100; i++) { + double[] x = Mathematics.zeroes(i); + double[] res = problem.evaluate(x); + assertEquals(1, res.length); + assertEquals(0.0, res[0]); + } + } +} \ No newline at end of file diff --git a/test/eva2/tools/SerializerTest.java b/test/eva2/tools/SerializerTest.java index 23bcaa28..9082f127 100644 --- a/test/eva2/tools/SerializerTest.java +++ b/test/eva2/tools/SerializerTest.java @@ -68,7 +68,6 @@ public class SerializerTest { */ @Test public void testStoreObject() { - System.out.println("storeObject"); OutputStream outStream = null; Serializable s = null; Serializer.storeObject(outStream, s); @@ -81,7 +80,6 @@ public class SerializerTest { */ @Test public void testLoadObject_InputStream() { - System.out.println("loadObject"); InputStream inputStream = null; Object expResult = null; Object result = Serializer.loadObject(inputStream);