aresio/fst-pso

First swarm iteration happens twice with same parameters

CharlyEmpereurmot opened this issue · 5 comments

Hello Marco,

I noticed that the first 'swarm iteration' happens twice with the same parameters for all particles. I.e. if you have 4 particles, at 'particle evaluations' 1 and 5 the particles use exactly the same parameters, same for 'particle evaluations' 2 and 6, etc. And then only at 'particle evaluation' 9 it will start trying different parameters. I am using FST-PSO 1.7.3.

 * Swarm size now set to 4 particles
 *** Launching optimization ***
 * Creating and evaluating particles
 * 4 particles created, initial guesses added to the swarm.
 * FST-PSO will now assess the local and global best particles.

FITNESS EVAL NB: 1
UPDATE params set: [0.244, 0.197, 0.299, etc]

FITNESS EVAL NB: 2
UPDATE params set: [0.238, 0.206, 0.293, etc]

FITNESS EVAL NB: 3
UPDATE params set: [0.245, 0.188, 0.306, etc]

FITNESS EVAL NB: 4
UPDATE params set: [0.246, 0.194, 0.297, etc]
 * New best particle in the swarm is #0 with fitness 230.504000 (it: 0).
 * New best particle in the swarm is #1 with fitness 80.182000 (it: 0).
 * New best particle in the swarm is #3 with fitness 72.405000 (it: 0).

FITNESS EVAL NB: 5
UPDATE params set: [0.244, 0.197, 0.299, etc] # identical to eval 1

FITNESS EVAL NB: 6
UPDATE params set: [0.238, 0.206, 0.293, etc] # identical to eval 2

FITNESS EVAL NB: 7
UPDATE params set: [0.245, 0.188, 0.306, etc] # identical to eval 3

FITNESS EVAL NB: 8
UPDATE params set: [0.246, 0.194, 0.297, etc] # identical to eval 4
 * 1th iteration out of 3 completed.

FITNESS EVAL NB: 9 # here it starts trying other parameters
UPDATE params set: [0.2453868081310974, 0.19327890557174587, 0.2959479827351297, etc]

Looking at results I generated with FST-PSO version 1.5 and possibly before, this was already the case. Again, it is also possible I am doing something wrong ..

FITNESS EVAL NB: 16
UPDATE params set: [0.24257050572152014, 0.1975, 0.29882421181623314, etc]
 * 3th iteration out of 3 completed.

FITNESS EVAL NB: 17
UPDATE params set: [0.2432027187894477, 0.19835368832566844, 0.30078192074319715, etc]

FITNESS EVAL NB: 18
UPDATE params set: [0.24019788669186828, 0.20179999999999998, 0.29889918191167936, etc]

FITNESS EVAL NB: 19
UPDATE params set: [0.24603723427691615, 0.1935529050019288, 0.2991581010618643, etc]

FITNESS EVAL NB: 20
UPDATE params set: [0.24463520171616907, 0.19834759587538675, 0.29944214488146254, etc]
# and only here it terminates

And the total of evaluations is 20 while we used 4 particles and max_iter = 3. My understanding is that it should perform 12 iterations. Possibly 16 if you count 4 particles initialization + 3 swarm iterations of 4 particles. But 20 is still one extra swarm move with these parameters, right ?

I can't thank you enough for your invaluable help :)

Showing this with test example is more straightforward:

#!/usr/bin/python3
from fstpso import FuzzyPSO	

def example_fitness( particle ):
	global nb_eval
	nb_eval +=1
	print(nb_eval, '--', particle)
	return sum(map(lambda x: x**2, particle))
	
if __name__ == '__main__':
	dims = 2
	nb_eval = 0
	FP = FuzzyPSO()
	FP.set_search_space( [[-10, 10]]*dims )	
	FP.set_fitness(example_fitness, skip_test=True)
	result =  FP.solve_with_fstpso()
	# print "Best solution:", result[0]
	# print "Whose fitness is:", result[1]

Produces identical swarm iterations nb 1 and 2:
I mean:

  • particle iteration 1 = particle iteration 13
  • particle iteration 2 = particle iteration 14
  • etc
* Max distance: 28.284271
 * Search space boundaries set to: [[-10, 10], [-10, 10]]
 * Max velocities set to: [20.0, 20.0]
 * Number of particles automatically set to 12
 * Maximum number of iterations set to 100
 * Enabled settings: [cognitive] [social] [inertia] [minvelocity] [maxvelocity]

 *** Launching optimization ***
 * Creating and evaluating particles
 * 12 particles created.
 * FST-PSO will now assess the local and global best particles.
1 -- [-2.331010962632984, -5.5413022057399814]
2 -- [-9.5955407554451, -5.289655874765813]
3 -- [2.250722516465075, -7.702596878717314]
4 -- [-1.9412423424763663, -3.506552335300716]
5 -- [5.4451352613432675, 5.117821183376082]
6 -- [6.008881867631793, -3.050921308702936]
7 -- [7.162819401831374, -0.845276245078999]
8 -- [-3.4320112774149703, 0.5371579147613232]
9 -- [-6.582821614040224, 5.801504128642351]
10 -- [2.423104920680146, 0.42093030392196695]
11 -- [6.65776368853053, -0.09627510320617105]
12 -- [-1.8962235089723762, 6.632879742323972]
 * New best particle in the swarm is #0 with fitness 36.139642 (it: 0).
 * New best particle in the swarm is #3 with fitness 16.064331 (it: 0).
 * New best particle in the swarm is #7 with fitness 12.067240 (it: 0).
 * New best particle in the swarm is #9 with fitness 6.048620 (it: 0).
13 -- [-2.331010962632984, -5.5413022057399814]
14 -- [-9.5955407554451, -5.289655874765813]
15 -- [2.250722516465075, -7.702596878717314]
16 -- [-1.9412423424763663, -3.506552335300716]
17 -- [5.4451352613432675, 5.117821183376082]
18 -- [6.008881867631793, -3.050921308702936]
19 -- [7.162819401831374, -0.845276245078999]
20 -- [-3.4320112774149703, 0.5371579147613232]
21 -- [-6.582821614040224, 5.801504128642351]
22 -- [2.423104920680146, 0.42093030392196695]
23 -- [6.65776368853053, -0.09627510320617105]
24 -- [-1.8962235089723762, 6.632879742323972]
25 -- [-0.05913559576691574, -2.5413022057399814]
26 -- [-6.5955407554451, -2.2896558747658133]
27 -- [2.558209600055463, -4.7964457103194515]
28 -- [1.0587576575236337, -1.6468352779387485]
29 -- [2.860384736483917, 2.1178211833760816]
30 -- [3.0088818676317928, -0.050921308702935875]
31 -- [5.497141238883, 0.08958686163589746]
32 -- [-0.43201127741497025, 0.36723574015370297]
33 -- [-6.139961666789684, 2.8015041286423514]
34 -- [2.423104920680146, 0.42093030392196695]
35 -- [3.6577636885305296, 0.2210966782072723]
36 -- [1.1037764910276238, 3.632879742323972]
 * New best particle in the swarm is #3 with fitness 3.833034 (it: 1).
 * New best particle in the swarm is #7 with fitness 0.321496 (it: 1).
37 -- [1.7159967703442534, -3.009777031341246]
38 -- [-3.771666009166456, -0.8110475300618851]
39 -- [2.463757589604339, -2.078258922396853]
40 -- [1.8961845789863307, -1.099464995311608]
41 -- [4.05758369165736, 0.9386385651946831]
42 -- [2.4184016660160585, 1.4118013370774045]
43 -- [2.9971412388829997, -0.03996075210668895]
44 -- [-1.5455905686718459, 0.37922252686912455]
45 -- [-3.212695492456304, 2.4738567173809507]

And a last extra swarm iteration:

1199 -- [-0.037322262252632674, -0.0038581325027873866]
1200 -- [0.005015623038536391, 0.019320260378808185]
1201 -- [-0.01086015143499279, -0.006346133635552793]
1202 -- [0.005929075787157342, 0.008730395050364693]
1203 -- [0.0055157410655147735, 0.010904442029501332]
1204 -- [-0.004923371829103622, -0.00043739325481123754]
1205 -- [0.004689018169637689, 0.003252988808415706]
1206 -- [-0.014203077461652086, 0.006282758952534906]
1207 -- [-0.0009193518207214549, 0.003398321981433538]
1208 -- [0.011316827170022132, -0.01266443807778226]
1209 -- [-0.013336034375200017, 0.013528214937258098]
1210 -- [0.0037401093087087046, 0.014179551244113318]
1211 -- [-0.04732226236923308, 0.006141867613813022]
1212 -- [-0.004984376961463609, 0.009320260378808185]
 * 100th iteration out of 100 completed.
1213 -- [-0.0008601513903446906, 0.0036538664090953066]
1214 -- [-0.0040709242128426585, -0.00885840748642916]
1215 -- [-0.004484258961975856, -0.004550454771790363]
1216 -- [0.005076628238167333, -0.010437393322082192]
1217 -- [-0.005310981830362311, -0.006747011191584294]
1218 -- [0.012082271227967244, 0.016282758952534906]
1219 -- [-0.01091935188565173, -0.006601678083496737]
1220 -- [-0.005433566346200918, 0.00260956114477213]
1221 -- [-0.0033360343752000168, 0.0035282149372580975]
1222 -- [-0.0062598906949516385, -0.00792678056895393]
1223 -- [0.0523586288310153, -0.004173479615135043]
1224 -- [0.005015623295736002, -0.016749379943821863]

I refactored the termination criterion and the iteration method. Can you please check out the new version, I think it wil fix this as well.

I'm using 1.7.5. Using the 2nd example it still perform 1224 fitness evaluations and there are still 2 extra swarm iterations, 1 of them happening in the end:

* 100th iteration out of 100 completed.
1213 -- [-0.0008601513903446906, 0.0036538664090953066]
......
1224 -- [0.005015623295736002, -0.016749379943821863]

However, now the 2 first swarm iterations do use different sets of parameters, which is cool. I mean:

  • particle iteration 1 != particle iteration 13
  • particle iteration 2 != particle iteration 14
  • etc
* Max distance: 28.284271
* Search space boundaries set to: [[-10, 10], [-10, 10]]
* Max velocities set to: [20.0, 20.0]
* Number of particles automatically set to 12
* Iterations set to 100
* Fitness evaluations budget set to 1200
* Enabled settings: [cognitive] [social] [inertia] [minvelocity] [maxvelocity]

*** Launching optimization ***
* Creating and evaluating particles
* 12 particles created.
* FST-PSO will now assess the local and global best particles.
1 -- [-6.4846873877819755, 1.075015444661334]
2 -- [-4.163115066004217, 9.49381155787243]
3 -- [-4.509588169473558, -6.245029127539579]
4 -- [-8.583555753230884, -2.0688411502434185]
5 -- [-3.797511050487998, -8.453092273747627]
6 -- [-8.407499088999174, -3.5820484487482034]
7 -- [2.29912430178927, 1.8624975264984638]
8 -- [1.95742968700994, 8.408300524646052]
9 -- [0.8861727669304713, -3.2106315357583153]
10 -- [7.562517651342848, 5.705115275021724]
11 -- [-0.49555103405036327, -7.096344761920291]
12 -- [-3.1967492249469505, 4.077023045471167]
* New best particle in the swarm is #0 with fitness 43.206829 (it: 0).
* New best particle in the swarm is #6 with fitness 8.754870 (it: 0).
13 -- [-3.4846873877819755, 1.4411067634296333]

So this is almost perfect now

I'm using version 1.7.9 now and I still observe what I think is an extra iteration. I mean if I set nb_particles = 2 and max_iter = 1, the process I see is:

START

Fuzzy Self-Tuning PSO - v1.7.9
* All parameters display ******
* FST-PSO will now assess the local and global best particles.

Swarm iter nb 1 that includes 2 particles evaluations
*** All prepared, launching optimization ***
Swarm iter nb 2 that includes 2 particles evaluations
* 1th iteration out of 1 completed. [##############################]
Swarm iter nb 3 that includes 2 particles evaluations
END

Now I understand swarm iter nb 1 is initialization, but I still believe swarm iter nb 3 is an extra one instead of the expected behavior

It actually seems OK now