testpso.cpp
Go to the documentation of this file.
1 
22 #include <stdio.h>
23 #include <time.h>
24 #include <string>
25 #include <fstream>
26 #include <iostream>
27 #include "pso/pso.h"
28 
29 using namespace std;
30 using namespace pso;
31 
32 int main()
33 {
34  Particle*p[40];
35  float w;
36 
37  Particle*temp;
38  float randX,randY;
39  srand((int)time(NULL));
40  for(int i=0;i<40;++i)
41  {
42  randX=(rand()/(float)RAND_MAX)*30.0f;
43  randY=(rand()/(float)RAND_MAX)*30.0f;
44  cout<<"randX="<<randX<<endl;
45  cout<<"randY="<<randY<<endl;
46 
47  p[i]=new Particle(randX,randY);
48  cout<<"The temp info is X:"<<p[i]->getX()<<" Y:"<<p[i]->getY()<<endl;
49 
50  }
51 
52  Coordinate gBest;
53  int bestIndex=0;
54  float bestP;
55  bestP=p[0]->getP();
56  gBest=p[0]->getPBest();
57  for(int i=1;i<40;++i)
58  {
59  if(p[i]->getP()<bestP)
60  {
61  bestP=p[i]->getP();
62  gBest=p[i]->getPBest();
63  bestIndex=i;
64  }
65  }
66 
67  cout<<"Now the initial gBest is X:"<<gBest.x<<" Y:"<<gBest.y<<endl;
68  cout<<"And the p[0] is X:"<<p[0]->getX()<<" Y:"<<p[0]->getY()<<endl;
69  cout<<"And the p[39] is X:"<<p[39]->getX()<<" Y:"<<p[39]->getY()<<endl;
70  cout<<"Now p[0].p="<<p[0]->getP()<<endl;
71 
72  for(int k=0;k<100;++k)
73  {
74  w=0.9f-(0.9f-0.4f)*k/99.0f;
75 
76  for(int i=0;i<40;++i)
77  {
78  temp=p[i];
79  temp->setV(gBest,w);
80  temp->setCoordinate();
81  temp->setP();
82  }
83  bestP=p[0]->getP();
84  gBest=p[0]->getPBest();
85  for(int i=1;i<40;++i)
86  {
87  temp=p[i];
88  if(temp->getP()<bestP)
89  {
90  bestP=temp->getP();
91  gBest=temp->getPBest();
92  bestIndex=i;
93  }
94  }
95  cout<<"Now gBest is X:"<<gBest.x<<" Y:"<<gBest.y<<" and the minP="<<p[bestIndex]->getP()<<endl;
96  cout<<"bestIndex="<<bestIndex<<endl;
97  }
98 
99  getchar();
100  return 0;
101 }
void setCoordinate()
Definition: pso.h:139
Definition: pso.h:41
f
void setV(Coordinate gBest, float w)
Definition: pso.h:112
float getP() const
Definition: pso.h:102
Coordinate getPBest() const
Definition: pso.h:107
float y
Definition: pso.h:46
float x
Definition: pso.h:45
void setP()
Definition: pso.h:90
float getY() const
Definition: pso.h:158
int main()
Definition: testpso.cpp:32
float getX() const
Definition: pso.h:153


pso
Author(s):
autogenerated on Mon Jun 10 2019 14:02:26