Exemple #1
0
    def evolve(self):
        new_population = []
        new_scores = defaultdict(int)
        mutation_p = 0.01
        for x in xrange(self.population_size - 40):
            first = choice(self.classifiers)
            second = choice(self.classifiers)
            if self.scores[first] >= self.scores[second]:
                new_population.append(first)
                new_scores[first] = self.scores[first]
            else:
                new_population.append(second)
                new_scores[second] = self.scores[second]

        def crossover(first, second):
            idx = choice(xrange(len(first.condition)))
            condition = first[:idx] + second[idx:]
            action = choice([first.action, second.action])
            return Classifier(condition=condition, action=action)

        crossovers = []
        for x in xrange(self.population_size - len(new_population)):
            first = choice(new_population)
            second = choice(new_population)
            crossovers.append(crossover(first, second))

        def mutate_cond(clf):
            idx = choice(xrange(len(clf.condition)))
            condition = str(clf.condition[:idx]) + choice(
                ["0", "1", "#"]) + str(clf.condition[idx + 1:])
            return Classifier(condition=condition, action=clf.action)

        def mutate_action(clf):
            action = choice(self.actions)
            return Classifier(condition=clf.condition, action=action)

        mutants = []
        for clf in new_population:
            if random_f() < mutation_p:
                new_clf = mutate_cond(clf)
                mutants.append(new_clf)
        for clf in new_population:
            if random_f() < mutation_p:
                new_clf = mutate_action(clf)
                mutants.append(new_clf)

        self.classifiers = new_population + crossovers + mutants
        self.scores = new_scores
Exemple #2
0
    def evolve(self):
        new_population = []
        new_scores = defaultdict(int)
        mutation_p = 0.01
        for x in xrange(self.population_size - 40):
            first = choice(self.classifiers)
            second = choice(self.classifiers)
            if self.scores[first] >= self.scores[second]:
                new_population.append(first)
                new_scores[first] = self.scores[first]
            else:
                new_population.append(second)
                new_scores[second] = self.scores[second]

        def crossover(first, second):
            idx = choice(xrange(len(first.condition)))
            condition = first[:idx] + second[idx:]
            action = choice([first.action, second.action])
            return Classifier(condition=condition, action=action)

        crossovers = []
        for x in xrange(self.population_size - len(new_population)):
            first = choice(new_population)
            second = choice(new_population)
            crossovers.append(crossover(first, second))

        def mutate_cond(clf):
            idx = choice(xrange(len(clf.condition)))
            condition = str(clf.condition[:idx]) + choice(["0", "1", "#"]) + str(clf.condition[idx + 1 :])
            return Classifier(condition=condition, action=clf.action)

        def mutate_action(clf):
            action = choice(self.actions)
            return Classifier(condition=clf.condition, action=action)

        mutants = []
        for clf in new_population:
            if random_f() < mutation_p:
                new_clf = mutate_cond(clf)
                mutants.append(new_clf)
        for clf in new_population:
            if random_f() < mutation_p:
                new_clf = mutate_action(clf)
                mutants.append(new_clf)

        self.classifiers = new_population + crossovers + mutants
        self.scores = new_scores
Exemple #3
0
 def __init__(self, secs=None, random=None, debug=False):
     self.debug=debug
     if secs:
         secs=float(secs)
         self.dwell=lambda : secs
     else:
         rand_spec=random
         rand_spec_doc="""
             "scale:offset"
             scale
             (scale,offset)
         """
         if isinstance(rand_spec, basestring):
             scale,offset=map(float,rand_spec.split(':'))
         elif isinstance(rand_spec, (list,tuple)):
             scale,offset=rand_spec
         elif isinstance(rand_spec, (int,float)):
             scale,offset=rand_spec,0
         else:
             raise RuntimeError("invalid random sleep specification. 'random' must be one of:\n" \
                                    + rand_spec_doc)
         self.dwell=lambda : scale*random_f()+offset