From b1ff4cfa0e29b4f8b98829047e2f457281164b13 Mon Sep 17 00:00:00 2001 From: Maximilian Schmidt Date: Thu, 30 Apr 2020 16:23:17 +0900 Subject: [PATCH] Fix imports: No global imports, all relative --- gp/cartesian_graph.py | 18 +++++++++--------- gp/genome.py | 12 +++++++----- gp/node.py | 2 +- gp/population.py | 17 +++++++---------- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/gp/cartesian_graph.py b/gp/cartesian_graph.py index dd6a3604..09072266 100644 --- a/gp/cartesian_graph.py +++ b/gp/cartesian_graph.py @@ -12,8 +12,8 @@ torch = None -from .node import InputNode, OutputNode -import gp +from .node import Node, InputNode, OutputNode +from .genome import Genome from typing import Callable, List, Optional, Dict @@ -22,7 +22,7 @@ class CartesianGraph: Genome. """ - def __init__(self, genome: gp.Genome) -> None: + def __init__(self, genome: Genome) -> None: """Init function. Parameters @@ -52,7 +52,7 @@ def pretty_str(self) -> str: """ n_characters = 24 - def pretty_node_str(node: gp.Node) -> str: + def pretty_node_str(node: Node) -> str: s = node.pretty_str(n_characters) assert len(s) == n_characters return s @@ -87,7 +87,7 @@ def empty_node_str() -> str: return s - def parse_genome(self, genome: gp.Genome) -> None: + def parse_genome(self, genome: Genome) -> None: if genome.dna is None: raise RuntimeError("dna not initialized") @@ -119,15 +119,15 @@ def _hidden_column_idx(self, idx: int) -> int: return (idx - self._n_inputs) // self._n_rows @property - def input_nodes(self) -> List[gp.Node]: + def input_nodes(self) -> List[Node]: return self._nodes[: self._n_inputs] @property - def hidden_nodes(self) -> List[gp.Node]: + def hidden_nodes(self) -> List[Node]: return self._nodes[self._n_inputs : -self._n_outputs] @property - def output_nodes(self) -> List[gp.Node]: + def output_nodes(self) -> List[Node]: return self._nodes[-self._n_outputs :] def _determine_active_nodes(self) -> Dict[int, set]: @@ -195,7 +195,7 @@ def __call__(self, x: List[float]) -> List[float]: return [node._output for node in self.output_nodes] - def __getitem__(self, key: int) -> gp.Node: + def __getitem__(self, key: int) -> Node: return self._nodes[key] def to_str(self) -> str: diff --git a/gp/genome.py b/gp/genome.py index dc885a95..8692f22d 100644 --- a/gp/genome.py +++ b/gp/genome.py @@ -1,8 +1,10 @@ -import gp +from __future__ import annotations + import numpy as np from typing import Generator, List, Optional, Tuple +from .node import Node from .primitives import Primitives @@ -17,7 +19,7 @@ def __init__( n_columns: int, n_rows: int, levels_back: int, - primitives: List[gp.Node], + primitives: List[Node], ) -> None: """Init function. @@ -34,7 +36,7 @@ def __init__( levels_back : int Number of previous columns that an entry in the genome can be connected with. - primitives : List[gp.Node] + primitives : List[Node] List of primitives that the genome can refer to. """ if n_inputs <= 0: @@ -410,10 +412,10 @@ def _mutate_hidden_region( return silent_mutation @property - def primitives(self) -> gp.Primitives: + def primitives(self) -> Primitives: return self._primitives - def clone(self) -> gp.Genome: + def clone(self) -> Genome: """Clone the genome. Returns diff --git a/gp/node.py b/gp/node.py index e2a0df58..4569543e 100644 --- a/gp/node.py +++ b/gp/node.py @@ -1,7 +1,7 @@ from __future__ import annotations from typing import List, Union -from cartesian_graph import CartesianGraph +from .cartesian_graph import CartesianGraph primitives_dict = {} # maps string of class names to classes diff --git a/gp/population.py b/gp/population.py index 4586b80e..d755b000 100755 --- a/gp/population.py +++ b/gp/population.py @@ -1,4 +1,3 @@ -import gp import numpy as np from .individual import Individual, IndividualMultiGenome @@ -48,28 +47,28 @@ def __init__( self._generate_random_parent_population() @property - def champion(self) -> gp.Individual: + def champion(self) -> Individual: """Return parent with the highest fitness. """ return max(self._parents, key=lambda ind: ind.fitness) @property - def parents(self) -> List[gp.Individual]: + def parents(self) -> List[Individual]: return self._parents @parents.setter - def parents(self, new_parents: List[gp.Individual]) -> None: + def parents(self, new_parents: List[Individual]) -> None: self.generation += 1 self._parents = new_parents - def __getitem__(self, idx: int) -> gp.Individual: + def __getitem__(self, idx: int) -> Individual: return self._parents[idx] def _generate_random_parent_population(self) -> None: self._parents = self._generate_random_individuals(self.n_parents) self._label_new_individuals(self._parents) - def _label_new_individuals(self, individuals: List[gp.Individual]) -> None: + def _label_new_individuals(self, individuals: List[Individual]) -> None: for ind in individuals: ind.idx = self._max_idx + 1 self._max_idx += 1 @@ -90,9 +89,7 @@ def _generate_random_individuals(self, n: int) -> List[Individual]: return individuals - def crossover( - self, breeding_pool: List[gp.Individual], n_offsprings: int - ) -> List[gp.Individual]: + def crossover(self, breeding_pool: List[Individual], n_offsprings: int) -> List[Individual]: """Create an offspring population via crossover. Parameters @@ -127,7 +124,7 @@ def crossover( assert len(breeding_pool) >= n_offsprings return sorted(breeding_pool, key=lambda x: -x.fitness)[:n_offsprings] - def mutate(self, offsprings: List[gp.Individual]) -> List[gp.Individual]: + def mutate(self, offsprings: List[Individual]) -> List[Individual]: """Mutate a list of offspring invididuals. Parameters