Exemplo n.º 1
0
def pso_algo(f, s, bounds, params, maxrounds, tol, nochange):
    n = len(bounds)
    pcurr, vcurr, pbest, fbest, pgbest, fgbest = pso_init(f, s, bounds)
    t = 0
    samebest = 0
    while t < maxrounds:
        fgbest_compare = fgbest
        for i in range(s):
            for d in range(n):
                vcurr[i][d] = veloc_update(pcurr[i][d], vcurr[i][d],
                                           pbest[i][d], pgbest[d], params)
            newp = pcurr[i] + vcurr[i]
            for d in range(n):
                if newp[d] > bounds[d][0] and newp[d] < bounds[d][1]:
                    #Adding 0 creates a new object in memory instead of variable that references same object
                    pcurr[i][d] = newp[d] + 0
            fcurr = f(pcurr[i])
            if fcurr < fbest[i]:
                fbest[i] = fcurr + 0
                pbest[i] = pcurr[i] + 0
                if fcurr < fgbest:
                    fgbest = fcurr + 0
                    pgbest = pcurr[i] + 0
        t += 1

        if abs(fgbest_compare - fgbest) > tol:
            samebest = 0
        else:
            samebest += 1

        if samebest >= nochange:
            break
    return pgbest, fgbest, t
Exemplo n.º 2
0
def pso_algo_par(f, s, bounds, params, maxrounds, tol, nochange):
    n = len(bounds)
    pcurr, vcurr, pbest, fbest, pgbest, fgbest = pso_init(f, s, bounds)
    t = 0
    samebest = 0
    while t < maxrounds:
        fgbest_compare = fgbest
        inputs = zip(pcurr, vcurr, pbest, fbest, repeat(pgbest),
                     repeat(params), repeat(bounds), repeat(f))

        results_0 = pool.starmap(point_update, inputs)
        results = list(map(list, zip(*results_0)))

        pcurr = np.array(list(results)[0])
        vcurr = np.array(list(results)[1])
        pbest = np.array(list(results)[2])
        fbest = np.array(list(results)[3])

        if min(fbest) < fgbest:
            #Adding 0 creates a new object in memory instead of variable that references same object
            fgbest = min(fbest) + 0
            pgbest = np.copy(pbest[fbest == fgbest], order="k")[0]
        t += 1

        if abs(fgbest_compare - fgbest) > tol:
            samebest = 0
        else:
            samebest += 1

        if samebest >= nochange:
            break
    return pgbest, fgbest, t
Exemplo n.º 3
0
def pso_algo(f, s, bounds, params, maxrounds, p=0.9, optimum_dis=0.01):
    n = len(bounds)
    pcurr, vcurr, pbest, fbest, pgbest, fgbest = pso_init(f, s, bounds)
    t = 0
    while t < maxrounds:
        for i in range(s):
            for d in range(n):
                vcurr[i][d] = veloc_update(pcurr[i][d], vcurr[i][d],
                                           pbest[i][d], pgbest[d], params)
            newp = pcurr[i] + vcurr[i]
            for d in range(n):
                if newp[d] > bounds[d][0] and newp[d] < bounds[d][1]:
                    pcurr[i][d] = newp[d] + 0
            fcurr = f(pcurr[i])
            if fcurr < fbest[i]:
                fbest[i] = fcurr + 0
                pbest[i] = pcurr[i] + 0
                if fcurr < fgbest:
                    fgbest = fcurr + 0
                    pgbest = pcurr[i] + 0
        t += 1
        # pcurr_best = pcurr[fbest > min(heapq.nlargest(int(fbest.size*p), fbest))]
        # if dist(pcurr_best, pgbest) <= optimum_dis:
        #     break
    return [pcurr, pgbest]
Exemplo n.º 4
0
def pso_algo_par(f, N, bounds, params, maxrounds, tol, nochange):

    # Initialize all necessary variables
    pcurr, vcurr, pbest, fbest, pgbest, fgbest = pso_init(f, N, bounds)
    t = 0
    samebest = 0

    # Start algorithm
    while t < maxrounds:
        fgbest_compare = fgbest

        # Puts inputs in format so each list is the info for one particle, where sublist is [pcurr_i, vcurr_i, etc]
        inputs = zip(pcurr, vcurr, pbest, fbest, repeat(pgbest), repeat(params), repeat(bounds), repeat(f))

    # Calls starmap using the pool object to run point_update on each particle using inputs as formatted above
        # results_0 is a list where each sublist is the output for each particle
        results_0 = pool.starmap(point_update, inputs)

        # Reformat results so each sublist corresponds to variable rather than particle
        results = list(map(list, zip(*results_0)))

        # Assign the sublists of results to the variables themselves
        pcurr = np.array(list(results)[0])
        vcurr = np.array(list(results)[1])
        pbest = np.array(list(results)[2])
        fbest = np.array(list(results)[3])


        # Updates global best
        if min(fbest) < fgbest:
            #Adding 0 creates a new object in memory instead of variable that references same object
            fgbest = min(fbest) + 0
            pgbest = np.copy(pbest[fbest == fgbest], order="k")[0]

        t += 1


        # Stopping criteria
        if abs(fgbest_compare - fgbest) > tol:
            samebest = 0
        else:
            samebest += 1
        if samebest >= nochange:
            break

    return pgbest, fgbest, t
Exemplo n.º 5
0
def pso_algo(f, N, bounds, params, maxrounds, tol, nochange):

    # Initialize all necessary variables
    D = len(bounds)
    pcurr, vcurr, pbest, fbest, pgbest, fgbest = pso_init(f, N, bounds)
    t = 0
    samebest = 0

    # Start algorithm
    while t < maxrounds:
        fgbest_compare = fgbest
        for i in range(N):

            # Updates velocity of each dimension of the particle 
            for d in range(D):
                vcurr[i][d] = veloc_update(pcurr[i][d], vcurr[i][d], pbest[i][d], pgbest[d], params)

            # Only updates the dimension of pcurr if it would be within the bounds
            newp = pcurr[i] + vcurr[i]

            # Only updates the dimension of pcurr if it would be within the bounds
            for d in range(D):
                if newp[d] > bounds[d][0] and newp[d] < bounds[d][1]:
                    #Adding 0 creates a new object in memory instead of variable that references same object
                    pcurr[i][d] = newp[d] + 0 

            # Updates the particle's best and global best
            fcurr = f(pcurr[i])
            if fcurr < fbest[i]:
                fbest[i] = fcurr + 0
                pbest[i] = pcurr[i] + 0
                if fcurr < fgbest:
                    fgbest = fcurr + 0
                    pgbest = pcurr[i] + 0
        t += 1


        # Stopping criteria
        if abs(fgbest_compare - fgbest) > tol :
            samebest = 0
        else :
            samebest += 1
        if samebest >= nochange :
            break

    return pgbest, fgbest, t