Skip to content

Commit

Permalink
Cleaned up code and updated docstrings
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriele A. Ron <[email protected]>
  • Loading branch information
Macr0Nerd committed Dec 27, 2023
1 parent 52a87c1 commit 97f0363
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/project_dilemma/interfaces/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand All @@ -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
"""
Expand Down
8 changes: 4 additions & 4 deletions src/project_dilemma/interfaces/generational_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
"""
Expand All @@ -27,15 +27,15 @@ 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

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)
Expand Down
8 changes: 4 additions & 4 deletions src/project_dilemma/interfaces/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/project_dilemma/interfaces/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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')

Expand Down
19 changes: 10 additions & 9 deletions src/project_dilemma/object_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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']):
Expand Down Expand Up @@ -76,15 +77,15 @@ 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
:type config: ProjectDilemmaConfig
: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:
Expand Down
9 changes: 4 additions & 5 deletions src/project_dilemma/simulations/basic_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@
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

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,
round_mutations: bool = False,) -> Round:
"""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
Expand Down Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import math

from project_dilemma.interfaces import GenerationalSimulation, Node


Expand Down

0 comments on commit 97f0363

Please sign in to comment.