Skip to content

Commit 45349c4

Browse files
committed
Update unit tests
1 parent e5c18fd commit 45349c4

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

src/jmetal/util/archive.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
from typing import TypeVar, Generic, List, Optional
2+
from abc import ABC, abstractmethod
3+
from scipy.spatial import cKDTree
4+
5+
S = TypeVar('S')
6+
7+
class Archive(Generic[S], ABC):
8+
def __init__(self):
9+
self.solution_list: List[S] = []
10+
11+
@abstractmethod
12+
def add(self, solution: S) -> bool:
13+
pass
14+
15+
def get(self, index: int) -> S:
16+
return self.solution_list[index]
17+
18+
def size(self) -> int:
19+
return len(self.solution_list)
20+
21+
def get_name(self) -> str:
22+
return self.__class__.__name__
23+
124
import copy
225
import random
326
import threading

tests/util/test_archive.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import unittest
2+
import time
3+
import random
4+
15
import unittest
26

37
from jmetal.core.solution import Solution
@@ -113,6 +117,35 @@ def test_should_adding_three_solutions_work_properly_if_two_of_them_are_equal(se
113117
self.assertFalse(result)
114118
self.assertTrue(solution1 in self.archive.solution_list or solution3 in self.archive.solution_list)
115119

120+
def test_should_add_high_dimensional_solutions(self):
121+
"""Test behavior with solutions having more than 2 objectives. Only one solution should remain due to dominance logic."""
122+
archive = NonDominatedSolutionsArchive()
123+
s1 = Solution(1, 5)
124+
s1.objectives = [0.0, 1.0, 2.0, 3.0, 4.0]
125+
s2 = Solution(1, 5)
126+
s2.objectives = [1.0, 2.0, 3.0, 4.0, 5.0]
127+
s3 = Solution(1, 5)
128+
s3.objectives = [0.5, 1.5, 2.5, 3.5, 4.5]
129+
archive.add(s1)
130+
archive.add(s2)
131+
archive.add(s3)
132+
# Only one solution should remain, as the dominance logic removes dominated solutions
133+
self.assertEqual(1, archive.size())
134+
self.assertTrue(s1 in archive.solution_list or s2 in archive.solution_list or s3 in archive.solution_list)
135+
136+
def test_should_add_with_numerical_tolerance(self):
137+
"""Test adding nearly identical solutions (numerical tolerance). Only one should be kept if they are equal within tolerance."""
138+
archive = NonDominatedSolutionsArchive(objective_tolerance=1e-5)
139+
s1 = Solution(1, 2)
140+
s1.objectives = [1.000000, 2.000000]
141+
s2 = Solution(1, 2)
142+
s2.objectives = [1.000001, 2.000001]
143+
archive.add(s1)
144+
archive.add(s2)
145+
# Only one solution should be kept, as they are equal within the tolerance
146+
self.assertEqual(1, archive.size())
147+
self.assertTrue(s1 in archive.solution_list or s2 in archive.solution_list)
148+
116149

117150
class CrowdingDistanceArchiveTestCases(unittest.TestCase):
118151
def setUp(self):

0 commit comments

Comments
 (0)