Skip to content

Inquiry about PSO code #1

@MeedocTheMedic

Description

@MeedocTheMedic

Hi, I have a question about how you implemented particle swarm optimization. In pso.py, the section of code that updates the new routes has a bit of code that I'm not sure of the function of.

`

            for i in range(len(self.cities)):
                if new_route[i] != particle.pbest[i]:
                    swap = (i, particle.pbest.index(new_route[i]), self.pbest_probability)
                    temp_velocity.append(swap)
                    new_route[swap[0]], new_route[swap[1]] = \
                        new_route[swap[1]], new_route[swap[0]]

            for i in range(len(self.cities)):
                if new_route[i] != gbest[i]:
                    swap = (i, gbest.index(new_route[i]), self.gbest_probability)
                    temp_velocity.append(swap)
                    gbest[swap[0]], gbest[swap[1]] = gbest[swap[1]], gbest[swap[0]]

            particle.velocity = temp_velocity

            for swap in temp_velocity:
                if random.random() <= swap[2]:
                    new_route[swap[0]], new_route[swap[1]] = \
                        new_route[swap[1]], new_route[swap[0]]

            particle.route = new_route
            particle.update_costs_and_pbest()`

In the second for loop, what does gbest[swap[0]], gbest[swap[1]] = gbest[swap[1]], gbest[swap[0]] do? gbest doesn't seem to get used as it just gets replaced. It seems like you implemented pso by first making the new route the same as the personal best, then made the global best the same as the new route, and then returned the new route back to what it was originally based on a given probability. The first and third for loops make sense, but the second one is a bit confusing. I ran the code after commenting out gbest[swap[0]], gbest[swap[1]] = gbest[swap[1]], gbest[swap[0]] and it seemed to run the same. Could you clarify what that line of code is for? Thank you for your time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions