Ignore:
Timestamp:
03/11/14 14:45:29 (10 years ago)
Author:
sz
Message:

reformatting

File:
1 edited

Legend:

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

    r145 r168  
    99
    1010#define FIELDSTRUCT GenoOper_f9
    11 static ParamEntry GENOf9param_tab[]=
     11static ParamEntry GENOf9param_tab[] =
    1212{
    13         {"Genetics: f9",1,1,},
    14         {"f9_mut",0,0,"Mutation probability","f 0 1 0.1",FIELD(mut_prob),"How many genes should be mutated during single mutation (1=all genes, 0.1=ten percent)",},
    15         {0,},
     13        { "Genetics: f9", 1, 1, },
     14        { "f9_mut", 0, 0, "Mutation probability", "f 0 1 0.1", FIELD(mut_prob), "How many genes should be mutated during single mutation (1=all genes, 0.1=ten percent)", },
     15        { 0, },
    1616};
    1717#undef FIELDSTRUCT
     
    2323        par.select(this);
    2424        par.setDefault();
    25         supported_format='9';
     25        supported_format = '9';
    2626}
    2727
     
    2929{
    3030        if (!gene[0]) return 1; //empty is not valid
    31         bool ok=true;
     31        bool ok = true;
    3232        int i;
    33         for(i=0;i<strlen(gene);i++) if (!strchr(turtle_commands_f9,gene[i])) {ok=false; break;}
    34         return ok ? GENOPER_OK : i+1;
     33        for (i = 0; i < strlen(gene); i++) if (!strchr(turtle_commands_f9, gene[i])) { ok = false; break; }
     34        return ok ? GENOPER_OK : i + 1;
    3535}
    3636
     
    3939{
    4040        SString validated; //new genotype (everything except turtle_commands_f9 is skipped)
    41         for(int i=0;i<strlen(gene);i++)
    42                 if (strchr(turtle_commands_f9,gene[i])) validated+=gene[i];  //validated contains only turtle_commands_f9
     41        for (int i = 0; i < strlen(gene); i++)
     42                if (strchr(turtle_commands_f9, gene[i])) validated += gene[i];  //validated contains only turtle_commands_f9
    4343        free(gene);
    44         gene=strdup(validated); //reallocate
     44        gene = strdup(validated); //reallocate
    4545        return GENOPER_OK;
    4646}
    4747
    4848///Very simple mutation
    49 int GenoOper_f9::mutate(char *&gene,float &chg,int &method)
     49int GenoOper_f9::mutate(char *&gene, float &chg, int &method)
    5050{
    51         method=0;
    52         int changes=0,len=strlen(gene);
    53         int symbols=strlen(turtle_commands_f9);
     51        method = 0;
     52        int changes = 0, len = strlen(gene);
     53        int symbols = strlen(turtle_commands_f9);
    5454
    55         for(int i=0;i<len;i++)
     55        for (int i = 0; i < len; i++)
    5656        {
    57                 if (rnd01<mut_prob) //normalize prob with the length of the genotype
     57                if (rnd01 < mut_prob) //normalize prob with the length of the genotype
    5858                {
    59                         char oldgene=gene[i];
    60                         gene[i]=turtle_commands_f9[randomN(symbols)];
    61                         if (gene[i]!=oldgene) changes++;
     59                        char oldgene = gene[i];
     60                        gene[i] = turtle_commands_f9[randomN(symbols)];
     61                        if (gene[i] != oldgene) changes++;
    6262                }
    6363        }
    6464
    65         if (rnd01<mut_prob) //add or delete a random char
     65        if (rnd01 < mut_prob) //add or delete a random char
    6666        {
    6767                SString newgeno(gene);
    68                 if (randomN(2)==0) //add
     68                if (randomN(2) == 0) //add
    6969                {
    70                         int symbols=strlen(turtle_commands_f9);
    71                         int p=randomN(len+1);  //random location
     70                        int symbols = strlen(turtle_commands_f9);
     71                        int p = randomN(len + 1);  //random location
    7272                        //printf("before add: %s\n",(const char*)newgeno);
    73                         newgeno=newgeno.substr(0,p)+SString(turtle_commands_f9+randomN(symbols),1)+newgeno.substr(p);
     73                        newgeno = newgeno.substr(0, p) + SString(turtle_commands_f9 + randomN(symbols), 1) + newgeno.substr(p);
    7474                        //printf("after add: %s\n",(const char*)newgeno);
    7575                        changes++;
    76                 } else if (len>1) //delete
     76                }
     77                else if (len > 1) //delete
    7778                {
    78                         int p=randomN(len);  //random location
     79                        int p = randomN(len);  //random location
    7980                        //printf("before delete: %s\n",(const char*)newgeno);
    80                         newgeno=newgeno.substr(0,p)+newgeno.substr(p+1);
     81                        newgeno = newgeno.substr(0, p) + newgeno.substr(p + 1);
    8182                        //printf("after delete: %s\n",(const char*)newgeno);
    8283                        changes++;
    8384                }
    8485                free(gene);
    85                 gene=strdup(newgeno); //reallocate
     86                gene = strdup(newgeno); //reallocate
    8687        }
    8788
    88         chg=(float)changes/len;
    89         return changes>0?GENOPER_OK:GENOPER_OPFAIL; //no changes => OPFAIL so that genman will call mutate again
     89        chg = (float)changes / len;
     90        return changes > 0 ? GENOPER_OK : GENOPER_OPFAIL; //no changes => OPFAIL so that GenMan will call mutate again
    9091}
    9192
    9293///A simple one-point crossover
    93 int GenoOper_f9::crossOver(char *&g1,char *&g2,float& chg1,float& chg2)
     94int GenoOper_f9::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
    9495{
    95         int len1=strlen(g1),len2=strlen(g2);
    96         int p1=randomN(len1);  //random cut point for first genotype
    97         int p2=randomN(len2);  //random cut point for second genotype
    98         char *child1=(char*)malloc(p1+len2-p2+1);
    99         char *child2=(char*)malloc(p2+len1-p1+1);
    100         strncpy(child1,g1,p1);   strcpy(child1+p1,g2+p2);
    101         strncpy(child2,g2,p2);   strcpy(child2+p2,g1+p1);
    102         free(g1); g1=child1;
    103         free(g2); g2=child2;
    104         chg1=(float)p1/strlen(child1);
    105         chg2=(float)p2/strlen(child2);
     96        int len1 = strlen(g1), len2 = strlen(g2);
     97        int p1 = randomN(len1);  //random cut point for first genotype
     98        int p2 = randomN(len2);  //random cut point for second genotype
     99        char *child1 = (char*)malloc(p1 + len2 - p2 + 1);
     100        char *child2 = (char*)malloc(p2 + len1 - p1 + 1);
     101        strncpy(child1, g1, p1);   strcpy(child1 + p1, g2 + p2);
     102        strncpy(child2, g2, p2);   strcpy(child2 + p2, g1 + p1);
     103        free(g1); g1 = child1;
     104        free(g2); g2 = child2;
     105        chg1 = (float)p1 / strlen(child1);
     106        chg2 = (float)p2 / strlen(child2);
    106107        return GENOPER_OK;
    107108}
     
    110111unsigned long GenoOper_f9::style(const char *g, int pos)
    111112{
    112         char ch=g[pos];
    113         unsigned long style=GENSTYLE_CS(0,GENSTYLE_INVALID); //default, should be changed below
    114         char *ptr=strchr((char*)turtle_commands_f9,ch);
     113        char ch = g[pos];
     114        unsigned long style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below
     115        char *ptr = strchr((char*)turtle_commands_f9, ch);
    115116        if (ptr)
    116117        {
    117                 int pos=ptr-turtle_commands_f9;
    118                 int axis=pos/2;
    119                 style=GENSTYLE_RGBS(axis==0?200:0,axis==1?200:0,axis==2?200:0,GENSTYLE_NONE);
     118                int pos = ptr - turtle_commands_f9;
     119                int axis = pos / 2;
     120                style = GENSTYLE_RGBS(axis == 0 ? 200 : 0, axis == 1 ? 200 : 0, axis == 2 ? 200 : 0, GENSTYLE_NONE);
    120121        }
    121122        return style;
Note: See TracChangeset for help on using the changeset viewer.