📄 m2n.hxx
字号:
// ver.0.1// Time-stamp: <2007-07-12 13:26:17 handa>//#ifndef M2N_HH#define M2N_HH#include "selectio.h"#include "chromoso.h"#include <vector>#include <set>using namespace std;template <class T> class m2n: public selection<T> { int max(double a, double b) { return a > b;} int min(double a, double b) { return a < b;} int (m2n::*fpcompare)(double, double);public: m2n(random_number& r, int m = 1): selection<T>(r) { if (m == 1) fpcompare = &m2n<T>::max; else fpcompare = &m2n<T>::min; } virtual void mating(list<chromosome<T> *>& pop, vector<chromosome<T> *>& pool, int flag) { int i; list<chromosome<T> *> selected; list<chromosome<T> *> elitists; list<chromoIntP>::iterator is; list<chromoIntP>::iterator il; cout << pop.size() << " " << pool.size() << "hi "<< endl; chromosome<T> * front = pop.front(); double max = front->fitness(0); elitists.push_front(front); while(selected.size() != pool.size()) { for(il = pop.begin(); il != pop.end(); il++) { if (find(selected.begin(), selected.end(), *il) != selected.end()) continue; if ((*il)->fitness(0) == max) { elitists.push_front(*il); } if ((this->*fpcompare)((*il)->fitness(0), max)) { max = (*il)->fitness(0); elitists.clear(); elitists.push_front(*il); } } if (selected.size() + elitists.size() > pool.size()) { while(selected.size() != pool.size()) { int itmp = (int) this->rn.urand(elitists.size()); is = elitists.begin(); for (int j = 0; j < itmp; j++) is++; selected.push_front(*is); elitists.erase(is); } } else { for (is = elitists.begin(); is != elitists.end(); is++) { selected.push_front(*is); } elitists.clear(); for(il = pop.begin(); il != pop.end(); il++) { if (find(selected.begin(), selected.end(), *il) == selected.end()) { max = (*il)->fitness(0); elitists.push_front(*il); break; } } } } i = 0; for(il = selected.begin(); il != selected.end(); il++) { (*il)->setFlag(flag); pool[i++] = (*il); } } virtual void postprocessing(list<chromosome<T> *>& pool, list<chromosome<T> *>& pop, int flag) { list<chromoIntP>::iterator il; chromoIntP cp; if (0) { int flag2 = 1-flag; for(il = pop.begin(); il != pop.end(); il++) { while (il != pop.end() && (*il)->isFlag() == flag2) { cp = (*il); il = pop.erase(il); delete cp; } }} pop.clear(); for(il = pool.begin(); il != pool.end();il++) { (*il)->setFlag(flag); (*il)->fitness() = (*il)->objfunc(); pop.push_front(*il); } pool.clear(); }};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -