⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ecga.cpp

📁 这是遗传算法的源代码
💻 CPP
字号:
// -*- c++ -*-////  File:         ecga.cpp////  Description:  C++ implementation of the class ecga.//                Contains the ECGA loop.////  Author:       Fernando Lobo////  Date:         June/1999////  Extended to deal with chi-ary problems by Luis de la Ossa//  GCC 3.4 and 4 series compliance by Kumara Sastry ////  Date:         March/2006#include <iostream>#include <iomanip>#include <fstream>#include "ecga.hpp"#include "parameter.hpp"#include "population.hpp"#include "mpm.hpp"#include "utility.hpp"extern randomG RANDOM;//----------------------------------------------------------void ecga::report( std::ofstream &outfile, population *pop, int gen ){  //  // get information about the best individual in the population  //  chromosome bestChrom;  bestChrom = (*pop)[ pop->best() ];  //  // print it  //  std::cout << "generation      : " << gen << std::endl	    << "best fitness    : " << pop->maxfit() << std::endl	    << "avg fitness     : " << pop->avgfit() << std::endl	    << "best chromosome : " << bestChrom << std::endl	    << "------------------------------------------------------------"	    << std::endl;  outfile << "Generation " << gen << std::endl;  outfile << *pop;}bool ecga::done( population *pop, int gen ){  switch( parameter::stop_criteria )   {     case ALLELE_CONVERGENCE : return sc_allele_conv( *pop, gen );     case MAX_GENERATIONS    : return sc_maxgen( *pop, gen );     default:         error("This should never happen.");   }}//// runs the ECGA and sends output information to the 'outfile'.//void ecga::run( std::ofstream &outfile ){  int gen = 0;  //  // first population with random individuals  //  population *pop = new population( parameter::popsize );         pop->random();    pop->evaluate();  pop->statistics();     report( outfile, pop, gen );  while( !done( pop, gen ) ) {     gen++;    //    // apply selection    //    population *temp_pop = new population( pop->popsize() );    pop->selection( temp_pop );          delete pop;     // delete unnecessary temporary    pop = temp_pop;        //    // model the population with a greedy MPM search    //    mpm MPM( parameter::lchrom );       MPM.model( pop, outfile );      if( parameter::report_MPM )       std::cout << "MPM: " << MPM << std::endl;        //    // generate a new population using the MPM    //    temp_pop = new population( pop->popsize() );     MPM.generate( pop, temp_pop );      delete pop;    pop = temp_pop;        //    // evaluate individuals and do the report    //    pop->evaluate();                      pop->statistics();                    report( outfile, pop, gen );  }    //  // cleanup  //  delete pop;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -