genetic algorithm - Python List Error -


i writing simple genetic program test out process, got following error:

traceback (most recent call last):   file "evolution.py", line 43, in <module>     nextgen += test[operator.indexof(list(fits), m+chance)] typeerror: iteration on non-sequence 

on code:

#!/usr/bin/env python  import random import operator  mutationchance = 0.01 simtime        = 1000  # quadratic optimizer class organism:     def __init__(self, x):         self.x = x         self.a = 1         self.b = 2         self.c = 3         self.epsilon = 0.01     def fitness(self):         return self.a*(self.x**2) + self.b*(self.x) + self.c     def mutate(self):         self.x += random.random() - 0.5     def describe(self):         return self.x  def condmutate(org, chance):     if random.random() <= chance:         org.mutate()     return org  test = [organism(i) in range(-10, 10)]      generation = 0  while generation < simtime:     fits = [test[i].fitness() in range(0, len(test) - 1)]     sumf = sum(fits)     = 0     nextgen = list()     while < len(fits) / 2:         chance = random.random() * sumf         j = 0         shg = [fits[i] - chance in range(0, len(test) - 1)]         m = min(shg)         nextgen += test[operator.indexof(list(fits), m+chance)]         nextgen += test[operator.indexof(list(fits), m+chance)]     test = [condmutate(test[i], mutationchance) in range(0, len(nextgen) - 1)]     generation += 1  print "result: ", max([test[i].describe() in range(0, len(test) - 1)]) 

i new python, may newbie mistake.

to add element nextgen, use nextgen.append(...) instead. += wrong operator.

specifically, += on list concatenates iterable, not element.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -