BFOA
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 #include <math.h> 6 7 #define PI (3.1415926f) 8 #define RND ((float)rand() / (RAND_MAX + 1)) 9 #define X_DIM 30 10 11 //float domx[X_DIM][2] = 12 //{ 13 // { -1.0f, 2.0f}, { -1.0f, 2.0f} 14 //}; 15 16 float domx[2] = { -100.0f, 100.0f}; 17 18 const int S = 100; // 细菌个数 19 float bacterium[S][X_DIM]; // 细菌 20 const int Nc = 50; // 趋化的次数 21 float bacterafitness[S][Nc]; // 适应度 22 const int Ns = 10; // 趋化操作中单向运动的最大步数4 23 const int Nre = 20; // 复制次数 24 const int Ned = 20; // 驱散次数 25 const float Ped = 0.25f; // 驱散概率 26 const float Ci = 1.0f; // 步长 27 28 float gbest; 29 float gx[X_DIM]; 30 31 32 33 const float d_at = 0.05f; // 吸引剂的数量 34 const float w_at = 0.05f; // 吸引剂的释放速度 35 const float h_re = 0.05f; // 排斥剂的数量 36 const float w_re = 0.05f; // 排斥剂的释放速度 37 38 39 40 41 42 float get_y1( float x[X_DIM] ) 43 { 44 return 4 - ( x[0] * sin( 4 * PI * x[0] ) - x[1] * sin( 4 * PI * x[1] + PI + 1 ) ); 45 } 46 47 float get_y( float x[X_DIM] ) 48 { 49 register int i; 50 register float sum; 51 52 sum = 0.0f; 53 for ( i = 0; i < X_DIM; i ++ ) 54 { 55 sum -= x[i] * x[i]; 56 } 57 return sum; 58 } 59 60 float fitness( float y ) 61 { 62 return y; 63 } 64 65 float get_jcc( int idx ) 66 { 67 register int i, j; 68 register float a, allsum, sum, sum1, sum2; 69 70 71 allsum = 0.0f; 72 for ( i = 0; i < S; i ++ ) 73 { 74 sum = 0.0f; 75 for ( j = 0; j < X_DIM; j ++ ) 76 { 77 a = bacterium[i][j] - bacterium[idx][j]; 78 sum += a * a; 79 } 80 81 sum1 = -w_at * sum; 82 sum1 = -d_at * exp( sum1 ); 83 84 sum2 = -w_re * sum; 85 sum2 = h_re * exp( sum2 ); 86 87 allsum += sum1 + sum2; 88 } 89 90 91 return allsum; 92 } 93 94 95 96 97 98 99 100 void init_single_bacterium( float x[X_DIM] ) 101 { 102 register int i; 103 104 for ( i = 0; i < X_DIM; i ++ ) 105 { 106 x[i] = domx[0] + RND * ( domx[1] - domx[0] ); 107 } 108 } 109 110 111 112 void init_bacterium() 113 { 114 register int i, j; 115 116 for ( i = 0; i < S; i ++ ) 117 { 118 for ( j = 0; j < X_DIM; j ++ ) 119 { 120 bacterium[i][j] = domx[0] + RND * ( domx[1] - domx[0] ); 121 } 122 } 123 } 124 125 void get_delta( float delta[X_DIM] ) 126 { 127 register int i; 128 register float tmp; 129 130 tmp = 0.0f; 131 for ( i = 0; i < X_DIM; i ++ ) 132 { 133 delta[i] = ( RND - 0.5f ) * 2; 134 135 tmp += delta[i] * delta[i]; 136 } 137 138 tmp = sqrt( tmp ); 139 140 for ( i = 0; i < X_DIM; i ++ ) 141 { 142 delta[i] /= tmp; 143 } 144 } 145 146 147 148 149 150 151 152 int main() 153 { 154 register int i, j, k, l, m, n; 155 float f, f1, y, flast, tmpfit; 156 float delta[X_DIM]; 157 float tmpbactera[X_DIM]; 158 float bfsum[S]; 159 int Sr; 160 161 162 srand( ( unsigned int )time( NULL ) ); 163 164 gbest = -10000000000.0f; 165 Sr = S / 2; 166 init_bacterium(); 167 168 169 for ( l = 0; l < Ned; l ++ ) 170 { 171 for ( k = 0; k < Nre; k ++ ) 172 { 173 for ( j = 0; j < Nc; j ++ ) 174 { 175 for ( i = 0; i < S; i ++ ) 176 { 177 y = get_y( bacterium[i] ); 178 179 if ( y > gbest ) 180 { 181 gbest = y; 182 memcpy( gx, bacterium[i], sizeof( gx ) ); 183 } 184 185 f = fitness( y ); 186 f += get_jcc( i ); 187 188 flast = f; 189 190 get_delta( delta ); 191 192 for ( n = 0; n < X_DIM; n ++ ) 193 { 194 tmpbactera[n] = Ci * delta[n] + bacterium[i][n]; 195 } 196 197 for ( m = 0; m < Ns; m ++ ) 198 { 199 f1 = fitness( get_y( tmpbactera ) ); 200 if ( f1 > flast ) 201 { 202 flast = f1; 203 for ( n = 0; n < X_DIM; n ++ ) 204 { 205 tmpbactera[n] += Ci * delta[n]; 206 } 207 } 208 else 209 { 210 break; 211 } 212 } 213 214 memcpy( bacterium[i], tmpbactera, sizeof( bacterium[0] ) ); 215 bacterafitness[i][j] = flast; 216 } 217 218 219 printf( "[%02d,%02d,%02d]\tgbest=%f\t(%f,%f)\n", l, k, j, gbest, gx[0], gx[1] ); 220 } 221 222 223 for ( i = 0; i < S; i ++ ) 224 { 225 bfsum[i] = 0.0f; 226 for ( j = 0; j < Nc; j ++ ) 227 { 228 bfsum[i] += bacterafitness[i][j]; 229 } 230 } 231 232 for ( n = 0; n < Sr; n ++ ) 233 { 234 i = n; 235 tmpfit = bfsum[n]; 236 for ( j = n + 1; j < S; j ++ ) 237 { 238 if ( bfsum[j] > tmpfit ) 239 { 240 tmpfit = bfsum[j]; 241 i = j; 242 } 243 } 244 245 if ( i != n ) 246 { 247 memcpy( tmpbactera, bacterium[n], sizeof( tmpbactera ) ); 248 memcpy( bacterium[n], bacterium[i], sizeof( bacterium[0] ) ); 249 memcpy( bacterium[i], tmpbactera, sizeof( bacterium[0] ) ); 250 } 251 } 252 253 for ( i = 0; i < Sr; i ++ ) 254 { 255 memcpy( bacterium[Sr + i], bacterium[i], sizeof( bacterium[0] ) ); 256 } 257 } 258 259 for ( i = 0; i < S; i ++ ) 260 { 261 if ( RND < Ped ) 262 { 263 init_single_bacterium( bacterium[i] ); 264 } 265 } 266 } 267 268 return 0; 269 }