-
Notifications
You must be signed in to change notification settings - Fork 4
/
Ant.py
189 lines (139 loc) · 3.84 KB
/
Ant.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import abc
from .Pheromone import *
from .Decision import *
from .Update import *
from . import DefaultParameters as dp
class Ant(abc.ABC):
#@abc.abstractmethod
#def getPheromoneValue(self, component):
# pass
#@abc.abstractmethod
#def setPheromoneValue(self, component, value):
# pass
#@abc.abstractmethod
#def getHeuristicValue(self, component):
# pass
#@abc.abstractmethod
#def getComponentCost(self, component):
# pass
#@abc.abstractmethod
#def getAlpha(self):
# pass
#@abc.abstractmethod
#def getBeta(self):
# pass
#@abc.abstractmethod
#def getRho(self):
# pass
#@abc.abstractmethod
#def getQ(self):
# pass
#@abc.abstractmethod
#def getTau0(self):
# pass
#@abc.abstractmethod
#def makeDecision(self, components):
# pass
@abc.abstractmethod
def constructSolution(self):
pass
#@abc.abstractmethod
#def updatePheromones(self):
# pass
#@abc.abstractmethod
#def canUpdatePheromones(self):
# pass
#@abc.abstractmethod
def localSearch(self):
pass
@abc.abstractmethod
def setGlobalBest(self, globalBest):
pass
@abc.abstractmethod
def setIterBest(self, iterBest):
pass
@abc.abstractmethod
def getGlobalBest(self):
pass
@abc.abstractmethod
def getIterBest(self):
pass
@abc.abstractmethod
def getSolutionValue(self):
pass
#@abc.abstractmethod
#def getSolutionComponents(self):
# pass
#@abc.abstractmethod
#def sharePheromoneStructure(self, other):
# pass
#@abc.abstractmethod
#def getDeltaTau(self, component):
# pass
@abc.abstractmethod
def makeLeader(self):
pass
#@abc.abstractmethod
#def isLeader(self):
# pass
#@abc.abstractmethod
#def pheromoneDecay(self):
# pass
class ConcreteAnt(Ant, PheromoneDict):
def __init__(self, alpha = dp.alpha, beta = dp.beta, rho = dp.rho, Q = dp.Q, tau0 = dp.tau0, **kwargs):
self.alpha = alpha
self.beta = beta
self.rho = rho
self.Q = Q
self.tau0 = tau0
self.iterBest = None
self.globalBest = None
self.leader = False
super().__init__(**kwargs)
def getAlpha(self):
return self.alpha
def getBeta(self):
return self.beta
def getRho(self):
return self.rho
def getQ(self):
return self.Q
def getTau0(self):
return self.tau0
def setIterBest(self, iterBest):
self.iterBest = iterBest
def setGlobalBest(self, globalBest):
self.globalBest = globalBest
def getIterBest(self):
return self.iterBest
def getGlobalBest(self):
return self.globalBest
def isLeader(self):
return self.leader
def makeLeader(self):
self.leader = True
#@abc.abstractmethod
#def getCurrentTau(self):
# pass
def getSolutionValue(self):
return sum(self.getComponentCost(c) for c in self.getSolutionComponents())
class AS_Ant(ConcreteAnt, AS_Decision, AllUpdate):
pass
class ACS_Ant(ConcreteAnt, ACS_Decision, ACSIterBestUpdate):
def __init__(self, phi = dp.phi, q0 = dp.q0, **kwargs):
self.q0 = q0
self.phi = phi
super().__init__(**kwargs)
def getq0(self):
return self.q0
def getPhi(self):
return self.phi
class MMAS_Ant(ConcreteAnt, MMAS_Decision, MMASIterBestUpdate):
def __init__(self, minPheromone = dp.minPheromone, maxPheromone = dp.maxPheromone, **kwargs):
self.minPheromone = minPheromone
self.maxPheromone = maxPheromone
super().__init__(**kwargs)
def getMinPheromone(self):
return self.minPheromone
def getMaxPheromone(self):
return self.maxPheromone