Changeset 802 for cpp/frams/genetics/fB


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

Crossing over with less bloat, but still biologically-inspired

Location:
cpp/frams/genetics/fB
Files:
4 edited

Legend:

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

    r797 r802  
    170170}
    171171
    172 bool GenoConv_fBH::getNextCharId(SString genotype, int &i)
     172bool GenoConv_fBH::getNextCharId(const SString& genotype, int &i)
    173173{
    174174        i++;
     
    191191}
    192192
    193 bool GenoConv_fBH::getNeuroClass(SString gene, SString &def, int nclassdefcount)
     193bool GenoConv_fBH::getNeuroClass(const SString& gene, SString &def, int nclassdefcount)
    194194{
    195195        SString lastdef = "N";
     
    218218}
    219219
    220 int GenoConv_fBH::processNextLetter(fH_Builder &creature, fH_Handle *&currhandle, Param &par, SString gene, int &propindex, int &i, std::vector<IRange> ranges[3], int &nclassdefcount)
     220int GenoConv_fBH::processNextLetter(fH_Builder &creature, fH_Handle *&currhandle, Param &par, const SString& gene, int &propindex, int &i, std::vector<IRange> ranges[3], int &nclassdefcount)
    221221{
    222222        if (propindex >= par.getPropCount())
  • cpp/frams/genetics/fB/fB_conv.h

    r797 r802  
    1212{
    1313private:
    14         bool getNextCharId(SString genotype, int &i);
     14        bool getNextCharId(const SString& genotype, int &i);
    1515        double convertCharacterTo01(char c);
    1616        double convertCharacterToWeight(char c);
    1717        static fH_Handle* convertCharacterToHandle(char c, int dims, int start, int end, std::vector<IRange> ranges[3]);
    18         int processNextLetter(fH_Builder &creature, fH_Handle *&currhandle, Param &par, SString gene, int &propindex, int &i, std::vector<IRange> ranges[3], int &nclassdefcount);
    19         bool getNeuroClass(SString gene, SString &def, int nclassdefcount);
     18        int processNextLetter(fH_Builder &creature, fH_Handle *&currhandle, Param &par, const SString& gene, int &propindex, int &i, std::vector<IRange> ranges[3], int &nclassdefcount);
     19        bool getNeuroClass(const SString& gene, SString &def, int nclassdefcount);
    2020
    2121public:
  • cpp/frams/genetics/fB/fB_general.h

    r797 r802  
    1111{
    1212public:
    13         static int geneCount(SString geno)
     13        static int geneCount(const SString& geno)
    1414        {
    1515                int start = 0;
     
    2727        }
    2828
    29         static SString getGene(int i, SString genotype, int &start, int &end)
     29        static SString getNonNestedGene(int i, const SString& genotype, int &start, int &end)
     30        {
     31                int count = -1;
     32                start = 0;
     33                int tmp = 0;
     34                SString result = "";
     35                do {
     36                        count++;
     37                        if (start < genotype.len())
     38                                result = getNextGene(start, genotype, tmp, start);
     39                        else
     40                                start = -1;
     41                } while (start != -1 && count < i);
     42                start = tmp;
     43                end = start;
     44                return result;
     45        }
     46
     47        static int geneCountNoNested(const SString& geno)
     48        {
     49                int start = 0;
     50                int count = -1;
     51                int tmp = 0;
     52                do {
     53                        count++;
     54                        if (start < geno.len())
     55                                getNextGene(start, geno, tmp, start);
     56                        else
     57                                start = -1;
     58                } while (start != -1);
     59                return count;
     60        }
     61
     62        static SString getGene(int i, const SString& genotype, int &start, int &end)
    3063        {
    3164                start = 0;
     
    6295        }
    6396
     97        static SString getNextGene(int searchbegin, const SString& genotype, int &start, int &end)
     98        {
     99                start = searchbegin;
     100                int count = -1;
     101                do {
     102                        count++;
     103                        start = genotype.indexOf("aa", start) + 1;
     104                        int quotecount = 0;
     105                        for (int q = 0; q < start; q++) if (genotype[q] == '\"') quotecount++;
     106                        if (quotecount % 2 != 0) count--; // 'aa' sequence is within quotes
     107                } while (start - 1 != -1 && count != 0);
     108                if (start - 1 == -1) // there is no gene with a given "i"
     109                {
     110                        start = -1;
     111                        end = -1;
     112                        return "";
     113                }
     114                end = start;
     115                int quotecount = 0;
     116                do {
     117                        quotecount = 0;
     118                        end = genotype.indexOf("zz", end);
     119                        if (end != -1)
     120                        {
     121                                for (int q = start; q < end; q++) if (genotype[q] == '\"') quotecount++;
     122                                if (quotecount % 2 != 0) end++;
     123                        }
     124                } while (quotecount % 2 != 0 && end != -1);
     125
     126                if (end == -1) end = genotype.len();
     127                else end += 2;
     128                start -= 1;
     129                return genotype.substr(start, end - start);
     130        }
    64131private:
    65132        fB_GenoHelpers() {}
  • 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.