-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpso.cpp
199 lines (173 loc) · 5.04 KB
/
pso.cpp
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
/*
* File: pso.cpp
* Author: diepeerk
*
* Created on 4 de octubre de 2010, 11:48 AM
*/
#include "pso.h"
#include <cstdlib>
#include <iostream>
#include "particle.h"
#include <cstdio>
#include <fstream>
#include <ctime>
using namespace std;
#define INERTIA_MAX 1
#define INERTIA_MIN 0
pso::pso(int nurses, int days, int shifts)
{
int cN;
this->nurses = nurses;
this->days = days;
this->shifts = shifts;
/*asigno memoria dinamicamente a matriz de g_best (mejor posicion global) para obtener g_best[nurses+1][days*shifts] */
g_best = (int**)malloc((nurses+1)*sizeof(int*));
for(cN=0; cN<(nurses+1);cN++){
g_best[cN] = (int*)malloc(days*shifts*sizeof(int*));
}
}
pso::pso(const pso& orig) {
}
/*libero memoria asignada a g_best*/
pso::~pso() {
for(int cN = 0; cN <= nurses ; cN++ ){
free(g_best[cN]);
}
free(g_best);
}
void pso::setW(float value)
{
w = value;
}
void pso::setR1()
{
r1 = (float) rand()/RAND_MAX;
}
void pso::setR2()
{
r2 = (float) rand()/RAND_MAX;
}
void pso::setC1(float value)
{
c1 = value;
}
void pso::setC2(float value)
{
c2 = value;
}
void pso::run(int SIZE, int ITERATIONS)
{
clock_t start = clock();
int iter;
/* INICIALIZO PSO */
particle *swarm[SIZE];
int s, t, s_best, cN, cDS;
s_best = 0; //indice de mi mejor global lo inicializo en 0
/*inicializo el enjambre de particulas*/
printf("PSO PARAMETERS\n\tc1: %f c2: %f w: %f size: %d iterations: %d\n\n",c1,c2,w,SIZE,ITERATIONS);
for(s=0; s < SIZE; s++){
swarm[s] = new particle(nurses,days,shifts);
swarm[s]->setPsoParameters(c1,c2,r1,r2,w);
swarm[s]->setCoverageMatrix(coverage);
swarm[s]->setPreferenceMatrix(preference);
swarm[s]->init();
}
g_fitness = swarm[s_best]->getFitness();
/*copio la informacion del mejor global a g_best */
for(cN = 0; cN <= nurses; cN++){
for(cDS = 0; cDS < days*shifts ; cDS++){
g_best[cN][cDS] = swarm[s_best]->getPositionMatrix()[cN][cDS];
}
}
iter = 0;
/*itero ITERATIONS veces*/
for(t = 0 ; t < ITERATIONS ; t++){
/*aplico control de parametros a la inercia y a los factores r1 y r2*/
printf("Iteracion %d\tGBest = %d\n",t,g_fitness);
iter++;
if(iter == ITERATIONS*0.05){
float random,random2,random3;
iter = 0;
if(c1 > 0.5){
random = (float) rand()/RAND_MAX;
c1 = c1 - random*c1;
}else{
random = (float) rand()/RAND_MAX;
c1 = c1 + random*c1;
}
if(c2 > 0.5){
random2 = (float) rand()/RAND_MAX;
c2 = c2 - random2*c2;
}else{
random2 = (float) rand()/RAND_MAX;
c2 = c2 + random2*c2;
}
if(w > 0.5){
random3 = (float) rand()/RAND_MAX;
w = w - random3*w;
}else{
random3 = (float) rand()/RAND_MAX;
w = w + random3*w;
}
printf("CAMBIE a c1 = %f ; c2 = %f ; w = %f\n",c1,c2,w);
}
/*actualizo velocidad y posicion de cada particula del enjambre */
for(s=0; s < SIZE; s++){
swarm[s]->setPsoParameters(c1,c2,r1,r2,w);
swarm[s]->update(g_best);
}
/*obtengo el indice del mejor global y lo guardo en s_best*/
for(s=0; s < SIZE; s++){
if(swarm[s]->getFitness() < g_fitness){
iter = 0;
s_best = s;
g_fitness = swarm[s]->getFitness();
/*copio la informacion del mejor global a g_best */
for(cN = 0; cN <= nurses; cN++){
for(cDS = 0; cDS < days*shifts ; cDS++){
g_best[cN][cDS] = swarm[s_best]->getPositionMatrix()[cN][cDS];
}
}
}
}
}
/*imprimo resultados*/
//printf("\t\t\t\tPREFERENCE\n\n");
//swarm[0]->printPreferenceMatrix();
printf("\t\t\t\tBEST SOLUTION\n\n");
printf("\tFitness: %d\n\n",g_fitness);
printf("\tSolution:");
printGBest();
/*escribo en un archivo el fitness*/
ofstream data("output.res");
data << g_fitness << endl;
data.close();
/*libero memoria reservada al enjambre*/
for(s=0; s < SIZE; s++){
delete swarm[s];
}
printf ( "Tiempo de procesamiento: %f [segs]\n", ((double)clock() - start)/CLOCKS_PER_SEC);
}
void pso::setPreferenceMatrix(int **preference)
{
this->preference = preference;
}
void pso::setCoverageMatrix(int **coverage)
{
this->coverage = coverage;
}
void pso::printGBest(){
int count = 0;
for(int cN = 0; cN <= nurses; cN++){
printf("\n\t");
for(int cDS = 0; cDS < days*shifts ; cDS++){
count ++;
printf("%d ", g_best[cN][cDS]);
if(count == shifts){
printf("\t");
count = 0;
}
}
}
printf("\n");
}