Changeset 1313 for cpp/frams/genetics/f4


Ignore:
Timestamp:
07/11/24 17:15:51 (5 months ago)
Author:
Maciej Komosinski
Message:

Color mutations in f1 and f4, and a new syntax for "allowed modifiers" (opposite to previous "excluded modifiers") with optional probabilities for each modifier

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

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/f4/f4_conv.cpp

    r1274 r1313  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    189189        }
    190190
    191         char tmpLine[100];
     191        char tmpLine[256];
    192192        MultiRange range = C->genoRange;
    193193
     
    200200                        // new part object for firstend
    201201                        // coordinates are left to be computed by Model
    202                         sprintf(tmpLine, "fr=%g,ing=%g,as=%g",
    203                                 /*1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation
     202                        sprintf(tmpLine, "fr=%g,ing=%g,as=%g,vr=%g,vg=%g,vb=%g",
     203                                /*1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation, C->P.cred, C->P.cgreen, C->P.cblue
    204204                                //C->firstend.x, C->firstend.y, C->firstend.z
    205205                        );
     
    216216                }
    217217                // new part object for lastend
    218                 sprintf(tmpLine, "fr=%g,ing=%g,as=%g",
     218                sprintf(tmpLine, "fr=%g,ing=%g,as=%g,vr=%g,vg=%g,vb=%g",
    219219                        //C->lastend.x, C->lastend.y, C->lastend.z
    220                         /*"vol=" 1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation
     220                        /*"vol=" 1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation, C->P.cred, C->P.cgreen, C->P.cblue
    221221                );
    222222                C->p2_refno = addFromString(PartType, tmpLine, &range);
     
    241241                C->joint_refno = addFromString(JointType, tmpLine, &range);
    242242                if (C->joint_refno < 0) return -13;
     243                getJoint(C->joint_refno)->vcolor = (getPart(jj_p1_refno)->vcolor + getPart(jj_p2_refno)->vcolor) / 2; //joint gets the average color of both connected parts
    243244                this->checkpoint();
    244245        }
  • cpp/frams/genetics/f4/f4_general.cpp

    r1259 r1313  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    14141414                        char *ptr = (char*)(genot + pos_inout);
    14151415                        string original = "";
    1416                         while (GenoOperators::strchrn0(all_modifiers_no_comma, *ptr)) //only processes a section of chars known in all_modifiers_no_comma, other characters will exit the loop
     1416                        while (GenoOperators::strchr_no0(all_modifiers_no_comma, *ptr)) //only processes a section of chars known in all_modifiers_no_comma, other characters will exit the loop
    14171417                        {
    14181418                                original += *ptr;
     
    14221422                        if (advanced > 0) //found modifiers
    14231423                        {
    1424                                 string simplified = GenoOperators::simplifiedModifiers(original);
     1424                                string simplified = GenoOperators::simplifiedModifiers(original, F14_MODIFIERS_COLOR);
    14251425                                // add a node for each char in "simplified"
    14261426                                for (size_t i = 0; i < simplified.length(); i++)
    14271427                                {
    1428                                         int pos = GenoOperators::strchrn0(genot + pos_inout, simplified[i]) - genot; //unnecessarily finding the same char, if it occurrs multiple times in simplified
     1428                                        int pos = GenoOperators::strchr_no0(genot + pos_inout, simplified[i]) - genot; //unnecessarily finding the same char, if it occurrs multiple times in simplified
    14291429                                        f4_Node *node = new f4_Node(simplified[i], par, pos); //location is approximate. In the simplification process we don't trace where the origin(s) of the simplified[i] gene were. We provide 'pos' as the first occurrence of simplified[i] (for example, all 'L' will have the same location assigned, but at least this is where 'L' occurred in the genotype, so in case of any modification of a node (repair, removal, whatever... even mapping of genes) the indicated gene will be one of the responsible ones)
    14301430                                        par = node;
  • cpp/frams/genetics/f4/f4_oper.cpp

    r1298 r1313  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1717// TODO add mapping genotype character ranges for neural [connections]
    1818// TODO The f0 genotypes for /*4*/<<RX>X>X> and RX(X,X) are identical, but if you replace R with C or Q, there are small differences (they were present both before and after the change in C,Q effects in the f1 converter in 2023-06, see conv_f1_f0_cq_influence) - check why (modifiers affecting cells=sticks are applied differently or skip some initial sticks?) and perhaps unify with f1?
    19 // TODO F4_SIMPLIFY_MODIFIERS in f4_general.cpp: currently it works while parsing (which is a bit "cheating": we get a phenotype that is a processed version of the genotype, thus some changes in modifiers in the genotype have no effect on its phenotype). Another (likely better) option, instead of simplifying while parsing, would be during mutations (like it is done in f1): when mutations add/modify/remove a modifier node, they could "clean" the tree by simplifying modifiers on the same subpath just as GenoOperators::simplifiedModifiers() does. This way, simplifying would be only performed when we actually modify a part of a genotype, not each time we interpret it, and there would be no hidden mechanism: all visible genes would have an expected effect on the phenotype.
     19// TODO F4_SIMPLIFY_MODIFIERS in f4_general.cpp: currently it works while parsing (which is a bit "cheating": we get a phenotype that is a processed version of the genotype, thus some changes in modifiers in the genotype have no effect on its phenotype). This is especially noticeable with color modifiers, where simplification only accepts MAX_NUMBER_SAME_TYPE_COLOR. Another (likely better) option, instead of simplifying while parsing, would be during mutations (like it is done in f1): when mutations add/modify/remove a modifier node, they could "clean" the tree by simplifying modifiers on the same subpath just as GenoOperators::simplifiedModifiers() does. This way, simplifying would be only performed when we actually modify a part of a genotype, not each time we interpret it, and there would be no hidden mechanism: all visible genes would have an expected effect on the phenotype. In other words, if a genotype happened to have more modifiers of the same type than allowed by GenoOperators::simplifiedModifiers(), they would all be effective (like in f1) and not silently ignored when interpreting that genotype. For example in /*4*/GGGgGX> due to simplifying modifiers (removing the oldest), only the last G remains effective when MAX_NUMBER_SAME_TYPE_COLOR=1, and it is impossible to adjust colors precisely even by manually editing the genotype - like it is in f1, even though the same simplifying function is used, just in a different moment.
    2020// TODO improve the way modifiers are handled in the f4->f1 approximate converter (used extremely rarely just for illustration)
    2121
     
    3232
    3333
    34 const char *Geno_f4::all_modifiers = F14_MODIFIERS ","; //comma in f4 is handled the same way (simple node, F4_ADD_SIMP) as modifiers. See also all_modifiers_no_comma in f4_general.cpp.
    3534
    3635// codes that can be changed (apart from being added/deleted)
     
    5655
    5756        { "f4_mut_max_rep", 1, 0, "Maximum number for '#' repetitions", "d 2 20 6", FIELD(mut_max_rep), "Maximum allowed number of repetitions for the '#' repetition gene", },
    58         { "f4_mut_exmod", 1, 0, "Excluded modifiers", "s 0 30", FIELD(excluded_modifiers), "Modifiers that will not be added nor deleted during mutation\n(all: " F14_MODIFIERS ")", },
     57        { "f4_mut_modifiers", 1, 0, "Allowed modifiers", "s 0 100", FIELD(allowed_modifiers), "Modifier symbols that will be added or deleted during mutation\n(from the full set: " F14_MODIFIERS ").\n\nYou may use the extended syntax: after every allowed symbol, you may include its probability value in parentheses.\nWithout parentheses, all allowed symbols behave as if they had (1.0) appended.\nIf you include (0.0) after a symbol, this bans that symbol as if it was not present in this string.", },
    5958        { 0, },
    6059};
     
    8483void Geno_f4::setDefaults()
    8584{
    86         excluded_modifiers = F14_MODIFIERS_RARE F14_MODIFIERS_VISUAL;
     85        allowed_modifiers = F14_MODIFIERS_BASIC F14_MODIFIERS_COLOR_SPORADIC;
    8786}
    8887
     
    183182                        f4_Node *node_new = NULL; //stick or neuron or neural connection
    184183                        if (rndDouble(1) < STICK_OR_NEURON)
     184                        {
    185185                                node_new = new f4_Node('X', node_new_div, node_new_div->pos);
     186                                //now add one color modifier before this new X, just as in f1's Geno_f1::addMutationColoredX()
     187                                if (prob[F4_ADD] > 0 && probadd[F4_ADD_SIMP] > 0) //user wants modifier mutations, so we are allowed to prepend a random color modifier to "X"
     188                                {
     189                                        char color_mod = GenoOperators::getRandomColorModifier(allowed_modifiers.c_str(), F14_MODIFIERS_COLOR); //may return 0 if no color modifiers available
     190                                        if (color_mod != 0)
     191                                        {
     192                                                //adding a new color modifier node just as in the F4_ADD_SIMP case later below.
     193                                                node_new->parent->removeChild(node_new);
     194                                                f4_Node *n2 = new f4_Node(color_mod, node_new->parent, node_new->parent->pos);
     195                                                n2->addChild(node_new);
     196                                                node_new->parent = n2;
     197                                        }
     198                                }
     199                        }
    186200                        else
    187201                        {
     
    220234                                }
    221235                        }
    222                         new f4_Node('>', node_new, node_new->pos); //adds to node_new
     236                        new f4_Node('>', node_new, node_new->pos); //adds '>' to node_new
    223237                        node_mutated->parent = node_new_div;
    224238                        // now, swap children with 50% chance
     
    294308                {
    295309                        // add simple node
    296                         int modifier_index = GenoOperators::getRandomChar(all_modifiers, excluded_modifiers.c_str());
    297                         if (modifier_index < 0)
     310                        SString allowed_modifiers_and_comma = allowed_modifiers + ","; // traditionally, in f4, comma was treated here equally with modifiers
     311                        char modifier = GenoOperators::getRandomModifier(allowed_modifiers_and_comma.c_str());
     312                        if (modifier == 0)
    298313                                return GENOPER_OPFAIL;
    299314                        node_mutated->parent->removeChild(node_mutated);
    300315                        // old source: choose a simple node from ADD_SIMPLE_CODES
    301316                        //f4_Node *n2 = new f4_Node(ADD_SIMPLE_CODES[rndUint(strlen(ADD_SIMPLE_CODES))], node_mutated->parent, node_mutated->parent->pos);
    302                         f4_Node *n2 = new f4_Node(all_modifiers[modifier_index], node_mutated->parent, node_mutated->parent->pos);
     317                        f4_Node *n2 = new f4_Node(modifier, node_mutated->parent, node_mutated->parent->pos);
    303318                        n2->addChild(node_mutated);
    304319                        node_mutated->parent = n2;
     
    722737        else if (strchr(">", ch))                style = GENSTYLE_RGBS(0, 0, 100, GENSTYLE_NONE);
    723738        else if (strchr(STYL4CAT_DIGIT, ch))     style = GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE);
    724         else if (strchr(STYL4CAT_MODIFIC, ch))   style = GENSTYLE_RGBS(100, 100, 100, GENSTYLE_NONE);
     739        else if (strchr(STYL4CAT_MODIFIC, ch))
     740        {
     741                if (strchr(F14_MODIFIERS_COLOR, ch)) //color modifier - less important so less visible on white
     742                        style = GENSTYLE_RGBS(200, 200, 200, GENSTYLE_NONE);
     743                else //non-color modifier
     744                        style = GENSTYLE_RGBS(100, 100, 100, GENSTYLE_NONE);
     745        }
    725746        else if (strchr(STYL4CAT_NEUMOD, ch))    style = GENSTYLE_RGBS(0, 150, 0, GENSTYLE_NONE);
    726747        if (isalpha(ch))
  • cpp/frams/genetics/f4/f4_oper.h

    r1234 r1313  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    5959        paInt mut_max_rep;                ///maximum allowed number of repetitions for the '#' repetition gene
    6060
    61         SString excluded_modifiers;       ///<Modifiers that are excluded in mutation process
    62         static const char *all_modifiers;
     61        SString allowed_modifiers;        //to be used in mutations
    6362
    6463private:
Note: See TracChangeset for help on using the changeset viewer.