Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inquiry about PSO code #1

Open
MeedocTheMedic opened this issue Nov 23, 2022 · 0 comments
Open

Inquiry about PSO code #1

MeedocTheMedic opened this issue Nov 23, 2022 · 0 comments

Comments

@MeedocTheMedic
Copy link

MeedocTheMedic commented Nov 23, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant