예제 #1
0
파일: pso.py 프로젝트: LukeKort/Opp
def pso(n_particles,n_variables,n_iterations,tolerance,a,b,pso_only_w,pso_only_c1,pso_only_c2):

    from random_matrix import radom_generator #random generator. takes x and y vector dimentions between the limits a and b
    import functions
    reload(functions)  #uptade the changes made in the function file
    from functions import objective #objetive function(s)
    from functions import constraints #constraint function(s)

    best_result_acum = np.empty((n_iterations)) #preallocation
    v = radom_generator(n_variables,n_particles,a,b) #velocity matrix
    x_aux = radom_generator(n_variables,n_particles,a,b)
    x = radom_generator(n_variables,n_particles,a,b)
    x_best = np.zeros((n_variables))

    cc = 1 #controler counter

    t_0 = time.time() #start time

    for i in range(n_iterations):  
        x_0=x.copy() #stores the last x before uptade
        for j in range(n_particles):
            v[:,j]= pso_only_w*v[:,j] + rand.random()*pso_only_c1*(x_aux[:,j]-x[:,j]) + rand.random()*pso_only_c2*(x_best - x[:,j]) #new velocity matrix
            x[:,j]=x_0[:,j]+v[:,j] #new position matrix
            for k in range(n_variables): #test with the limits (a,b)
                if x[k,j]<a[k]:
                    x[k,j]=a[k]+(b[k]-a[k])*rand.random()
                if x[k,j]>b[k]:
                    x[k,j]=a[k]+(b[k]-a[k])*rand.random()
            if (constraints(x[:,j])) is True: #teste the new x within the constraints functions
                if (objective(x[:,j])) < objective(x_aux[:,j]): #teste the new x within the objetive function
                    x_aux[:,j] = x[:,j].copy() #setting new best particle position
                    
                    if cc == 1:
                        results = np.full(n_particles,objective(x_aux[:,j])) #the 1st best value will fill the results vector
                        best_result_acum = np.full(n_iterations,objective(x_aux[:,j]))
                        cc += 1
                    else:    
                        results[j] = objective(x_aux[:,j]) #save result per particle
                    
                    best_result = min(results).copy() #find best result of all particles
                    best_result_acum[i] = best_result.copy()
                    idx = results.tolist().index(best_result) #find the best result's index inside the results vector
                    x_best = x_aux[:,idx] #find the best result's position
        
        if tolerance >= np.amax(abs(x-x_0)): #break for setting tolerance
            break

    t_end = time.time() #finish time
    t_total = t_end - t_0 #total processing time

    if cc == 1:
        best_result = x_best = 'Not found!' #if the problem can't be solved, rise the messange

    print('#Particle Swarm\nMelhor resultado:',best_result,'\nPosição:',x_best,'\nTempo de execução:',(t_total),'s\n')
    
    return({'best_result':best_result,'acumulate_result':best_result_acum,'x_best':x_best,'t_total':t_total,'max_n_iteration':i})
예제 #2
0
def lj(n_variables, n_iterations, tolerance, a, b, lj_only_in, lj_only_c):

    from random_matrix import radom_generator  #random generator. takes x and y vector dimentions between the limits a and b
    import functions
    reload(functions)  #uptade the changes made in the function file
    from functions import objective  #objetive function(s)
    from functions import constraints  #constraint function(s)

    best_result_acum = np.empty((n_iterations))  #preallocation
    x_0 = (radom_generator(n_variables, 1, a, b))  #postion matrix
    x = r = np.ones((n_variables, 1))

    t_0 = time.time()  #start time

    for i in range(n_iterations):  #external cicle
        x_aux = x_0.copy()
        for j in range(lj_only_in):  #internal cicle
            x[:,
              0] = x_0[:,
                       0] + (2 * rand.random() - 1) * r[:, 0]  #particle update
            if (constraints(
                    x[:, 0])) is True:  #teste within the constraints functions
                if (objective(x[:, 0])) < objective(
                        x_0[:, 0]):  #teste particle update
                    x_0 = x  #setting new particle position

        best_result = objective(x_0[:, 0])  #find best result of each iteration
        best_result_acum[i] = best_result

        r[:, 0] = (1 - lj_only_c) * abs(x[:, 0] - x_aux[:, 0])

        if tolerance >= np.amax(r):  #break for setting tolerance
            x_best = x_0  #find the best result's position
            break

    x_best = x_0[:, 0]  #find the best result's position

    t_end = time.time()  #finish time
    t_total = t_end - t_0  #total processing time

    print('#Luus Jaakola\nMelhor resultado:', best_result, '\nPosição:',
          x_best, '\nTempo de execução:', (t_total), 's\n')

    return ({
        'best_result': best_result,
        'acumulate_result': best_result_acum,
        'x_best': x_best,
        't_total': t_total,
        'max_n_iteration': i
    })
예제 #3
0
파일: alcateia.py 프로젝트: LukeKort/Opp
def alcateia(n_particles, n_variables, n_iterations, tolerance, a, b,
             alcateia_only_ic, alcateia_only_id):

    from random_matrix import radom_generator  #random generator. takes x and y vector dimentions between the limits a and b
    import functions
    reload(functions)  #uptade the changes made in the function file
    from functions import objective  #objetive function(s)
    from functions import constraints  #constraint function(s)

    x = delta = np.zeros((n_variables, n_particles),
                         dtype=float)  #preallocation
    #results = np.ones((n_particles)) #preallocation
    best_result_acum = np.empty((n_iterations))  #preallocation

    r = radom_generator(n_variables, n_particles, a, b)
    x_0 = (radom_generator(n_variables, n_particles, a, b))  #postion matrix

    cc = 1  #controler counter

    t_0 = time.time()  #start time

    for i in range(n_iterations):  #external cicle
        for j in range(alcateia_only_ic):  #internal cicle
            for k in range(n_particles):  #calculos per particle
                delta[:, k] = (-1 + 2 * rand.random()) * r[:, k]
                x[:, k] = x_0[:, k] + delta[:, k]  #particle update
                for l in range(n_variables):  #test with the limits (a,b)
                    if x[l, k] < a[l]:
                        x[l, k] = a[l] + (b[l] - a[l]) * rand.random()
                    if x[l, k] > b[l]:
                        x[l, k] = a[l] + (b[l] - a[l]) * rand.random()
                if (constraints(x[:, k])
                    ) is True:  #teste within the constraints functions
                    if (objective(x[:, k])) < objective(
                            x_0[:, k]):  #teste particle update
                        x_0[:, k] = x[:, k]  #setting new particle position
                        if cc == 1:
                            results = np.full(n_particles, objective(x_0[:,
                                                                         k]))
                            cc += 1
                        else:
                            results[k] = objective(
                                x_0[:, k])  #save result per particle
                        best_result = min(
                            results)  #find best result of all particles
                        best_result_acum[i] = best_result
                        idx = results.tolist().index(
                            best_result
                        )  #find the best result's index inside the results vector
                        x_best = x_0[:, idx]  #find the best result's position

        if cc != 1:
            for w in range(
                    n_particles
            ):  #uptade x_0 within the best result of last external cicle
                r[:, w] = abs(x_0[:, w] - x[:, w])
                x_0[:, w] = alcateia_only_id * x_0[:, w] + (
                    1 - alcateia_only_id) * x_best

        if tolerance >= np.amax(r):  #break for setting tolerance
            #best_result_acum[:,i:n_iterations]=best_result #comple the rest of the vector with the last value
            break

    t_end = time.time()  #finish time
    t_total = t_end - t_0  #total processing time

    if cc == 1:
        best_result = x_best = 'Não encontrado!'

    print('#Alcateia\nMelhor resultado:', best_result, '\nPosição:', x_best,
          '\nTempo de execução:', (t_total), 's\n')

    return ({
        'best_result': best_result,
        'acumulate_result': best_result_acum,
        'x_best': x_best,
        't_total': t_total,
        'max_n_iteration': i
    })