From ccb1a8cdd3288920c06a354372e23860e541156d Mon Sep 17 00:00:00 2001 From: Bhaveshmeghwal21 <118756339+Bhaveshmeghwal21@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:57:14 +0530 Subject: [PATCH 1/2] fix(metrics): ignore a NaN combined_score when computing fitness get_fitness_score returned a NaN combined_score as it was. NaN loses every comparison, so a program with that fitness could never be beaten and stayed the best program. Fall back to the other metrics, as for any other unusable combined_score. --- openevolve/utils/metrics_utils.py | 5 ++- tests/test_nan_combined_score.py | 52 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/test_nan_combined_score.py diff --git a/openevolve/utils/metrics_utils.py b/openevolve/utils/metrics_utils.py index 351001849..7a3e62778 100644 --- a/openevolve/utils/metrics_utils.py +++ b/openevolve/utils/metrics_utils.py @@ -98,7 +98,10 @@ def get_fitness_score( # Always prefer combined_score if available if "combined_score" in metrics: try: - return float(metrics["combined_score"]) + combined_score = float(metrics["combined_score"]) + # NaN loses every comparison, so a program scored NaN could never be beaten + if not math.isnan(combined_score): + return combined_score except (ValueError, TypeError): pass diff --git a/tests/test_nan_combined_score.py b/tests/test_nan_combined_score.py new file mode 100644 index 000000000..5dd59a607 --- /dev/null +++ b/tests/test_nan_combined_score.py @@ -0,0 +1,52 @@ +""" +A NaN `combined_score` is not a usable fitness. + +`get_fitness_score` already skips NaN when it averages the other metrics, but it +returned a NaN `combined_score` as it was. NaN compares false against everything, +so a program with that fitness could never be beaten and stayed the best program. +""" + +import math +import os +import unittest + +os.environ.setdefault("OPENAI_API_KEY", "test") + +from openevolve.config import Config +from openevolve.database import Program, ProgramDatabase +from openevolve.utils.metrics_utils import get_fitness_score + +NAN = float("nan") + + +class TestNanCombinedScore(unittest.TestCase): + def test_nan_combined_score_falls_back_to_the_other_metrics(self): + fitness = get_fitness_score({"combined_score": NAN, "accuracy": 0.5, "speed": 0.25}) + + self.assertEqual(fitness, 0.375) + + def test_nan_combined_score_alone_scores_zero(self): + fitness = get_fitness_score({"combined_score": NAN}) + + self.assertEqual(fitness, 0.0) + + def test_valid_combined_score_is_still_used(self): + fitness = get_fitness_score({"combined_score": 0.8, "accuracy": 0.1}) + + self.assertFalse(math.isnan(fitness)) + self.assertEqual(fitness, 0.8) + + def test_better_program_replaces_one_with_a_nan_combined_score(self): + config = Config() + config.database.in_memory = True + config.database.num_islands = 1 + database = ProgramDatabase(config.database) + + database.add(Program(id="nan", code="x = 1", metrics={"combined_score": NAN})) + database.add(Program(id="good", code="x = 2", metrics={"combined_score": 0.9})) + + self.assertEqual(database.best_program_id, "good") + + +if __name__ == "__main__": + unittest.main() From 626a097778a9c14087570ce33e3f36f0a8e39dc6 Mon Sep 17 00:00:00 2001 From: Bhaveshmeghwal21 <118756339+Bhaveshmeghwal21@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:16:16 +0530 Subject: [PATCH 2/2] fix(metrics): rank a NaN combined_score last instead of averaging the other metrics Averaging the other metrics for a NaN combined_score let a program with a NaN score outrank valid programs when it reported another metric such as eval_time. Return -inf so it ranks below every other program, and test that a valid program is not replaced by one. --- openevolve/utils/metrics_utils.py | 8 ++--- tests/test_nan_combined_score.py | 59 ++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/openevolve/utils/metrics_utils.py b/openevolve/utils/metrics_utils.py index 7a3e62778..852f3606d 100644 --- a/openevolve/utils/metrics_utils.py +++ b/openevolve/utils/metrics_utils.py @@ -90,7 +90,8 @@ def get_fitness_score( feature_dimensions: List of MAP-Elites dimensions to exclude from fitness Returns: - Fitness score (combined_score if available, otherwise average of non-feature metrics) + Fitness score (combined_score if available, otherwise average of non-feature metrics). + A NaN combined_score gives -inf, so that program ranks below every other. """ if not metrics: return 0.0 @@ -99,9 +100,8 @@ def get_fitness_score( if "combined_score" in metrics: try: combined_score = float(metrics["combined_score"]) - # NaN loses every comparison, so a program scored NaN could never be beaten - if not math.isnan(combined_score): - return combined_score + # Every comparison with NaN is False, so a NaN score would never be beaten. Rank it last + return float("-inf") if math.isnan(combined_score) else combined_score except (ValueError, TypeError): pass diff --git a/tests/test_nan_combined_score.py b/tests/test_nan_combined_score.py index 5dd59a607..e9c58446b 100644 --- a/tests/test_nan_combined_score.py +++ b/tests/test_nan_combined_score.py @@ -1,15 +1,16 @@ """ -A NaN `combined_score` is not a usable fitness. +A NaN `combined_score` must rank as the worst score. -`get_fitness_score` already skips NaN when it averages the other metrics, but it -returned a NaN `combined_score` as it was. NaN compares false against everything, -so a program with that fitness could never be beaten and stayed the best program. +NaN is neither greater nor smaller than any number, so a program with a NaN fitness +could never be beaten: if it was added first it stayed the best program. It must not +outrank valid programs either, so `get_fitness_score` ranks it below every score. """ -import math import os import unittest +import numpy as np + os.environ.setdefault("OPENAI_API_KEY", "test") from openevolve.config import Config @@ -19,34 +20,50 @@ NAN = float("nan") -class TestNanCombinedScore(unittest.TestCase): - def test_nan_combined_score_falls_back_to_the_other_metrics(self): - fitness = get_fitness_score({"combined_score": NAN, "accuracy": 0.5, "speed": 0.25}) - - self.assertEqual(fitness, 0.375) +def make_database() -> ProgramDatabase: + config = Config() + config.database.in_memory = True + config.database.num_islands = 1 + return ProgramDatabase(config.database) - def test_nan_combined_score_alone_scores_zero(self): - fitness = get_fitness_score({"combined_score": NAN}) - self.assertEqual(fitness, 0.0) +class TestNanCombinedScore(unittest.TestCase): + def test_nan_combined_score_ranks_below_every_score(self): + for nan in (NAN, np.nan, np.float32("nan")): + with self.subTest(nan=repr(nan)): + fitness = get_fitness_score({"combined_score": nan, "accuracy": 0.9}) - def test_valid_combined_score_is_still_used(self): - fitness = get_fitness_score({"combined_score": 0.8, "accuracy": 0.1}) + self.assertEqual(fitness, float("-inf")) + self.assertLess(fitness, get_fitness_score({"combined_score": -1e9})) - self.assertFalse(math.isnan(fitness)) - self.assertEqual(fitness, 0.8) + def test_valid_combined_score_is_unchanged(self): + self.assertEqual(get_fitness_score({"combined_score": 0.8, "accuracy": 0.1}), 0.8) + self.assertEqual(get_fitness_score({"combined_score": float("inf")}), float("inf")) def test_better_program_replaces_one_with_a_nan_combined_score(self): - config = Config() - config.database.in_memory = True - config.database.num_islands = 1 - database = ProgramDatabase(config.database) + database = make_database() database.add(Program(id="nan", code="x = 1", metrics={"combined_score": NAN})) database.add(Program(id="good", code="x = 2", metrics={"combined_score": 0.9})) self.assertEqual(database.best_program_id, "good") + def test_nan_combined_score_does_not_replace_a_valid_program(self): + database = make_database() + + database.add( + Program(id="good", code="x = 1", metrics={"combined_score": 0.9, "eval_time": 0.4}) + ) + database.add( + Program( + id="nan", + code="x = 2", + metrics={"combined_score": NAN, "sum_radii": 2.6, "eval_time": 3.0}, + ) + ) + + self.assertEqual(database.best_program_id, "good") + if __name__ == "__main__": unittest.main()