Skip to content

Commit

Permalink
Add min/max and default validation to attributes.
Browse files Browse the repository at this point in the history
Remove unused code.
  • Loading branch information
CodeReclaimers committed May 3, 2022
1 parent 2371fcd commit 5590604
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![Build Status](https://app.travis-ci.com/CodeReclaimers/neat-python.svg?branch=master)](https://app.travis-ci.com/github/CodeReclaimers/neat-python)
[![Coverage Status](https://coveralls.io/repos/CodeReclaimers/neat-python/badge.svg?branch=master&service=github)](https://coveralls.io/github/CodeReclaimers/neat-python?branch=master)
[![Downloads](https://static.pepy.tech/personalized-badge/neat-python?period=total&units=international_system&left_color=grey&right_color=blue&left_text=Downloads)](https://pepy.tech/project/neat-python)

## About ##

Expand All @@ -23,9 +24,9 @@ The documentation is available on [Read The Docs](http://neat-python.readthedocs

## Citing ##

Here are APA and Bibtex entries you can use to cite this project in a publication. The listed authors are the
maintainers of all iterations of the project up to this point. If you have contributed and would like your name added
to the citation, please submit an issue or email alan@codereclaimers.com.
Here are APA and Bibtex entries you can use to cite this project in a publication. The listed authors are the originators
and/or maintainers of all iterations of the project up to this point. If you have contributed and would like your name added
to the citation, please submit an issue or email alan@codereclaimers.com.

APA
```
Expand Down
19 changes: 13 additions & 6 deletions neat/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ def mutate_value(self, value, config):

return value

def validate(self, config):
pass
def validate(self, config):
min_value = getattr(config, self.min_value_name)
max_value = getattr(config, self.max_value_name)
if max_value < min_value:
raise RuntimeError("Invalid min/max configuration for {self.name}")


class IntegerAttribute(BaseAttribute):
Expand Down Expand Up @@ -121,7 +124,10 @@ def mutate_value(self, value, config):
return value

def validate(self, config):
pass
min_value = getattr(config, self.min_value_name)
max_value = getattr(config, self.max_value_name)
if max_value < min_value:
raise RuntimeError("Invalid min/max configuration for {self.name}")


class BoolAttribute(BaseAttribute):
Expand Down Expand Up @@ -163,7 +169,9 @@ def mutate_value(self, value, config):
return value

def validate(self, config):
pass
default = str(getattr(config, self.default_name)).lower()
if default not in ('1', 'on', 'yes', 'true', '0', 'off', 'no', 'false', 'random', 'none'):
raise RuntimeError("Invalid default value for {self.name}")


class StringAttribute(BaseAttribute):
Expand Down Expand Up @@ -200,6 +208,5 @@ def validate(self, config):
if default not in ('none', 'random'):
options = getattr(config, self.options_name)
if default not in options:
raise RuntimeError(f'Invalid activation function name: {default}')
raise RuntimeError(f'Invalid initial value {default} for {self.name}')
assert default in options

2 changes: 0 additions & 2 deletions neat/nn/feed_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ def create(genome, config):
for layer in layers:
for node in layer:
inputs = []
node_expr = [] # currently unused
for conn_key in connections:
inode, onode = conn_key
if onode == node:
cg = genome.connections[conn_key]
inputs.append((inode, cg.weight))
node_expr.append("v[{}] * {:.7e}".format(inode, cg.weight))

ng = genome.nodes[node]
aggregation_function = config.genome_config.aggregation_function_defs.get(ng.aggregation)
Expand Down
14 changes: 5 additions & 9 deletions tests/test_simple_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,18 @@ def test_serial_bad_config():


def test_serial_bad_configA():
"""Test if bad_configurationA causes a RuntimeError on trying to create the population."""
# Load configuration.
"""Test if bad_configurationA causes a RuntimeError on loading configuration."""
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'bad_configurationA')
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)

try:
# Create the population, which is the top-level object for a NEAT run.
p = neat.Population(config)
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)
except RuntimeError:
pass
else:
raise Exception(
"Should have had a RuntimeError with bad_configurationA")
raise Exception("Should have had a RuntimeError with bad_configurationA")


def test_serial_extinction_exception():
Expand Down

0 comments on commit 5590604

Please sign in to comment.