-
Notifications
You must be signed in to change notification settings - Fork 1
/
PSOInit.py
138 lines (61 loc) · 3.25 KB
/
PSOInit.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
import numpy as np
############################ Contents ############################
# This contains the two initialization functions that are called by PSOMain.py
# This also contains two initialization functions specifically for velocity and
# position which are called by the two main initialization functions
############################ Variable descriptions ############################
# n is the number of dimensions (int)
# s is the number of particles (int)
# bounds of the search area - of the form [[x1min, x1max], ... , [xnmin, xnmax]]
# bound is the search area bound for one dimension (one entry of bounds)
# f is the function to be optimized
# pcurr is the current position of each particles
# vcurr is the current velocity of each particles
# pbest is the best position of each particle
# fbest is the minimum value found for each particle
# fgbest is the overall minimum value found of all particles
# pgbest is the overall best position of each particle
############################ Initialization Functions ############################
# Takes in bound for one dimension, and the number of particles (s)
# Randomly generations the position in that dimension of each particle based on
# a uniform distribution within the bounds for the dimension
def posit_init(bound, s):
return np.random.uniform(bound[0], bound[1], s)
# Takes in bound for one dimension, and the number of particles (s)
# Randomly generations the velocity in that dimension of each particle based on
# a uniform distribution between the range times -1 and the range itself
def veloc_init(bound, s):
dimrange = abs(bound[1] - bound[0])
return np.random.uniform(-dimrange, dimrange, s)
# Called by PSOMain.py
# Takes in the function, the number of particles (s), and the bounds
# Uses these to initialize all necessary starting values for PSO
def pso_init(f, s, bounds):
n = len(bounds)
# initializes current particle positions and velocities using functions above
pcurr = list(map(posit_init, bounds, [s] * n))
pcurr = np.array(list(map(list, zip(*pcurr))))
vcurr = list(map(veloc_init, bounds, [s] * n))
vcurr = np.array(list(map(list, zip(*vcurr))))
# best particle position and value automatically starts the same as current
pbest = np.copy(pcurr, order="k")
fbest = np.array(list(map(f, pbest)))
# global best position and value are calculated
fgbest = min(fbest)
pgbest = np.copy(pbest[fbest == fgbest], order="k")[0]
return pcurr, vcurr, pbest, fbest, pgbest, fgbest
# Called by PSOMain.py
# Takes in the function, the number of particles (s), and the bounds
# Uses these to initialize all necessary starting values for QPSO
def qpso_init(f, s, bounds):
n = len(bounds)
# initializes current particle positions using function above
pcurr = list(map(posit_init, bounds, [s] * n))
pcurr = np.array(list(map(list, zip(*pcurr))))
# best particle position and value automatically starts the same as current
pbest = np.copy(pcurr, order="k")
fbest = np.array(list(map(f, pbest)))
# global best position and value are calculated
fgbest = min(fbest)
pgbest = np.copy(pbest[fbest == fgbest], order="k")[0]
return pcurr, pbest, fbest, pgbest, fgbest