21 GADefineIdentity(
"GAListGenome", GAID::ListGenome);
29 static int DestructiveMutator(
GAGenome &c,
float pmut)
37 float nMut = pmut * STA_CAST(
float, n);
41 for (
int i = 0; i < n; i++)
43 if (GAFlipCoin(pmut) && child.warp(i))
52 for (
int i = 0; i < nMut; i++)
54 if (child.warp(GARandomInt(0, n - 1)))
60 return (STA_CAST(
int, nMut));
69 static int SwapMutator(
GAGenome &c,
float pmut)
77 float nMut = pmut * STA_CAST(
float, n);
82 for (i = 0; i < n; i++)
86 child.swap(i, GARandomInt(0, n - 1));
93 for (i = 0; i < nMut; i++)
94 child.swap(GARandomInt(0, n - 1), GARandomInt(0, n - 1));
98 return (STA_CAST(
int, nMut * 2));
117 if (sis.size() > bro.size())
118 return (
float)(sis.size() - bro.size());
119 if (sis.size() < bro.size())
120 return (
float)(bro.size() - sis.size());
127 for (
int i = siter.size() - 1; i >= 0; i--)
129 auto sptr = siter.next();
130 auto bptr = biter.next();
131 if (sptr !=
nullptr && bptr !=
nullptr)
132 count += ((*sptr == *bptr) ? 0 : 1);
165 int a = GARandomInt(0, mom.size());
166 int b = GARandomInt(0, dad.size());
172 sis.GAList<T>::copy(mom);
173 list = dad.GAList<T>::clone(b);
176 T *site = sis.warp(a);
177 while (sis.tail() != site)
194 bro.GAList<T>::copy(dad);
195 list = mom.GAList<T>::clone(a);
198 T *site = bro.warp(b);
199 while (bro.tail() != site)
248 if (mom.size() != dad.size())
250 GAErr(GA_LOC, mom.className(),
"cross", GAError::BadParentLength);
254 int a = GARandomInt(0, mom.size());
255 int b = GARandomInt(0, dad.size());
264 sis.GAList<T>::copy(mom);
266 index = diter.warp(a);
267 for (i = a; i < b; i++, index = diter.next())
269 if (*sis.head() == *index)
275 for (j = 1; (j < sis.size()) && (*sis.next() != *index);
288 bro.GAList<T>::copy(dad);
290 index = miter.warp(a);
291 for (i = a; i < b; i++, index = miter.next())
293 if (*bro.head() == *index)
299 for (j = 1; (j < bro.size()) && (*bro.next() != *index);
318 if (mom.size() != dad.size())
320 GAErr(GA_LOC, mom.className(),
"cross", GAError::BadParentLength);
324 int a = GARandomInt(0, mom.size());
325 int b = GARandomInt(0, dad.size());
328 int i, j, index, nc = 0;
333 sis.GAList<T>::copy(mom);
339 for (i = 0, index = b; i < sis.size(); i++, index++)
341 if (index >= sis.size())
343 if (GAListIsHole(sis, dad, index, a, b))
346 for (; i < sis.size() - b + a; i++, index++)
348 if (index >= sis.size())
356 }
while (GAListIsHole(sis, dad, j, a, b));
362 for (i = a, sis.warp(a), diter.warp(a); i < b;
363 i++, sis.next(), diter.next())
365 if (*sis.current() != *diter.current())
368 for (j = i + 1; j < b; j++)
369 if (*siter.next() == *diter.current())
386 bro.GAList<T>::copy(dad);
392 for (i = 0, index = b; i < bro.size(); i++, index++)
394 if (index >= bro.size())
396 if (GAListIsHole(bro, mom, index, a, b))
400 for (; i < bro.size() - b + a; i++, index++)
402 if (index >= bro.size())
410 }
while (GAListIsHole(bro, mom, j, a, b));
416 for (i = a, bro.warp(a), miter.warp(a); i < b;
417 i++, bro.next(), miter.next())
419 if (*bro.current() != *miter.current())
422 for (j = i + 1; j < b; j++)
423 if (*biter.next() == *miter.current())
445 if (mom.size() != dad.size())
447 GAErr(GA_LOC, mom.className(),
"cross", GAError::BadParentLength);
452 mask.size(mom.size());
459 sis.GAList<T>::copy(mom);
465 while (*diter.current() != *sis.head())
467 for (i = 0; i < sis.size(); i++, sis.next())
469 if (*sis.current() == *diter.current())
482 for (i = 0; i < sis.size(); i++)
485 *sis.current() = *diter.current();
496 bro.GAList<T>::copy(dad);
502 while (*miter.current() != *bro.head())
504 for (i = 0; i < bro.size(); i++, bro.next())
506 if (*bro.current() == *miter.current())
519 for (i = 0; i < bro.size(); i++)
522 *bro.current() = *miter.current();
534 GAListGenome(GAGenome::Evaluator f =
nullptr,
void *u =
nullptr)
536 DEFAULT_LIST_COMPARATOR)
540 crossover(DEFAULT_LIST_CROSSOVER);
557 clone(GAGenome::CloneMethod flag = CloneMethod::CONTENTS)
const override
560 if (flag == CloneMethod::CONTENTS)
566 cpy->GAGenome::copy(*
this);
571 void copy(
const GAGenome &orig)
override
586 int write(std::ostream &os)
const override
588 os <<
"node next prev contents\n";
593 os << this->hd <<
" ";
595 os << this->hd->next <<
" ";
597 os << this->hd->prev <<
" ";
599 os << &(DYN_CAST(
GANode<T> *, this->hd)->contents) <<
"\n";
601 for (
GANodeBASE *tmp = this->hd->next; tmp && tmp != this->hd;
607 os << tmp->next <<
" ";
609 os << tmp->prev <<
" ";
611 os << &(DYN_CAST(
GANode<T> *, tmp)->contents) <<
"\n";
623 bool equal(
const GAGenome &c)
const override
628 if (this->size() != b.size())
632 T *tmpA = iterA.head(), *tmpB = iterB.head();
636 if (tmpA && tmpB && *tmpA != *tmpB)
640 }
while (tmpA && tmpA != head);
653 int swap(
unsigned int i,
unsigned int j)
663 int insert(
GAList<T> *t, GAListBASE::Location where = GAListBASE::AFTER)
668 int insert(
const T &t, GAListBASE::Location where = GAListBASE::AFTER)
686 for (
int i = a; i < b; i++)
688 if (*citer.current() == *piter.current())
The base genome class just defines the genome interface - how to mutate, crossover,...
Definition: GAGenome.h:200
GAListGenome.
Definition: GAListGenome.hpp:19
Definition: GAList.hpp:345
Container for nodes that have a list structure.
Definition: GAList.hpp:88
int destroy()
Remove the current node from the list and free the memory it was using.
Definition: GAList.hpp:174
int insert(GAList< T > *t, GAListBASE::Location where=GAListBASE::AFTER)
Inserts the contents of list in to the current list and removes it from the original list.
Definition: GAList.hpp:259
T * remove()
Remove the current node from the list.
Definition: GAList.hpp:232
int swap(unsigned int a, unsigned int b)
Swap two nodes in the list.
Definition: GAList.hpp:199
void copy(const GAList< T > &orig)
Make a complete copy of the list and return a pointer to the new list.
Definition: GAList.hpp:153
GAMask.
Definition: GAMask.h:15
This is the basic node object.
Definition: GANode.h:18