Conditions | 10 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ArtificialBeeColonyAlgorithm.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import random as rnd |
||
105 | def run(self): |
||
106 | self.init() |
||
107 | FEs = self.FoodNumber |
||
108 | while FEs < self.nFES: |
||
109 | self.Best.toString() |
||
110 | for i in range(self.FoodNumber): |
||
111 | newSolution = copy.deepcopy(self.Foods[i]) |
||
112 | param2change = int(rnd.random() * self.D) |
||
113 | neighbor = int(self.FoodNumber * rnd.random()) |
||
114 | newSolution.Solution[param2change] = self.Foods[i].Solution[param2change] \ |
||
115 | + (-1 + 2 * rnd.random()) * \ |
||
116 | (self.Foods[i].Solution[param2change] - |
||
117 | self.Foods[neighbor].Solution[param2change]) |
||
118 | newSolution.repair() |
||
119 | newSolution.evaluate() |
||
120 | if newSolution.Fitness < self.Foods[i].Fitness: |
||
121 | self.checkForBest(newSolution) |
||
122 | self.Foods[i] = newSolution |
||
123 | self.Trial[i] = 0 |
||
124 | else: |
||
125 | self.Trial[i] += 1 |
||
126 | FEs += self.FoodNumber |
||
127 | self.CalculateProbs() |
||
128 | t, s = 0, 0 |
||
129 | while t < self.FoodNumber: |
||
130 | if rnd.random() < self.Probs[s]: |
||
131 | t += 1 |
||
132 | Solution = copy.deepcopy(self.Foods[s]) |
||
133 | param2change = int(rnd.random() * self.D) |
||
134 | neighbor = int(self.FoodNumber * rnd.random()) |
||
135 | while neighbor == s: |
||
136 | neighbor = int(self.FoodNumber * rnd.random()) |
||
137 | Solution.Solution[param2change] = self.Foods[s].Solution[param2change] \ |
||
138 | + (-1 + 2 * rnd.random()) * ( |
||
139 | self.Foods[s].Solution[param2change] - |
||
140 | self.Foods[neighbor].Solution[param2change]) |
||
141 | Solution.repair() |
||
142 | Solution.evaluate() |
||
143 | FEs += 1 |
||
144 | if Solution.Fitness < self.Foods[s].Fitness: |
||
145 | self.checkForBest(newSolution) |
||
146 | self.Foods[s] = Solution |
||
147 | self.Trial[s] = 0 |
||
148 | else: |
||
149 | self.Trial[s] += 1 |
||
150 | s += 1 |
||
151 | if s == self.FoodNumber: |
||
152 | s = 0 |
||
153 | |||
154 | mi = self.Trial.index(max(self.Trial)) |
||
155 | if self.Trial[mi] >= self.Limit: |
||
156 | self.Foods[mi] = SolutionABC(self.D, self.Lower, self.Upper) |
||
157 | self.Foods[mi].evaluate() |
||
158 | FEs += 1 |
||
159 | self.Trial[mi] = 0 |
||
160 | return self.Best.Fitness |
||
161 |