Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6f70f72

Browse files
committed
random number gen update
1 parent0bb5267 commit6f70f72

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

‎src/chicken_swarm.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
importnumpyasnp
17-
fromnumpy.randomimportGenerator,MT19937,shuffle,choice,randint,uniform
17+
fromnumpy.randomimportGenerator,MT19937
1818
importsys
1919
importtime
2020
np.seterr(all='raise')
@@ -178,7 +178,7 @@ def __init__(self, NO_OF_PARTICLES,
178178

179179
elif (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)
182182
self.chicken_info= \
183183
np.hstack([self.chicken_info,
184184
np.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
188188
groupAssigned=False
189189
while (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
191191
ifself.chicken_info[0][chicken_idx]==2:#is mother hen
192192
# get the group the random mother hen is from
193193
mother_group=self.chicken_info[1][chicken_idx]
@@ -278,7 +278,7 @@ def move_rooster(self, particle):
278278

279279
# choose a random rooster
280280
rooster_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
283283
random_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

301301
defmove_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
319319
while (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

324324
random_chicken_loc=self.M[:,random_chicken_idx]
325325
fitness_random_chicken=np.linalg.norm(self.F_Pb[:,random_chicken_idx])
@@ -334,14 +334,14 @@ def move_hen(self, particle):
334334
clipped_val=np.clip(((fitness_this_chicken-fitness_rooster)/(np.abs(fitness_this_chicken)+epsilon)),-709.00,709.00)
335335
S1=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
341341
clipped_val=np.clip((fitness_random_chicken-fitness_this_chicken),-709.00,709.00)
342342
S2=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
347347
self.M[:,particle]=self.M[:,particle]+term_1+term_2
@@ -354,7 +354,7 @@ def move_chick(self, particle):
354354

355355
mother_idx=int(self.chicken_info[2][particle])# the the idx of the mother chicken
356356
mother_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

359359
defreorganize_swarm(self):
360360
# rank the chickens' fitness vals and establish hierarchial order
@@ -418,7 +418,7 @@ def reorganize_swarm(self):
418418
elif (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)
422422
self.chicken_info= \
423423
np.hstack([self.chicken_info,
424424
np.vstack([classList[i],hen_group,-1])])
@@ -428,7 +428,7 @@ def reorganize_swarm(self):
428428
# CLASSIFICATION(0-4), GROUP(0-m), MOTHER-CHILD ID
429429
groupAssigned=False
430430
while (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
432432
ifself.chicken_info[0][chicken_idx]==2:#is mother hen
433433
# get the group the random mother hen is from
434434
mother_group=self.chicken_info[1][chicken_idx]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp