Skip to content

Commit 12af9df

Browse files
author
Timur Gilmullin
committed
#14: refactor gfilter.py
1 parent e31bbc9 commit 12af9df

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

pybrain/supervised/evolino/gfilter.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
__author__ = 'Michael Isik'
2-
1+
# -*- coding: utf-8 -*-
32

43
from pybrain.supervised.evolino.variate import UniformVariate, GaussianVariate
54

5+
__author__ = 'Michael Isik'
6+
7+
68
class Filter(object):
79
""" Base class for all kinds of operators on the population during the
810
evolutionary process like mutation, selection or evaluation.
911
"""
1012
def __init__(self):
1113
pass
14+
1215
def apply(self, population):
1316
""" Applies an operation on a population. """
1417
raise NotImplementedError()
1518

19+
1620
def isiter(obj):
1721
try:
1822
iter(obj)
@@ -21,7 +25,6 @@ def isiter(obj):
2125
return False
2226

2327

24-
2528
class SimpleGenomeManipulation(Filter):
2629
""" Abstract filter class for simple genome manipulation. """
2730
def __init__(self):
@@ -38,12 +41,16 @@ def _manipulateGenome(self, genome, manfunc=None):
3841
If omitted, self._manipulateValue() is used.
3942
See its documentation for the signature description.
4043
"""
41-
assert isiter(genome)
42-
if manfunc is None: manfunc = self._manipulateValue
44+
if not isiter(genome):
45+
raise Exception("genome must be iterable!")
46+
47+
if manfunc is None:
48+
manfunc = self._manipulateValue
4349

4450
for i, v in enumerate(genome):
4551
if isiter(v):
4652
self._manipulateGenome(v, manfunc)
53+
4754
else:
4855
genome[i] = manfunc(v)
4956

@@ -54,21 +61,17 @@ def _manipulateValue(self, value):
5461
raise NotImplementedError()
5562

5663

57-
5864
class SimpleMutation(SimpleGenomeManipulation):
5965
mutationVariate = None
6066
""" A simple mutation filter, which uses a gaussian variate per default
6167
for mutation.
6268
"""
6369
def __init__(self):
64-
""" :key kwargs: See setArgs() method documentation
65-
"""
6670
SimpleGenomeManipulation.__init__(self)
6771
self.mutationVariate = GaussianVariate()
6872
self.mutationVariate.alpha = 0.1
6973
self.verbosity = 0
7074

71-
7275
def apply(self, population):
7376
""" Apply the mutation to the population
7477
@@ -93,10 +96,8 @@ def _manipulateValue(self, value):
9396
self.mutationVariate.x0 = value
9497
newval = self.mutationVariate.getSample()
9598
# print("MUTATED: ", value, "--->", newval)
96-
return newval
97-
98-
9999

100+
return newval
100101

101102

102103
class Randomization(SimpleGenomeManipulation):
@@ -107,6 +108,7 @@ def __init__(self, minval=0., maxval=1.):
107108
SimpleGenomeManipulation.__init__(self)
108109
self._minval = minval
109110
self._maxval = maxval
111+
self._uniform_variate = None
110112

111113
def apply(self, population):
112114
self._uniform_variate = UniformVariate(self._minval, self._maxval)
@@ -116,8 +118,3 @@ def apply(self, population):
116118
def _manipulateValue(self, value):
117119
""" See SimpleGenomeManipulation._manipulateValue() for more information """
118120
return self._uniform_variate.getSample()
119-
120-
121-
122-
123-

0 commit comments

Comments
 (0)