def genetic_algorithm(pop, points, generation_number):
    fitness_array = pop_fitness(pop, points)
    fitness_array_cumlative = cumulative_sum(fitness_array)
    summation = fitness_array_cumlative[len(fitness_array_cumlative) - 1]

    # Proof that the value converges
    # print("%f && %f " % (min(fitness_array), max(fitness_array)))

    next_pop = []
    for _ in range(pop_size):
        r1, r2 = random.uniform(0, summation), random.uniform(0, summation)
        idx1 = bisect.bisect_left(fitness_array_cumlative, r1)
        idx2 = bisect.bisect_left(fitness_array_cumlative, r2)

        Offspring1, Offspring2 = cross_over(pop[idx1], pop[idx2])

        mutate(Offspring1, generation_number)
        mutate(Offspring2, generation_number)
        next_pop.append(Offspring1)
        next_pop.append(Offspring2)

    # Merge the new and old populations and take the best of both
    pop = pop + next_pop
    pop.sort(key=lambda x: ff.fitness(x, points), reverse=True)
    next_gen = pop[:pop_size // 2] + create_population(len(pop[0]) - 1, pop_size // 2)

    best_chromosome = next_gen[0]
    best_val_old_pop = ff.fitness(best_chromosome, points)

    return next_gen, best_val_old_pop, best_chromosome
 def test_plot_rotation(self):
     c = Camera(0.48271098732948303, 1920, 1080, [5, 0, 0], [math.pi / 2, 0, math.pi])
     genome = np.array([5, 0, 0, math.pi / 2, 0, math.pi])
     t = Person('Max Mustermann', np.array([0, 0, 0]), 1,
         np.array([0.7, -0.2, 0]), np.array([0.7, 0.2, 0]))
     lt = Person('Agnes Angeschaute', np.array([2, 0, 0]), 1,
         np.array([1.3, -0.2, 0]), np.array([1.3, 0.2, 0]))
     persons = [t, lt]
     #PositionProcess.optimizeAllShots(t, t, c, [t], [], genome, [2])
     snapshot = SceneSnapshot(t, lt, c, persons, [], [], [2])
     optimizer = PositionProcess.CameraOptimizer(snapshot, 2)
     #optimizer.fitness(genome)
     winkel = arange(-math.pi, math.pi, 0.01)
     s = arange(-math.pi, math.pi, 0.01)
     startposition = np.array([5, 0, 0])
     #startposition.reshape(-1, 1)
     for i in range(0, len(winkel)):
         p = startposition.dot(FitnessFunction.rotate(np.array([0, 0, 0, math.pi / 2, winkel[i]])))
         s[i] = FitnessFunction.fitness(np.array([p[0],p[1],p[2],math.pi/2,winkel[i]+0.5*math.pi]), optimizer)
         #s[i] = optimizer.fitness(np.array([5,0,0,math.pi/2,winkel[i]+math.pi/2]))
         #s[i] = optimizer.getPersonQuality(np.array([p[0],p[1],p[2], math.pi / 2, winkel[i] + math.pi/2]), t, 1.2,
         #    personFitnessByImage, occultationWeight)
         #s[i] = optimizer.getPersonQuality(np.array([5,0,0, math.pi / 2, winkel[i] + math.pi/2]), t, 1.2,
         #    personFitnessByImage, occultationWeight)
         #s[i] = PositionProcess.getImageAngles(np.array([p[0],p[1],p[2], math.pi / 2, winkel[i] + math.pi*0.5]), t)[0]
         #s[i] = PositionProcess.getImageAngles(np.array([5,0,0,winkel[i]+math.pi / 2, math.pi/2]), t)[0]
     ax = subplot(111)
     ax.plot(winkel, s)
     ax.grid(True)
     ticklines = ax.get_xticklines()
     ticklines.extend(ax.get_yticklines())
     gridlines = ax.get_xgridlines()
     gridlines.extend(ax.get_ygridlines())
     ticklabels = ax.get_xticklabels()
     ticklabels.extend(ax.get_yticklabels())
     for line in ticklines:
         line.set_linewidth(3)
     for line in gridlines:
         line.set_linestyle('-')
     for label in ticklabels:
         label.set_color('r')
         label.set_fontsize('medium')
     show()
def pop_fitness(pop, points):
    return [ff.fitness(i, points) for i in pop]