11import random as rd
22import numpy as np
33
4+
45class Fourmis :
5- def __init__ (self , engine , fromLocation , locationList , pheromonesTime = 100 , antNumber = 1000 , pheromonesMax = 1 , pheromonesMin = 0.01 ):
6+ def __init__ (self , engine , fromLocation , locationList , pheromonesTime = 100 , antNumber = 1000 , pheromonesMax = 1 ,
7+ pheromonesMin = 0.01 ):
68 """Prend la liste des cases a visiter"""
79 self .engine = engine
810 self .distances = engine .maze .distanceMetagraph
@@ -11,26 +13,27 @@ def __init__(self, engine, fromLocation, locationList, pheromonesTime = 100, ant
1113 self .locationNumber = len (self .locationList )
1214 self .fromLocation = fromLocation
1315 self .antNumber = antNumber
14- self .pheromonesDico = {fLocation : {tLocation : [1. / pheromonesTime ]* pheromonesTime for tLocation in [self .fromLocation ]+ self .locationList } for fLocation in [self .fromLocation ]+ self .locationList }
16+ self .pheromonesDico = {fLocation : {tLocation : [1. / pheromonesTime ] * pheromonesTime for tLocation in
17+ [self .fromLocation ] + self .locationList } for fLocation in
18+ [self .fromLocation ] + self .locationList }
1519 self .pheromonesMax = pheromonesMax
1620 self .pheromonesMin = pheromonesMin
1721
1822 def process (self ):
1923 for i in range (self .antNumber ):
20- locationList = self .locationList .copy () # On copie la liste pour pouvoir la modifier
24+ locationList = self .locationList .copy () # On copie la liste pour pouvoir la modifier
2125 distanceSum = 1
2226 n = len (locationList )
2327 currentLocation = self .fromLocation
2428 while n > 0 :
2529 (case , pheromones ) = self .weightedChoice (locationList , currentLocation )
26- n -= 1
27- self .pheromonesDico [currentLocation ][case ][- 1 ] += max (np .sqrt (1. / distanceSum ), self .pheromonesMin )
30+ n -= 1
31+ self .pheromonesDico [currentLocation ][case ][- 1 ] += max (np .sqrt (1. / distanceSum ), self .pheromonesMin )
2832 distanceSum += self .distances [currentLocation ][case ]
29- #On passe à la case suivante
33+ # On passe à la case suivante
3034 currentLocation = case
3135 self .decalerPheromones ()
3236 return self .retournerCheminOpt ()
33-
3437
3538 def weightedChoice (self , list , currentLocation ):
3639 """Fait un tirage au sort sans remise en affectant les poids, retourne l'élément choisi et son poids"""
@@ -42,21 +45,21 @@ def weightedChoice(self, list, currentLocation):
4245 currentPhero = 0
4346 for elt in list :
4447 eltPhero = self .getPheromonesPath (currentLocation , elt )
45- if rand < (currentPhero + eltPhero )/ nbPhero :
48+ if rand < (currentPhero + eltPhero ) / nbPhero :
4649 list .remove (elt )
47- return (elt , eltPhero )
48- currentPhero += eltPhero
50+ return (elt , eltPhero )
51+ currentPhero += eltPhero
4952
5053 def getPheromonesPath (self , fromLocation , toLocation ):
5154 res = 0
5255 for pheromones in self .pheromonesDico [fromLocation ][toLocation ]:
53- res += pheromones
56+ res += pheromones
5457 return res
5558
5659 def decalerPheromones (self ):
5760 """Retire un cycle de vie aux phéromones"""
58- for fromLocation in self .locationList :
59- for toLocation in self .locationList :
61+ for fromLocation in self .locationList :
62+ for toLocation in self .locationList :
6063 n = self .pheromonesDico [fromLocation ][toLocation ]
6164 self .pheromonesDico [fromLocation ][toLocation ].pop (0 )
6265 self .pheromonesDico [fromLocation ][toLocation ].append (self .pheromonesMin )
@@ -68,18 +71,17 @@ def retournerCheminOpt(self):
6871 maxPheromones = np .inf
6972 maxPheromonesToPathIndex = None
7073 for j in range (len (toVisitList )):
71- currentPheromones = self .getPheromonesPath (orderedVisitList [i ],toVisitList [j ])
72- if currentPheromones < maxPheromones :
74+ currentPheromones = self .getPheromonesPath (orderedVisitList [i ], toVisitList [j ])
75+ if currentPheromones < maxPheromones :
7376 maxPheromones = currentPheromones
7477 maxPheromonesToPathIndex = j
7578 orderedVisitList .append (toVisitList [maxPheromonesToPathIndex ])
7679 toVisitList .pop (maxPheromonesToPathIndex )
77- orderedVisitList .pop (0 ) # On retire le fromLocation du début
80+ orderedVisitList .pop (0 ) # On retire le fromLocation du début
7881 return orderedVisitList
7982
80-
8183 def partiePositive (self , x ):
8284 if x <= 0 :
8385 return 0
8486 else :
85- return x
87+ return x
0 commit comments