Skip to content

Commit

Permalink
Fix imports: No global imports, all relative
Browse files Browse the repository at this point in the history
  • Loading branch information
mschmidt87 committed Apr 30, 2020
1 parent 80d1ddf commit b1ff4cf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
18 changes: 9 additions & 9 deletions gp/cartesian_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -22,7 +22,7 @@ class CartesianGraph:
Genome.
"""

def __init__(self, genome: gp.Genome) -> None:
def __init__(self, genome: Genome) -> None:
"""Init function.
Parameters
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 7 additions & 5 deletions gp/genome.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gp/node.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
17 changes: 7 additions & 10 deletions gp/population.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import gp
import numpy as np

from .individual import Individual, IndividualMultiGenome
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b1ff4cf

Please sign in to comment.