testpso.cpp
Go to the documentation of this file.
00001 
00022 #include <stdio.h>
00023 #include <time.h>  
00024 #include <string>
00025 #include <fstream>  
00026 #include <iostream>
00027 #include "pso/pso.h"
00028 
00029 using namespace std;
00030 using namespace pso;
00031 
00032 int main()
00033 {
00034     Particle*p[40];  
00035     float w;
00036   
00037     Particle*temp;  
00038     float randX,randY;  
00039     srand((int)time(NULL));  
00040     for(int i=0;i<40;++i)  
00041     {
00042         randX=(rand()/(float)RAND_MAX)*30.0f;  
00043         randY=(rand()/(float)RAND_MAX)*30.0f;  
00044         cout<<"randX="<<randX<<endl;  
00045         cout<<"randY="<<randY<<endl;  
00046   
00047         p[i]=new Particle(randX,randY);  
00048         cout<<"The temp info is X:"<<p[i]->getX()<<" Y:"<<p[i]->getY()<<endl;  
00049       
00050     }  
00051 
00052     Coordinate gBest;
00053     int bestIndex=0;  
00054     float bestP;
00055     bestP=p[0]->getP();  
00056     gBest=p[0]->getPBest();  
00057     for(int i=1;i<40;++i)  
00058     {  
00059         if(p[i]->getP()<bestP)  
00060         {  
00061             bestP=p[i]->getP();  
00062             gBest=p[i]->getPBest();  
00063             bestIndex=i;  
00064         }  
00065     }  
00066 
00067     cout<<"Now the initial gBest is X:"<<gBest.x<<" Y:"<<gBest.y<<endl;  
00068     cout<<"And the p[0] is X:"<<p[0]->getX()<<" Y:"<<p[0]->getY()<<endl;  
00069     cout<<"And the p[39] is X:"<<p[39]->getX()<<" Y:"<<p[39]->getY()<<endl;  
00070     cout<<"Now p[0].p="<<p[0]->getP()<<endl;
00071 
00072     for(int k=0;k<100;++k)
00073     {
00074         w=0.9f-(0.9f-0.4f)*k/99.0f;
00075 
00076         for(int i=0;i<40;++i)  
00077         {  
00078             temp=p[i];  
00079             temp->setV(gBest,w);  
00080             temp->setCoordinate();  
00081             temp->setP();
00082         }  
00083         bestP=p[0]->getP();  
00084         gBest=p[0]->getPBest();  
00085         for(int i=1;i<40;++i)  
00086         {  
00087             temp=p[i];  
00088             if(temp->getP()<bestP)  
00089             {  
00090                 bestP=temp->getP();  
00091                 gBest=temp->getPBest();  
00092                 bestIndex=i;  
00093             }
00094         }  
00095         cout<<"Now gBest is X:"<<gBest.x<<" Y:"<<gBest.y<<" and the minP="<<p[bestIndex]->getP()<<endl;  
00096         cout<<"bestIndex="<<bestIndex<<endl;  
00097     }
00098   
00099     getchar();
00100     return 0;
00101 }


pso
Author(s):
autogenerated on Thu Jun 6 2019 18:52:34