Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Dec 20, 2024
1 parent c5a1c8a commit 5dd7e0f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
import org.moeaframework.core.Solution;
import org.moeaframework.core.population.NondominatedPopulation;
import org.moeaframework.core.spi.ProblemFactory;
import org.moeaframework.core.variable.BinaryIntegerVariable;
import org.moeaframework.core.variable.RealVariable;
import org.moeaframework.problem.Problem;

@Ignore("abstract test class")
public class AbstractProblemTest {

private static final double EPS = 0.0001;

protected void testSolve(String problemName) {
try (Problem problem = ProblemFactory.getInstance().getProblem(problemName)) {
Assert.assertNotNull("Problem not defined", problem);
Expand Down Expand Up @@ -63,14 +66,40 @@ protected void testSolution(String problemName, double[] variables, double[] exp
problem.evaluate(solution);

try {
Assert.assertArrayEquals("Objectives do not match", expectedObjectives, solution.getObjectiveValues(), Settings.EPS);
Assert.assertArrayEquals("Objectives do not match", expectedObjectives, solution.getObjectiveValues(), EPS);
} catch (AssertionError e) {
System.out.println("Actual Objectives: " + Arrays.toString(solution.getObjectiveValues()));
throw e;
}

try {
Assert.assertArrayEquals("Constraints do not match", expectedConstraints, solution.getConstraintValues(), EPS);
} catch (AssertionError e) {
System.out.println("Actual Constraints: " + Arrays.toString(solution.getConstraintValues()));
throw e;
}

Assert.assertEquals("Feasibility does not match", isFeasible, solution.isFeasible());

}
}

protected void testSolution(String problemName, int[] variables, double[] expectedObjectives, double[] expectedConstraints, boolean isFeasible) {
try (Problem problem = ProblemFactory.getInstance().getProblem(problemName)) {
Solution solution = problem.newSolution();
BinaryIntegerVariable.setInt(solution, variables);

problem.evaluate(solution);

try {
Assert.assertArrayEquals("Objectives do not match", expectedObjectives, solution.getObjectiveValues(), EPS);
} catch (AssertionError e) {
System.out.println("Actual Objectives: " + Arrays.toString(solution.getObjectiveValues()));
throw e;
}

try {
Assert.assertArrayEquals("Constraints do not match", expectedConstraints, solution.getConstraintValues(), Settings.EPS);
Assert.assertArrayEquals("Constraints do not match", expectedConstraints, solution.getConstraintValues(), EPS);
} catch (AssertionError e) {
System.out.println("Actual Constraints: " + Arrays.toString(solution.getConstraintValues()));
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void testLowerBound() {
testSolution("ElectricMotor",
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 },
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 },
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 },
new double[] { 0.05000000000380373, 288.6959885484, 200.00000000000003, 194.5734766747823, -2.1178860361093237E-5, 0.9829575175304347, 0.10000000000380374, 288.6959885484, 200.00000000000003, 194.5734766747823, -2.1178860361093237E-5, 0.9829575175304347, 0.12500000000380374, 288.6959885484, 200.00000000000003, 194.5734766747823, -2.1178860361093237E-5, 0.9829575175304347, 0.15000000000380373, 288.6959885484, 200.00000000000003, 194.5734766747823, -2.1178860361093237E-5, 0.9829575175304347, 0.20000000000380375, 288.6959885484, 200.00000000000003, 194.5734766747823, -2.1178860361093237E-5, 0.9829575175304347, 0.25000000000380374, 288.6959885484, 200.00000000000003, 194.5734766747823, -2.1178860361093237E-5, 0.9829575175304347, 0.3000000000038037, 288.6959885484, 200.00000000000003, 194.5734766747823, -2.1178860361093237E-5, 0.9829575175304347, 0.3500000000038037, 288.6959885484, 200.00000000000003, 194.5734766747823, -2.1178860361093237E-5, 0.9829575175304347, 0.40000000000380376, 288.6959885484, 200.00000000000003, 194.5734766747823, -2.1178860361093237E-5, 0.9829575175304347, 0.5000000000038037, 288.6959885484, 200.00000000000003, 194.5734766747823, -2.1178860361093237E-5, 0.9829575175304347 },
false);
}

Expand All @@ -45,7 +45,7 @@ public void testUpperBound() {
testSolution("ElectricMotor",
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 },
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 },
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 },
new double[] { 0.04692963432538349, 372.03768, 10.0, 588388.4728341918, 0.08781367214553605, 0.9739676521739131, 0.0969296343253835, 372.03768, 10.0, 588388.4728341918, 0.08781367214553605, 0.9739676521739131, 0.12192963432538349, 372.03768, 10.0, 588388.4728341918, 0.08781367214553605, 0.9739676521739131, 0.14692963432538347, 372.03768, 10.0, 588388.4728341918, 0.08781367214553605, 0.9739676521739131, 0.1969296343253835, 372.03768, 10.0, 588388.4728341918, 0.08781367214553605, 0.9739676521739131, 0.24692963432538348, 372.03768, 10.0, 588388.4728341918, 0.08781367214553605, 0.9739676521739131, 0.2969296343253835, 372.03768, 10.0, 588388.4728341918, 0.08781367214553605, 0.9739676521739131, 0.3469296343253835, 372.03768, 10.0, 588388.4728341918, 0.08781367214553605, 0.9739676521739131, 0.39692963432538353, 372.03768, 10.0, 588388.4728341918, 0.08781367214553605, 0.9739676521739131, 0.4969296343253835, 372.03768, 10.0, 588388.4728341918, 0.08781367214553605, 0.9739676521739131 },
false);
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/moeaframework/benchmarks/RadarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
*/
package org.moeaframework.benchmarks;

import org.junit.Before;
import org.junit.Test;

public class RadarTest extends AbstractProblemTest {

@Before
public void setUp() {
requiresMatlab();
}

@Test
public void testSolve() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/moeaframework/benchmarks/WDSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void testReferenceSet() {
@Test
public void testLowerBound() {
testSolution("WDS(GOY)",
new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new double[] { 0.17467288672924042, -6.119567394256592 },
new double[] { -2390.6025390625 },
false);
Expand All @@ -43,7 +43,7 @@ public void testLowerBound() {
@Test
public void testUpperBound() {
testSolution("WDS(GOY)",
new double[] { 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999, 7.999999999999999 },
new int[] { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 },
new double[] { 0.3297256529331207, 0.7125067114830017 },
new double[] { 0.0 },
true);
Expand Down

0 comments on commit 5dd7e0f

Please sign in to comment.