46 this->fitnessFunction = fitnessFunction;
64 pb_Fit =
new double[p];
68 for(
int i=0; i < p; i++){
69 pb[i] =
new double[n];
80 for(
int i=0; i < p; i++){
102 for(
int j, i=0; i < p; i++){
103 for(j=0; j < n; j++) v[i][j] = RAND_DOUBLE(seed, s1, s2)/10;
104 for(j=0; j < n; j++) x[i][j] = RAND_DOUBLE(seed, s1, s2);
105 COPY_ARR(x[i], pb[i], n);
106 pb_Fit[i] = fit[i] = fitnessFunction(x[i], n);
107 if(pb_Fit[i] < Gb_Fit){
108 COPY_ARR(pb[i], Gb, n);
123 double currW = w - (w / M) * nEvals;
124 while(!found && nEvals < M){
125 for(i=0; i < p; i++){
126 for(j=0; j < n; j++){
127 v[i][j] = currW * v[i][j] + c1 * RAND_DOUBLE(seed, 0, 1) * (pb[i][j] - x[i][j])
128 + c2 * RAND_DOUBLE(seed, 0, 1) * (Gb[j] - x[i][j]);
132 else if(x[i][j] > s2)
135 fit[i] = fitnessFunction(x[i], n);
138 for(i=0; i < p; i++){
139 if(fit[i] < pb_Fit[i]){
140 COPY_ARR(x[i], pb[i], n);
142 if(pb_Fit[i] < Gb_Fit){
144 COPY_ARR(pb[i], Gb, n);
150 if(decreasingW) currW -= w / M;
161 COPY_ARR(pb[gb], _x, n);
193 gettimeofday(&tv,NULL);
int getNEvals()
Get the number of fitness function evaluations performed up to the moment.
void next(int M)
Obtain the next improvement.
unsigned int getRandomSeed()
Get a random number to be used as seed for the random number generator.
double fitnessFunction(double *x, int n)
Fitness function implementation.
void startup()
Startup the PSO.
double getFitness()
Get the best fitness value found up to the moment.
int getBestPos(double *_x)
Get the best result obtained up to the moment (global best).
PSO(callback_t fitnessFunction, double s1, double s2, int p, int n, double w, double c1, double c2)
A standard implementation of PSO.