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

📄 ga.cc

📁 关于遗传算法的c++程序,本文采用了实数编码
💻 CC
字号:
#include <iostream.h>#include <stdlib.h>#include <iomanip.h>typedef float Real;extern Real ran1(int*);extern Real ran1(int newseed);extern Real ran1();extern Real ran1(int* idum);#include "Ranges.h"#include "Citizen.h"#include "Population.h"////////////////////////////////////////////////////////////////////////////////                                 main                                  ////////////////////////////////////////////////////////////////////////////////int main(){////  Input parameters//   int Nparams;       //  Number of parameters to be fit   int Nbits;         //  Number of bits per parameter   int Npop;          //  Number of people in the population   int Ngen;          //  Number of generations to calculate   int iseed;         //  Random number seed   int flag;          //  Information flag   int eflag;         //  Elitism flag   Real JmutRate;     //  Jump mutation rate   Real CmutRate;     //  Creep mutation rate////  Other variables://    flag = 0 is for no information printed out.//    flag = 1 is for graphing a particular run: //             best fit vs generation number.//    flag = 2 is for graphing a particular run: //             average fit vs generation number.//    flag = 3 is for detailed info on the run.//    flag = 4 is for detailed info on the run, //             including best fit for each generation.//    flag = 5 is for detailed info on the run, //             including average fit for each generation.//    flag = 6 is for detailed info on the run, //             including convergence fraction of the bits.////    eflag is the elitism flag, and it enables the elitism option.//    igen is the current generation number.//    best is the fit fit of the current population.//    avrg is the average fit of the current population.//    ranges is the list of parameter ranges, complete with the //           calculated highest necessary power of two.//    p1 is the population.  More than one population could //           be run simultaniously if need be.//   int igen = 0;   Real best,avrg,cvrg;   Ranges ranges;   Population p1;   Citizen c1;////  Initialization procedures://     1) Initialize the parameters for the run, and the parameter ranges.//     2) Initialize the population.//     3) Print out information.//   void init(int*,int*,int*,int*,int*,int*,int*,Real*,Real*,Ranges*);   init(&Nparams,&Nbits,&Npop,&Ngen,&iseed,&flag,&eflag,        &JmutRate,&CmutRate,&ranges);   p1.initP(ranges,&iseed,Npop,Nparams,Nbits);   best = p1.bestF();   avrg = p1.averageF();   cvrg = p1.bitFrac();   if (flag == 1) cout << igen << " " << best << endl;   if (flag == 2) cout << igen << " " << avrg << endl;   if (flag == 4) cout << "Generation " << igen << " has best fit of "                        << best << endl;   if (flag == 5) cout << "Generation " << igen << " has average fit of "                        << avrg << endl;   if (flag == 6) cout << "Generation " << igen << " has convergence of "                        << cvrg << endl;////  Main loop.//   do {      igen++;      p1.mateParents(&iseed,ranges,JmutRate,CmutRate);      if (eflag) p1.elitism(&iseed);      p1.transferChildren(ranges);      p1.fitParents();      best = p1.bestF();      avrg = p1.averageF();      cvrg = p1.bitFrac();      if (flag == 1) cout << igen << " " << best << endl;      if (flag == 2) cout << igen << " " << avrg << endl;      if (flag == 4) cout << "Generation " << igen << " has best fit of "                           << best << endl;      if (flag == 5) cout << "Generation " << igen << " has average fit of "                           << avrg << endl;      if (flag == 6) cout << "Generation " << igen << " has convergence of "                           << cvrg << endl;   }  while (igen < Ngen);////  Print out information about the run//   if ((flag >= 3) && (flag <= 6))  {      int ibest = p1.bestI();      Real fbest = p1.bestF();      Citizen cbest;      cbest.setSize(Nparams,Nbits);      cbest = p1.cit(ibest);      cout << endl << "                     BEST FIT" << endl;      cout << "================================================" << endl;      cout << "The best Citizen in the Population is " << ibest << endl;      cout << "The best fit in the Population is " << fbest << endl << endl;      cout << "The parameter set of the best Citizen is below" << endl;      cout << "----------------------------------------------" << endl;      cbest.showC();      cout << "================================================" << endl;   }   return 0;}////////////////////////////////////////////////////////////////////////////////                                 init                                  ////////////////////////////////////////////////////////////////////////////////void init(int* Nparams,int* Nbits,int* Npop, int* Ngen, int* iseed, int* flag,          int* eflag, Real* JmutRate, Real* CmutRate,Ranges* ranges) {////  This routine initializes the parameters for the GA run.//  It takes all input from cin.  init_flag = 1 turns on the debugging info.//   int init_flag = 0;////  Initialize the input parameters//   cin >> *Nparams;   cin >> *Nbits;   cin >> *Npop;   cin >> *Ngen;   cin >> *iseed;   cin >> *flag;   cin >> *eflag;   cin >> *JmutRate;   cin >> *CmutRate;////  Automatic setting of the mutation rates//   if ((*JmutRate) == -1.0) *JmutRate = 1.0/(*Npop);   if ((*CmutRate) == -1.0) *CmutRate = (*Nbits)*(*JmutRate);////  Write out the information//   if (init_flag) cout << "The number of parameters is " << *Nparams << endl;   if (init_flag) cout << "The number of bits in a gene is " << *Nbits << endl;   if (init_flag) cout << "The number of the population is " << *Npop << endl;   if (init_flag) cout << "The number of itereations is " << *Ngen << endl;   if (init_flag) cout << "The random number seed is " << *iseed << endl;   if (init_flag) cout << "The information flag is " << *flag << endl;   if (init_flag) cout << "The elitism flag is " << *eflag << endl;   if (init_flag) cout << "The jump mutation rate is " << *JmutRate << endl;;   if (init_flag) cout << "The creep mutation rate is " << *CmutRate << endl;;////  Initialize the ranges//   (*ranges).initR(*Nparams);   if (init_flag) (*ranges).showR();}////////////////////////////////////////////////////////////////////////////////                       random number generator                         //////////////////////////////////////////////////////////////////////////////////// ran1.cc -- adaptation of random number generator from Num. Rec. 1st edition//            for C++static int seed = -1237;Real ran1(int*);Real ran1(int newseed)    {    seed = newseed;    if(seed > 0) seed = -seed;    return ran1(&seed);    }Real ran1()    { return ran1(&seed); }Real ran1(int* idum){    const int M1 = 259200;    const int IA1 = 7141;    const int IC1 = 54773;    const Real RM1 = (1.0/M1);    const int M2 = 134456;    const int IA2 = 8121;    const int IC2 = 28411;    const Real RM2 = (1.0/M2);    const int M3 = 243000;    const int IA3 = 4561;    const int IC3 = 51349;    static int ix1,ix2,ix3;    static Real r[98];    Real temp;    static int iff=0;    int j;    void error(const char*);        if (*idum < 0 || iff == 0) {	iff=1;	ix1=(IC1-(*idum)) % M1;	ix1=(IA1*ix1+IC1) % M1;	ix2=ix1 % M2;	ix1=(IA1*ix1+IC1) % M1;	ix3=ix1 % M3;	for (j=1;j<=97;j++) {	    ix1=(IA1*ix1+IC1) % M1;	    ix2=(IA2*ix2+IC2) % M2;	    r[j]=(ix1+ix2*RM2)*RM1;	}	*idum=1;    }    ix1=(IA1*ix1+IC1) % M1;    ix2=(IA2*ix2+IC2) % M2;    ix3=(IA3*ix3+IC3) % M3;    j=1 + ((97*ix3)/M3);    if (j > 97 || j < 1) error("RAN1: This cannot happen.");    temp=r[j];    r[j]=(ix1+ix2*RM2)*RM1;    return temp;}////////////////////////////////////////////////////////////////////////////////                           error message                               ////////////////////////////////////////////////////////////////////////////////// error.cc -- Print out an error message and abort for debuggingvoid error(const char* s)    {    cerr << endl << s << endl;    cout << endl << s << endl;    cout.flush();    abort();    }

⌨️ 快捷键说明

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