-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcall_cpso.cc
295 lines (272 loc) · 9.76 KB
/
call_cpso.cc
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* main.cc */
#include <stdio.h>
#include <math.h>
#include <time.h> // time_t time(time_t *tloc);
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <ctype.h> // tolower
#include <vector>
#include "call_cpso.h"
#include "printdate.h"
#include "timesyshms.h"
#include "assert.h"
#include "eval.h"
#include "qmultiply.h"
#include "ranlib.h"
#include "support.h"
#include "ls.h"
extern FILE *logFile;
extern Eval evaluate;
Representation **cnv_state_to_rep2(const State &state)
{
register int i;
Representation **retval;
retval = new Representation *[5];
retval[0] = new RealVector(1);
retval[0]->write(state.T.x, 0);
retval[1] = new RealVector(1);
retval[1]->write(state.T.y, 0);
retval[2] = new RealVector(1);
retval[2]->write(state.T.z, 0);
retval[3] = new RealVector(4);
retval[3]->write(state.Q.x, 0);
retval[3]->write(state.Q.y, 1);
retval[3]->write(state.Q.z, 2);
retval[3]->write(state.Q.w, 3);
retval[4] = new RealVector(state.ntor);
for(i=0; i<state.ntor; i++) {
retval[4]->write(state.tor[i], i);
}
return(retval);
}
Individual cnv_state_to_ind2(const State &original, const int ntor)
{
// BEGIN ADDITION
// Added by gmm, 27-MAR-97, to solve these compiler warnings:
//
// call_ls.cc:59: warning: In this statement, the initialization of a non-const reference requires a temporary for "Genotype(5,cnv_state_to_rep(original))". (reftemporary)
// call_ls.cc:59: warning: In this statement, the initialization of a non-const reference requires a temporary for "Phenotype(5,cnv_state_to_rep(original))". (reftemporary)
// call_ls.cc:59: warning: In this statement, the initialization of a non-const reference requires a temporary for "(Individual(Genotype(5,cnv_state_to_rep(original)),Phenotype(5,cnv_state_to_rep(original))))". (reftemporary)
Genotype temp_Gtype;
Phenotype temp_Ptype;
temp_Gtype = Genotype(5, cnv_state_to_rep2(original));
temp_Ptype = Phenotype(5, cnv_state_to_rep2(original));
Individual temp(temp_Gtype, temp_Ptype);
return(temp);
// END ADDITION
}
State call_cpso(Local_Search *const local_method,
const State& sInit,
const int n_exec, const int S, //swarm size
const int D, //number of dimensions:7 + ntor
double *const xmin, double *const xmax,
const unsigned int num_evals,
const int K, ConstDouble c1, ConstDouble c2,
const int outlev)
{
int nb_eval = 0;
Position Xi[S_max]; //Max swarm size set to 1024 in constants.h
Velocity Vi[S_max];
Position P[S_max];
State sNew[S_max];
State sTemp;
Individual iTemp;
int g, s, d, i, m , best;
double pso_energy;
double energy_prev;
double min= 0.0;
int init_links;
int LINKS[S_max][S_max];
float prev_value[S_max];
int evaluations=0;
double Vmin[D_max], Vmax[D_max];
//Constants for the Constriction PSO
double chi,phi;
//State sbNew[S_max];
std::vector<State> sbNew(S_max, State(sInit));
int sb = 0;
int ntor = sInit.ntor;
evaluate.reset();
// Calculating chi for CPSO
phi = c1 + c2;
//Use formula from Clerc, M. Kennedy, J., Evolutionary
//Computation, IEEE Transactions on , vol. 6, no. 1, 58-73 (2002).
//TODO: check reference
chi = 2.0 / (2.0 - phi - sqrt( (phi * phi) - (4 * phi) ));
chi = fabs(chi);
fprintf(logFile, "\nConstants used for the Constriction PSO \nc1: %lf c2: %lf phi: %lf chi: %lf S: %d\n", c1, c2, phi, chi, S);
// Initialization of the Particles
for( s = 0; s < S ; s++)
{
//Updating evaluations
evaluations++;
if (outlev > LOGRUNBASIC )
{
pr(logFile, "PSO: Updating the swarm at move %d (= %d and %u evaluations)\n", nb_eval+1, evaluations, (unsigned int) evaluate.evals());
}
Xi[s].size = D;
Vi[s].size = D;
//Initialise the Particle
initialiseParticle(s, D, Xi, Vi, xmin, xmax, Vmin, Vmax);
sNew[s]=sInit;
copyDimension(&sNew[s], Xi[s]);
//First Evaluations
Xi[s].f = evaluate.evalpso(&sNew[s]);//E-test
if (outlev >= 2) {
pr(logFile, "\n(Initialization) Particle %d has Energy= %8.2lf\n", s, Xi[s].f);
printState(logFile, sNew[s], 0);
}
P[s] = Xi[s];// Best Postion = current One
// RG store the current energy value of particle
prev_value[s] = BIG;
} //for loop on index
//Find the best
best=0;
for (s=0; s<S; s++)
{
if (P[s].f < P[best].f) best=s;
}
pso_energy = P[best].f;
if (n_exec==1) min=pso_energy;
energy_prev=pso_energy;
init_links = 1;
nb_eval=0;
//loop: ITERATIONS
//Iteration of the runs
//for (nb_eval=1; nb_eval < num_evals; nb_eval++)
do
{
if (init_links == 1)
{
//Who informs who, at random
for (s=0; s<S; s++)
{
for(m=0; m<S; m++) LINKS[m][s]=0; //Init to "no link"
LINKS[s][s]=1; // Each particle informs itself
}
for (m=0; m<S; m++) //Other Links
{
for(i = 0; i < K; i++)
{
s = (int) random_range(0, S-1);
LINKS[m][s]=1;
}
}
}
sb = 0;
// The Swarm moves
for (s=0; s<S; s++)
{
//Updating evaluations
evaluations++;
if (outlev >= LOGRUNV)
{
pr(logFile, "\nPSO: Updating the swarm at move %d (= %d and %u evaluations)\n", nb_eval+1, evaluations, (unsigned int) evaluate.evals());
}
//..find the best informant
g = s;
for (m=0; m<S; m++)
{
if (LINKS[m][s] == 1 && P[m].f < P[g].f) g = m;
}
//..compute the new velocity and move
for (d=0; d<D; d++)
{
// -----Constriction -PSO ------Begin-
if (Xi[s].f >= prev_value[s]) // Relaxation Velocity Update
{
Vi[s].v[d] = chi * (Vi[s].v[d] + c1 * random_range(0,1) * (P[s].x[d] - Xi[s].x[d]) + c2 * random_range(0,1) * (P[g].x[d] - Xi[s].x[d]));
}
if (Vi[s].v[d] > Vmax[d])
{
Vi[s].v[d] = Vmax[d];
}
Xi[s].x[d] = Xi[s].x[d] + Vi[s].v[d];
// -----Constriction -PSO ------End---
}
//..interval confinement (keep in the box)
for (d=0; d<D; d++)
{
if (d>5)
{
if (Xi[s].x[d] > PI)
{
//printf("\n Value Initial = %lf", Xi[s].x[d]);
Xi[s].x[d] = Xi[s].x[d] - ((int)(Xi[s].x[d] / PI) * PI);
//Vi[s].v[d]= 0.0 ;
}
if (Xi[s].x[d] < -PI)
{
//Xi[s].x[d] %= -PI;
Xi[s].x[d] = Xi[s].x[d] - ((int)(Xi[s].x[d] / -PI) * -PI);
//Vi[s].v[d]= 0.0 ;
}
}
else
{
if(Xi[s].x[d] < xmin[d])
{
Xi[s].x[d]= random_range(xmin[d], xmax[d]);
Vi[s].v[d]= random_range(Vmin[d], Vmax[d]) ;
}
if(Xi[s].x[d] > xmax[d])
{
Xi[s].x[d]= random_range(xmin[d], xmax[d]);
Vi[s].v[d]= random_range(Vmin[d], Vmax[d]) ;
}
}
}//for
// copy the dimension to State Variable
copyDimension(&sNew[s], Xi[s]);
//...evaluate the new position
prev_value[s] = Xi[s].f;
Xi[s].f= evaluate.evalpso(&sNew[s]);//E-test
if (outlev >= LOGRUNVV)
{
pr(logFile,"\nSwarmMove: (%d) \tParticle: %d \tEnergy= %8.2lf\n", nb_eval + 1, s + 1, Xi[s].f);
printState(logFile, sNew[s], 0);
}
if(Xi[s].f <= Xi[sb].f) sb=s;
} //s
copyDimension(&sbNew[s], Xi[sb]);
if (local_method !=NULL)
{
iTemp = cnv_state_to_ind2(sbNew[s], (D-7));
local_method->search(iTemp);
sbNew[s] = iTemp.state(ntor);
//pr(logFile, "\nsbNew[%d].T.x==%f @@@\n", s, sbNew[s].T.x);
};
for (s=0; s<S; s++)
{
//... Update the best previous position
if (Xi[s].f < P[s].f)
{
P[s] = Xi[s];
//...update the best of the bests
if(P[s].f < P[best].f) best = s;
}
}
nb_eval++;
//Swarm - Begin Activity moved as a function
swarmActivity(S, D, Xi, nb_eval, outlev);
//Swarm - End
// If no improvement, information links will be reinitialised
pso_energy=P[best].f;
if(pso_energy >= energy_prev) init_links = 1;
else init_links = 0;
energy_prev = pso_energy;
if (outlev >= LOGRUNV)
{
pr(logFile, "PSO- Run: %2d \tPSObest Energy@Swarm_Move: %4d \tP= %8.2lf \t(nbeval= %d and %u )\n", n_exec+1, nb_eval+1, pso_energy, evaluations, (unsigned int) evaluate.evals());
};
} while(evaluate.evals() < num_evals);
//Result is stored in the History state for writePDBQ and for clustering ....
sTemp=sInit;
copyDimension(&sTemp, P[best]);
mkUnitQuat(&sTemp.Q); // MP TODO probably unnecc
//printState(logFile, sTemp, 2);
return (sTemp);
} //call_cpso
/* END OF PROGRAM */
/* EOF */