Skip to content

Commit dc6607b

Browse files
authored
Merge pull request #24 from TimeDelta/codex/write-unit-tests-for-pareto.py
Add unit tests for Pareto
2 parents e3c4741 + 3ea2bff commit dc6607b

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tests/test_pareto.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os, sys
2+
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
3+
import pytest
4+
5+
from pareto import dominates, nondominated_sort
6+
from metrics import MSELoss, TimeCost
7+
8+
9+
class MaxMetric:
10+
"""Simple metric with maximization objective for testing."""
11+
12+
name = "MaxMetric"
13+
objective = "max"
14+
15+
16+
def test_dominates_all_min_objectives():
17+
metrics = {
18+
1: {MSELoss: 0.1, TimeCost: 1.0},
19+
2: {MSELoss: 0.2, TimeCost: 2.0},
20+
}
21+
assert dominates(metrics, 1, 2)
22+
assert not dominates(metrics, 2, 1)
23+
24+
25+
def test_dominates_mixed_objectives():
26+
metrics = {
27+
1: {MSELoss: 0.1, MaxMetric: 0.5},
28+
2: {MSELoss: 0.2, MaxMetric: 0.4},
29+
3: {MSELoss: 0.1, MaxMetric: 0.5},
30+
}
31+
# Genome 1 dominates 2 because it is better in both metrics
32+
assert dominates(metrics, 1, 2)
33+
# Genome 1 does not dominate 3 because all metrics are equal
34+
assert not dominates(metrics, 1, 3)
35+
# Genome 2 does not dominate 1 due to worse metrics
36+
assert not dominates(metrics, 2, 1)
37+
38+
39+
def test_nondominated_sort():
40+
metrics = {
41+
1: {MSELoss: 1.0, MaxMetric: 2.0},
42+
2: {MSELoss: 2.0, MaxMetric: 1.0},
43+
3: {MSELoss: 1.0, MaxMetric: 1.0},
44+
}
45+
fronts = nondominated_sort(metrics)
46+
# Expected order: [1] dominates 3, which dominates 2
47+
assert fronts == [[1], [3], [2]]
48+

0 commit comments

Comments
 (0)