@@ -333,14 +333,37 @@ def move_hen(self, particle):
333333clipped_val = np .clip (((fitness_this_chicken - fitness_rooster )/ (np .abs (fitness_this_chicken )+ epsilon )),- 709.00 ,709.00 )
334334S1 = np .exp (clipped_val )
335335# S1*RANDOM(0-to-1)*(LocationRoosterGroupmate-thisChickenLocation)
336- term_1 = S1 * self .rng .uniform (0 ,1 )* (rooster_loc - self .M [particle ])
336+ # these terms can overflow because of the exp()
337+ # some S2 results are evaluating on the scale of 8.218407461554972e+307
338+ # these clipped bounds are effectively are zero and inf
339+ # term_1 = S1*self.rng.uniform(0,1)*(rooster_loc-self.M[particle])
340+ # still dealing with overflow issues. apply cap to S1
341+ if S1 > 10e50 :
342+ S1 = 10e50
343+ elif S1 < - 10e50 :
344+ S1 = - 10e50
345+ clipped_term1 = np .clip ((S1 * self .rng .uniform (0 ,1 )* (rooster_loc - self .M [particle ])),- 10e50 ,10e10 )
346+ term_1 = clipped_term1
337347
338348#S2 = np.exp(float(fitness_random_chicken-fitness_this_chicken))
339349#np.exp(...) throws overflow errors. Using clip as a generic catch
340350clipped_val = np .clip ((fitness_random_chicken - fitness_this_chicken ),- 709.00 ,709.00 )
341351S2 = np .exp (clipped_val )
342352#S2*RANDOM(0-to-1)*(LoctionRandomChickenInSwarm-thisChickenLocation)
343- term_2 = S2 * self .rng .uniform (0 ,1 )* (random_chicken_loc - self .M [particle ])
353+
354+ # these terms can overflow because of the exp()
355+ # some S2 results are evaluating on the scale of 8.218407461554972e+307
356+ # these clipped bounds are effectively are zero and inf
357+ #term_2 = S2*self.rng.uniform(0,1)*(random_chicken_loc-self.M[particle])
358+ # This still causes overflow:
359+ # clipped_term2 = np.clip((S2*self.rng.uniform(0,1)*(random_chicken_loc-self.M[particle])), -10e50, 10e10)
360+ if S2 > 10e50 :
361+ S2 = 10e50
362+ elif S2 < - 10e50 :
363+ S2 = - 10e50
364+
365+ clipped_term2 = np .clip ((S2 * self .rng .uniform (0 ,1 )* (random_chicken_loc - self .M [particle ])),- 10e50 ,10e10 )
366+ term_2 = clipped_term2
344367
345368# new_loc = old_loc + term_1 + term_2
346369self .M [particle ]= self .M [particle ]+ term_1 + term_2
@@ -404,23 +427,21 @@ def reorganize_swarm(self):
404427group_nums = np .arange (self .RN )
405428
406429# first rooster, to reset the array
407- self .chicken_info = np .vstack ([0 ,0 ,- 1 ])
430+ self .chicken_info = np .array ([0 ,0 ,- 1 ])
408431
409432for i in range (1 ,int (self .number_of_particles )):
410433if classList [i ]== 0 :#rooster
411434# assign to the next group (i-1), and done.
412435# CLASSIFICATION(0-4), GROUP(0-m), MOTHER-CHILD ID
413436self .chicken_info = \
414- np .hstack ([self .chicken_info ,
415- np .vstack ([classList [i ],i ,- 1 ])])
437+ np .vstack ([self .chicken_info , [classList [i ],i ,- 1 ]])
416438
417439elif (classList [i ]== 1 )or (classList [i ]== 2 ):#hen, mother hen
418440# assign to a random group.
419441# CLASSIFICATION(0-4), GROUP(0-m), MOTHER-CHILD ID
420442hen_group = self .rng .choice (group_nums )
421443self .chicken_info = \
422- np .hstack ([self .chicken_info ,
423- np .vstack ([classList [i ],hen_group ,- 1 ])])
444+ np .vstack ([self .chicken_info ,[classList [i ],hen_group ,- 1 ]])
424445
425446elif classList [i ]== 3 :#chick
426447# select a random hen to be the 'mother' and assign to group
@@ -433,8 +454,7 @@ def reorganize_swarm(self):
433454mother_group = self .chicken_info [chicken_idx ][1 ]
434455# assign chick to group, and to that mother hen
435456self .chicken_info = \
436- np .hstack ([self .chicken_info ,
437- np .vstack ([classList [i ],mother_group ,chicken_idx ])])
457+ np .vstack ([self .chicken_info ,[classList [i ],mother_group ,chicken_idx ]])
438458
439459groupAssigned = True
440460