Skip to content

Commit

Permalink
Merge branch 'master' of github.com:erp12/pyshgp
Browse files Browse the repository at this point in the history
  • Loading branch information
erp12 committed Jun 16, 2020
2 parents 7c9230c + 29401e6 commit 733783a
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 40 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ Although PushGP is constantly changing through research and publication, `pyshgp
```sh
pip install pyshgp
```
- That's it! Read through the docs and examples to learn more.

### Build Frome source
### Build From source

- Clone the repo
- cd into the `pyshgp` repo directory
- run `pip install . --upgrade`
- Thats it! Check out the examples and documentation.
- That's it! Read through the docs and examples to learn more.

### Running Tests

Expand Down
3 changes: 0 additions & 3 deletions examples/integer_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,3 @@ def target_function(a, b):
)

est.fit(X=X, y=y)
print(est.solution.program.pretty_str())
print(est.predict(X))
print(est.score(X, y))
3 changes: 0 additions & 3 deletions examples/leaky_relu.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,3 @@ def target_function(x: float) -> (float, float):
)

est.fit(X=X, y=y)
print(est.solution.program.pretty_str())
print(est.predict(X))
print(est.score(X, y))
3 changes: 0 additions & 3 deletions examples/odd.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,3 @@
)

est.fit(X, y)
print(est.solution.program.pretty_str())
print(est.predict(X))
print(est.score(X, y))
3 changes: 2 additions & 1 deletion examples/point_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def point_from_floats(f1, f2):

if __name__ == "__main__":
est.fit(X, y)
print("Best program found:")
print(est.solution.program.pretty_str())
print(est.predict(X))
print("Errors:")
print(est.score(X, y))
11 changes: 5 additions & 6 deletions examples/software_synthesis/replace_space_with_newline.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@ def random_char():
start = time.time()
est.fit(X=X_train, y=y_train)
end = time.time()
print("train_error: ", est.solution.total_error)
print("test_error: ", np.sum(est.score(X_test, y_test)))
print("runtime: ", end - start)
print("final_generation: ", est.search.generation)
print("best_genome: ", est.solution.genome)
print("best_program_str:", est.solution.program.pretty_str())
print("========================================")
print("post-evolution stats")
print("========================================")
print("Runtime: ", end - start)
print("Test Error: ", np.sum(est.score(X_test, y_test)))
3 changes: 2 additions & 1 deletion examples/string_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def target_function(s):
)

est.fit(X=X, y=y)
print("Best program found:")
print(est.solution.program.pretty_str())
print(est.predict(X))
print("Errors:")
print(est.score(X, y))
3 changes: 2 additions & 1 deletion examples/string_demo_annealing.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def target_function(s):
)

est.fit(X=X, y=y)
print("Best program found:")
print(est.solution.program.pretty_str())
print(est.predict(X))
print("Errors:")
print(est.score(X, y))
8 changes: 5 additions & 3 deletions pyshgp/gp/estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pyshgp.push.interpreter import PushInterpreter, DEFAULT_INTERPRETER
from pyshgp.push.config import PushConfig
from pyshgp.push.program import ProgramSignature
from pyshgp.tap import tap
from pyshgp.tap import tap, set_verbosity
from pyshgp.utils import list_rindex
from pyshgp.validation import check_is_fitted, check_X_y

Expand Down Expand Up @@ -90,6 +90,7 @@ def __init__(self,
self.parallelism = parallelism
self.verbose = verbose
self.ext = kwargs
set_verbosity(self.verbose)

# Initialize attributes that will be set later.
self.evaluator = None
Expand Down Expand Up @@ -126,8 +127,7 @@ def _build_search_algo(self):
initial_genome_size=self.initial_genome_size,
simplification_steps=self.simplification_steps,
parallelism=self.parallelism,
push_config=self.push_config,
verbose=self.verbose
push_config=self.push_config
)
self.search = sr.get_search_algo(self._search_name, config=search_config, **self.ext)

Expand All @@ -154,6 +154,8 @@ def fit(self, X, y):
self.evaluator = DatasetEvaluator(X, y, interpreter=self.interpreter)
self._build_search_algo()
self.solution = self.search.run()
if self.search.config.parallel_context is not None:
self.search.config.parallel_context.pool.close()

def predict(self, X):
"""Execute the synthesized push program on a dataset.
Expand Down
5 changes: 1 addition & 4 deletions pyshgp/gp/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from multiprocessing import Pool, Manager

from pyshgp.push.program import ProgramSignature
from pyshgp.tap import tap, set_verbosity
from pyshgp.tap import tap
from pyshgp.utils import DiscreteProbDistrib
from pyshgp.gp.evaluation import Evaluator
from pyshgp.gp.genome import GeneSpawner, GenomeSimplifier
Expand Down Expand Up @@ -90,7 +90,6 @@ def __init__(self,
initial_genome_size: Tuple[int, int] = (10, 50),
simplification_steps: int = 2000,
parallelism: Union[int, bool] = True,
verbose: int = 0,
**kwargs):
self.signature = signature
self.evaluator = evaluator
Expand All @@ -100,9 +99,7 @@ def __init__(self,
self.error_threshold = error_threshold
self.initial_genome_size = initial_genome_size
self.simplification_steps = simplification_steps
self.verbose = verbose
self.ext = kwargs
set_verbosity(self.verbose)

self.parallel_context = None
if isinstance(parallelism, bool):
Expand Down
1 change: 1 addition & 0 deletions pyshgp/gp/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def select_one(self, population: Population) -> Individual:
pass

@tap
@abstractmethod
def select(self, population: Population, n: int = 1) -> Sequence[Individual]:
"""Return `n` individuals from the population.
Expand Down
33 changes: 21 additions & 12 deletions pyshgp/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import numpy as np
from abc import ABC
from functools import wraps
from typing import Dict, Sequence, Optional, MutableMapping
from typing import Dict, Sequence, Optional, MutableMapping, Tuple


class Tap(ABC):
Expand All @@ -28,31 +28,31 @@ class Tap(ABC):
that state will change code behavior.
"""

def pre(self, id: str, args, kwargs):
def pre(self, id: str, args: Tuple, kwargs: Dict):
"""Perform a particular side-effect directly before the associated function/method is called.
Parameters
----------
id : str
The ID of the tap, generated based off of the name qualified name of the function.
args : list
args : Tuple
The positional of the function call. For methods, ``args[0]`` is the class instance.
kwargs : dict
kwargs : Dict
The keyword args of the function call.
"""
pass

def post(self, id: str, args, kwargs, returned):
def post(self, id: str, args: Tuple, kwargs: Dict, returned):
"""Perform a particular side-effect directly before the associated function/method is called.
Parameters
----------
id : str
The ID of the tap, generated based off of the name qualified name of the function.
args : list
args : Tuple
The positional of the function call. For methods, ``args[0]`` is the class instance.
kwargs : dict
kwargs : Dict
The keyword args of the function call.
returned : Any
The returned value of the function call.
Expand Down Expand Up @@ -215,6 +215,9 @@ def __init__(self, *,
def pre(self, id: str, args, kwargs, obj=None):
"""Print run config and/or all atoms to stdout."""
search = args[0]
print("========================================")
print("Setup")
print("========================================")
if self.pre_print_config:
print("Search Configuration:")
attrs = ["signature", "evaluator", "spawner", "population_size", "max_generations", "error_threshold",
Expand All @@ -228,21 +231,27 @@ def pre(self, id: str, args, kwargs, obj=None):
print(search.config.spawner.literals)
print("ERC Generators:")
print(search.config.spawner.erc_generators)
print("========================================")
print("Start Run")
print("========================================")

def post(self, id: str, args, kwargs, returned, obj=None):
"""Print a summary of the run result to stdout."""
search = args[0]
print("========================================")
print("End Run")
print("========================================")
if search.is_solved():
print("Solution found.")
else:
print("No solution found.")

if self.post_print_best:
print("Best individual found:")
print("Genome:", search.best_seen.genome)
print("Program:", search.best_seen.program)
print("Error vector:", search.best_seen.error_vector)
print("Total error:", search.best_seen.total_error)
print("Best Seen Individual")
print("\tGenome:\n\t", search.best_seen.genome)
print("\tProgram:\n\t", search.best_seen.program.pretty_str())
print("\tError vector:\n\t", search.best_seen.error_vector)
print("\tTotal error:\n\t", search.best_seen.total_error)


class StdOutSearchStepTap(Tap):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def read(fname):
keywords=["push gp", "genetic programming", "pushgp", "gp", "push"],
author="Eddie Pantridge",
author_email="[email protected]",
license="LGPL",
license="MIT",
url="https://github.com/erp12/pyshgp",
packages=find_packages(
exclude=('examples', 'examples.*', 'tests', 'tests.*', 'docs', 'docs_source')
Expand Down

0 comments on commit 733783a

Please sign in to comment.