1414
1515
1616import numpy as np
17- from numpy .random import Generator ,MT19937 , shuffle , choice , randint , uniform
17+ from numpy .random import Generator ,MT19937
1818import sys
1919import time
2020np .seterr (all = 'raise' )
@@ -178,7 +178,7 @@ def __init__(self, NO_OF_PARTICLES,
178178
179179elif (classList [i - 1 ]== 1 )or (classList [i - 1 ]== 2 ):#hen, mother hen
180180# assign to a random group.
181- hen_group = choice (group_nums )
181+ hen_group = self . rng . choice (group_nums )
182182self .chicken_info = \
183183np .hstack ([self .chicken_info ,
184184np .vstack ([classList [i - 1 ],hen_group ,- 1 ])])
@@ -187,7 +187,7 @@ def __init__(self, NO_OF_PARTICLES,
187187# select a random hen to be the 'mother' and assign to group
188188groupAssigned = False
189189while (groupAssigned == False ):
190- chicken_idx = randint (0 ,i - 1 )# index after a chick will always be the chick
190+ chicken_idx = self . rng . integers (0 ,i - 1 , endpoint = False )# index after a chick will always be the chick
191191if self .chicken_info [0 ][chicken_idx ]== 2 :#is mother hen
192192# get the group the random mother hen is from
193193mother_group = self .chicken_info [1 ][chicken_idx ]
@@ -278,7 +278,7 @@ def move_rooster(self, particle):
278278
279279# choose a random rooster
280280rooster_arr = np .arange (self .RN )
281- random_rooster_idx = choice (rooster_arr )
281+ random_rooster_idx = self . rng . choice (rooster_arr )
282282# use L2 norm for fitness to account for multi-objective funcs
283283random_rooster_fitness = np .linalg .norm (self .F_Pb [:,random_rooster_idx ])
284284
@@ -295,7 +295,7 @@ def move_rooster(self, particle):
295295
296296
297297#update new location based on random()
298- self .M [:,particle ]= self .M [:,particle ]* (1 + np . random .normal (0 ,sig_squared ))
298+ self .M [:,particle ]= self .M [:,particle ]* (1 + self . rng .normal (0 ,sig_squared ))
299299
300300
301301def move_hen (self ,particle ):
@@ -314,12 +314,12 @@ def move_hen(self, particle):
314314
315315# get the random chicken information
316316# initial random
317- random_chicken_idx = randint (0 ,self .number_of_particles )
317+ random_chicken_idx = self . rng . integers (0 ,self .number_of_particles )
318318# random cannot be the idx of the rooster, the current chicken, or be from a chick
319319while (random_chicken_idx == group_rooster_idx )or \
320320 (random_chicken_idx == particle )or \
321321 (int (self .chicken_info [0 ][random_chicken_idx ])== 3 ):
322- random_chicken_idx = randint (0 ,self .number_of_particles )
322+ random_chicken_idx = self . rng . integers (0 ,self .number_of_particles )
323323
324324random_chicken_loc = self .M [:,random_chicken_idx ]
325325fitness_random_chicken = np .linalg .norm (self .F_Pb [:,random_chicken_idx ])
@@ -334,14 +334,14 @@ def move_hen(self, particle):
334334clipped_val = np .clip (((fitness_this_chicken - fitness_rooster )/ (np .abs (fitness_this_chicken )+ epsilon )),- 709.00 ,709.00 )
335335S1 = np .exp (clipped_val )
336336# S1*RANDOM(0-to-1)*(LocationRoosterGroupmate-thisChickenLocation)
337- term_1 = S1 * uniform (0 ,1 )* (rooster_loc - self .M [:,particle ])
337+ term_1 = S1 * self . rng . uniform (0 ,1 )* (rooster_loc - self .M [:,particle ])
338338
339339#S2 = np.exp(float(fitness_random_chicken-fitness_this_chicken))
340340#np.exp(...) throws overflow errors. Using clip as a generic catch
341341clipped_val = np .clip ((fitness_random_chicken - fitness_this_chicken ),- 709.00 ,709.00 )
342342S2 = np .exp (clipped_val )
343343#S2*RANDOM(0-to-1)*(LoctionRandomChickenInSwarm-thisChickenLocation)
344- term_2 = S2 * uniform (0 ,1 )* (random_chicken_loc - self .M [:,particle ])
344+ term_2 = S2 * self . rng . uniform (0 ,1 )* (random_chicken_loc - self .M [:,particle ])
345345
346346# new_loc = old_loc + term_1 + term_2
347347self .M [:,particle ]= self .M [:,particle ]+ term_1 + term_2
@@ -354,7 +354,7 @@ def move_chick(self, particle):
354354
355355mother_idx = int (self .chicken_info [2 ][particle ])# the the idx of the mother chicken
356356mother_loc = self .M [:,mother_idx ]
357- self .M [:,particle ]= self .M [:,particle ]+ choice ([0 ,2 ])* (mother_loc - self .M [:,particle ])
357+ self .M [:,particle ]= self .M [:,particle ]+ self . rng . choice ([0 ,2 ])* (mother_loc - self .M [:,particle ])
358358
359359def reorganize_swarm (self ):
360360# rank the chickens' fitness vals and establish hierarchial order
@@ -418,7 +418,7 @@ def reorganize_swarm(self):
418418elif (classList [i ]== 1 )or (classList [i ]== 2 ):#hen, mother hen
419419# assign to a random group.
420420# CLASSIFICATION(0-4), GROUP(0-m), MOTHER-CHILD ID
421- hen_group = choice (group_nums )
421+ hen_group = self . rng . choice (group_nums )
422422self .chicken_info = \
423423np .hstack ([self .chicken_info ,
424424np .vstack ([classList [i ],hen_group ,- 1 ])])
@@ -428,7 +428,7 @@ def reorganize_swarm(self):
428428# CLASSIFICATION(0-4), GROUP(0-m), MOTHER-CHILD ID
429429groupAssigned = False
430430while (groupAssigned == False ):
431- chicken_idx = randint (0 ,i )# index after a chick will always be the chick
431+ chicken_idx = self . rng . integers (0 ,i )# index after a chick will always be the chick
432432if self .chicken_info [0 ][chicken_idx ]== 2 :#is mother hen
433433# get the group the random mother hen is from
434434mother_group = self .chicken_info [1 ][chicken_idx ]