int Mutator_old(GAGenome& g, float pmut) { GAListGenome<int> &child=(GAListGenome<int> &)g; register int n, i; if ((GARandomFloat() >= pmut) || (pmut <= 0)) return 0; n = child.size(); if (GARandomFloat()<0.5) { child.swap(GARandomInt(0,n-1),GARandomInt(0,n-1)); // swap only one time } else { int nNodes = GARandomInt(1,((int)(n/2-1))); // displace nNodes child.warp(GARandomInt(0,n-1)); // with or without GAList<int> TmpList; // inversion for(i=0; i<nNodes; i++) { int *iptr = child.remove(); TmpList.insert(*iptr,GAListBASE::AFTER); delete iptr; child.next(); } int invert; child.warp(GARandomInt(0,n-nNodes)); invert = (GARandomFloat()<0.5) ? 0 : 1; if (invert) TmpList.head(); else TmpList.tail(); for(i=0; i<nNodes; i++) { int *iptr = TmpList.remove(); child.insert(*iptr,GAListBASE::AFTER); delete iptr; if (invert) TmpList.prev(); else TmpList.next(); } } child.head(); // set iterator to root node return (1); }
void GADCrowdingGA::step() { if(pop->size() == 0) return; GAGenome *child = pop->individual(0).clone(); GAList<int> indpool; for (int i=0; i<pop->size(); i++) indpool.insert(i); do { int *ip; indpool.warp(GARandomInt(0,indpool.size()-1)); // select mom ip=indpool.remove(); GAGenome *mom = &pop->individual(*ip); delete ip; indpool.warp(GARandomInt(0,indpool.size()-1)); // select dad ip=indpool.remove(); GAGenome *dad = &pop->individual(*ip); delete ip; stats.numsel += 2; // create child stats.numcro += (*scross)(*mom, *dad, child, 0); stats.nummut += child->mutate(pMutation()); stats.numeval += 1; double d1 = child->compare(*mom); // replace closest parent double d2 = child->compare(*dad); if (d1 < d2) { if (minmax == MINIMIZE) { if (child->score() < mom->score()) { mom->copy(*child); stats.numrep += 1; } } else { if (child->score() > mom->score()) { mom->copy(*child); stats.numrep += 1; } } } else { if (minmax == MINIMIZE) { if (child->score() < dad->score()) { dad->copy(*child); stats.numrep += 1; } } else { if (child->score() > dad->score()) { dad->copy(*child); stats.numrep += 1; } } } } while (indpool.size()>1); pop->evaluate(gaTrue); stats.update(*pop); delete child; }