source: cpp/frams/genetics/f9/f9_oper.cpp @ 870

Last change on this file since 870 was 779, checked in by Maciej Komosinski, 7 years ago

Unified file names of all files involved in genetic conversions and operations so that they start with "f<format>_"

  • Property svn:eol-style set to native
File size: 3.7 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
[120]4
[779]5#include "f9_oper.h"
6#include "f9_conv.h"
[120]7#include <common/nonstd.h> //randomN, rnd01
8
9
10#define FIELDSTRUCT GenoOper_f9
[168]11static ParamEntry GENOf9param_tab[] =
[120]12{
[168]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, },
[120]16};
17#undef FIELDSTRUCT
18
19
20GenoOper_f9::GenoOper_f9()
21{
22        par.setParamTab(GENOf9param_tab);
23        par.select(this);
24        par.setDefault();
[168]25        supported_format = '9';
[120]26}
27
[513]28int GenoOper_f9::checkValidity(const char* gene, const char *genoname)
[120]29{
30        if (!gene[0]) return 1; //empty is not valid
[168]31        bool ok = true;
[319]32        size_t i;
[168]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;
[120]35}
36
37///Remove all invalid letters from the genotype
[513]38int GenoOper_f9::validate(char *&gene, const char *genoname)
[120]39{
40        SString validated; //new genotype (everything except turtle_commands_f9 is skipped)
[319]41        for (size_t i = 0; i < strlen(gene); i++)
[168]42                if (strchr(turtle_commands_f9, gene[i])) validated += gene[i];  //validated contains only turtle_commands_f9
[120]43        free(gene);
[348]44        gene = strdup(validated.c_str()); //reallocate
[120]45        return GENOPER_OK;
46}
47
48///Very simple mutation
[168]49int GenoOper_f9::mutate(char *&gene, float &chg, int &method)
[120]50{
[168]51        method = 0;
52        int changes = 0, len = strlen(gene);
53        int symbols = strlen(turtle_commands_f9);
[120]54
[168]55        for (int i = 0; i < len; i++)
[120]56        {
[168]57                if (rnd01 < mut_prob) //normalize prob with the length of the genotype
[120]58                {
[168]59                        char oldgene = gene[i];
60                        gene[i] = turtle_commands_f9[randomN(symbols)];
61                        if (gene[i] != oldgene) changes++;
[120]62                }
63        }
64
[168]65        if (rnd01 < mut_prob) //add or delete a random char
[120]66        {
67                SString newgeno(gene);
[168]68                if (randomN(2) == 0) //add
[120]69                {
[168]70                        int symbols = strlen(turtle_commands_f9);
71                        int p = randomN(len + 1);  //random location
[120]72                        //printf("before add: %s\n",(const char*)newgeno);
[168]73                        newgeno = newgeno.substr(0, p) + SString(turtle_commands_f9 + randomN(symbols), 1) + newgeno.substr(p);
[120]74                        //printf("after add: %s\n",(const char*)newgeno);
75                        changes++;
[168]76                }
77                else if (len > 1) //delete
[120]78                {
[168]79                        int p = randomN(len);  //random location
[120]80                        //printf("before delete: %s\n",(const char*)newgeno);
[168]81                        newgeno = newgeno.substr(0, p) + newgeno.substr(p + 1);
[120]82                        //printf("after delete: %s\n",(const char*)newgeno);
83                        changes++;
84                }
85                free(gene);
[348]86                gene = strdup(newgeno.c_str()); //reallocate
[120]87        }
88
[168]89        chg = (float)changes / len;
90        return changes > 0 ? GENOPER_OK : GENOPER_OPFAIL; //no changes => OPFAIL so that GenMan will call mutate again
[120]91}
92
93///A simple one-point crossover
[168]94int GenoOper_f9::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
[120]95{
[168]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);
[120]107        return GENOPER_OK;
108}
109
110///Applying some colors and font styles...
[247]111uint32_t GenoOper_f9::style(const char *g, int pos)
[120]112{
[168]113        char ch = g[pos];
[247]114        uint32_t style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below
[168]115        char *ptr = strchr((char*)turtle_commands_f9, ch);
[120]116        if (ptr)
117        {
[168]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);
[120]121        }
122        return style;
123}
Note: See TracBrowser for help on using the repository browser.