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

📄 generatingrules.hxx

📁 改算法基于遗传算法
💻 HXX
字号:
// class definition for the OneMax Problems//                         Time-stamp: <2007-07-13 15:54:09 handa>//#include "chromoso.h"#include "random.h"#include <vector>#ifndef ONEMAX_HH#define ONEMAX_HHusing namespace std;template<class T> class generatingRules: public environment<T> {protected:  long numOfFitEvals;  int** data;  int* tch;  int dataSize;public:   int isNoFit() {return 1;  }    int noi() { return dataSize;}  int superior(vector<double> fit1, vector<double> fit2) {    if (fit1[0] > fit2[0]) { // This function is maximiized one.      return TRUE;    }    return FALSE;  }    int newGene(int dim, int pattern) {    if (this->rn.urand(1.0) < 0.5) {      return 2;    }    return data[pattern][dim];  }  int newDecedent(int pattern) {    return tch[pattern];  }  virtual void generateMatchingSet(list<chromosome<T> *>& pop, 				   list<chromosome<T> *>& matched, 				   int i)   {    list<chromoIntP>::iterator il;    matched.clear();    for(il = pop.begin(); il != pop.end(); il++) {      int size = (*il)->Size();       int flag = 1;      if ((*il)->isDecedent() != tch[i]) continue;      for(int j = 0; j < size; j++) {	int itmp = (*il)->getgene(j); 	if (itmp != 2 && itmp != data[i][j]) {	  flag = 0;	  break;	}      }      if (flag) {	matched.push_back(*il);      }    }  }  virtual double maxFitInMatchingSet(list<chromosome<T> *>& pop, 				     int i)   {    list<chromoIntP>::iterator il;    double max = 0.0;    for(il = pop.begin(); il != pop.end(); il++) {      int size = (*il)->Size();       int flag = 1;      if ((*il)->isDecedent() != tch[i]) continue;      for(int j = 0; j < size; j++) {	int itmp = (*il)->getgene(j); 	if (itmp != 2 && itmp != data[i][j]) {	  flag = 0;	  break;	}      }      if (flag) {	max = (max < (*il)->fitness(0)) ? (*il)->fitness(0) : max;       }    }    return max;  }  virtual double calc(int dummy, chromosome<int>& arg) {     // This function is single object function.    // In this case, first argument should be dummy.    int i,j;    int size = arg.Size();     double ret = 0.0;    int matched = 0;    int gotit = 0;    double specificity = 0.0;    double rDontCareBase = 0.001 / size;    for(j = 0; j < size; j++) {      if (arg.getgene(j) == 2) 	specificity += rDontCareBase;    }    for (i =0; i< dataSize;i++) {      int flag  = 1;      for(j = 0; j < size; j++) {	int itmp = arg.getgene(j); 	if (itmp != 2 && itmp != data[i][j]) {	  flag = 0;	  break;	}      }      if (flag) {	if (arg.isDecedent() == tch[i]) gotit++;	matched++;      }    }          if (matched == 0) return 0;    ret = specificity + gotit /(double) matched;    numOfFitEvals++;    return ret;  }  long isNumOfFitEvals() { return numOfFitEvals; }  generatingRules(random_number& arg1, int len, int nod, char * fname)    : environment<int>(len, arg1)  {    numOfFitEvals = 0;    FILE *fp;    int i,j;    dataSize = nod;    for (i =0; i< len; i++) {      this->lb[i] =0;      this->ub[i] =0;    }    this->lbd = 0;    this->ubd = 0;    data = new int*[nod];    for (i =0; i< nod; i++) {      data[i] = new int[len];    }    tch = new int[nod];    fp = fopen(fname, "r");    if (fp == NULL) {      cout << "file is not open " << fname << endl;      exit(0);    }    for (i =0; i< nod;i++) {      for (j =0; j< len; j++) {	fscanf(fp, "%d", &data[i][j]);	if (data[i][j] >= this->ub[j]) this->ub[j] = data[i][j]+1;	if (data[i][j] < this->lb[j]) this->lb[j] = data[i][j];      }      fscanf(fp, "%d", &tch[i]);      if (tch[i] >= this->ubd) this->ubd = data[i][j]+1;      if (tch[i] < this->lbd) this->lbd = data[i][j];    }    fclose(fp);  }};#endif

⌨️ 快捷键说明

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