nathanrooy / particle-swarm-optimization

Learn about particle swarm optimization (PSO) through Python!
GNU General Public License v3.0
255 stars 123 forks source link

Modify the way to copy list #4

Closed hongjiawu closed 5 years ago

hongjiawu commented 5 years ago

Hello Mr. Rooy,

Thanks for posting this solid particle swarm optimization code. I believe this already helped others a lot. But could you please have a look of line 44 of your code where you have:

self.pos_best_i=self.position_i

Here you would like to copy the content of position_i list to pos_best_i. However, the assignment just copies the reference to the list. Then, whenever the position_i gets modified in the future, post_best_i always gets modified as well because of this reference. Should we change it to the typical list.copy() or copy.copy() or whatever:)

nathanrooy commented 5 years ago

Thanks! I've updated the code.