コード例 #1
0
    def run(self):

        Tx, Ty = np.load('terminal_point_{}_{}.npy'.format(
            str(self.dim), str(self.n)))
        lenT = Tx.size
        data_pso = dict()

        distancevector = gd.get_distancevector(np.copy(Tx), np.copy(Ty))
        mst_size = pa.get_tree(distancevector)

        for i in range(self.max_iter):
            print('Iteration No :', i)
            t1 = t.time()
            res = call_methods(np.copy(Tx), np.copy(Ty), lenT, self.func)
            t2 = t.time()
            data_pso[res[0]] = (res[1], res[2], t2 - t1)

        min_pso = min(data_pso.keys())

        fp = open(self.file_name, 'w')
        fp.write("Size of the MST = " + str(mst_size) + '\n')
        fp.write('No. of Iterations :' + str(self.max_iter) + '\n')
        fp.write('PSO Min Wt :' + str(min_pso) + '\n')
        fp.write('X Coordinates :' + str(data_pso[min_pso][0]) + '\n')
        fp.write('Y Coordinates :' + str(data_pso[min_pso][1]) + '\n')
        fp.write('No. of Steiner points :' +
                 str(data_pso[min_pso][0].size - self.n) + '\n')
        fp.write('Time Required :' + str(data_pso[min_pso][2]) + '\n')
        fp.write('Error Ratio :' + str(min_pso / mst_size) + '\n')
        fp.close()
コード例 #2
0
def call_methods(Tx, Ty, lenT, func):

    # Setting the method into a method variable for dynamic linking
    global get_velocity_position
    get_velocity_position = func

    # Calling the method that controls the main optimisation algorithm
    bestparticle = particle_swarm_test(np.copy(Tx), np.copy(Ty), lenT)

    # Extracting the position of the Steiner Points from the best particle
    for i in range(bestparticle.Sx.size):
        if bestparticle.Sx[i] < min(Tx) or bestparticle.Sx[i] > max(Tx):
            continue
        if bestparticle.Sy[i] < min(Ty) or bestparticle.Sy[i] > max(Ty):
            continue

        Tx = np.append(Tx, bestparticle.Sx[i])
        Ty = np.append(Ty, bestparticle.Sy[i])

    # Finding the MST of the final RSMT
    distancevector = gd.get_distancevector(Tx, Ty)
    mst = pa.mst_prim(distancevector)
    mst_size = pa.get_tree(distancevector)

    # Building the final return tuple for the use in the main program
    return_set = (mst_size, Tx, Ty)

    # Ploting the RSMT
    pa.draw_gridgraph(Tx, Ty, mst, lenT, lenT)

    return return_set
コード例 #3
0
def get_fitness(Tx, Ty, particle):
    for i in range(len(particle.Sx)):
        if particle.Sx[i] < min(Tx) or particle.Sx[i] > max(Tx):
            continue
        if particle.Sy[i] < min(Ty) or particle.Sy[i] > max(Ty):
            continue
        Tx.append(particle.Sx[i])
        Ty.append(particle.Sy[i])

    distancevector = gd.get_distancevector(Tx, Ty)
    objectivefitness = pa.get_tree(distancevector)
    return objectivefitness
コード例 #4
0
def get_fitness(Tx, Ty, weed):
    for i in range(len(weed.Wx)):
        if weed.Wx[i] < min(Tx) or weed.Wx[i] > max(Tx):
            continue
        if weed.Wy[i] < min(Ty) or weed.Wy[i] > max(Ty):
            continue
        Tx.append(weed.Wx[i])
        Ty.append(weed.Wy[i])

    distancevector = gd.get_distancevector(Tx, Ty)
    objectivefitness = pa.get_tree(distancevector)
    return objectivefitness
コード例 #5
0
def call_methods(Tx, Ty, lenTx, lenTy):
    temp_len = Tx.size
    Rx = Tx[0:temp_len - lenTx]
    Ry = Ty[0:temp_len - lenTy]
    len_Rx = Rx.size
    Tx = Tx[0:temp_len - len_Rx]
    Ty = Ty[0:temp_len - len_Rx]
    #print("X coordinates =", Tx)
    #print("Y coordinates =", Ty)

    bestparticle = particle_swarm_test(Tx, Ty)

    temp_len = Tx.size
    Rx = Tx[0:temp_len - lenTx]
    Ry = Ty[0:temp_len - lenTy]
    len_Rx = Rx.size
    Tx = Tx[0:temp_len - len_Rx]
    Ty = Ty[0:temp_len - len_Rx]
    #print("Updated X coordinates", Tx)
    #print("Updated Y coordinates", Ty)
    '''print("X coordinates of best particle", bestparticle.Sx)
    print("Y coordinates of best particle", bestparticle.Sy)
    print("Updated X coordinates", Tx)
    print("Updated Y Coordinates", Ty)'''
    count = 0
    for i in range(len(bestparticle.Sx)):
        if bestparticle.Sx[i] < min(Tx) or bestparticle.Sx[i] > max(Tx):
            continue
        if bestparticle.Sy[i] < min(Ty) or bestparticle.Sy[i] > max(Ty):
            continue

        #Tx.append(bestparticle.Sx[i])
        #Ty.append(bestparticle.Sy[i])
        Tx = np.append(Tx, bestparticle.Sx[i])
        Ty = np.append(Ty, bestparticle.Sy[i])
        count = count + 1
    #print("Updated X Coordinates", Tx)
    #print("Updated Y Coordinates", Ty)
    distancevector = gd.get_distancevector(Tx, Ty)
    mst = pa.mst_prim(distancevector)
    mst_size = pa.get_tree(distancevector)
    #print("Size of Steiner Tree for PSO", mst_size)
    return_set = (mst_size, Tx, Ty)
    #pa.draw_gridgraph(Tx, Ty, mst,lenTx,lenTx)

    temp_len = Tx.size
    Tx = Tx[0:temp_len - count]
    Ty = Ty[0:temp_len - count]
    '''print("Restored X Coordinates=", Tx)
    print("Restored Y Coordinates=", Ty)
    print("End of PSO")'''
    return return_set
コード例 #6
0
def iwo_test(Tx, Ty):
    lenTx = len(Tx)
    lenTy = len(Ty)
    weedlist = []
    for i in range(10):
        Wx = gd.get_xdata(0, 500, 8)
        Wy = gd.get_ydata(0, 500, 8)
        Xp = reduce((lambda x, y: x + y), Wx) // len(Wx)
        Yp = reduce((lambda x, y: x + y), Wy) // len(Wy)
        weed = Weed(Wx, Wy, Xp, Yp, 0)
        weedlist.append(weed)
    # iwo_algorithm(Tx,Ty,weedlist,itermax,pmax,Smax,Smin,sigmafinal,sigmainit,n)
    wlist = iwo_algorithm(Tx, Ty, weedlist, 5, 150, 10, 1, 0.01, 1, 3)
    fmax = get_max_fitness(wlist)
    for weed in wlist:
        if fmax == weed.fitness:
            bestweed = weed
            break
    Rx = Tx[0:len(Tx) - lenTx]
    Ry = Ty[0:len(Ty) - lenTy]
    Tx = Tx[0:len(Tx) - len(Rx)]
    Ty = Ty[0:len(Ty) - len(Ry)]
    print("X-Coordinates of best weed",
          list(map(lambda x: math.ceil(x), bestweed.Wx)))
    print("Y-Coordinates of best weed",
          list(map(lambda y: math.ceil(y), bestweed.Wy)))
    count = 0
    bestweed.Wx = list(map(lambda x: math.ceil(x), bestweed.Wx))
    bestweed.Wy = list(map(lambda y: math.ceil(y), bestweed.Wy))
    for i in range(len(bestweed.Wx)):
        if bestweed.Wx[i] < min(Tx) or bestweed.Wx[i] > max(Tx):
            continue
        if bestweed.Wy[i] < min(Ty) or bestweed.Wy[i] > max(Ty):
            continue

        Tx.append(bestweed.Wx[i])
        Ty.append(bestweed.Wy[i])
        count = count + 1
    print("Updated X Coordinates", Tx)
    print("Updated Y Coordinates", Ty)
    distancevector = gd.get_distancevector(Tx, Ty)
    mst = pa.mst_prim(distancevector)
    mst_size = pa.get_tree(distancevector)
    print("Size of Steiner Tree for IWO", mst_size)
    pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTy)
    Tx = Tx[0:len(Tx) - count]
    Ty = Ty[0:len(Ty) - count]
    print("Restored X Coordinates=", Tx)
    print("Restored Y Coordinates=", Ty)
    print("End of IWO")
コード例 #7
0
def get_fitness(Tx, Ty, pigeon):
    for i in range(pigeon.Sx.size):
        if pigeon.Sx[i] < min(Tx) or pigeon.Sx[i] > max(Tx):
            continue
        if pigeon.Sy[i] < min(Ty) or pigeon.Sy[i] > max(Ty):
            continue
        Tx = np.append(Tx, pigeon.Sx[i])
        #print(Tx)
        Ty = np.append(Ty, pigeon.Sy[i])
        #print(Ty)

    distancevector = gd.get_distancevector(Tx, Ty)
    objectivefitness = pa.get_tree(distancevector)
    return objectivefitness
コード例 #8
0
def call_methods(Tx, Ty, lenTx, lenTy):
    # Tx = gd.get_xdata(0, 500, 10)
    # Ty = gd.get_ydata(0, 500, 10)
    '''
    lenTx = len(Tx)
    lenTy = len(Ty)
    '''
    # Calculating mST for PIO
    #print("X coordinates =", Tx)
    #print("Y coordinates =", Ty)
    bestpigeon = pigeon_test(Tx, Ty)
    temp_len = Tx.size
    Rx = Tx[0:temp_len - lenTx]

    len_Rx = Rx.size
    Tx = Tx[0:temp_len - len_Rx]
    Ty = Ty[0:temp_len - len_Rx]
    # print("Updated X coordinates", Tx)
    # print("Updated Y coordinates", Ty)
    '''print("X coordinates of best pigeon", bestpigeon.Sx)
    print("Y coordinates of best pigeon", bestpigeon.Sy)
    print("Updated X coordinates", Tx)
    print("Updated Y Coordinates", Ty)'''
    count = 0
    for i in range(len(bestpigeon.Sx)):
        if bestpigeon.Sx[i] < min(Tx) or bestpigeon.Sx[i] > max(Tx):
            continue
        if bestpigeon.Sy[i] < min(Ty) or bestpigeon.Sy[i] > max(Ty):
            continue

        #Tx.append(math.floor(bestpigeon.Sx[i]))
        #Ty.append(math.floor(bestpigeon.Sy[i]))
        Tx = np.append(Tx, np.floor(bestpigeon.Sx[i]))
        Ty = np.append(Ty, np.floor(bestpigeon.Sy[i]))
        count = count + 1
    #print("Updated X Coordinates", Tx)
    #print("Updated Y Coordinates", Ty)
    distancevector = gd.get_distancevector(Tx, Ty)
    mst = pa.mst_prim(distancevector)
    mst_size = pa.get_tree(distancevector)
    #print("Size of Steiner Tree for PIO", mst_size)
    #pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTy)
    return_set = (mst_size, Tx, Ty)
    Tx = Tx[0:len(Tx) - count]
    Ty = Ty[0:len(Ty) - count]
    '''print("Restored X Coordinates=", Tx)
    print("Restored Y Coordinates=", Ty)
    print("End of PIO")'''
    return return_set
コード例 #9
0
def call_methods(Tx, Ty, lenTx, lenTy):
    '''
    lenTx = len(Tx)
    lenTy = len(Ty)
    '''
    Rx = Tx[0:len(Tx) - lenTx]
    Ry = Ty[0:len(Tx) - lenTy]
    Tx = Tx[0:len(Tx) - len(Rx)]
    Ty = Ty[0:len(Ty) - len(Ry)]
    print("X coordinates =", Tx)
    print("Y coordinates =", Ty)

    bestparticle = particle_swarm_test(Tx, Ty)
    Rx = Tx[0:len(Tx) - lenTx]
    Ry = Ty[0:len(Ty) - lenTy]
    Tx = Tx[0:len(Tx) - len(Rx)]
    Ty = Ty[0:len(Ty) - len(Ry)]
    #print("Updated X coordinates", Tx)
    #print("Updated Y coordinates", Ty)
    print("X coordinates of best particle", bestparticle.Sx)
    print("Y coordinates of best particle", bestparticle.Sy)
    print("Updated X coordinates", Tx)
    print("Updated Y Coordinates", Ty)
    count = 0
    for i in range(len(bestparticle.Sx)):
        if bestparticle.Sx[i] < min(Tx) or bestparticle.Sx[i] > max(Tx):
            continue
        if bestparticle.Sy[i] < min(Ty) or bestparticle.Sy[i] > max(Ty):
            continue

        Tx.append(bestparticle.Sx[i])
        Ty.append(bestparticle.Sy[i])
        count = count + 1
    print("Updated X Coordinates", Tx)
    print("Updated Y Coordinates", Ty)
    distancevector = gd.get_distancevector(Tx, Ty)
    mst = pa.mst_prim(distancevector)
    mst_size = pa.get_tree(distancevector)
    print("Size of Steiner Tree for PSO", mst_size)
    pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTx)

    Tx = Tx[0:len(Tx) - count]
    Ty = Ty[0:len(Ty) - count]
    print("Restored X Coordinates=", Tx)
    print("Restored Y Coordinates=", Ty)
    print("End of PSO")
コード例 #10
0
def call_methods(Tx, Ty, lenTx, lenTy):
    # Tx = gd.get_xdata(0, 500, 10)
    # Ty = gd.get_ydata(0, 500, 10)
    '''
    lenTx = len(Tx)
    lenTy = len(Ty)
    '''
    # Calculating mST for PIO
    print("X coordinates =", Tx)
    print("Y coordinates =", Ty)

    bestpigeon = pigeon_test(Tx, Ty)
    Rx = Tx[0:len(Tx) - lenTx]
    Ry = Ty[0:len(Ty) - lenTy]
    Tx = Tx[0:len(Tx) - len(Rx)]
    Ty = Ty[0:len(Ty) - len(Ry)]

    # print("Updated X coordinates", Tx)
    # print("Updated Y coordinates", Ty)
    print("X coordinates of best pigeon", bestpigeon.Sx)
    print("Y coordinates of best pigeon", bestpigeon.Sy)
    print("Updated X coordinates", Tx)
    print("Updated Y Coordinates", Ty)
    count = 0
    for i in range(len(bestpigeon.Sx)):
        if bestpigeon.Sx[i] < min(Tx) or bestpigeon.Sx[i] > max(Tx):
            continue
        if bestpigeon.Sy[i] < min(Ty) or bestpigeon.Sy[i] > max(Ty):
            continue

        Tx.append(math.floor(bestpigeon.Sx[i]))
        Ty.append(math.floor(bestpigeon.Sy[i]))
        count = count + 1
    print("Updated X Coordinates", Tx)
    print("Updated Y Coordinates", Ty)
    distancevector = gd.get_distancevector(Tx, Ty)
    mst = pa.mst_prim(distancevector)
    mst_size = pa.get_tree(distancevector)
    print("Size of Steiner Tree for PIO", mst_size)
    pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTy)
    Tx = Tx[0:len(Tx) - count]
    Ty = Ty[0:len(Ty) - count]
    print("Restored X Coordinates=", Tx)
    print("Restored Y Coordinates=", Ty)
    print("End of PIO")
コード例 #11
0
def call_methods(Tx, Ty, lenTx, lenTy):
    # Tx = gd.get_xdata(0, 500, 100)
    # Ty = gd.get_ydata(0, 500, 100)

    lenTx = Tx.size
    lenTy = Ty.size

    # Calculating mST for CPSO
    Rx = Tx[0:len(Tx) - lenTx]
    Ry = Ty[0:len(Tx) - lenTy]
    Tx = Tx[0:len(Tx) - len(Rx)]
    Ty = Ty[0:len(Ty) - len(Ry)]
    #print("X coordinates =", Tx)
    #print("Y coordinates =", Ty)
    bestparticle = constriction_factor_particle_swarm_test(Tx, Ty)
    Rx = Tx[0:len(Tx) - lenTx]
    Ry = Ty[0:len(Ty) - lenTy]
    Tx = Tx[0:len(Tx) - len(Rx)]
    Ty = Ty[0:len(Ty) - len(Ry)]
    # print("Updated X coordinates", Tx)
    # print("Updated Y coordinates", Ty)
    '''print("X coordinates of best particle", bestparticle.Sx)
    print("Y coordinates of best particle", bestparticle.Sy)
    print("Updated X coordinates", Tx)
    print("Updated Y Coordinates", Ty)'''
    count = 0
    for i in range(len(bestparticle.Sx)):
        if bestparticle.Sx[i] < min(Tx) or bestparticle.Sx[i] > max(Tx):
            continue
        if bestparticle.Sy[i] < min(Ty) or bestparticle.Sy[i] > max(Ty):
            continue

        Tx = np.append(Tx, math.ceil(bestparticle.Sx[i]))
        Ty = np.append(Ty, math.ceil(bestparticle.Sy[i]))
        count = count + 1
    #print("Updated X Coordinates", Tx)
    #print("Updated Y Coordinates", Ty)
    distancevector = gd.get_distancevector(Tx, Ty)
    mst = pa.mst_prim(distancevector)
    mst_size = pa.get_tree(distancevector)
    #print("Size of Steiner Tree for Constricted-PSO", mst_size)
    return_set = (mst_size, Tx, Ty)
    #pa.draw_gridgraph(Tx, Ty, mst,lenTx,lenTy)
    return return_set
コード例 #12
0
    def run(self):

        Tx, Ty = np.load('terminal_point_{}_{}.npy'.format(
            str(self.dim), str(self.n)))
        lenT = Tx.size
        data_pso = dict()
        len_S = lenT - 2  # Total number of Steiner points is total Terminal points - 2

        distancevector = gd.get_distancevector(np.copy(Tx), np.copy(Ty))
        mst_size = pa.get_tree(distancevector)

        for i in range(self.max_iter):
            print('Iteration No :', i)

            # Creating and initiating the Particles
            create_particles(Tx, Ty, len_S)

            t1 = t.time()
            res = call_methods(np.copy(Tx), np.copy(Ty), lenT, self.func)
            t2 = t.time()
            data_pso[res[0]] = (res[1], res[2], t2 - t1, res[3])

        min_pso = min(data_pso.keys())

        fp = open(self.file_name, 'w')
        fp.write("Size of the MST = " + str(mst_size) + '\n')
        fp.write('No. of Iterations :' + str(self.max_iter) + '\n')
        fp.write('PSO Min Wt :' + str(min_pso) + '\n')
        fp.write('X Coordinates :' + str(data_pso[min_pso][0]) + '\n')
        fp.write('Y Coordinates :' + str(data_pso[min_pso][1]) + '\n')
        fp.write('No. of Steiner points :' +
                 str(data_pso[min_pso][0].size - self.n) + '\n')
        fp.write('Time Required :' + str(data_pso[min_pso][2]) + '\n')
        fp.write('Error Ratio :' + str(min_pso / mst_size) + '\n')
        fp.close()

        # Ploting the RSMT
        pa.draw_gridgraph(Tx, Ty, data_pso[min_pso][3], lenT, lenT)
コード例 #13
0
def get_fitness(Tx, Ty, particle):
    # Method to find the Objective Fitness (MST Weight)

    # Setting the checking boundaries
    maxTx = np.amax(Tx)
    minTx = np.amin(Tx)
    maxTy = np.amax(Ty)
    minTy = np.amin(Ty)

    # Constructing the coordinate set of the Tree with Steiner Points
    for i in range(particle.Sx.size):
        if particle.Sx[i] < minTx or particle.Sx[i] > maxTx:
            continue
        if particle.Sy[i] < minTy or particle.Sy[i] > maxTy:
            continue

        Tx = np.append(Tx, particle.Sx[i])
        Ty = np.append(Ty, particle.Sy[i])

    # Finding the Objective Fitness
    distancevector = gd.get_distancevector(Tx, Ty)
    objectivefitness = pa.get_tree(distancevector)
    return objectivefitness
コード例 #14
0
def call_methods(Tx, Ty, lenT, func):

    # Performance controler of the PSO

    global cost_vect
    global px_vect
    global py_vect

    # Setting the method into a method variable for dynamic linking
    global get_velocity_position
    get_velocity_position = func

    bestparticle = Particle(fitness=math.inf)

    # Running the GPSO Iterations
    for i in range(gpso_max_iter):
        # Running PSO algorithm
        bestparticle_local = particle_swarm_test(np.copy(Tx), np.copy(Ty),
                                                 lenT)

        if bestparticle_local.fitness < bestparticle.fitness:
            bestparticle = bestparticle_local

        # Running Gradient Cycles
        px = np.mean(bestparticle_local.Sx)
        py = np.mean(bestparticle_local.Sy)

        for _ in range(grad_max_iter):

            cost = search_function(px, py)
            cost_vect.append(cost)
            px_vect.append(px)
            py_vect.append(py)
            grad = gradient(px, py)
            px, py = update_params(px, py, grad[0], grad[1])

        new_Sx = bestparticle_local.Sx + round(px)
        new_Sy = bestparticle_local.Sy + round(py)

        new_fitness = get_fitness(np.copy(Tx), np.copy(Ty),
                                  Particle(new_Sx, new_Sy))
        if new_fitness < bestparticle.fitness:
            bestparticle.fitness = new_fitness
            bestparticle.Sx = new_Sx
            bestparticle.Sy = new_Sy

    # Extracting the position of the Steiner Points from the best particle
    for i in range(bestparticle.Sx.size):
        if bestparticle.Sx[i] < min(Tx) or bestparticle.Sx[i] > max(Tx):
            continue
        if bestparticle.Sy[i] < min(Ty) or bestparticle.Sy[i] > max(Ty):
            continue

        Tx = np.append(Tx, bestparticle.Sx[i])
        Ty = np.append(Ty, bestparticle.Sy[i])

    # Finding the MST of the final RSMT
    distancevector = gd.get_distancevector(Tx, Ty)
    mst = pa.mst_prim(distancevector)
    mst_size = pa.get_tree(distancevector)

    # Building the final return tuple for the use in the main program
    return_set = (mst_size, Tx, Ty, mst)

    return return_set
コード例 #15
0
import prim_algorithm as pa
import gridgraphdemo as gd
import pigeon_optimization_algorithm as poa
import particle_swarm_optimization as pso
import constriction_factor_pso as cpso

Tx = gd.get_xdata(0, 500, 20)  #Change values over here and test
Ty = gd.get_ydata(0, 500, 20)  #Change values over here and test
lenTx = len(Tx)
lenTy = len(Ty)

print("X Coordinates", Tx)
print("Y Coordinates", Ty)
distancevector = gd.get_distancevector(Tx, Ty)
mst = pa.mst_prim(distancevector)
mst_size = pa.get_tree(distancevector)
print("Size of the MST = ", mst_size)
pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTy)

poa.call_methods(Tx, Ty, lenTx, lenTy)

pso.call_methods(Tx, Ty, lenTx, lenTy)

cpso.call_methods(Tx, Ty, lenTx, lenTy)
コード例 #16
0
import weighted_particle_swarm_optimisation as wpso
from os import open

#Tx = gd.get_xdata(0, 500, 20)
#Ty = gd.get_ydata(0, 500, 20)
#lenTx = len(Tx)
#lenTy = len(Ty)
dim = 500
n = 10
Tx, Ty = np.load('terminal_point_{}_{}.npy'.format(str(dim), str(n)))
lenTx = Tx.size
lenTy = lenTx

#print("X Coordinates", Tx)
#print("Y Coordinates", Ty)
distancevector = gd.get_distancevector(Tx, Ty)
mst = pa.mst_prim(distancevector)
mst_size = pa.get_tree(distancevector)
pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTy)

data_pio = dict()
data_pso = dict()
data_cpso = dict()
data_wpso = dict()

max_iter = 5

for i in range(max_iter):
    print('Iteration No :', i)
    t1 = t.time()
    res = poa.call_methods(Tx, Ty, lenTx, lenTy)