예제 #1
0
def initialize(obj, dim, algo):
    fitness = []
    for i in range(0, no_particles):
        p = Particle()  # Object for the Particle Class
        particles.append(
            p)  # Add the particle object to the :code:`particles` list
        p._pos = np.random.randint(
            2, size=dim)  # create of particles and dimension
        p._velocity = np.random.uniform(
            -1, 1, size=dim)  # create a velocity for all the particle
        p._fitness = obj.getAccuracy(obj.data_cleaning(p._pos),
                                     algo)  # Fitness of the particle
        fitness.append(
            p._fitness
        )  # An array that stores all the fitness of each and every particle
        p._personalBest = p._pos  # the personal best of the particle
    max_fitness_index = fitness.index(
        max(fitness))  # stores the index of particle with best fitness
    best_fitness_obj = particles[
        max_fitness_index]  # best fitness object in the swarm
    best_pos = particles[
        max_fitness_index]._pos  # stores the position of the best particle  with max fitness
    best_fitness = particles[
        max_fitness_index]._fitness  # the best in the swarm
    fitness_overall.append(
        best_fitness)  # add the current best fitness to fitness overall
    pos_overall.append(
        best_pos)  # add the current best position to position overall
    return