source: cpp/frams/genetics/fT/oper_fTest.cpp @ 717

Last change on this file since 717 was 348, checked in by Maciej Komosinski, 10 years ago
  • explicit c_str() in SString instead of (const char*) cast
  • genetic converters and GenMan? are now thread-local which enables multi-threaded simulator separation
  • Property svn:eol-style set to native
File size: 3.8 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.
[66]4
[194]5#include "oper_fTest.h"
6#include <common/nonstd.h> //randomN, rnd01
[66]7
[194]8/**
9Sample output (simple examples of various genetic operations) produced by genooper_test_fTest.cpp:
10$ ./genooper_test_fTest.exe
11GATCGATTACA   [mutated 0.0%]
12GATCCATTACA   [mutated 9.1%]
13GATCCTGTACA   [mutated 27.3%]
14GATCCTGTACA   [mutated 0.0%]
15GATCCTGTACA   [mutated 0.0%]
16GATCCTGTATA   [mutated 9.1%]
17GATCCTGTATA   [mutated 0.0%]
18GATACTGTATA   [mutated 9.1%]
19GATACTGTATA   [mutated 9.1%]
20GATACTGTATA   [mutated 0.0%]
21
22Crossing over the last mutant,
23GATACTGTATA
24and the simplest genotype
25GATCGATTACA
26:
27Offspring 1:
28GATACTCGATTACA  (35.7% genes from parent1)
29Offspring 2:
30GATGTATA  (25.0% genes from parent2)
31
32Checking genotype:
33ATGsomethingCG... error at position 4.
34After validation:
35ATGCG
36...and how about YOUR genotype?
[66]37*/
38
[194]39
40// To test this genetic format, you can also use the general genooper_test app that supports all genetic formats:
41// $ ./genooper_test.exe /*T*/AAAAAAAAAAA
42
43
44
45#define FIELDSTRUCT GenoOper_fTest
46static ParamEntry GENOtestparam_tab[] =   //external access to ftest genetic parameters
[66]47{
[194]48        { "Genetics: fTest", 1, 1, },
49        { "fTest_mut", 0, 0, "Mutation probability", "f 0 1", FIELD(prob), "How many genes should be mutated during single mutation (1=all genes, 0.1=ten percent)", },
50        { 0, },
[66]51};
52#undef FIELDSTRUCT
53
[194]54GenoOper_fTest::GenoOper_fTest()
[66]55{
[194]56        par.setParamTab(GENOtestparam_tab);
57        par.select(this);
58        supported_format = 'T'; //'0' for f0, '1' for f1, 'F' for fF, etc.
59        prob = 0.1;
[66]60}
61
62///The only letters allowed are A,T,G,C
[194]63int GenoOper_fTest::checkValidity(const char* gene)
[66]64{
[194]65        if (!gene[0]) return 1; //empty is not valid
66        bool ok = true;
[319]67        size_t i;
[194]68        for (i = 0; i < strlen(gene); i++) if (!strchr("ATGC", gene[i])) { ok = false; break; }
69        return ok ? GENOPER_OK : i + 1;
[66]70}
71
72///Remove all invalid letters from the genotype
[194]73int GenoOper_fTest::validate(char *&gene)
[66]74{
[194]75        SString validated; //new genotype (everything except ATGC is skipped)
[319]76        for (size_t i = 0; i < strlen(gene); i++)
[194]77                if (strchr("ATGC", gene[i])) validated += gene[i];  //validated contains only ATGC
78        free(gene);
[348]79        gene = strdup(validated.c_str()); //reallocate
[194]80        return GENOPER_OK;
[66]81}
82
[194]83///Very simple mutation; should be improved to guarantee at least one gene changed
84int GenoOper_fTest::mutate(char *&geno, float &chg, int &method)
[66]85{
[194]86        static char a[] = "ATGC";
87        method = 0;
88        int changes = 0, len = strlen(geno);
89        for (int i = 0; i < len; i++)
90                if (rnd01 < prob) //normalize prob with length of genotype
91                {
92                        geno[i] = a[randomN(4)];
93                        changes++;
94                }
95        chg = (float)changes / len;
96        return GENOPER_OK;
[66]97}
98
99///A simple one-point crossover
[194]100int GenoOper_fTest::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
[66]101{
[194]102        int len1 = strlen(g1), len2 = strlen(g2);
103        int p1 = randomN(len1);  //random cut point for first genotype
104        int p2 = randomN(len2);  //random cut point for second genotype
105        char *child1 = (char*)malloc(p1 + len2 - p2 + 1);
106        char *child2 = (char*)malloc(p2 + len1 - p1 + 1);
107        strncpy(child1, g1, p1);   strcpy(child1 + p1, g2 + p2);
108        strncpy(child2, g2, p2);   strcpy(child2 + p2, g1 + p1);
109        free(g1); g1 = child1;
110        free(g2); g2 = child2;
111        chg1 = (float)p1 / strlen(child1);
112        chg2 = (float)p2 / strlen(child2);
113        return GENOPER_OK;
[66]114}
115
116///Applying some colors and font styles...
[247]117uint32_t GenoOper_fTest::style(const char *g, int pos)
[66]118{
[194]119        char ch = g[pos];
[247]120        uint32_t style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below
[194]121        if (ch == 'A') style = GENSTYLE_RGBS(200, 0, 0, GENSTYLE_BOLD);
122        if (ch == 'T') style = GENSTYLE_RGBS(0, 200, 0, GENSTYLE_BOLD);
123        if (ch == 'G') style = GENSTYLE_RGBS(0, 0, 200, GENSTYLE_NONE);
124        if (ch == 'C') style = GENSTYLE_RGBS(200, 200, 0, GENSTYLE_NONE);
125        return style;
[66]126}
Note: See TracBrowser for help on using the repository browser.