diff --git a/src/project_dilemma/interfaces/algorithm.py b/src/project_dilemma/interfaces/algorithm.py index 5cb8451..4938467 100644 --- a/src/project_dilemma/interfaces/algorithm.py +++ b/src/project_dilemma/interfaces/algorithm.py @@ -15,7 +15,7 @@ """ from abc import abstractmethod from collections.abc import Sequence -from typing import Optional, Self, Type +from typing import Optional, Self import project_dilemma.interfaces.base as pd_int_base @@ -37,9 +37,9 @@ class Algorithm(pd_int_base.Base): ] algorithm_id: str - mutations: Optional[Sequence[Type[Self]]] = None + mutations: Optional[Sequence[type[Self]]] = None - def __init__(self, mutations: Optional[Sequence[Type[Self]]] = None, **kwargs) -> None: + def __init__(self, mutations: Optional[Sequence[type[Self]]] = None, **kwargs) -> None: self.mutations = mutations def __eq__(self, other: Self): @@ -51,7 +51,7 @@ def decide(rounds: pd_int_base.Rounds) -> bool: """decide whether to cooperate or not :param rounds: the list of moves - :type rounds: MoveList + :type rounds: Rounds :return: whether to cooperate or not :rtype: bool """ diff --git a/src/project_dilemma/interfaces/generational_simulation.py b/src/project_dilemma/interfaces/generational_simulation.py index 680bbd9..5186ac7 100644 --- a/src/project_dilemma/interfaces/generational_simulation.py +++ b/src/project_dilemma/interfaces/generational_simulation.py @@ -2,7 +2,7 @@ from collections.abc import MutableMapping, Sequence from copy import deepcopy import sys -from typing import Any, Optional, Type +from typing import Any, Optional import project_dilemma.interfaces.base as pd_int_base import project_dilemma.interfaces.node as pd_int_node @@ -15,7 +15,7 @@ class GenerationalSimulation(pd_int_simulation.SimulationBase): :var generations: number of generations to run :vartype generations: int :var generational_simulation: simulation class to use - :vartype simulation: Type[project_dilemma.interfaces.simulation.SimulationBase] + :vartype simulation: type[SimulationBase] :var simulation_kwargs: keyword arguments to pass into the simulation :vartype simulation_kwargs: MutableMapping[str, Any] """ @@ -27,7 +27,7 @@ class GenerationalSimulation(pd_int_simulation.SimulationBase): ] generations: int - generational_simulation: Type[pd_int_simulation.SimulationBase] + generational_simulation: type[pd_int_simulation.SimulationBase] simulation_kwargs: MutableMapping[str, Any] _simulation_data: pd_int_base.Generations @@ -35,7 +35,7 @@ def __init__(self, simulation_id: str, nodes: Sequence[pd_int_node.Node], generations: int, - generational_simulation: Type[pd_int_simulation.SimulationBase], + generational_simulation: type[pd_int_simulation.SimulationBase], simulation_data: Optional[pd_int_base.Simulations] = None, **kwargs): super().__init__(nodes=nodes, simulation_id=simulation_id, simulation_data=simulation_data) diff --git a/src/project_dilemma/interfaces/node.py b/src/project_dilemma/interfaces/node.py index 189e26e..75610ea 100644 --- a/src/project_dilemma/interfaces/node.py +++ b/src/project_dilemma/interfaces/node.py @@ -14,7 +14,7 @@ limitations under the License. """ import random -from typing import Self, Type +from typing import Self import project_dilemma.interfaces.algorithm as pd_int_algorithm import project_dilemma.interfaces.base as pd_int_base @@ -28,7 +28,7 @@ class Node(pd_int_base.Base): :var node_id: id of the node :vartype node_id: str :var algorithm: cooperation algorithm - :vartype algorithm: Type[Algorithm] + :vartype algorithm: type[Algorithm] """ _required_attributes = [ 'algorithm', @@ -37,9 +37,9 @@ class Node(pd_int_base.Base): ] node_id: str - algorithm: Type[pd_int_algorithm.Algorithm] + algorithm: type[pd_int_algorithm.Algorithm] - def __init__(self, node_id: str, algorithm: Type[pd_int_algorithm.Algorithm], **kwargs): + def __init__(self, node_id: str, algorithm: type[pd_int_algorithm.Algorithm], **kwargs): self.node_id = node_id self.algorithm = algorithm diff --git a/src/project_dilemma/interfaces/simulation.py b/src/project_dilemma/interfaces/simulation.py index 09c5b57..fd1ddc9 100644 --- a/src/project_dilemma/interfaces/simulation.py +++ b/src/project_dilemma/interfaces/simulation.py @@ -29,7 +29,7 @@ class SimulationBase(pd_int_base.Base): all the nodes must have unique node ids :var nodes: node data for the simulation - :vartype nodes: Sequence[Node] + :vartype nodes: Sequence[Type[Node]] :var simulation_id: id of the simulation :vartype simulation_id: str :var simulation_data: simulation round data @@ -45,7 +45,7 @@ class SimulationBase(pd_int_base.Base): simulation_id: str _simulation_data: pd_int_base.Generations | pd_int_base.Simulations - _nodes: Sequence[pd_int_node.Node] + _nodes: Sequence[type[pd_int_node.Node]] @abstractmethod def __init__(self, @@ -59,11 +59,11 @@ def __init__(self, self.simulation_data = simulation_data @property - def nodes(self) -> Sequence[pd_int_node.Node]: + def nodes(self) -> Sequence[type[pd_int_node.Node]]: return self._nodes @nodes.setter - def nodes(self, nodes: Sequence[pd_int_node.Node]): + def nodes(self, nodes: Sequence[type[pd_int_node.Node]]): if max(Counter([node.node_id for node in nodes]).values()) > 1: raise ValueError('All node ids provided must be unique') diff --git a/src/project_dilemma/object_loaders.py b/src/project_dilemma/object_loaders.py index 819347c..fa7a1d7 100644 --- a/src/project_dilemma/object_loaders.py +++ b/src/project_dilemma/object_loaders.py @@ -2,21 +2,22 @@ import json import os.path import sys -from typing import Dict, List, Type +from typing import Dict, List from project_dilemma.config import ProjectDilemmaConfig from project_dilemma.interfaces import Algorithm, Generations, Node, SimulationBase, Simulations from project_dilemma.simulations import simulations_map -def create_nodes(config: ProjectDilemmaConfig, algorithms_map: Dict[str, Type[Algorithm]]) -> List[Node]: +def create_nodes(config: ProjectDilemmaConfig, algorithms_map: Dict[str, type[Algorithm]]) -> List[type[Node]]: """create the simulation nodes :param config: configuration data :type config: ProjectDilemmaConfig :param algorithms_map: map of algorithm class names to algorithms - :type algorithms_map: Dict[str, Type[Algorithm]] - :return: + :type algorithms_map: Dict[str, type[Algorithm]] + :return: list of nodes + :rtype: List[type[Node]] """ nodes = [] @@ -26,18 +27,18 @@ def create_nodes(config: ProjectDilemmaConfig, algorithms_map: Dict[str, Type[Al return nodes -def load_algorithms(config: ProjectDilemmaConfig) -> Dict[str, Type[Algorithm]]: +def load_algorithms(config: ProjectDilemmaConfig) -> Dict[str, type[Algorithm]]: """load all algorithms used :param config: configuration data :type config: ProjectDilemmaConfig :return: map of algorithm class names to algorithms - :rtype: Dict[str, Type[Algorithm]] + :rtype: Dict[str, type[Algorithm]] """ sys.path.append(config['algorithms_directory']) algorithms = [node['algorithm'] for node in config['nodes']] - algorithm_map: Dict[str, Type[Algorithm]] = {} + algorithm_map: Dict[str, type[Algorithm]] = {} for algorithm in algorithms: if algorithm_map.get(algorithm['object']): @@ -76,7 +77,7 @@ def load_simulation_data(config: ProjectDilemmaConfig) -> Generations | Simulati return data -def load_simulation(config: ProjectDilemmaConfig, *, generational: bool = False) -> Type[SimulationBase]: +def load_simulation(config: ProjectDilemmaConfig, *, generational: bool = False) -> type[SimulationBase]: """load the simulation :param config: configuration data @@ -84,7 +85,7 @@ def load_simulation(config: ProjectDilemmaConfig, *, generational: bool = False) :param generational: if the generational simulation should be loaded :type generational: bool :return: the configured simulation - :rtype: Type[SimulationBase] + :rtype: type[SimulationBase] """ key = 'simulation' if generational: diff --git a/src/project_dilemma/simulations/basic_simulation.py b/src/project_dilemma/simulations/basic_simulation.py index 35cc4a8..1621e9f 100644 --- a/src/project_dilemma/simulations/basic_simulation.py +++ b/src/project_dilemma/simulations/basic_simulation.py @@ -13,7 +13,6 @@ See the License for the specific language governing permissions and limitations under the License. """ -from abc import abstractmethod from collections.abc import Sequence import random from typing import Optional @@ -21,7 +20,7 @@ from project_dilemma.interfaces import Node, Round, Rounds, Simulation, Simulations -def play_round(nodes: Sequence[Node], +def play_round(nodes: Sequence[type[Node]], rounds: Rounds, *, mutations_per_mille: int, @@ -29,8 +28,8 @@ def play_round(nodes: Sequence[Node], """run a round of prisoners dilemma with each node :param nodes: nodes to run - :type nodes: Sequence[Node] - :param rounds: lsit of rounds + :type nodes: Sequence[type[Node]] + :param rounds: list of rounds :type rounds: RoundList :param mutations_per_mille: rate that mutations should occur per mille :type mutations_per_mille: int @@ -94,7 +93,7 @@ def run_simulation(self) -> Simulations: """run the simulation :return: simulation results - :rtype: RoundList + :rtype: Rounds """ game_id = ':'.join(sorted(node.node_id for node in self.nodes)) diff --git a/src/project_dilemma/simulations/standard_generational_simulation.py b/src/project_dilemma/simulations/standard_generational_simulation.py index 9338b0e..b330e41 100644 --- a/src/project_dilemma/simulations/standard_generational_simulation.py +++ b/src/project_dilemma/simulations/standard_generational_simulation.py @@ -1,5 +1,3 @@ -import math - from project_dilemma.interfaces import GenerationalSimulation, Node