Code:
#include<iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
#define limit 2 //varibale
#define num 200// number of particale
#define t 200 // number of steps
FILE *fout;
class particale
{
public:
double X[limit][t]; //possion LIMIT FOR VARIBALES T FOR NUMBER OF steps
double V[limit][t]; //velocity
double PPB[limit][t]; // particale personal best
double y_p_b[t]; //solution for ppb
};
class gbest
{
public:
int particale_num;
int step;
double X[limit];
double y_s_gb;
};
void rand1(double &y) //RANDOM number between 0-1 for the exp
{
y=rand()/float (RAND_MAX);
}
void rand(double &x) //jump between 5-5
{
x=(rand()/float(RAND_MAX))*10;
}
void y_soloution(double x1,double x2,double &y)
//solution for the equation.
{
double a,b,c,d,e,f,g,o,p,q,r,s,t1;
double dd,ee,ff,gg,oo,pp,qq,ss,rr,tt;
a =pow(( ( x1 - 5 ) / 0.94 ),2);
b =pow( ( (x2 - 5 ) / 0.94 ),2);
c = ( a + b );
if (c <= 50)
y= 50;
if (c>50)
y=c;
d =pow( ( ( x1 - 3 ) / 0.05 ),2);
e =pow( ( ( x2 - 3 ) / 0.05 ),2);
f =pow( ( ( x1 - 5 ) / 0.05 ),2);
g =pow( ( ( x2 - 5 ) / 0.05),2);
o =pow( ( ( x1 - 1 ) / 0.05 ),2);
p =pow( ( ( x2 - 1) / 0.05 ),2);
q = pow(( ( x1 - 7 ) / 0.05 ),2);
r = pow(( ( x2 - 7 ) / 0.05 ),2);
s = pow(( ( x1 - 9 ) / 0.05 ),2);
t1 =pow( ( ( x2 - 9 ) / 0.05 ),2);
dd =pow( ( ( x1 - 2 ) / 0.05 ),2);
ee =pow( ( ( x2 - 2 ) / 0.05 ),2);
ff =pow( ( ( x1 - 4 ) / 0.05 ),2);
gg =pow( ( ( x2 - 4 ) / 0.05),2);
oo =pow( ( ( x1 - 6 ) / 0.05 ),2);
pp =pow( ( ( x2 - 6) / 0.05 ),2);
qq = pow(( ( x1 - 8 ) / 0.05 ),2);
rr = pow(( ( x2 - 8 ) / 0.05 ),2);
ss = pow(( ( x1 - 10 ) / 0.05 ),2);
tt =pow( ( ( x2 - 10) / 0.05 ),2);
if( ( d + e+10 ) < 50)
y = d + e+10 ;
if( ( f + g+2 ) < 50)
y = f + g+2;
if( ( o + p+3 ) < 50 )
y = o + p+3;
if( ( q + r+4 ) < 50 )
y = q + r+4 ;
if( ( s + t1+5) < 50 )
y = s + t1+5;
if( ( dd + ee+6 ) < 50)
y = dd + ee+6 ;
if( ( ff + gg+7 ) < 50)
y = ff + gg+7;
if( ( oo + pp+8 ) < 50 )
y = oo + pp+8;
if( ( qq + rr +9) < 50 )
y = qq + rr+9 ;
if( ( ss + tt+10 ) < 50 )
y = ss + tt+10;
}
int main()
{
int i,j,p;
double y1,y_p_b,r1,r2;
particale a[num];
gbest g_b;
srand(time(0));
for (i=0;i<num;i++) // calibration first
{
rand(a[i].X[0][1]); //GET SEP 1 POSSION X1
rand(a[i].X[1][1]); //GET STEP 1 POSSION X2
rand(a[i].V[0][1]); // GET STEP 1 velocity X1
rand(a[i].V[1][1]); //GET STEP 1 velocity X2
a[i].PPB[0][0]=a[i].X[0][1]; //PPB STEP 0 X1
a[i].PPB[1][0]=a[i].X[1][1]; //PPB STEP 0 X2
g_b.particale_num=i; // global best
g_b.X[1]=a[i].PPB[1][0]; //x1 global best
g_b.X[0]=a[i].PPB[0][0]; //x2 global best
y_soloution(g_b.X[0],g_b.X[1],g_b.y_s_gb); //ysoloution global best
g_b.step=0; //number of step global best
for (i=1;i<t;i++) //main loop start a step 1 (step 0 before)
{
for (j=0;j<num;j++) //loop to check for every particle ppb gb ,num of dogs birds
{
y_soloution(a[j].PPB[0][i-1],a[j].PPB[1][i-1],a[j].y_p_b[i-1]); //SOLOUTION PPB STEP I
y_soloution(a[j].X[0][i],a[j].X[1][i],y1); //SOLOUTION X1 X2 STEP I+1
if (y1>a[j].y_p_b[i-1])
//if ppb for the previous step is lower E than the ppb for the next step will be him
{
a[j].PPB[0][i]=a[j].PPB[0][i-1];
a[j].PPB[1][i]=a[j].PPB[1][i-1];
a[j].y_p_b[i]=a[j].y_p_b[i-1];
}
else
/*if the x1 x2 of this step is lower in E than the ppb of the previous step than
the x1 x2 ==>y will be the step ppb.*/
{
a[j].PPB[0][i]=a[j].X[0][i];
a[j].PPB[1][i]=a[j].X[1][i];
a[j].y_p_b[i]=y1;
}
if (a[j].y_p_b[i]< g_b.y_s_gb) //changing global best if needed
{
g_b.particale_num=j; // global best
g_b.X[0]=a[j].PPB[0][i];
g_b.X[1]=a[j].PPB[1][i];
y_soloution(g_b.X[0],g_b.X[1],g_b.y_s_gb);
g_b.step=i;
}
}
for (j=0;j<num;j++)
/*number of dogs loop for changing the next step velocity
and after that the possion*/
{
for (p=0;p<limit;p++) //number of dimendional of every dog
{
rand1(r1);
rand1(r2);
a[j].V[p][i+1]=a[j].V[p][i]*0.65+2*r1*(a[j].PPB[p][i]-a[j].X[p][i])+2*r2*(g_b.X[p]-a[j].X[p][i]);
a[j].X[p][i+1]=a[j].X[p][i]+a[j].V[p][i+1];
}
}
cout<<"interact "<<t<<" particale number " <<g_b.particale_num<<" X1/2 POSSION " <<g_b.X[0]<<" "<< g_b.X[1]<<" Y= "<<g_b.y_s_gb<<" step "<<g_b.step<<endl;
}
cout<<"final"<<" particale number " <<g_b.particale_num<<" X1/2 POSSION " <<g_b.X[0]<<" "<< g_b.X[1]<<" Y= "<<g_b.y_s_gb<<" step "<<g_b.step<<endl;
for (i=0;i<num;i++) //write to screen
{
cout<<" particale number "<<i<<" personal best possion "<< a[i].PPB[0][t-1]<<" "<<a[i].PPB[1][t-1]<<endl;
}
}