-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpso.py
More file actions
154 lines (121 loc) · 4.4 KB
/
pso.py
File metadata and controls
154 lines (121 loc) · 4.4 KB
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
"""
Particle Swarm Optimisation for Numerical Optimisation
Type:- Paricle Swarm Optimizer with inertia
x id (t) = f (w(t), x id (t − 1), v id (t − 1), p id , p gd )
v id (t) = w(t) ∗ v id (t − 1) + c 1 ϕ 1 (p id − x id (t − 1)) + c 2 ϕ 2 (p gd − x id (t − 1))
w(t) = ((T max − t) ∗ (w start − w end ))/Tmax + w_end
"""
import numpy as np
import random
import benchmarkFunctions
def function(x):
d = len(x)
sum = 0
prod = 1
for i in range(1, d+1):
sum += x[i-1]**2 / 4000
prod *= np.cos(x[i-1] / np.sqrt(i))
return 1 + sum - prod
def initial_position(populationSize, dimension, minValue, maxValue, function):
position = np.zeros((populationSize, dimension))
for i in range(0, populationSize):
for j in range(0, dimension):
position[i, j] = random.uniform(minValue, maxValue)
position[i, -1] = function(position[i, 0:position.shape[1]-1])
return position
def initial_velocity(position, dimension, minValue, maxValue):
velocity = np.zeros((position.shape[0], dimension))
for i in range(0, velocity.shape[0]):
for j in range(0, velocity.shape[1]):
velocity[i, j] = random.uniform(minValue, maxValue)
return velocity
def update_individual(position, individual):
for i in range(0, position.shape[0]):
if (individual[i, -1] > position[i, -1]):
for j in range(0, position.shape[1]):
individual[i, j] = position[i, j]
return individual
def velocity_update(position, velocity, individual, best_global, w):
phi1 = 2
phi2 = 2
c1 = np.random.rand()
c2 = np.random.rand()
velocity = np.zeros((position.shape[0], velocity.shape[1]))
for i in range(0, velocity.shape[0]):
for j in range(0, velocity.shape[1]-1):
velocity[i, j] = w*velocity[i, j] + phi1*c1 * \
(individual[i, j] - position[i, j]) + \
phi2*c2*(best_global[j] - position[i, j])
return velocity
def update_position(position, velocity, minValue, maxValue, function):
for i in range(0, position.shape[0]):
for j in range(0, position.shape[1] - 1):
position[i, j] = np.clip(
(position[i, j] + velocity[i, j]), minValue, maxValue)
position[i, -1] = function(position[i, 0:position.shape[1]-1])
return position
def PSO(populationSize, dimension, minValue, maxValue, iterations, function, verbose):
position = initial_position(populationSize, dimension, minValue, maxValue, function)
velocity = initial_velocity(position, dimension, minValue, maxValue)
individual = np.copy(position)
best_global = position[position[:, -1].argmin()].copy()
w_start = 0.9
w_end = 0.4
for i in range(1,iterations+1):
if (verbose == True and i % 5 == 0):
print('Iteration= ', i, ' f(x) = ', best_global[-1])
position = update_position(position, velocity, minValue, maxValue, function)
individual = update_individual(position, individual)
value = np.copy(individual[individual[:, -1].argsort()][0, :])
if (best_global[-1] > value[-1]):
best_global = np.copy(value)
w = w_end + ((iterations - (i-1)) * (w_start - w_end))/iterations
velocity = velocity_update(position, velocity, individual, best_global, w)
return best_global
griewank = {
'populationSize': 125,
'dimension': 10,
'minValue': -600,
'maxValue': 600,
'iterations': 500,
'verbose': True,
'function': benchmarkFunctions.griewank
}
rastrigin = {
'populationSize': 125,
'dimension': 10,
'minValue': -15,
'maxValue': 15,
'iterations': 1000,
'verbose': False,
'function': benchmarkFunctions.rastrigin
}
rosenbrock = {
'populationSize': 125,
'dimension': 10,
'minValue': -15,
'maxValue': 15,
'iterations': 500,
'verbose': False,
'function': benchmarkFunctions.rosenbrock
}
ackley = {
'populationSize': 125,
'dimension': 10,
'minValue': -32.768,
'maxValue': 32.768,
'iterations': 500,
'verbose': False,
'function': benchmarkFunctions.ackley
}
schwefel_10 = {
'populationSize': 125,
'dimension': 10,
'minValue': -500,
'maxValue': 500,
'iterations': 500,
'verbose': True,
'function': benchmarkFunctions.schwefel
}
solution = PSO(**griewank)
print("Solution: ", solution, "\n", "Value: ", solution[-1])