patternpythonMinorCanonical
Particle swarm optimization - follow-up
Viewed 0 times
swarmoptimizationparticlefollow
Problem
This is a followup post to Particle Swarm Optimization.
I wrote a script in Python for particle swarm optimization and I posted it here to get comments on the design. I was told that encapsulating the list used to hold the particles in a class would be a good idea, but it seemed like putting the Particle class I already had inside as a subclass was a good idea.
Now, I'm asking if it was indeed a good idea, and moreover if I made any rookie errors in using a subclass. For example, I wasn't completely sure about shared variables from a parent to a child subclass, so I just passed the ones I needed in the subclass initialization (worldWidth, worldHeight, and k).
Here is the "top" of the class, with the initialization of the parent class, the initializer function for the list, and the entirety of the the subclass. I've omitted all of the public methods of the parent class. For the whole code, see the repository.
```
class ParticleList:
"""ParticleList encapsulates the list of particles and functions used to
manipulate their attributes
"""
def __init__(self, NP, I, C, SR, LR, WW, WH, MV, K, FN):
"""create an array, assign values, and initialize each particle"""
self.pList = []
self.numParticles = NP
self.inertia = I
self.cognition = C
self.socialRate = SR
self.localRate = LR
self.worldWidth = WW
self.worldHeight = WH
self.maxVelocity = MV
self.k = K
self.fname = FN
self.createParticles()
def createParticles(self):
"""create a list of particles and then create neighborhoods if it's called for (k > 0)"""
for i in range(0,self.numParticles):
self.pList.append(self.Particle(i, self.worldWidth, self.worldHeight, self.k))
#fill neighbor lists
if self.k > 0:
for p in self.pList:
for x in ra
I wrote a script in Python for particle swarm optimization and I posted it here to get comments on the design. I was told that encapsulating the list used to hold the particles in a class would be a good idea, but it seemed like putting the Particle class I already had inside as a subclass was a good idea.
Now, I'm asking if it was indeed a good idea, and moreover if I made any rookie errors in using a subclass. For example, I wasn't completely sure about shared variables from a parent to a child subclass, so I just passed the ones I needed in the subclass initialization (worldWidth, worldHeight, and k).
Here is the "top" of the class, with the initialization of the parent class, the initializer function for the list, and the entirety of the the subclass. I've omitted all of the public methods of the parent class. For the whole code, see the repository.
```
class ParticleList:
"""ParticleList encapsulates the list of particles and functions used to
manipulate their attributes
"""
def __init__(self, NP, I, C, SR, LR, WW, WH, MV, K, FN):
"""create an array, assign values, and initialize each particle"""
self.pList = []
self.numParticles = NP
self.inertia = I
self.cognition = C
self.socialRate = SR
self.localRate = LR
self.worldWidth = WW
self.worldHeight = WH
self.maxVelocity = MV
self.k = K
self.fname = FN
self.createParticles()
def createParticles(self):
"""create a list of particles and then create neighborhoods if it's called for (k > 0)"""
for i in range(0,self.numParticles):
self.pList.append(self.Particle(i, self.worldWidth, self.worldHeight, self.k))
#fill neighbor lists
if self.k > 0:
for p in self.pList:
for x in ra
Solution
I'm not experienced enough at OOP to give you meaningful feedback on your class models, but here are two quick things I noticed when scanning my novice eyes over your code:
-
Undocumented parameters. I don't know if
-
Too many parameters. One thing that would simultaneously clean up your code and make it easier to use would be to pass all those parameters to your
, and then your
The advantage of doing that is you don't have to remember what order parameters get passed into your initialization function when you use it.
-
Undocumented parameters. I don't know if
NP, I, C, etc. are meant to be ints, floats, tuples, or what. I'd suggest adding comments to explain the parameters and their types in the docstring of your __init__ function. -
Too many parameters. One thing that would simultaneously clean up your code and make it easier to use would be to pass all those parameters to your
ParticleList.__init__() as a single dictionary. You could form the dictionary like this:params = {'number_of_particles': NP, 'max_velocity': MV, 'world_width': WW, ...}, and then your
__init__() would just be def __init__(self, params). In the body of the function you could unpack it:self.number_of_particles = params['number_of_particles']
self.max_velocity = params['max_velocity']
...The advantage of doing that is you don't have to remember what order parameters get passed into your initialization function when you use it.
Code Snippets
params = {'number_of_particles': NP, 'max_velocity': MV, 'world_width': WW, ...}self.number_of_particles = params['number_of_particles']
self.max_velocity = params['max_velocity']
...Context
StackExchange Code Review Q#96921, answer score: 2
Revisions (0)
No revisions yet.