Beispiel #1
0
#def annotate(label, x, y):
#    return plt.annotate(
#                        label,
#                        xy = (x, y), xytext = (-20, 20),
#                        textcoords = 'offset points',
#                        ha = 'right', va = 'bottom',
#                        bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
#                        arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))

#annotates = [annotate(label, x, y) for label, x, y in zip(labels, xs, ys)]

#for particle in particles:
#    print(str(particle) + ' fitness {}'.format(utility_function(particle.position)))

global_solution = None
for i in arange(1, 1000):
    #for annotate, particle_lines in zip(annotates, particles_lines):
    for particle in particles:
        factor = -1 if random() > 0.5 else 1
        delta = Point(factor * random() / 100, factor * random() / 100)
        particle.calculate_fitness(delta)

    global_best = max([(particle.p_best, utility_function.f(particle.p_best))
                       for particle in particles],
                      key=lambda x: x[1])
    global_solution = global_best[0]

    for particle in particles:
        particle.move(global_best[0])

print('Global solution {}'.format(global_solution))
Beispiel #2
0
#def annotate(label, x, y):
#    return plt.annotate(
#                        label,
#                        xy = (x, y), xytext = (-20, 20),
#                        textcoords = 'offset points',
#                        ha = 'right', va = 'bottom',
#                        bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
#                        arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))

#annotates = [annotate(label, x, y) for label, x, y in zip(labels, xs, ys)]

#for particle in particles:
#    print(str(particle) + ' fitness {}'.format(utility_function(particle.position)))

global_solution = None
for i in arange(1, 1000):
    #for annotate, particle_lines in zip(annotates, particles_lines):
    for particle in particles:
        factor = -1 if random() > 0.5 else 1
        delta = Point(factor * random()/100, factor * random()/100)
        particle.calculate_fitness(delta)

    global_best = max([(particle.p_best, utility_function.f(particle.p_best)) for particle in particles],
                      key=lambda x: x[1])
    global_solution = global_best[0]

    for particle in particles:
        particle.move(global_best[0])

print('Global solution {}'.format(global_solution))
Beispiel #3
0
 def calculate_fitness(self, delta):
     candidate = self.position + delta
     if utility_function.f(candidate) > utility_function.f(self.__position):
         self.__particle_best = candidate