gmalik9 / pygep

Automatically exported from code.google.com/p/pygep
0 stars 2 forks source link

Division by zero #23

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Following the example "regression.py" I've written the following micro-example:

class DataPoint(object):
    SAMPLE = []
    LO, HI = -20.0, 20.0
    SIZE = 50
    RANGE = HI - LO

    def __init__(self, x):
        self.x = float(x)
        self.y = (x ** 2) - (3.7 * x) + (7.1)

    def __str__(self):
        return "(" + str(self.x) + ", " + str(self.y) + ")"

    @staticmethod
    def populate():
        for _ in xrange(DataPoint.SIZE):
            x = DataPoint.LO + (random.random()  * DataPoint.SIZE)
            DataPoint.SAMPLE.append(DataPoint(x))

class Parabola(Chromosome):
    functions = add_op, subtract_op, multiply_op, divide_op
    terminals = 'x', '?'

    def _fitness(self):
        return 1.0 / sum(map(lambda p: ((self(p) - p.y) ** 2),  DataPoint.SAMPLE)) / (DataPoint.SIZE * 1.0)

    def _solved(self):
        self.fitness < 0.001

if __name__ == "__main__":
    DataPoint.populate()
    pop = Population(Parabola, 50, 6, 1, sum_linker)

If I run it, I got this:

>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/petrux/Projects/PyGEP/pygep/src/test01.py", line 42, in <module>
    pop = Population(Parabola, 50, 6, 1, sum_linker)
  File "pygep/population.py", line 117, in __init__
    self._update_stats()
  File "pygep/population.py", line 148, in _update_stats
    self.mean, self.stdev, _ = stats.fitness_stats(self)
  File "pygep/util/stats.py", line 32, in fitness_stats
    total = sum(i.fitness for i in population)
  File "pygep/util/stats.py", line 32, in <genexpr>
    total = sum(i.fitness for i in population)
  File "pygep/chromosome.py", line 263, in <lambda>
    fitness = property(lambda self: self._fitness(), doc='Fitness value')
  File "pygep/util/__init__.py", line 46, in wrapper
    setattr(self, cache_name, func(self))
  File "/home/petrux/Projects/PyGEP/pygep/src/test01.py", line 35, in _fitness
    return 1.0 / sum(map(lambda p: ((self(p) - p.y) ** 2),  DataPoint.SAMPLE)) / (DataPoint.SIZE * 1.0)
  File "/home/petrux/Projects/PyGEP/pygep/src/test01.py", line 35, in <lambda>
    return 1.0 / sum(map(lambda p: ((self(p) - p.y) ** 2),  DataPoint.SAMPLE)) / (DataPoint.SIZE * 1.0)
  File "pygep/chromosome.py", line 252, in __call__
    return self.linker(*[g(obj) for g in self.genes])
  File "pygep/util/__init__.py", line 81, in wrapper
    memo[key] = results = func(self, key)
  File "pygep/gene/karva.py", line 76, in __call__
    self._evaluation[i] = allele(*args)
  File "pygep/functions/mathematical/arithmetic.py", line 37, in <lambda>
    divide_op   = symbol('/')(lambda i, j: float(i) / j)
ZeroDivisionError: float division by zero

It seems that the random number generation allows a division by zero.

Original issue reported on code.google.com by giulio.p...@gmail.com on 26 Nov 2013 at 10:31