Beispiel #1
0
    def evolvedGrammarHaiku(self):

        bestHaiku = ""
        bestFitness = 0
        currFitness = 0

        individual = Individual(self.grammar, self.season)
        newHaiku = self.grammarHaiku()

        best = []

        for i in xrange(25):
            print i
            newHaiku = individual.mutate(newHaiku)
            currFitness = individual.fitness(newHaiku)

            print newHaiku
            print currFitness
            print "\n"

            if currFitness > bestFitness:
                best.append((currFitness, newHaiku))
                bestHaiku = newHaiku
                bestFitness = currFitness

        print "Best haiku is:"
        print bestHaiku
        print bestFitness
        print self.grammar

        print "Best Haiku: "
        print sorted(best)
Beispiel #2
0
    def createEvolvedHaiku(self):
        individual = Individual(self.grammar)
        initialHaiku = self.createNaiveHaiku()

        fitnessLevel = individual.fitness(initialHaiku)

        bestHaiku = initialHaiku
        bestFitness = fitnessLevel

        for i in xrange(5000):
            initialHaiku = individual.naiveMutate(initialHaiku)
            fitnessLevel = individual.fitness(initialHaiku)

            if fitnessLevel > bestFitness:
                bestHaiku = initialHaiku
                bestFitness = fitnessLevel

            print initialHaiku + "\n"
            print "count is: " + str(i)

        print "Best Haiku is :\n" + bestHaiku
        print "\nEvaluation is:\n" + str(bestFitness)