Changeset 1273 for cpp/frams/genetics/fB
- Timestamp:
- 09/09/23 15:10:49 (16 months ago)
- Location:
- cpp/frams/genetics/fB
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/fB/fB_oper.cpp
r1130 r1273 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-202 1Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2023 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 5 //TODO parsing quotes/neurons seems too relaxed, for example the genotype aag"S""acalaaaafbc is considered valid 6 //TODO Same with numbers: "----1.23" is valid 7 //TODO reconsider: Horizontal gene transfer - copying a single random gene from each parent to the beginning of the other parent: should the gene be copied (seems to cause bloat!) or rather moved? 8 //Neurons ("N") can grow even without using quotes and providing neuron classname in the genotype, for example aaaaabbccvaaapdgfddaalaandwddbaajt (this works likely as designed, but investigate and reconsider); also valid neuron definitions inside the genotype are sometimes not expressed 4 9 5 10 #include <frams/util/sstring.h> … … 19 24 { "Genetics: fB: Mutation", }, 20 25 { "Genetics: fB: Crossover", }, 21 { "fB_mut_substitut ion", 1, 0, "Substitution", "f 0 1 0.6", FIELD(mutationprobs[FB_SUBSTITUTION]), "Probability of mutation by changing single random letter ingenotype", },22 { "fB_mut_insert ion", 1, 0, "Insertion", "f 0 1 0.095", FIELD(mutationprobs[FB_INSERTION]), "Probability of mutation by inserting characters in random place ofgenotype", },23 { "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 inrandom place of genotype", },24 { "fB_mut_delet ion", 1, 0, "Deletion", "f 0 1 0.1", FIELD(mutationprobs[FB_DELETION]), "Probability of mutation by deleting random characters ingenotype", },25 { "fB_mut_duplicat ion", 1, 0, "Duplication", "f 0 1 0.0", FIELD(mutationprobs[FB_DUPLICATION]), "Probability of mutation by copying single *gene* ofgenotype and appending it to the beginning of this genotype", },26 { "fB_mut_translocat ion", 1, 0, "Translocation", "f 0 1 0.15", FIELD(mutationprobs[FB_TRANSLOCATION]), "Probability of mutation by replacing two substrings ingenotype", },27 { "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", },28 { "fB_cross_crossover", 2, 0, "Crossing over", "f 0 1 1.0", FIELD(crossoverprobs[FB_CROSSING_OVER]), "Probability of crossing over byrandom distribution of genes from both parents to both children", },26 { "fB_mut_substitute", 1, 0, "Substitution", "f 0 100 1", FIELD(mutationprobs[FB_SUBSTITUTION]), "Relative probability of changing a single random character (or a neuron) in the genotype", }, 27 { "fB_mut_insert", 1, 0, "Insertion", "f 0 100 3", FIELD(mutationprobs[FB_INSERTION]), "Relative probability of inserting a random character in a random place of the genotype", }, 28 { "fB_mut_insert_neuron", 1, 0, "Insertion of a neuron", "f 0 100 3", FIELD(mutationprobs[FB_INSERTION_NEURON]), "Relative probability of inserting a neuron in a random place of genotype", }, 29 { "fB_mut_delete", 1, 0, "Deletion", "f 0 100 4", FIELD(mutationprobs[FB_DELETION]), "Relative probability of deleting a random character (or a neuron) in the genotype", }, 30 { "fB_mut_duplicate", 1, 0, "Duplication", "f 0 100 0", FIELD(mutationprobs[FB_DUPLICATION]), "Relative probability of copying a single *gene* of the genotype and appending it to the beginning of this genotype", }, 31 { "fB_mut_translocate", 1, 0, "Translocation", "f 0 100 4", FIELD(mutationprobs[FB_TRANSLOCATION]), "Relative probability of swapping two substrings in the genotype", }, 32 { "fB_cross_gene_transfer", 2, 0, "Horizontal gene transfer", "f 0 100 0", FIELD(crossoverprobs[FB_GENE_TRANSFER]), "Relative probability of crossing over by copying a single random gene from each parent to the beginning of the other parent", }, 33 { "fB_cross_crossover", 2, 0, "Crossing over", "f 0 100 100", FIELD(crossoverprobs[FB_CROSSING_OVER]), "Relative probability of crossing over by a random distribution of genes from both parents to both children", }, 29 34 { 0, }, 30 35 }; … … 128 133 if (!genotype.getNextToken(pos, strdims, '\n')) 129 134 { 130 return GENOPER_O PFAIL;135 return GENOPER_OK; 131 136 } 132 137 // parse dimension … … 134 139 if (!ExtValue::parseInt(strdims.c_str(), dims, true, false)) 135 140 { 136 return GENOPER_O PFAIL;141 return GENOPER_OK; 137 142 } 138 143 SString line; … … 171 176 else 172 177 { 173 return GENOPER_O PFAIL;178 return GENOPER_OK; 174 179 } 175 180 } … … 247 252 { 248 253 std::list<SString> tokenized = tokenizeSequence(line); 249 int rndid = rndUint( tokenized.size()); // select random letter from genotype254 int rndid = rndUint((int)tokenized.size()); // select random letter from genotype 250 255 // increment/decrement character - when overflow happens, this method 251 256 // uses the "reflect" approach … … 282 287 break; 283 288 } 284 case FB_ NCLASSINS:289 case FB_INSERTION_NEURON: 285 290 { 286 291 std::list<SString> tokenized = tokenizeSequence(line); 287 292 std::list<SString>::iterator it = tokenized.begin(); 288 int rndid = rndUint( tokenized.size()); // select random insertion point293 int rndid = rndUint((int)tokenized.size()); // select random insertion point 289 294 std::advance(it, rndid); 290 295 NeuroClass *cls = getRandomNeuroClass(Model::SHAPETYPE_BALL_AND_STICK); … … 307 312 chg = 1.0 / line.length(); 308 313 std::list<SString> tokenized = tokenizeSequence(line); 309 int rndid = rndUint( tokenized.size()); // select random insertion point314 int rndid = rndUint((int)tokenized.size()); // select random insertion point 310 315 std::list<SString>::iterator it = tokenized.begin(); 311 316 std::advance(it, rndid); … … 321 326 std::list<SString> tokenized = tokenizeSequence(line); 322 327 std::list<SString>::iterator it = tokenized.begin(); 323 int rndid = rndUint( tokenized.size()); // select random deletion point328 int rndid = rndUint((int)tokenized.size()); // select random deletion point 324 329 std::advance(it, rndid); 325 330 tokenized.erase(it); … … 343 348 for (int i = 0; i < 4; i++) 344 349 { 345 cuts[i] = rndUint( tokenized.size());350 cuts[i] = rndUint((int)tokenized.size()); 346 351 } 347 352 std::sort(cuts.begin(), cuts.end()); … … 407 412 case FB_GENE_TRANSFER: 408 413 { 409 // get random gene fromfirst parent414 // get a random gene from the first parent 410 415 int choice = rndUint(fB_GenoHelpers::geneCount(parent1)); 411 416 int start, end; … … 414 419 child2 = gene + parent2; 415 420 chg2 = (float)parent2.length() / (float)child2.length(); 416 // do the same for second parent421 // do the same for the second parent 417 422 choice = rndUint(fB_GenoHelpers::geneCount(parent2)); 418 423 gene = fB_GenoHelpers::getGene(choice, parent2, start, end); … … 554 559 { 555 560 pos--; 556 if (isdigit(geno[pos]) == 0) 557 { 558 return GENSTYLE_CS(0, GENSTYLE_INVALID); 559 } 560 } 561 return GENSTYLE_RGBS(0, 0, 200, GENSTYLE_BOLD); 562 } 563 if (islower(ch) == 0) 564 { 565 return GENSTYLE_CS(0, GENSTYLE_INVALID); 566 } 567 uint32_t style = GENSTYLE_CS(GENCOLOR_TEXT, GENSTYLE_NONE); 568 if (ch == 'a' && pos > 0 && (geno[pos - 1] == 'a' || geno[pos - 1] == '\n')) 561 if (isdigit(geno[pos]) == 0) //going left we encountered some non-digit character 562 { 563 return GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE); //so 'ch' is any digit in the genotype (neural property value etc.); for simplicity, digits as parts of neuroclass name or property name also get included here 564 } 565 } 566 return GENSTYLE_RGBS(0, 0, 200, GENSTYLE_BOLD); //only digits up to the beginning, so this is the dimensionality value 567 } 568 if (ch == '-' || ch == '.') 569 return GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE); 570 if (ch == '"') 571 return GENSTYLE_RGBS(150, 0, 150, GENSTYLE_BOLD); //quotes encompass neuron definitions. To further distinguish the text inside quotes from the text outside quotes, we would need to determine the number of '"' from the beginning, i.e. linear search through the entire genotype. We don't want to do it - it would mean the complexity of len(geno)^2 if performed for each symbol in the genotype independently, like this function does. Below we perform an approximate partial scan. 572 if (isupper(ch) || strchr("@|*", ch)) 573 return GENSTYLE_RGBS(150, 0, 150, GENSTYLE_BOLD); //neuroclass 574 if (strchr(":,=", ch)) 575 return GENSTYLE_RGBS(150, 0, 150, GENSTYLE_NONE); //these symbols occur exclusively inside "...neuron...", so let's make the entire neuron section "...neuron..." more visually uniform by using the same violet color as the neuroclass name and quotes have 576 if (islower(ch)) //how to color the current lower-case letter? 577 { 578 static const int SCAN_RANGE = 8; //how many characters before the current one to scan to discover some context and find out if we are likely in the neuroclass name or the property name. Reduces computational complexity. Example genotype fragments: abcabc"T:r=0.9, ry=4.088, rz=1.213"abcabc or abc"N:in=0.0, fo=0.17, si=999.0"abc 579 int i = pos; 580 while (i > 0 && pos - i < SCAN_RANGE) 581 { 582 i--; //go back one char 583 if (isupper(geno[i])) 584 return GENSTYLE_RGBS(150, 0, 150, GENSTYLE_BOLD); //neuroclass 585 if (geno[i] == ',' || geno[i] == ':') //this is what must occur before property name starts 586 return GENSTYLE_RGBS(255, 140, 0, GENSTYLE_BOLD); //property 587 if (!(isalpha(geno[i]) || isspace(geno[i]))) //going left we encountered any char that is not a letter or space 588 break; 589 } 590 } 591 592 uint32_t style = GENSTYLE_CS(GENCOLOR_TEXT, GENSTYLE_NONE); //if the current character did not fall into any of the above cases, assume default black style 593 if (ch == 'a' && (geno[pos + 1] == 'a' || (pos > 0 && geno[pos - 1] == 'a'))) //start codon, "aa" 569 594 { 570 595 style = GENSTYLE_RGBS(0, 200, 0, GENSTYLE_BOLD); 571 596 } 572 else if (ch == 'z' && pos > 0 && geno[pos - 1] == 'z')597 else if (ch == 'z' && (geno[pos + 1] == 'z' || (pos > 0 && geno[pos - 1] == 'z'))) //stop codon, "zz" 573 598 { 574 599 style = GENSTYLE_RGBS(200, 0, 0, GENSTYLE_BOLD); -
cpp/frams/genetics/fB/fB_oper.h
r821 r1273 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 18Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2023 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 11 11 /** @name Codes for general fB mutation types */ 12 12 //@{ 13 #define FB_SUBSTITUTION 0 ///<Relative probability of mutation by changing single random letter in genotype (substitution)14 #define FB_INSERTION 1 ///<Relative probability of mutation by inserting characters in random place ofgenotype15 #define FB_ NCLASSINS 2 ///<Relative probability of mutation by inserting neuron class definition inrandom place of genotype16 #define FB_DELETION 3 ///<Relative probability of mutation by deleting random characters ingenotype17 #define FB_DUPLICATION 4 ///<Relative probability of mutation by copying single *gene* ofgenotype and appending it to the beginning of this genotype18 #define FB_TRANSLOCATION 5 ///<Relative probability of mutation by replacing two substrings ingenotype19 #define FB_MUT_COUNT 6 ///<Count of mutation types13 #define FB_SUBSTITUTION 0 ///<Relative probability of changing a single random character (or a neuron) in the genotype 14 #define FB_INSERTION 1 ///<Relative probability of inserting a random character in a random place of the genotype 15 #define FB_INSERTION_NEURON 2 ///<Relative probability of inserting a neuron in a random place of genotype 16 #define FB_DELETION 3 ///<Relative probability of deleting a random character (or a neuron) in the genotype 17 #define FB_DUPLICATION 4 ///<Relative probability of copying a single *gene* of the genotype and appending it to the beginning of this genotype 18 #define FB_TRANSLOCATION 5 ///<Relative probability of swapping two substrings in the genotype 19 #define FB_MUT_COUNT 6 ///<Count of mutation types 20 20 //@} 21 21 22 22 /** @name Codes for fB cross over types */ 23 23 //@{ 24 #define FB_GENE_TRANSFER 0 ///<Relative probability of crossing over by transferring single genes from both parents to beginning of each other25 #define FB_CROSSING_OVER 1 ///<Relative probability of crossing over by random distribution of genes from both parents to both children24 #define FB_GENE_TRANSFER 0 ///<Relative probability of crossing over by copying a single random gene from each parent to the beginning of the other parent 25 #define FB_CROSSING_OVER 1 ///<Relative probability of crossing over by a random distribution of genes from both parents to both children 26 26 #define FB_XOVER_COUNT 2 ///<Count of crossing over types 27 27 //@} … … 48 48 int crossOver(char *&g1, char *&g2, float& chg1, float& chg2); 49 49 50 virtual const char* getSimplest() { return " 5\naaazz"; }50 virtual const char* getSimplest() { return "3\naaazz"; } 51 51 52 52 uint32_t style(const char *geno, int pos);
Note: See TracChangeset
for help on using the changeset viewer.