Lucretiel / genetics

A python library for genetic algorithms
GNU Lesser General Public License v2.1
235 stars 61 forks source link

hello world! example #4

Open unnir opened 7 years ago

unnir commented 7 years ago

I have these error messages for the hello world example.

Traceback (most recent call last):
  File "/genetics-master/hello_world.py", line 42, in <module>
    best, best_score, average_score = dna_stats(population)
  File "/genetics-master/hello_world.py", line 32, in dna_stats
    best_dna = max(population)
TypeError: unorderable types: WordDNA() > WordDNA()
EmbolismSoil commented 7 years ago

you should provide the method '__gt__' so that the 'max' function can choice an greater instance of WordDNA when running:

class WordDNA(genetics.arrayed_segment(len(solution), LetterComponent)):
    def score(self):
        return sum(comp.value == letter for comp, letter in zip(self, solution))

    def __str__(self):
        return ''.join(comp.value for comp in self)

    def __gt__(self, other):
        return self.score() > other.score()