From af0ea597f2fde8b9996eaabd9ee096a5c23c971a Mon Sep 17 00:00:00 2001 From: Giovanni Squillero Date: Wed, 23 Aug 2023 16:55:45 +0200 Subject: [PATCH] Improved compatibility with py3.9 --- byron/classes/evaluator.py | 4 ++-- byron/classes/individual.py | 12 +++--------- byron/classes/parameter.py | 4 ++-- byron/ea/parametric.py | 4 +++- byron/ea/vanilla.py | 4 ++-- byron/registry.py | 4 ++-- 6 files changed, 14 insertions(+), 18 deletions(-) diff --git a/byron/classes/evaluator.py b/byron/classes/evaluator.py index 28f89b6..9e761a0 100644 --- a/byron/classes/evaluator.py +++ b/byron/classes/evaluator.py @@ -33,7 +33,7 @@ 'ScriptEvaluator', ] -from typing import Callable, Sequence +from typing import Callable, Sequence, Optional from abc import ABC, abstractmethod from dataclasses import dataclass from itertools import zip_longest @@ -444,7 +444,7 @@ class ScriptEvaluator(EvaluatorABC): def __init__( self, script_name: str, - args: Sequence[str] | None = None, + args: Optional[Sequence[str]] = None, *, filename_format: str = 'phenotype_{i:x}.txt', timeout: int | None = 60, diff --git a/byron/classes/individual.py b/byron/classes/individual.py index 6419709..122c45b 100644 --- a/byron/classes/individual.py +++ b/byron/classes/individual.py @@ -110,8 +110,8 @@ class Individual(Paranoid): __COUNTER: int = 0 _genome: nx.classes.MultiDiGraph - _fitness: FitnessABC | None - _lineage: Lineage | None + _fitness: Optional[FitnessABC] + _lineage: Optional[Lineage] _age: Age _str: str @@ -365,7 +365,7 @@ def run_paranoia_checks(self) -> bool: # ==[check nodes (semantic)]========================================= assert all( - n < self._genome.graph['node_count'] for n in self._genome + n < self._genome.graph["node_count"] for n in self._genome ), f"{PARANOIA_VALUE_ERROR}: Invalid 'node_count' attribute ({self._genome.graph['node_count']})" assert all( @@ -402,12 +402,6 @@ def run_paranoia_checks(self) -> bool: return True - def consolidate_genome(self) -> None: - self._genome = nx.convert_node_labels_to_integers(self._genome) - fasten_subtree_parameters(NodeReference(self._genome, NODE_ZERO)) - self._genome.graph['node_count'] = len(self._genome) - self.run_paranoia_checks() - def describe( self, *, diff --git a/byron/classes/parameter.py b/byron/classes/parameter.py index 133692e..512046a 100644 --- a/byron/classes/parameter.py +++ b/byron/classes/parameter.py @@ -28,7 +28,7 @@ __all__ = ['ParameterABC', 'ParameterNumericABC', 'ParameterArrayABC', 'ParameterStructuralABC', 'ParameterSharedABC'] from abc import ABC, abstractmethod -from typing import Any +from typing import Any, Optional from networkx.classes import MultiDiGraph @@ -101,7 +101,7 @@ class ParameterStructuralABC(ParameterABC): __slots__ = [] # Preventing the automatic creation of __dict__ - _node_reference: NodeReference | None + _node_reference: Optional[NodeReference] def __init__(self): super().__init__() diff --git a/byron/ea/parametric.py b/byron/ea/parametric.py index 1de171a..b714aee 100644 --- a/byron/ea/parametric.py +++ b/byron/ea/parametric.py @@ -27,6 +27,8 @@ __all__ = ["parametric_ea"] +from typing import Optional + from byron.operators import * from byron.sys import * from byron.classes.selement import * @@ -52,7 +54,7 @@ def parametric_ea( mu: int = 10, lambda_: int = 20, max_generation: int = 100, - max_fitness: FitnessABC | None = None, + max_fitness: Optional[FitnessABC] = None, top_best: int = None, lifespan: int = None, operators: list = None, diff --git a/byron/ea/vanilla.py b/byron/ea/vanilla.py index 598c998..c8ff91a 100644 --- a/byron/ea/vanilla.py +++ b/byron/ea/vanilla.py @@ -27,7 +27,7 @@ __all__ = ["vanilla_ea"] -from typing import Any +from typing import Any, Optional from time import perf_counter_ns, process_time_ns from datetime import timedelta @@ -73,7 +73,7 @@ def vanilla_ea( mu: int = 10, lambda_: int = 20, max_generation: int = 100, - max_fitness: Any | None = None, + max_fitness: Optional = None, population_extra_parameters: dict = None, ) -> Population: r"""A simple evolutionary algorithm diff --git a/byron/registry.py b/byron/registry.py index 3da8553..c920e89 100644 --- a/byron/registry.py +++ b/byron/registry.py @@ -29,7 +29,7 @@ __all__ = ["Statistics", "fitness_function", "genetic_operator", "get_byron_type"] -from typing import Callable +from typing import Callable, Optional from dataclasses import dataclass from functools import wraps @@ -99,7 +99,7 @@ def __str__(self): def fitness_function( - func: Callable[..., FitnessABC] | None = None, /, *, type_: type[FitnessABC] = None, backend: str | None = 'list' + func: Optional[Callable[..., FitnessABC]] = None, /, *, type_: type[FitnessABC] = None, backend: str | None = 'list' ): if type_ is None: type_ = lambda f: fitness.make_fitness(f)