Ignore:
Timestamp:
06/07/18 17:41:59 (6 years ago)
Author:
Maciej Komosinski
Message:

Crossing over with less bloat, but still biologically-inspired

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/fB/fB_oper.cpp

    r797 r802  
    1919        { "Genetics: fB: Crossover", },
    2020        { "fB_mut_substitution", 1, 0, "Substitution", "f 0 1 0.6", FIELD(mutationprobs[FB_SUBSTITUTION]), "Probability of mutation by changing single random letter in genotype", },
    21         { "fB_mut_insertion", 1, 0, "Insertion", "f 0 1 0.95", FIELD(mutationprobs[FB_INSERTION]), "Probability of mutation by inserting characters in random place of genotype", },
    22         { "fB_mut_nclassins", 1, 0, "Insertion of neuron class definition", "f 0 1 0.05", FIELD(mutationprobs[FB_NCLASSINS]), "Probability of mutation by inserting neuron class definition in random place of genotype", },
     21        { "fB_mut_insertion", 1, 0, "Insertion", "f 0 1 0.095", FIELD(mutationprobs[FB_INSERTION]), "Probability of mutation by inserting characters in random place of genotype", },
     22        { "fB_mut_nclassins", 1, 0, "Insertion of neuron class definition", "f 0 1 0.005", FIELD(mutationprobs[FB_NCLASSINS]), "Probability of mutation by inserting neuron class definition in random place of genotype", },
    2323        { "fB_mut_deletion", 1, 0, "Deletion", "f 0 1 0.1", FIELD(mutationprobs[FB_DELETION]), "Probability of mutation by deleting random characters in genotype", },
    24         { "fB_mut_duplication", 1, 0, "Duplication", "f 0 1 0.05", FIELD(mutationprobs[FB_DUPLICATION]), "Probability of mutation by copying single *gene* of genotype and appending it to the beginning of this genotype", },
     24        { "fB_mut_duplication", 1, 0, "Duplication", "f 0 1 0.0", FIELD(mutationprobs[FB_DUPLICATION]), "Probability of mutation by copying single *gene* of genotype and appending it to the beginning of this genotype", },
    2525        { "fB_mut_translocation", 1, 0, "Translocation", "f 0 1 0.15", FIELD(mutationprobs[FB_TRANSLOCATION]), "Probability of mutation by replacing two substrings in genotype", },
    26         { "fB_cross_gene_transfer", 2, 0, "Horizontal gene transfer", "f 0 1 0.8", FIELD(crossoverprobs[FB_GENE_TRANSFER]), "Probability of crossing over by transferring single genes from both parents to beginning of each other", },
    27         { "fB_cross_crossover", 2, 0, "Crossing over", "f 0 1 0.2", FIELD(crossoverprobs[FB_CROSSING_OVER]), "Probability of crossing over by random distribution of genes from both parents to both children", },
     26        { "fB_cross_gene_transfer", 2, 0, "Horizontal gene transfer", "f 0 1 0.0", FIELD(crossoverprobs[FB_GENE_TRANSFER]), "Probability of crossing over by transferring single genes from both parents to beginning of each other", },
     27        { "fB_cross_crossover", 2, 0, "Crossing over", "f 0 1 1.0", FIELD(crossoverprobs[FB_CROSSING_OVER]), "Probability of crossing over by random distribution of genes from both parents to both children", },
    2828        { 0, },
    2929};
     
    281281                break;
    282282        }
     283        case FB_NCLASSINS:
     284        {
     285                std::list<SString> tokenized = tokenizeSequence(line);
     286                std::list<SString>::iterator it = tokenized.begin();
     287                int rndid = randomN(tokenized.size()); // select random insertion point
     288                std::advance(it, rndid);
     289                NeuroClass *cls = getRandomNeuroClass();
     290                if (cls)
     291                {
     292                        SString classdef = cls->getName();
     293                        Geno_fH::mutateNeuronProperties(classdef);
     294                        SString res = "\"";
     295                        res += classdef;
     296                        res += "\"";
     297                        tokenized.insert(it, res);
     298                        chg = (double)classdef.len() / line.len();
     299                        line = detokenizeSequence(tokenized);
     300                        break;
     301                }
     302        }
     303        /* no break */
    283304        case FB_INSERTION:
    284305        {
     
    291312                letter.directWrite()[0] = 'a' + randomN(26);
    292313                tokenized.insert(it, letter);
    293                 line = detokenizeSequence(tokenized);
    294                 break;
    295         }
    296         case FB_NCLASSINS:
    297         {
    298                 std::list<SString> tokenized = tokenizeSequence(line);
    299                 std::list<SString>::iterator it = tokenized.begin();
    300                 int rndid = randomN(tokenized.size()); // select random insertion point
    301                 std::advance(it, rndid);
    302                 NeuroClass *cls = getRandomNeuroClass();
    303                 SString classdef = cls->getName();
    304                 Geno_fH::mutateNeuronProperties(classdef);
    305                 SString res = "\"";
    306                 res += classdef;
    307                 res += "\"";
    308                 tokenized.insert(it, res);
    309                 chg = (double)classdef.len() / line.len();
    310314                line = detokenizeSequence(tokenized);
    311315                break;
     
    416420                break;
    417421        }
     422//      case FB_CROSSING_OVER:
     423//      {
     424//              // iterate through all genes of the first parent and assign them
     425//              // randomly to children
     426//              for (int i = 0; i < fB_GenoHelpers::geneCount(parent1); i++)
     427//              {
     428//                      int start, end;
     429//                      SString gene = fB_GenoHelpers::getGene(i, parent1, start, end);
     430//                      if (randomN(2) == 0)
     431//                      {
     432//                              child1 += gene;
     433//                              chg1 += 1.0f;
     434//                      }
     435//                      else
     436//                      {
     437//                              child2 += gene;
     438//                      }
     439//              }
     440//              chg1 /= fB_GenoHelpers::geneCount(parent1);
     441//
     442//              // do the same with second parent
     443//              for (int i = 0; i < fB_GenoHelpers::geneCount(parent2); i++)
     444//              {
     445//                      int start, end;
     446//                      SString gene = fB_GenoHelpers::getGene(i, parent2, start, end);
     447//                      if (randomN(2) == 0)
     448//                      {
     449//                              child1 += gene;
     450//                      }
     451//                      else
     452//                      {
     453//                              child2 += gene;
     454//                              chg2 += 1.0f;
     455//                      }
     456//              }
     457//              chg2 /= fB_GenoHelpers::geneCount(parent2);
     458//              break;
     459//      }
    418460        case FB_CROSSING_OVER:
    419461        {
    420                 // iterate through all genes of the first parent and assign them
    421                 // randomly to children
    422                 for (int i = 0; i < fB_GenoHelpers::geneCount(parent1); i++)
    423                 {
    424                         int start, end;
    425                         SString gene = fB_GenoHelpers::getGene(i, parent1, start, end);
    426                         if (randomN(2) == 0)
    427                         {
    428                                 child1 += gene;
    429                                 chg1 += 1.0f;
    430                         }
    431                         else
    432                         {
    433                                 child2 += gene;
    434                         }
    435                 }
    436                 chg1 /= fB_GenoHelpers::geneCount(parent1);
    437 
    438                 // do the same with second parent
    439                 for (int i = 0; i < fB_GenoHelpers::geneCount(parent2); i++)
    440                 {
    441                         int start, end;
    442                         SString gene = fB_GenoHelpers::getGene(i, parent2, start, end);
    443                         if (randomN(2) == 0)
    444                         {
    445                                 child1 += gene;
    446                         }
    447                         else
    448                         {
    449                                 child2 += gene;
    450                                 chg2 += 1.0f;
    451                         }
    452                 }
    453                 chg2 /= fB_GenoHelpers::geneCount(parent2);
     462                // get maximal count of genes from both parents
     463                int maxgenecount = max(fB_GenoHelpers::geneCountNoNested(parent1),
     464                                fB_GenoHelpers::geneCountNoNested(parent2));
     465
     466                // while there are genes in at least one genotype
     467                for (int i = 0; i < maxgenecount; i++)
     468                {
     469                        SString to1 = "", to2 = "";
     470                        int start = 0, end = 0;
     471
     472                        // if both parents have genes available, then distribute them
     473                        if (i < fB_GenoHelpers::geneCountNoNested(parent1) &&
     474                                        i < fB_GenoHelpers::geneCountNoNested(parent2))
     475                        {
     476                                if (randomN(2) == 0)
     477                                {
     478                                        to1 = fB_GenoHelpers::getNonNestedGene(i, parent1, start, end);
     479                                        to2 = fB_GenoHelpers::getNonNestedGene(i, parent2, start, end);
     480                                        chg1 += 1.0f;
     481                                        chg2 += 1.0f;
     482                                }
     483                                else
     484                                {
     485                                        to1 = fB_GenoHelpers::getNonNestedGene(i, parent2, start, end);
     486                                        to2 = fB_GenoHelpers::getNonNestedGene(i, parent1, start, end);
     487                                }
     488                        }
     489                        else if (i < fB_GenoHelpers::geneCountNoNested(parent1))
     490                        {
     491                                if (randomN(2) == 0)
     492                                {
     493                                        to1 = fB_GenoHelpers::getNonNestedGene(i, parent1, start, end);
     494                                        chg1 += 1.0f;
     495                                }
     496                                else
     497                                {
     498                                        to2 = fB_GenoHelpers::getNonNestedGene(i, parent1, start, end);
     499                                }
     500                        }
     501                        else // if (i < fB_GenoHelpers::geneCountNoNested(parent2))
     502                        {
     503                                if (randomN(2) == 0)
     504                                {
     505                                        to1 = fB_GenoHelpers::getNonNestedGene(i, parent2, start, end);
     506                                }
     507                                else
     508                                {
     509                                        to2 = fB_GenoHelpers::getNonNestedGene(i, parent2, start, end);
     510                                        chg2 += 1.0f;
     511                                }
     512                        }
     513                        child1 += to1;
     514                        child2 += to2;
     515                }
     516
     517                chg1 /= fB_GenoHelpers::geneCountNoNested(parent1);
     518                chg2 /= fB_GenoHelpers::geneCountNoNested(parent2);
    454519                break;
    455520        }
Note: See TracChangeset for help on using the changeset viewer.