Skip to content

Commit c5a1c8a

Browse files
committed
Update tests
1 parent c9e0a4f commit c5a1c8a

File tree

9 files changed

+434
-58
lines changed

9 files changed

+434
-58
lines changed

src/test/java/org/moeaframework/benchmarks/BenchmarkProviderTest.java renamed to src/test/java/org/moeaframework/benchmarks/AbstractProblemTest.java

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,68 @@
1717
*/
1818
package org.moeaframework.benchmarks;
1919

20+
import java.util.Arrays;
2021
import java.util.concurrent.TimeUnit;
2122

2223
import org.junit.Assert;
2324
import org.junit.Assume;
24-
import org.junit.Test;
25+
import org.junit.Ignore;
2526
import org.moeaframework.algorithm.NSGAII;
27+
import org.moeaframework.core.Settings;
28+
import org.moeaframework.core.Solution;
2629
import org.moeaframework.core.population.NondominatedPopulation;
2730
import org.moeaframework.core.spi.ProblemFactory;
31+
import org.moeaframework.core.variable.RealVariable;
2832
import org.moeaframework.problem.Problem;
2933

30-
/**
31-
* Tests to ensure each benchmark problem can be instantiated with the MOEA Framework and reference sets exist.
32-
*/
33-
public class BenchmarkProviderTest {
34+
@Ignore("abstract test class")
35+
public class AbstractProblemTest {
3436

35-
protected void test(String problemName) {
37+
protected void testSolve(String problemName) {
3638
try (Problem problem = ProblemFactory.getInstance().getProblem(problemName)) {
37-
Assert.assertNotNull(problem);
38-
Assert.assertEquals(problemName, problem.getName());
39+
Assert.assertNotNull("Problem not defined", problem);
40+
Assert.assertEquals("Problem name must match", problemName, problem.getName());
3941

4042
NSGAII algorithm = new NSGAII(problem);
4143
algorithm.run(1000);
4244

4345
NondominatedPopulation result = algorithm.getResult();
4446

45-
Assert.assertNotNull(result);
46-
Assert.assertFalse(result.isEmpty());
47+
Assert.assertNotNull("Expected non-null result", result);
48+
Assert.assertFalse("Expected non-empty result", result.isEmpty());
4749
}
4850
}
4951

5052
protected void testReferenceSet(String problemName) {
51-
Assert.assertNotNull("Missing reference set", ProblemFactory.getInstance().getReferenceSet(problemName));
53+
NondominatedPopulation referenceSet = ProblemFactory.getInstance().getReferenceSet(problemName);
54+
Assert.assertNotNull("Expected reference set", referenceSet);
55+
Assert.assertFalse("Expected non-empty reference set", referenceSet.isEmpty());
56+
}
57+
58+
protected void testSolution(String problemName, double[] variables, double[] expectedObjectives, double[] expectedConstraints, boolean isFeasible) {
59+
try (Problem problem = ProblemFactory.getInstance().getProblem(problemName)) {
60+
Solution solution = problem.newSolution();
61+
RealVariable.setReal(solution, variables);
62+
63+
problem.evaluate(solution);
64+
65+
try {
66+
Assert.assertArrayEquals("Objectives do not match", expectedObjectives, solution.getObjectiveValues(), Settings.EPS);
67+
} catch (AssertionError e) {
68+
System.out.println("Actual Objectives: " + Arrays.toString(solution.getObjectiveValues()));
69+
throw e;
70+
}
71+
72+
try {
73+
Assert.assertArrayEquals("Constraints do not match", expectedConstraints, solution.getConstraintValues(), Settings.EPS);
74+
} catch (AssertionError e) {
75+
System.out.println("Actual Constraints: " + Arrays.toString(solution.getConstraintValues()));
76+
throw e;
77+
}
78+
79+
Assert.assertEquals("Feasibility does not match", isFeasible, solution.isFeasible());
80+
81+
}
5282
}
5383

5484
protected void requiresMatlab() {
@@ -69,52 +99,5 @@ protected void requiresMatlab() {
6999
Assume.assumeNoException("Caught exception when invoking process", e);
70100
}
71101
}
72-
73-
@Test
74-
public void testCarSideImpact() {
75-
test("CarSideImpact");
76-
testReferenceSet("CarSideImpact");
77-
}
78-
79-
@Test
80-
public void testElectricMotor() {
81-
test("ElectricMotor");
82-
testReferenceSet("ElectricMotor");
83-
}
84-
85-
@Test
86-
public void testGAA() {
87-
test("GAA");
88-
testReferenceSet("GAA");
89-
}
90-
91-
@Test
92-
public void testHBV() {
93-
test("HBV");
94-
testReferenceSet("HBV");
95-
}
96-
97-
@Test
98-
public void testLakeProblem() {
99-
test("LakeProblem");
100-
testReferenceSet("LakeProblem");
101-
}
102-
103-
@Test
104-
public void testLRGV() {
105-
test("LRGV");
106-
}
107-
108-
@Test
109-
public void testRadar() {
110-
requiresMatlab();
111-
test("Radar");
112-
}
113-
114-
@Test
115-
public void testWDS() {
116-
test("WDS(GOY)");
117-
testReferenceSet("WDS(GOY)");
118-
}
119102

120103
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright 2009-2024 David Hadka and other contributors
2+
*
3+
* This file is part of the MOEA Framework.
4+
*
5+
* The MOEA Framework is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or (at your
8+
* option) any later version.
9+
*
10+
* The MOEA Framework is distributed in the hope that it will be useful, but
11+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13+
* License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with the MOEA Framework. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.moeaframework.benchmarks;
19+
20+
import org.junit.Test;
21+
22+
public class CarSideImpactTest extends AbstractProblemTest {
23+
24+
@Test
25+
public void testSolve() {
26+
testSolve("CarSideImpact");
27+
}
28+
29+
@Test
30+
public void testReferenceSet() {
31+
testReferenceSet("CarSideImpact");
32+
}
33+
34+
@Test
35+
public void testLowerBound() {
36+
testSolution("CarSideImpact",
37+
new double[] { 0.5, 0.45, 0.5, 0.5, 0.875, 0.4, 0.4 },
38+
new double[] { 15.576004000000003, 4.42725, 13.091381250000001 },
39+
new double[] { 1.07172109999999998, 0.23405105999999998, 0.20441605000000002, 0.48307069999999996, 29.380924, 32.569465, 39.67975, 4.42725, 10.1256125, 16.05715 },
40+
false);
41+
}
42+
43+
@Test
44+
public void testUpperBound() {
45+
testSolution("CarSideImpact",
46+
new double[] { 1.5, 1.35, 1.5, 1.5, 2.625, 1.2, 1.2 },
47+
new double[] { 42.768012, 3.58525, 10.61064375 },
48+
new double[] { 0.39336829999999995, 0.17596818000000003, 0.16976335, 0.24501710000000013, 24.512772, 20.246884999999995, 26.319249999999997, 3.58525, 8.3069375, 12.914349999999999 },
49+
true);
50+
}
51+
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright 2009-2024 David Hadka and other contributors
2+
*
3+
* This file is part of the MOEA Framework.
4+
*
5+
* The MOEA Framework is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or (at your
8+
* option) any later version.
9+
*
10+
* The MOEA Framework is distributed in the hope that it will be useful, but
11+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13+
* License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with the MOEA Framework. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.moeaframework.benchmarks;
19+
20+
import org.junit.Test;
21+
22+
public class ElectricMotorTest extends AbstractProblemTest {
23+
24+
@Test
25+
public void testSolve() {
26+
testSolve("ElectricMotor");
27+
}
28+
29+
@Test
30+
public void testReferenceSet() {
31+
testReferenceSet("ElectricMotor");
32+
}
33+
34+
@Test
35+
public void testLowerBound() {
36+
testSolution("ElectricMotor",
37+
new double[] { 100.0, 0.01, 1.0, 0.01, 0.01, 5.0E-4, 0.001, 0.1, 100.0, 0.01, 1.0, 0.01, 0.01, 5.0E-4, 0.001, 0.1, 100.0, 0.01, 1.0, 0.01, 0.01, 5.0E-4, 0.001, 0.1, 100.0, 0.01, 1.0, 0.01, 0.01, 5.0E-4, 0.001, 0.1, 100.0, 0.01, 1.0, 0.01, 0.01, 5.0E-4, 0.001, 0.1, 100.0, 0.01, 1.0, 0.01, 0.01, 5.0E-4, 0.001, 0.1, 100.0, 0.01, 1.0, 0.01, 0.01, 5.0E-4, 0.001, 0.1, 100.0, 0.01, 1.0, 0.01, 0.01, 5.0E-4, 0.001, 0.1, 100.0, 0.01, 1.0, 0.01, 0.01, 5.0E-4, 0.001, 0.1, 100.0, 0.01, 1.0, 0.01, 0.01, 5.0E-4, 0.001, 0.1 },
38+
new double[] { -2.1178860361093237E-5, 0.9829575175304347, -2.1178860361093237E-5, 0.9829575175304347, -2.1178860361093237E-5, 0.9829575175304347, -2.1178860361093237E-5, 0.9829575175304347, -2.1178860361093237E-5, 0.9829575175304347, -2.1178860361093237E-5, 0.9829575175304347, -2.1178860361093237E-5, 0.9829575175304347, -2.1178860361093237E-5, 0.9829575175304347, -2.1178860361093237E-5, 0.9829575175304347, -2.1178860361093237E-5, 0.9829575175304347 },
39+
new double[] { 0.05000000000380373, 288.6959885484, 0.0, 0.0, 0.0, 0.0, 0.10000000000380374, 288.6959885484, 0.0, 0.0, 0.0, 0.0, 0.12500000000380374, 288.6959885484, 0.0, 0.0, 0.0, 0.0, 0.15000000000380373, 288.6959885484, 0.0, 0.0, 0.0, 0.0, 0.20000000000380375, 288.6959885484, 0.0, 0.0, 0.0, 0.0, 0.25000000000380374, 288.6959885484, 0.0, 0.0, 0.0, 0.0, 0.3000000000038037, 288.6959885484, 0.0, 0.0, 0.0, 0.0, 0.3500000000038037, 288.6959885484, 0.0, 0.0, 0.0, 0.0, 0.40000000000380376, 288.6959885484, 0.0, 0.0, 0.0, 0.0, 0.5000000000038037, 288.6959885484, 0.0, 0.0, 0.0, 0.0 },
40+
false);
41+
}
42+
43+
@Test
44+
public void testUpperBound() {
45+
testSolution("ElectricMotor",
46+
new double[] { 1500.0, 1.0, 500.0, 1.0, 0.1, 0.1, 0.1, 6.0, 1500.0, 1.0, 500.0, 1.0, 0.1, 0.1, 0.1, 6.0, 1500.0, 1.0, 500.0, 1.0, 0.1, 0.1, 0.1, 6.0, 1500.0, 1.0, 500.0, 1.0, 0.1, 0.1, 0.1, 6.0, 1500.0, 1.0, 500.0, 1.0, 0.1, 0.1, 0.1, 6.0, 1500.0, 1.0, 500.0, 1.0, 0.1, 0.1, 0.1, 6.0, 1500.0, 1.0, 500.0, 1.0, 0.1, 0.1, 0.1, 6.0, 1500.0, 1.0, 500.0, 1.0, 0.1, 0.1, 0.1, 6.0, 1500.0, 1.0, 500.0, 1.0, 0.1, 0.1, 0.1, 6.0, 1500.0, 1.0, 500.0, 1.0, 0.1, 0.1, 0.1, 6.0 },
47+
new double[] { 0.08781367214553605, 0.9739676521739131, 0.08781367214553605, 0.9739676521739131, 0.08781367214553605, 0.9739676521739131, 0.08781367214553605, 0.9739676521739131, 0.08781367214553605, 0.9739676521739131, 0.08781367214553605, 0.9739676521739131, 0.08781367214553605, 0.9739676521739131, 0.08781367214553605, 0.9739676521739131, 0.08781367214553605, 0.9739676521739131, 0.08781367214553605, 0.9739676521739131 },
48+
new double[] { 0.04692963432538349, 372.03768, 0.0, 583388.4728341918, 0.0, 0.0, 0.0969296343253835, 372.03768, 0.0, 583388.4728341918, 0.0, 0.0, 0.12192963432538349, 372.03768, 0.0, 583388.4728341918, 0.0, 0.0, 0.14692963432538347, 372.03768, 0.0, 583388.4728341918, 0.0, 0.0, 0.1969296343253835, 372.03768, 0.0, 583388.4728341918, 0.0, 0.0, 0.24692963432538348, 372.03768, 0.0, 583388.4728341918, 0.0, 0.0, 0.2969296343253835, 372.03768, 0.0, 583388.4728341918, 0.0, 0.0, 0.3469296343253835, 372.03768, 0.0, 583388.4728341918, 0.0, 0.0, 0.39692963432538353, 372.03768, 0.0, 583388.4728341918, 0.0, 0.0, 0.4969296343253835, 372.03768, 0.0, 583388.4728341918, 0.0, 0.0 },
49+
false);
50+
}
51+
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright 2009-2024 David Hadka and other contributors
2+
*
3+
* This file is part of the MOEA Framework.
4+
*
5+
* The MOEA Framework is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or (at your
8+
* option) any later version.
9+
*
10+
* The MOEA Framework is distributed in the hope that it will be useful, but
11+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13+
* License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with the MOEA Framework. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.moeaframework.benchmarks;
19+
20+
import org.junit.Test;
21+
22+
public class GAATest extends AbstractProblemTest {
23+
24+
@Test
25+
public void testSolve() {
26+
testSolve("GAA");
27+
}
28+
29+
@Test
30+
public void testReferenceSet() {
31+
testReferenceSet("GAA");
32+
}
33+
34+
@Test
35+
public void testLowerBound() {
36+
testSolution("GAA",
37+
new double[] { 0.24, 7.0, 0.0, 5.5, 19.0, 85.0, 14.0, 3.0, 0.46, 0.24, 7.0, 0.0, 5.5, 19.0, 85.0, 14.0, 3.0, 0.46, 0.24, 7.0, 0.0, 5.5, 19.0, 85.0, 14.0, 3.0, 0.46 },
38+
new double[] { 73.239998, 1880.3199970000003, 62.38500200000003, 2.1867999999999994, 480.173996, 41699.24730800003, 3032.0586889999995, 15.726500000000003, 189.25630300000014, 1.9229626863835638E-16 },
39+
new double[] { 0.33805332444444436 },
40+
false);
41+
}
42+
43+
@Test
44+
public void testUpperBound() {
45+
testSolution("GAA",
46+
new double[] { 0.48, 11.0, 6.0, 5.968, 25.0, 110.0, 20.0, 3.75, 1.0, 0.48, 11.0, 6.0, 5.968, 25.0, 110.0, 20.0, 3.75, 1.0, 0.48, 11.0, 6.0, 5.968, 25.0, 110.0, 20.0, 3.75, 1.0 },
47+
new double[] { 75.19549799999994, 2097.8436029999993, 95.00900000000001, 2.078, 291.2477919999998, 47369.88729400002, 891.8127029999995, 17.929600000000004, 198.903706, 0.0 },
48+
new double[] { 2.3017063231666643 },
49+
false);
50+
}
51+
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright 2009-2024 David Hadka and other contributors
2+
*
3+
* This file is part of the MOEA Framework.
4+
*
5+
* The MOEA Framework is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or (at your
8+
* option) any later version.
9+
*
10+
* The MOEA Framework is distributed in the hope that it will be useful, but
11+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13+
* License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with the MOEA Framework. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.moeaframework.benchmarks;
19+
20+
import org.junit.Test;
21+
22+
public class HBVTest extends AbstractProblemTest {
23+
24+
@Test
25+
public void testSolve() {
26+
testSolve("HBV");
27+
}
28+
29+
@Test
30+
public void testReferenceSet() {
31+
testReferenceSet("HBV");
32+
}
33+
34+
@Test
35+
public void testLowerBound() {
36+
testSolution("HBV",
37+
new double[] { 0.0, 0.5, 1.0, 10.0, 0.0, 0.3, 0.0, 0.0, 24.0, -3.0, 0.0, 0.0, 0.0, 0.0 },
38+
new double[] { 9.910673, 3.527903, 1.225312, 1.327312 },
39+
new double[] { },
40+
true);
41+
}
42+
43+
@Test
44+
public void testUpperBound() {
45+
testSolution("HBV",
46+
new double[] { 100.0, 20.0, 100.0, 20000.0, 100.0, 1.0, 2000.0, 7.0, 120.0, 3.0, 20.0, 1.0, 0.8, 7.0 },
47+
new double[] { 0.9951112, 3.319143, 0.6665916, 0.9936301 },
48+
new double[] { },
49+
true);
50+
}
51+
52+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Copyright 2009-2024 David Hadka and other contributors
2+
*
3+
* This file is part of the MOEA Framework.
4+
*
5+
* The MOEA Framework is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or (at your
8+
* option) any later version.
9+
*
10+
* The MOEA Framework is distributed in the hope that it will be useful, but
11+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13+
* License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with the MOEA Framework. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.moeaframework.benchmarks;
19+
20+
import org.junit.Test;
21+
22+
public class LRGVTest extends AbstractProblemTest {
23+
24+
@Test
25+
public void testSolve() {
26+
testSolve("LRGV");
27+
}
28+
29+
@Test
30+
public void testLowerBound() {
31+
testSolution("LRGV",
32+
new double[] { 0.0, 0.0, 0.1, 0.1, 0.0, 0.0, 0.0, 0.0 },
33+
new double[] { 0.0678, 0.5427500000000001, 0.052332950946083, 0.0, 0.0, 8.237323224016374E-67 },
34+
new double[] { -0.6038095238095238, -0.4517676767676767, 0.0, 0.0 },
35+
false);
36+
}
37+
38+
@Test
39+
public void testUpperBound() {
40+
testSolution("LRGV",
41+
new double[] { 1.0, 1.0, 1.0, 0.4, 3.0, 0.0, 3.0, 0.0 },
42+
new double[] { 0.14687273772049875, 1.0, 0.7396398491493245, 0.0012601974979137415, 7.8E-4, 8.237323224016374E-67 },
43+
new double[] { 0.0, 0.0, -0.0059376725451394385, 0.0 },
44+
false);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)