Skip to content

Commit

Permalink
Simplify comparator structure
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanlukasczyk committed May 16, 2023
1 parent b6fbef9 commit 78afbc4
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 84 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,3 @@ repos:
rev: v1.1.2
hooks:
- id: reuse

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.267'
hooks:
- id: ruff
2 changes: 1 addition & 1 deletion src/pynguin/ga/algorithms/abstractmosaalgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from pynguin.ga.algorithms.archive import CoverageArchive
from pynguin.ga.algorithms.generationalgorithm import GenerationAlgorithm
from pynguin.ga.comparators.dominancecomparator import DominanceComparator
from pynguin.ga.operators.comparator import DominanceComparator
from pynguin.utils import randomness
from pynguin.utils.exceptions import ConstructionFailedException

Expand Down
7 changes: 0 additions & 7 deletions src/pynguin/ga/comparators/__init__.py

This file was deleted.

58 changes: 0 additions & 58 deletions src/pynguin/ga/comparators/preferencesortingcomparator.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# SPDX-License-Identifier: MIT
#
"""Provides a comparator for dominance comparisons."""
"""Provides comparators for the MOSA derivates."""
from __future__ import annotations

from typing import Generic
Expand Down Expand Up @@ -90,3 +90,46 @@ def compare( # noqa: C901
if dominate_1:
return -1 # chromosome_1 dominates
return 1 # chromosome_2 dominates


class PreferenceSortingComparator(Generic[C]):
"""A comparator for chromosomes based on the fitness value of two objects.
The comparator only considers the specified test goals.
"""

def __init__(self, goal: ff.FitnessFunction) -> None:
"""Initializes the comparator.
Args:
goal: The goal to respect for the comparison
"""
self.__objective = goal

def compare(self, chromosome_1: C | None, chromosome_2: C | None) -> int:
"""Compare the fitness value of two chromosomes focusing only on one goal.
Args:
chromosome_1: A chromosome
chromosome_2: A chromosome
Returns:
-1 if fitness of chromosome_1 is less than fitness of chromosome_2, 0 if the
fitness values of both solutions are equal, 1 otherwise
"""
if chromosome_1 is None:
return 1
if chromosome_2 is None:
return -1

value_1 = chromosome_1.get_fitness_for(self.__objective)
value_2 = chromosome_2.get_fitness_for(self.__objective)
if value_1 < value_2:
return -1
if value_1 > value_2:
return 1
if chromosome_1.length() < chromosome_2.length():
return -1
if chromosome_1.length() > chromosome_2.length():
return 1
return 0
6 changes: 2 additions & 4 deletions src/pynguin/ga/operators/ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import pynguin.configuration as config
import pynguin.ga.chromosome as chrom

from pynguin.ga.comparators.dominancecomparator import DominanceComparator
from pynguin.ga.comparators.preferencesortingcomparator import (
PreferenceSortingComparator,
)
from pynguin.ga.operators.comparator import DominanceComparator
from pynguin.ga.operators.comparator import PreferenceSortingComparator
from pynguin.utils import randomness
from pynguin.utils.orderedset import OrderedSet

Expand Down
6 changes: 0 additions & 6 deletions tests/ga/comparators/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
# SPDX-FileCopyrightText: 2019–2023 Pynguin Contributors
#
# SPDX-License-Identifier: MIT

# This file is part of Pynguin.
#
#
# SPDX-License-Identifier: MIT
#
from unittest.mock import MagicMock

import pytest

import pynguin.ga.chromosome as chrom
import pynguin.ga.comparators.dominancecomparator as dc
import pynguin.ga.computations as ff
import pynguin.ga.operators.comparator as dc

from pynguin.utils.orderedset import OrderedSet

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest

import pynguin.ga.chromosome as chrom
import pynguin.ga.comparators.preferencesortingcomparator as pc
import pynguin.ga.operators.comparator as pc


@pytest.fixture
Expand Down

0 comments on commit 78afbc4

Please sign in to comment.