-
Notifications
You must be signed in to change notification settings - Fork 4
/
Update.py
114 lines (87 loc) · 2.81 KB
/
Update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import abc
from .CooperativeConstructor import CooperativeConstructor
class Update(abc.ABC):
@abc.abstractmethod
def updatePheromones(self):
pass
@abc.abstractmethod
def setPheromoneValue(self, component, value):
pass
@abc.abstractmethod
def getPheromoneValue(self):
pass
@abc.abstractmethod
def getSolutionValue(self):
pass
@abc.abstractmethod
def getSolutionComponents(self):
pass
@abc.abstractmethod
def getQ(self):
pass
@abc.abstractmethod
def getDeltaTau(self, component):
pass
@abc.abstractmethod
def isLeader(self):
pass
@abc.abstractmethod
def pheromoneDecay(self):
pass
@abc.abstractmethod
def canUpdatePheromone(self):
pass
class TypicalUpdate(Update):
def updatePheromones(self):
if self.isLeader():
self.pheromoneDecay()
if self.canUpdatePheromone():
for component in self.getSolutionComponents():
self.setPheromoneValue(component, self.getPheromoneValue(component) + self.getQ()*self.getDeltaTau(component))
def getDeltaTau(self, component):
return 1/self.getSolutionValue()
class AllUpdate(TypicalUpdate, CooperativeConstructor):
def canUpdatePheromone(self):
return True
class IterBestUpdate(TypicalUpdate, CooperativeConstructor):
def canUpdatePheromone(self):
return self == self.getIterBest()
@abc.abstractmethod
def getIterBest(self):
pass
class GlobalBestUpdate(TypicalUpdate, CooperativeConstructor):
def canUpdatePheromone(self):
return self == self.getGlobalBest()
@abc.abstractmethod
def getGlobalBest(self):
pass
class ACSUpdate(TypicalUpdate):
def updatePheromones(self):
if self.canUpdatePheromone():
for component in self.getSolutionComponents():
rho = self.getRho()
tau = self.getPheromoneValue(component)
delta = self.getDeltaTau(component)
Q = self.getQ()
self.setPheromoneValue(component, (1-rho)*tau + rho*Q*delta)
@abc.abstractmethod
def getRho(self):
pass
class ACSIterBestUpdate(ACSUpdate, IterBestUpdate):
pass
class MMASUpdate(TypicalUpdate):
def updatePheromones(self):
super().updatePheromones()
if self.isLeader():
for component in self.getComponents():
p = self.getPheromoneValue(component)
p = min(max(p, self.getMinPheromone()), self.getMaxPheromone())
self.setPheromoneValue(component, p)
@abc.abstractmethod
def getMinPheromone(self):
pass
@abc.abstractmethod
def getMaxPheromone(self):
pass
class MMASIterBestUpdate(MMASUpdate, IterBestUpdate):
pass