示例#1
0
 def run(self,iteration):
     self.initialiseTrail('burma14.csv')
     bestRoute = []
     shortestCost = 10000
     print "ACO running..."
     for i in range(iteration):
         route = self.greedyInitialier()
         routeCost = lab1.getRouteCost(self.graph,route)
         self.pheromoneDecay()
         self.updateRoutePheromone(route)
         if routeCost<shortestCost:
             bestRoute = route
             shortestCost = lab1.getRouteCost(self.graph,route)
     return {'best route':bestRoute,'cost':shortestCost}
示例#2
0
    def __init__(self, graph, iteration, size_population, beta=1, alfa=1):
        self.graph = graph  # the graph
        self.iteration = iteration  # max of iteration
        self.size_population = size_population  # size population
        self.particles = []  # list of particles
        self.beta = beta  # the probability that all swap operators in swap sequence (gbest - x(t-1))
        self.alfa = alfa  # the probability that all swap operators in swap sequence (pbest - x(t-1))

        # initialized with a group of random particles (solutions)
        # solutions = self.graph.getRandomPaths(self.size_population)
        solutions = []
        for i in range(1,size_population):
            route = list(graph.vertices)
            random.shuffle(route)
            solutions.append(route)

        # creates the particles and initialization of swap sequences in all the particles
        for solution in solutions:
            # creates a new particle
            particle = Particle(solution=solution, cost=lab1.getRouteCost(graph,solution))
            # add the particle
            self.particles.append(particle)

        # updates "size_population"
        self.size_population = len(self.particles)
示例#3
0
def roullet_pick(graph, solutions):

    total = 0

    for route in solutions:
        total += 1/getRouteCost(graph,route)

    roullet_wheel = []
    sum_so_far = 0
    for key in solutions:
        sum_so_far += 1/getRouteCost(graph,key)
        roullet_wheel.append(sum_so_far/total)
    # print roullet_wheel
    parents = []
    random_list = numpy.random.uniform(0, 1, 2)
    # random_list = []
    # random_list.append(random.uniform(0, 1))
    # random_list.append(random.uniform(0, 1))

    # prob1 = random.uniform(0, 1)
    # prob2 = random.uniform(0, 1)

    prob1 = random_list[0]
    prob2 = random_list[1]

    for i in range(0,len(solutions)):
        if prob1 < roullet_wheel[i]:
            parents.append(solutions[i])
            break

    for i in range(0, len(solutions)):
        if prob2 < roullet_wheel[i]:
            parents.append(solutions[i])
            break
    # print parents
    return parents
示例#4
0
    def run(self):
        for i in range(self.iteration):
            # updates gbest (best particle of the population)
            self.gbest = min(self.particles, key=attrgetter('cost_pbest_solution'))

            # for each particle in the swarm
            for particle in self.particles:

                particle.velocity[:]  # cleans the speed of the particle
                temp_velocity = []
                solution_gbest = copy.copy(self.gbest.pbest)  # gets geno1 of the gbest
                solution_pbest = particle.pbest[:]  # copy of the pbest geno1
                solution_particle = particle.solution[:]  # gets copy of the current geno1 of the particle

                # generates all swap operators to calculate (pbest - x(t-1))
                for i in range(len(self.graph.vertices)):
                    if solution_particle[i] != solution_pbest[i]:
                        # generates swap operator
                        swap_operator = (i, solution_pbest.index(solution_particle[i]), self.alfa)

                        # append swap operator in the list of velocity
                        temp_velocity.append(swap_operator)

                        # makes the swap
                        aux = solution_pbest[swap_operator[0]]
                        solution_pbest[swap_operator[0]] = solution_pbest[swap_operator[1]]
                        solution_pbest[swap_operator[1]] = aux

                # generates all swap operators to calculate (gbest - x(t-1))
                for i in range(len(self.graph.vertices)):
                    if solution_particle[i] != solution_gbest[i]:
                        # generates swap operator
                        swap_operator = (i, solution_gbest.index(solution_particle[i]), self.beta)

                        # append swap operator in the list of velocity
                        temp_velocity.append(swap_operator)

                        # makes the swap
                        aux = solution_gbest[swap_operator[0]]
                        solution_gbest[swap_operator[0]] = solution_gbest[swap_operator[1]]
                        solution_gbest[swap_operator[1]] = aux

                # updates velocity
                particle.velocity = temp_velocity

                # generates new geno1 for particle
                for swap_operator in temp_velocity:
                    if random.random() <= swap_operator[2]:
                        # makes the swap
                        aux = solution_particle[swap_operator[0]]
                        solution_particle[swap_operator[0]] = solution_particle[swap_operator[1]]
                        solution_particle[swap_operator[1]] = aux

                # updates the current geno1
                particle.solution = solution_particle
                # gets cost of the current geno1
                cost_current_solution = lab1.getRouteCost(self.graph,solution_particle)
                # updates the cost of the current geno1
                particle.cost_current_solution = cost_current_solution

                # checks if current geno1 is pbest geno1
                if cost_current_solution < particle.cost_pbest_solution:
                    particle.pbest = solution_particle
                    particle.cost_pbest_solution = cost_current_solution
示例#5
0
def randomSearchInitialiser(graph):
    initializer = list(graph.vertices)
    random.shuffle(initializer)
    # initializer = initializer.append(initializer[0])
    return {'distance':lab1.getRouteCost(graph,initializer),'route':initializer}