int main(int argc, char *argv[]) { srand((unsigned) time(NULL)); particle_init(); pso(); printf("gbest: %f\n", gbest); for (int i = 0; i < 10; i++) { printf("rand%d: %f\n", i, rand0_1()); } printf("rand: %f\n", rand0_1()); printf("rand: %f\n", rand0_1()); return 0; }
void MainWindow::on_FPS_clicked() { //initial graph graphs gr; gr.setModal(true); int k = 0; while(k < COUNT){ TotalFitness = 0; population.clear(); newpopulation.clear(); generatePopulations(false); //sort by fitness sort(this->population.begin(), this->population.end(), by_fitness()); int j = 0; while(j < COUNT){ calc_prob_ranges(false); //offsprings for(int i=0; i<((COUNT/2)/2); i++){ float val = randomRange(0, 1); temp = generateOffspring(val); offsprint_x1 = temp.x; offsprint_y1 = temp.y; val = randomRange(0, 1); temp = generateOffspring(val); offsprint_x2 = temp.x; offsprint_y2 = temp.y; //mutation process mutation(offsprint_x1, offsprint_y2, offsprint_x2, offsprint_y1, false); } //merge new with old population population.insert(population.end(), newpopulation.begin(), newpopulation.end()); //clear new population newpopulation.clear(); //sort by compute sort(population.begin(), population.end(), by_fitness()); // erase unecessary elements/population: population.erase(population.begin()+COUNT,population.end()); //cout << "population for new iteration" << endl; TotalFitness = 0; for(int i=0; i < population.size(); i++ ){ TotalFitness = TotalFitness + population[i].fitness; //cout << population[i].x << "," << population[i].y << "," << population[i].fitness << endl; } //best of iteration best.push_back(population[0].fitness); //cout << "fps best" << population[0].fitness << endl; //avg of iteration avg.push_back(population[(COUNT/2)-1].fitness); j++; } k++; TotalFitness = 0; pso(); ais(); gr.data.push_back({best, avg, pso_best, ais_best, ais_avg,COUNT}); reverse(pso_best.begin(), pso_best.end()); reverse(ais_best.begin(), ais_best.end()); reverse(ais_avg.begin(), ais_avg.end()); reverse(best.begin(), best.end()); reverse(avg.begin(), avg.end()); best_best.push_back(best[0]); avg_avg.push_back(avg[0]); ais_avg_avg.push_back(ais_avg[0]); pso_bb.push_back(pso_best[0]); pso_best.clear(); ais_bb.push_back(ais_best[0]); ais_best.clear(); ais_avg.clear(); best.clear(); avg.clear(); } gr.data.push_back({best_best,avg_avg, pso_bb, ais_bb, ais_avg_avg,COUNT}); best_best.clear(); avg_avg.clear(); ais_bb.clear(); pso_bb.clear(); ais_avg_avg.clear(); // for(int i=0; i < COUNT; i++ ){ // gr.best.push_back({best[i].fitness}); // gr.avg.push_back({avg[i].fitness}); // } // cout << gr.best.size() << endl; // gr.generation = COUNT; gr.makePlot(0); gr.exec(); }
int main(int argc, char **argv) { int numParticles = 1024; int maxIters = 50; int numThreads = 32; float eps = 10^-6; if (checkCmdLineFlag(argc,(const char **)argv,"help")) { helper(); exit(0); } if (checkCmdLineFlag(argc, (const char **) argv, "n")) { numParticles = getCmdLineArgumentFloat(argc, (const char **)argv, "n"); } if (checkCmdLineFlag(argc, (const char **) argv, "m")) { maxIters = getCmdLineArgumentFloat(argc, (const char **)argv, "m"); } if (checkCmdLineFlag(argc, (const char **) argv, "threads")) { numThreads = getCmdLineArgumentFloat(argc, (const char **)argv, "threads"); } std::cout<<"PSO Algorithm: "<<" n= "<<numParticles<<", m= "<<maxIters<<", threads= "<<numThreads<<std::endl; PSO pso(numParticles); float cpu_time = pso.Solve(maxIters, eps); std::cout<<"CPU result: "<<std::endl; std::cout<<"a: "<<pso.gBest.x<<" b: "<<pso.gBest.y<<" iters: "<<pso.iters<<" time: "<<cpu_time<<"ms"<<std::endl; CudaPSO cuda_pso(numParticles); float cuda_time = cuda_pso.Solve(maxIters, numThreads, eps); std::cout<<"GPU result: "<<std::endl; std::cout<<" a: "<<cuda_pso.gBest.x<<" b: "<<cuda_pso.gBest.y<<" iters: "<<cuda_pso.iters<<" time: "<<cuda_time<<"ms"<<std::endl; std::cout<<std::endl<<"GPU perf./CPU perf. = "<<cpu_time/cuda_time<<std::endl; time_t now = time(NULL); struct tm timeinfo = *localtime(&now); char name[50]; strftime(name, sizeof(name),"%Y%m%d%H%M%S", &timeinfo); std::string filename = std::string(name)+std::string(".log"); std::ofstream fout(filename.c_str(), std::ios::app); fout<<"CPU RME\t\t"<<"GPU RME"<<std::endl; for (int i=0; i<std::min(pso.iters, cuda_pso.iters); i++) { fout<<pso.RME[i]<<"\t\t"<<cuda_pso.RME[i]<<std::endl; } fout.close(); std::cout<<std::endl<<"RMEs have been written into ./"<<filename<<std::endl; //uncomment if want to output RMEs to terminal /* std::cout<<std::endl; std::cout<<"CPU RME:"<<std::endl; for (int i=0; i<pso.iters; i++){ std::cout<<pso.RME[i]<<"\t"; } std::cout<<std::endl; std::cout<<"GPU RME:"<<std::endl; for (int i=0; i<cuda_pso.iters; i++){ std::cout<<cuda_pso.RME[i]<<"\t"; } */ return 0; }
void operator()(packed_oarchive& ar, const object& obj, const unsigned int) { packed_skeleton_oarchive pso(ar); pso << extract<T&>(obj.attr("object"))(); }
d_t_fit_min mpc_pso(d_t_in2 dx1_in,d_t_in1 dx2_in,d_t_in1 y_in,d_t_in1 u_in,d_t_in1 ref_in) { d_t_fit_min duu; ap_fixed<30,6,AP_RND_CONV,AP_SAT> Sx[20][2]={0.999999887494521, 0.000980908441683, 1.999999443182048, 0.002905510919606, 2.999998456081107, 0.005738004675732, 3.999996723214666, 0.009443945051661, 4.999994049306499, 0.013990193972083, 5.999990246489080, 0.019344870382393, 6.999985134022543, 0.025477302566354, 7.999978538024314, 0.032357982272474, 8.999970291208991, 0.039958520580513, 9.999960232638099, 0.048251605442091, 10.999948207479330, 0.057210960831894, 11.999934066774919, 0.066811307448406, 12.999917667218810, 0.077028324905356, 13.999898870942264, 0.087838615357362, 14.999877545307610, 0.099219668505353, 15.999853562709818, 0.111149827929432, 16.999826800385591, 0.123608258698829, 17.999797140229713, 0.136574916210506, 18.999764468618345, 0.150030516209793, 19.999728676239037, 0.163956505948230}; ap_fixed<30,8,AP_RND_CONV,AP_SAT> g[2][20]={ 0.295173465193610, 1.16571436196475, 2.58979392352947, 4.54641140982271, 7.01536269810910, 9.97721006504063, 13.4132531149660, 17.3055008110112, 21.6366445671002, 26.3900323606712, 31.5496438273702, 37.1000663004737, 43.0264717592035, 49.3145946514563, 55.9507105577808, 62.9216156646895, 70.2146070166061, 77.8174635169120, 85.7184276496769, 93.9061878947356, 0, 0.295173465193610, 1.71925302675833, 3.67587051305157, 6.14482180133796, 9.10666916826949, 12.5427122181949, 16.4349599142401, 20.7661036703291, 25.5194914639001, 30.6791029305990, 36.2295254037026, 42.1559308624323, 48.4440537546851, 55.0801696610096, 62.0510747679183, 69.3440661198349, 76.9469226201408, 84.8478867529057, 93.0356469979644}; //double g[2][20]; int i; int j; ap_fixed<20,2,AP_RND_CONV,AP_SAT> Umax=0.8; ap_fixed<20,2,AP_RND_CONV,AP_SAT> Umin=-0.8; d_t_state SxDx[20]; d_t_b b[4]; d_t_G G[2]; d_t_state Ep[20]; // double UU[2]={0.0,0.0}; b[0]=u_in - Umax; b[1]=u_in - Umax; b[2]=Umin-u_in ; b[3]=Umin-u_in ; //printf("b[0]=%lf\n",b[0].to_double()); //printf("b[2]=%lf\n",b[2].to_double()); mpc_pso_label3:for( i=0;i<20;i++) { SxDx[i]=Sx[i][0]*dx1_in+Sx[i][1]*dx2_in; //printf("SxDx[%d]=%lf\n",i,SxDx[i].to_double()); } mpc_pso_label11:for( i=0;i<20;i++) { Ep[i]=ref_in-SxDx[i]-y_in; } for(i=0;i<2;i++) { d_t_state sum=0.0; for(j=0;j<20;j++) sum+=g[i][j]*Ep[j]; //G[i]=10000000000*sum; G[i]=sum; //printf("G[%d]=%lf\n",i,G[i].to_double()); } duu=pso(G,b); //write_result_aa(duu.to_double()); //duu=duu+u_in; //printf("duu=%lf\n",duu.to_double()); return duu; }