Changeset 1317 for cpp/frams/genetics


Ignore:
Timestamp:
07/11/24 17:41:01 (6 months ago)
Author:
Maciej Komosinski
Message:

Randomly swap parents in crossover to avoid any imbalances or biases in crossover implementations (in case parent order in the implementation of operators would matter - while we assume it should not)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/genman.cpp

    r1316 r1317  
    380380                char *g2n = strdup(g2.getGenes().c_str()); //copy for crossover
    381381                chg1 = chg2 = 0;
     382                bool swap_parents = rndUint(2) == 0; //some implementations of crossover may be not entirely symmetric (f0 is an example). This is not ideal, because crossover(p1,p2) may demonstrate slightly different characteristics or biases than crossover(p2,p1) - maybe extremely rarely, maybe in corner or error cases, but still. Since we assume that "the order of parents should not matter for crossover", here we randomize the order of parents passed to crossover to balance the characteristics.
     383                if (swap_parents) std::swap(g1n, g2n);
    382384                if (gf->crossOver(g1n, g2n, chg1, chg2) == GENOPER_OK)
    383385                {
     386                        if (swap_parents) { std::swap(g1n, g2n); std::swap(chg1, chg2); } //gf->crossOver() got swapped parents, but we want to keep referring children and change fractions to the original parent order, so we restore the original order in the outcomes of gf->crossOver(). g1n and g2n are now children (or could be one child if gf->crossOver() provided only one).
    384387                        char *gn;
    385388                        if (g1n[0] && g2n[0]) if (rndUint(2) == 0) g1n[0] = 0; else g2n[0] = 0; //both provided? we want only one
Note: See TracChangeset for help on using the changeset viewer.