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

📄 citizen.h

📁 关于遗传算法的c++程序,本文采用了实数编码
💻 H
📖 第 1 页 / 共 2 页
字号:
   if (flag) cout << "Starting creep mutation" << endl;   if (ran1(iseed) < CmutRate) {      start_bit = inp*nb;      int bit = int((nb-1)*ran1(iseed)+1);      if (bit == 1) switch_bit = 2;      if (bit == (nb-1)) switch_bit = nb-2;      if ((bit != 1) && (bit != (nb-1))) {          if (ran1(iseed) > 0.5) switch_bit = bit+1;         else switch_bit = bit-1;      }      if (flag) cout << "Mutating parameter " << inp;      if (flag) cout << ", bit number is " << bit;      if (flag) cout << ", switch_bit number is " << switch_bit << endl;      bit += start_bit;      switch_bit += start_bit;      temp_bit = paramsB[bit];      paramsB[bit] = paramsB[switch_bit];      paramsB[switch_bit] = temp_bit;   }}////  The following routines are very self explanitory.  They each return //  a single thing, or preform a single, simple operation.////    1) numParams      - Returns the number of parameters to be fit.//    2) numBits        - Returns the number of bits per parameter.//    3) bitVal         - Returns the value of a single bit place, either 0 or 1.//    4) bitAdd         - Assigns a specific place a bit value.//    5) paramVal       - Returns the value of a single parameter.//    6) paramAdd       - Assigns the value of a single parameter.//    7) op =           - Assignment for a Citizen.//    8) checkRange     - Checks to see whether the parameter is in the range.//    9) fit            - Returns the fitness for a single citizen//   10) f1,f2,f3,f4,f5 - Test functions with parameters to be fit.//int Citizen::numParams() {   return paramsF.numElts();}int Citizen::numBits() {   int np = paramsF.numElts();   int npnb = paramsB.numElts();   return npnb/np;}int Citizen::bitVal(int bit) {   return paramsB[bit];}void Citizen::bitAdd(int bit,int bitval) {   int flag = 0;   if (flag) cout << "In bitAdd" << endl;   if (flag) cout << "bit is " << bit << endl;   if (flag) cout << "bitval is " << bitval << endl;   if (flag) cout << "paramsB[bit] = " << paramsB[bit] << endl;   paramsB[bit] = bitval;   if (flag) cout << "Leaving bitAdd" << endl;}Real Citizen::paramVal(int i) {   return paramsF[i];}void Citizen::paramAdd(int inp,Real param) {   int flag = 0;   if (flag) cout << "In paramAdd" << endl;   if (flag) cout << "parameter number is " << inp << endl;   if (flag) cout << "parameter is " << param << endl;   if (flag) cout << "paramsF[inp] = " << paramsF[inp] << endl;   paramsF[inp] = param;   if (flag) cout << "Leaving paramAdd" << endl;}Citizen& Citizen::operator=(Citizen *rhs) {   int i;   int np = numParams();   int nb = numBits();   cout << "Number of parameters is " << np << endl;   cout << "Number of bits is " << nb << endl;   for (i = 0; i < np;i++)       this-> paramsF[i] = rhs-> paramsF[i];   for (i = 0; i < np*nb;i++)       this-> paramsB[i] = rhs-> paramsB[i];   return *this; }int Citizen::checkRange(int inp, Real param, Ranges r) {////  This routine checks to see whether or not a decoded parameter is in the//  proper range.  Parameters often are created that are not in range, by //  illeagal combination of bits.  This is a boolean function, and returns 1 //  if the parameter is in range, and 0 if it is not in range.//   int flag = 0;   Real lr = r.lowRange(inp);   Real hr = r.highRange(inp);   if ((param >= lr) && (param <= hr)) return 1;   else {      if (flag) cout << "------------------------" << endl;      if (flag) cout << "Not in range" << endl;      if (flag) cout << "param is     " << param << endl;      if (flag) cout << "param num is " << inp   << endl;      if (flag) cout << "lowR  is     " << lr    << endl;      if (flag) cout << "highR is     " << hr    << endl;      if (flag) cout << "------------------------" << endl;      return 0;   }}/////////////////////////////////////////////////////////////////////////////////                           Fitting Functions                            /////////////////////////////////////////////////////////////////////////////////Real Citizen::fit() {////  This is the fitting function.  It returns a number to be minimized.//  This is the routine that needs to be provided by the user.  It a user//  wanted to use the genetic algorithm, all he or she would have to do is//  have this function return a number to be minimized.//   int fflag = 1;   if (fflag == 1) return f1();   if (fflag == 2) return f2();   if (fflag == 3) return f3();   if (fflag == 4) return f4();   if (fflag == 5) return f5();}Real Citizen::f1() {////  This function sums the first half of the parameter set with the //  reciprecals of the last half of the parameter set.  Hence, the first //  half of the parameters will tend toward their lower bounds, and the //  last half will tend towards their upper bounds, assuming that the //  ranges given for the parameters are positive.  If the number of //  parameters is even, then the minimum will tend toward Nparam/2.0,//  assuming that the ranges are all (0,1).////                                 1     1     1     1//  f1() =   x0 + x1 + x2 + x3 +  --- + --- + --- + ---//                                 x4    x5    x6    x7////   int i;   int np = numParams();   int np_2 = np/2;   Real xx = 0.0;   for(i = 0;i < np_2;i++)      xx += paramVal(i);   for(i = np_2;i < np;i++)      xx += 1.0/paramVal(i);   return xx;}Real Citizen::f2() {////  This function sums the reciprecals of all of the parameters in the set.//  All parameters will tend towards their upper bounds, assuming that the //  ranges given are positive.  For N = 8 parameters, with ranges (0,1), //  (0,2), ... (0,8), the lower bound is 2.7178571.  As N -> infinity, the //  lower bound tends toward 'e' which is 2.7182818 ... ////            1     1     1     1     1     1     1     1//  f2() =   --- + --- + --- + --- + --- + --- + --- + ---//            x0    x1    x2    x3    x4    x5    x6    x7//   int np = numParams();   Real xx = 0.0;   for(int i = 0;i < np;i++)      xx += 1.0/paramVal(i);   return xx;}Real Citizen::f3() {////  The following function has the property that the parameters should //  converge to x00,x01,x02,x03,and x04.  In order to minimize the function, //  f4() will tend toward zero as the parameters tend toward //  x01,x02,x03,x04 and x05 since it is always positive.////               2         2         2         2         2//  f3() = (x0-1)  + (x1-2)  + (x2-3)  + (x3-4)  + (x4-5)//   int np = numParams();   Real x01 = 1.0;   Real x02 = 2.0;   Real x03 = 3.0;   Real x04 = 4.0;   Real x05 = 5.0;   Real rx1 = (x01 - paramVal(0))*(x01 - paramVal(0));   Real rx2 = (x02 - paramVal(1))*(x02 - paramVal(1));   Real rx3 = (x03 - paramVal(2))*(x03 - paramVal(2));   Real rx4 = (x04 - paramVal(3))*(x04 - paramVal(3));   Real rx5 = (x05 - paramVal(4))*(x05 - paramVal(4));   Real xx = rx1 + rx2 + rx3 + rx4 + rx5;   return xx;}Real Citizen::f4() {////  The following function is a root finding function.  It has the property //  that the parameters should converge to ONE (not all) of the numbers  //  x00,x01,x02,x03,and x04.  This could be done successively to find the //  other roots.////                2          2          2          2          2//  f4() = (x0-10)  * (x1-20)  * (x2-30)  * (x3-40)  * (x4-50)//   int np = numParams();   Real x01 = 10.0;   Real x02 = 20.0;   Real x03 = 30.0;   Real x04 = 40.0;   Real x05 = 50.0;   Real rx1 = (x01 - paramVal(0))*(x01 - paramVal(0));   Real rx2 = (x02 - paramVal(1))*(x02 - paramVal(1));   Real rx3 = (x03 - paramVal(2))*(x03 - paramVal(2));   Real rx4 = (x04 - paramVal(3))*(x04 - paramVal(3));   Real rx5 = (x05 - paramVal(4))*(x05 - paramVal(4));   Real xx = rx1 * rx2 * rx3 * rx4 * rx5;   return xx;}Real Citizen::f5() {////  The following function is a root finding function.  It has the property that //  the parameters should converge to ONE (not all) of the numbers  x00,x01,x02,//  x03,and x04.  The parameter 50 can be excluded from being found by adding //  the last term on to the function.  We did this because 50 was found by f4() //  above, and we would like to find another root or roots.////                2          2          2          2          2        1//  f4() = (x0-10)  * (x1-20)  * (x2-30)  * (x3-40)  * (x4-50)   +   ----- 2//                                                                  (x4-50)   int np = numParams();   Real x01 = 10.0;   Real x02 = 20.0;   Real x03 = 30.0;   Real x04 = 40.0;   Real x05 = 50.0;   Real rx1 = (x01 - paramVal(0))*(x01 - paramVal(0));   Real rx2 = (x02 - paramVal(1))*(x02 - paramVal(1));   Real rx3 = (x03 - paramVal(2))*(x03 - paramVal(2));   Real rx4 = (x04 - paramVal(3))*(x04 - paramVal(3));   Real rx5 = (x05 - paramVal(4))*(x05 - paramVal(4));   Real xx = rx1 * rx2 * rx3 * rx4 * rx5;   Real yy = 1.0/(paramVal(4) - 50.0);   xx = xx + yy*yy;   return xx;}

⌨️ 快捷键说明

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