| @@ 21-45 (lines=25) @@ | ||
| 18 | ||
| 19 | def generateSolution(self): |
|
| 20 | self.Solution = [self.LB + (self.UB - self.LB) * rnd.random() |
|
| 21 | for _i in range(self.D)] |
|
| 22 | ||
| 23 | def evaluate(self): |
|
| 24 | self.Fitness = SolutionDE.FuncEval(self.D, self.Solution) |
|
| 25 | ||
| 26 | def repair(self): |
|
| 27 | for i in range(self.D): |
|
| 28 | if self.Solution[i] > self.UB: |
|
| 29 | self.Solution[i] = self.UB |
|
| 30 | if self.Solution[i] < self.LB: |
|
| 31 | self.Solution[i] = self.LB |
|
| 32 | ||
| 33 | def __eq__(self, other): |
|
| 34 | return self.Solution == other.Solution and self.Fitness == other.Fitness |
|
| 35 | ||
| 36 | ||
| 37 | class DifferentialEvolutionAlgorithm(object): |
|
| 38 | r"""Implementation of Differential evolution algorithm. |
|
| 39 | ||
| 40 | **Algorithm:** Differential evolution algorithm |
|
| 41 | ||
| 42 | **Date:** 2018 |
|
| 43 | ||
| 44 | **Author:** Uros Mlakar |
|
| 45 | ||
| 46 | **License:** MIT |
|
| 47 | ||
| 48 | **Reference paper:** |
|
| @@ 25-51 (lines=27) @@ | ||
| 22 | def repair(self): |
|
| 23 | for i in range(self.D): |
|
| 24 | if self.Solution[i] > self.UB: |
|
| 25 | self.Solution[i] = self.UB |
|
| 26 | ||
| 27 | if self.Solution[i] < self.LB: |
|
| 28 | self.Solution[i] = self.LB |
|
| 29 | ||
| 30 | def evaluate(self): |
|
| 31 | self.Fitness = SolutionABC.FuncEval(self.D, self.Solution) |
|
| 32 | ||
| 33 | def toString(self): |
|
| 34 | pass |
|
| 35 | ||
| 36 | ||
| 37 | class ArtificialBeeColonyAlgorithm(object): |
|
| 38 | r"""Implementation of Artificial Bee Colony algorithm. |
|
| 39 | ||
| 40 | **Algorithm:** Artificial Bee Colony algorithm |
|
| 41 | ||
| 42 | **Date:** 2018 |
|
| 43 | ||
| 44 | **Author:** Uros Mlakar |
|
| 45 | ||
| 46 | **License:** MIT |
|
| 47 | ||
| 48 | **Reference paper:** |
|
| 49 | Karaboga, D., and Bahriye B. "A powerful and efficient algorithm for |
|
| 50 | numerical function optimization: artificial bee colony (ABC) algorithm." |
|
| 51 | Journal of global optimization 39.3 (2007): 459-471. |
|
| 52 | ||
| 53 | """ |
|
| 54 | ||
| @@ 7-37 (lines=31) @@ | ||
| 4 | ||
| 5 | __all__ = ['SelfAdaptiveDifferentialEvolutionAlgorithm'] |
|
| 6 | ||
| 7 | ||
| 8 | class SolutionjDE(object): |
|
| 9 | ||
| 10 | def __init__(self, D, LB, UB, F, CR): |
|
| 11 | self.D = D |
|
| 12 | self.LB = LB |
|
| 13 | self.UB = UB |
|
| 14 | self.F = F |
|
| 15 | self.CR = CR |
|
| 16 | self.Solution = [] |
|
| 17 | self.Fitness = float('inf') |
|
| 18 | self.generateSolution() |
|
| 19 | ||
| 20 | def generateSolution(self): |
|
| 21 | """Generate solution.""" |
|
| 22 | ||
| 23 | self.Solution = [self.LB + (self.UB - self.LB) * rnd.random() |
|
| 24 | for _i in range(self.D)] |
|
| 25 | ||
| 26 | def evaluate(self): |
|
| 27 | """Evaluate solution.""" |
|
| 28 | ||
| 29 | self.Fitness = SolutionjDE.FuncEval(self.D, self.Solution) |
|
| 30 | ||
| 31 | def repair(self): |
|
| 32 | for i in range(self.D): |
|
| 33 | if self.Solution[i] > self.UB: |
|
| 34 | self.Solution[i] = self.UB |
|
| 35 | if self.Solution[i] < self.LB: |
|
| 36 | self.Solution[i] = self.LB |
|
| 37 | ||
| 38 | def __eq__(self, other): |
|
| 39 | return self.Solution == other.Solution and self.Fitness == other.Fitness |
|
| 40 | ||
| @@ 8-37 (lines=30) @@ | ||
| 5 | __all__ = ['GeneticAlgorithm'] |
|
| 6 | ||
| 7 | ||
| 8 | class Chromosome(object): |
|
| 9 | def __init__(self, D, LB, UB): |
|
| 10 | self.D = D |
|
| 11 | self.LB = LB |
|
| 12 | self.UB = UB |
|
| 13 | ||
| 14 | self.Solution = [] |
|
| 15 | self.Fitness = float('inf') |
|
| 16 | self.generateSolution() |
|
| 17 | ||
| 18 | def generateSolution(self): |
|
| 19 | self.Solution = [self.LB + (self.UB - self.LB) * rnd.random() |
|
| 20 | for _i in range(self.D)] |
|
| 21 | ||
| 22 | def evaluate(self): |
|
| 23 | self.Fitness = Chromosome.FuncEval(self.D, self.Solution) |
|
| 24 | ||
| 25 | def repair(self): |
|
| 26 | for i in range(self.D): |
|
| 27 | if self.Solution[i] > self.UB: |
|
| 28 | self.Solution[i] = self.UB |
|
| 29 | if self.Solution[i] < self.LB: |
|
| 30 | self.Solution[i] = self.LB |
|
| 31 | ||
| 32 | def __eq__(self, other): |
|
| 33 | return self.Solution == other.Solution and self.Fitness == other.Fitness |
|
| 34 | ||
| 35 | def toString(self): |
|
| 36 | print([i for i in self.Solution]) |
|
| 37 | ||
| 38 | ||
| 39 | class GeneticAlgorithm(object): |
|
| 40 | r"""Implementation of Genetic algorithm. |
|