|
1
|
|
|
# encoding=utf8 |
|
2
|
|
|
import random |
|
3
|
|
|
import numpy |
|
4
|
|
|
from NiaPy.benchmarks.utility import Utility |
|
5
|
|
|
|
|
6
|
|
|
__all__ = ['ParticleSwarmAlgorithm'] |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class ParticleSwarmAlgorithm(object): |
|
10
|
|
|
r"""Implementation of Particle Swarm Optimization algorithm. |
|
11
|
|
|
|
|
12
|
|
|
**Algorithm:** Particle Swarm Optimization algorithm |
|
13
|
|
|
|
|
14
|
|
|
**Date:** 2018 |
|
15
|
|
|
|
|
16
|
|
|
**Author:** Lucija Brezočnik, Grega Vrbančič, and Iztok Fister Jr. |
|
17
|
|
|
|
|
18
|
|
|
**License:** MIT |
|
19
|
|
|
|
|
20
|
|
|
**Reference paper:** |
|
21
|
|
|
Kennedy, J. and Eberhart, R. "Particle Swarm Optimization". |
|
22
|
|
|
Proceedings of IEEE International Conference on Neural Networks. |
|
23
|
|
|
IV. pp. 1942--1948, 1995. |
|
24
|
|
|
""" |
|
25
|
|
|
|
|
26
|
|
|
def __init__(self, D, NP, nFES, C1, C2, w, vMin, vMax, benchmark): |
|
27
|
|
|
r"""**__init__(self, NP, D, nFES, C1, C2, w, vMin, vMax, benchmark)**. |
|
28
|
|
|
|
|
29
|
|
|
Arguments: |
|
30
|
|
|
NP {integer} -- population size |
|
31
|
|
|
|
|
32
|
|
|
D {integer} -- dimension of problem |
|
33
|
|
|
|
|
34
|
|
|
nFES {integer} -- number of function evaluations |
|
35
|
|
|
|
|
36
|
|
|
C1 {decimal} -- cognitive component |
|
37
|
|
|
|
|
38
|
|
|
C2 {decimal} -- social component |
|
39
|
|
|
|
|
40
|
|
|
w {decimal} -- inertia weight |
|
41
|
|
|
|
|
42
|
|
|
vMin {decimal} -- minimal velocity |
|
43
|
|
|
|
|
44
|
|
|
vMax {decimal} -- maximal velocity |
|
45
|
|
|
|
|
46
|
|
|
benchmark {object} -- benchmark implementation object |
|
47
|
|
|
|
|
48
|
|
|
""" |
|
49
|
|
|
|
|
50
|
|
|
self.benchmark = Utility().get_benchmark(benchmark) |
|
51
|
|
|
self.NP = NP # population size; number of search agents |
|
52
|
|
|
self.D = D # dimension of the problem |
|
53
|
|
|
self.C1 = C1 # cognitive component |
|
54
|
|
|
self.C2 = C2 # social component |
|
55
|
|
|
self.w = w # inertia weight |
|
56
|
|
|
self.vMin = vMin # minimal velocity |
|
57
|
|
|
self.vMax = vMax # maximal velocity |
|
58
|
|
|
self.Lower = self.benchmark.Lower # lower bound |
|
59
|
|
|
self.Upper = self.benchmark.Upper # upper bound |
|
60
|
|
|
self.nFES = nFES # number of function evaluations |
|
61
|
|
|
self.eval_flag = True # evaluations flag |
|
62
|
|
|
self.evaluations = 0 # evaluations counter |
|
63
|
|
|
self.Fun = self.benchmark.function() |
|
64
|
|
|
|
|
|
|
|
|
|
65
|
|
|
self.Solution = numpy.zeros((self.NP, self.D)) # positions of search agents |
|
66
|
|
|
self.Velocity = numpy.zeros((self.NP, self.D)) # velocities of search agents |
|
67
|
|
|
|
|
68
|
|
|
self.pBestFitness = numpy.zeros(self.NP) # personal best fitness |
|
69
|
|
|
self.pBestFitness.fill(float("inf")) |
|
70
|
|
|
self.pBestSolution = numpy.zeros((self.NP, self.D)) # personal best solution |
|
71
|
|
|
|
|
72
|
|
|
self.gBestFitness = float("inf") # global best fitness |
|
73
|
|
|
self.gBestSolution = numpy.zeros(self.D) # global best solution |
|
74
|
|
|
|
|
75
|
|
|
def init(self): |
|
76
|
|
|
"""Initialize positions.""" |
|
77
|
|
|
for i in range(self.NP): |
|
78
|
|
|
for j in range(self.D): |
|
79
|
|
|
self.Solution[i][j] = random.random() * \ |
|
80
|
|
|
(self.Upper - self.Lower) + self.Lower |
|
81
|
|
|
|
|
82
|
|
|
def eval_true(self): |
|
83
|
|
|
"""Check evaluations.""" |
|
84
|
|
|
|
|
85
|
|
|
if self.evaluations == self.nFES: |
|
86
|
|
|
self.eval_flag = False |
|
87
|
|
|
|
|
88
|
|
|
def bounds(self, position): |
|
89
|
|
|
for i in range(self.D): |
|
90
|
|
|
if position[i] < self.Lower: |
|
91
|
|
|
position[i] = self.Lower |
|
92
|
|
|
if position[i] > self.Upper: |
|
93
|
|
|
position[i] = self.Upper |
|
94
|
|
|
return position |
|
95
|
|
|
|
|
96
|
|
|
def move_particles(self): |
|
97
|
|
|
|
|
98
|
|
|
self.init() |
|
99
|
|
|
|
|
100
|
|
|
while self.eval_flag is not False: |
|
101
|
|
|
for i in range(self.NP): |
|
102
|
|
|
self.Solution[i] = self.bounds(self.Solution[i]) |
|
103
|
|
|
|
|
104
|
|
|
self.eval_true() |
|
105
|
|
|
if self.eval_flag is not True: |
|
106
|
|
|
break |
|
107
|
|
|
|
|
108
|
|
|
Fit = self.Fun(self.D, self.Solution[i]) |
|
109
|
|
|
self.evaluations = self.evaluations + 1 |
|
110
|
|
|
|
|
111
|
|
|
if Fit < self.pBestFitness[i]: |
|
112
|
|
|
self.pBestFitness[i] = Fit |
|
113
|
|
|
self.pBestSolution[i] = self.Solution[i] |
|
114
|
|
|
|
|
115
|
|
|
if Fit < self.gBestFitness: |
|
116
|
|
|
self.gBestFitness = Fit |
|
117
|
|
|
self.gBestSolution = self.Solution[i] |
|
118
|
|
|
|
|
119
|
|
|
for i in range(self.NP): |
|
120
|
|
|
for j in range(self.D): |
|
121
|
|
|
self.Velocity[i][j] = (self.w * self.Velocity[i][j]) + \ |
|
122
|
|
|
(self.C1 * random.random() * (self.pBestSolution[i][j] - self.Solution[i][j])) + \ |
|
123
|
|
|
(self.C2 * random.random() * (self.gBestSolution[j] - self.Solution[i][j])) |
|
124
|
|
|
|
|
125
|
|
|
if self.Velocity[i][j] < self.vMin: |
|
126
|
|
|
self.Velocity[i][j] = self.vMin |
|
127
|
|
|
if self.Velocity[i][j] > self.vMax: |
|
128
|
|
|
self.Velocity[i][j] = self.vMax |
|
129
|
|
|
|
|
130
|
|
|
self.Solution[i][j] = self.Solution[i][j] + \ |
|
131
|
|
|
self.Velocity[i][j] |
|
132
|
|
|
|
|
133
|
|
|
return self.gBestFitness |
|
134
|
|
|
|
|
135
|
|
|
def run(self): |
|
136
|
|
|
return self.move_particles() |
|
137
|
|
|
|