Changeset 1330 for cpp/frams


Ignore:
Timestamp:
01/02/25 02:00:48 (5 days ago)
Author:
Maciej Komosinski
Message:

f9: less diversifying, more distance-preserving (DPX) crossover (children now have a similar length to parents). Additionally, avoids cutpoints that would cause a simple swap of complete parent genotypes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/f9/f9_oper.cpp

    r1274 r1330  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    8181}
    8282
    83 ///Simple mutation
     83///A simple mutation
    8484int GenoOper_f9::mutate(char *&gene, float &chg, int &method)
    8585{
     
    123123int GenoOper_f9::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
    124124{
     125        // printf("%s  \t  %s\n", g1, g2);
    125126        int len1 = (int)strlen(g1), len2 = (int)strlen(g2);
    126         int p1 = rndUint(len1);  //random cut point for the first genotype
    127         int p2 = rndUint(len2);  //random cut point for the second genotype
     127        double p = rndDouble(1); //random cut point
     128        int p1 = 1 + int(p * (len1 - 1));  //...mapped to the first parent, avoiding cuts that would cause a simple swap of the entire genotype. Unavoidable for a single-letter parent
     129        int p2 = 1 + int(p * (len2 - 1));  //...mapped to the second parent, as above
    128130        char *child1 = (char*)malloc(p1 + len2 - p2 + 1);
    129131        char *child2 = (char*)malloc(p2 + len1 - p1 + 1);
     
    132134        free(g1); g1 = child1;
    133135        free(g2); g2 = child2;
     136        //children non-empty if parents non-empty (valid)
    134137        chg1 = (float)p1 / strlen(child1);
    135138        chg2 = (float)p2 / strlen(child2);
     139        // printf("%s  \t  %s\n", child1, child2);
     140        // printf("%g  \t  %g\n", chg1, chg2);
    136141        return GENOPER_OK;
    137142}
Note: See TracChangeset for help on using the changeset viewer.