Changeset 194


Ignore:
Timestamp:
03/25/14 00:15:11 (11 years ago)
Author:
Maciej Komosinski
Message:

Updated sources of the fT (ATGC sequences) genetic operators - useful as an educational example for developers; added genooper_test_fTest.cpp as a demo; removed outdated sources that now have new counterparts

Location:
cpp
Files:
2 added
1 deleted
5 edited
2 copied

Legend:

Unmodified
Added
Removed
  • cpp/README.txt

    r193 r194  
    1717      +- errmgr      - error manager (logging/messaging)
    1818      +- genetics    - genetic operators and converters between formats
    19          +- f1       - ...for genetic format f1
    20          +- f9       - ...for genetic format f9
    21          +- fF       - ...for genetic format fF
     19         +- f1       - ...for genetic format f1 (recurrent encoding)
     20         +- f9       - ...for genetic format f9 (3D turtle)
     21         +- fF       - ...for genetic format fF (Foraminifera)
     22         +- fT       - ...for genetic format fT (educational: ATGC sequences)
    2223      +- model       - parts, joints, neurons, connections
    2324         +- geometry - compute dimensions, volume, surface area
  • cpp/frams/Makefile-GDK

    r191 r194  
    55include frams/Makefile-GDK-files
    66
    7 GDK_TESTS=genoconv_test gdk_test geno_test neuro_test loader_test serial_test multiline_f0_test f0_variants_test full_props part_shapes neuro_layout_test genooper_test geometry_apices_test geometry_info_test geometry_surface_test geometry_volume_test
     7GDK_TESTS=genoconv_test gdk_test geno_test genooper_test genooper_test_fTest neuro_test loader_test serial_test multiline_f0_test f0_variants_test full_props part_shapes neuro_layout_test geometry_apices_test geometry_info_test geometry_surface_test geometry_volume_test
    88
    99gdk_tests: $(GDK_TESTS)
     
    2222geno_test: $(GENO_TEST_OBJS)
    2323        $(CXX) $(GENO_TEST_OBJS) -o $@
     24
     25genooper_test: $(GENOOPER_TEST_OBJS)
     26        $(CXX) $(GENOOPER_TEST_OBJS) -o $@
     27
     28genooper_test_fTest: $(GENOOPER_TEST_FTEST_OBJS)
     29        $(CXX) $(GENOOPER_TEST_FTEST_OBJS) -o $@
    2430
    2531neuro_test: $(NEURO_TEST_OBJS)
     
    4753        $(CXX) $(NEURO_LAYOUT_TEST_OBJS) -o $@
    4854
    49 genooper_test: $(GENOOPER_TEST_OBJS)
    50         $(CXX) $(GENOOPER_TEST_OBJS) -o $@
    5155
    5256geometry_apices_test: $(GEOMETRY_APICES_TEST_OBJS)
     
    6266        $(CXX) $(GEOMETRY_VOLUME_TEST_OBJS) -o $@
    6367
     68
    6469#############################################
    6570
  • cpp/frams/Makefile-GDK-files

    r191 r194  
    22
    33# ALL_DIRS is later expanded by the shell, no spaces/newlines allowed, or it breaks
    4 ALL_DIRS={common,frams,frams/canvas,frams/config,frams/errmgr,frams/genetics,frams/genetics/f0,frams/genetics/f1,frams/genetics/f2,frams/genetics/f3,frams/genetics/f4,frams/genetics/f5,frams/genetics/f6,frams/genetics/f7,frams/genetics/f8,frams/genetics/f9,frams/genetics/fF,frams/model,frams/neuro,frams/neuro/impl,frams/param,frams/test,frams/util,frams/vm/classes,frams/virtfile,frams/_demos,frams/model/geometry,frams/_demos/geometry}
     4ALL_DIRS={common,frams,frams/canvas,frams/config,frams/errmgr,frams/genetics,frams/genetics/f0,frams/genetics/f1,frams/genetics/f2,frams/genetics/f3,frams/genetics/f4,frams/genetics/f5,frams/genetics/f6,frams/genetics/f7,frams/genetics/f8,frams/genetics/f9,frams/genetics/fF,frams/genetics/fT,frams/model,frams/neuro,frams/neuro/impl,frams/param,frams/test,frams/util,frams/vm/classes,frams/virtfile,frams/_demos,frams/model/geometry,frams/_demos/geometry}
    55
    66GENMANF9=frams/genetics/f9/oper_f9.o
    77GENMANFF=frams/genetics/fF/oper_fF.o
     8GENMANFT=frams/genetics/fT/oper_fTest.o
    89
    910CONVF1=frams/genetics/f1/conv_f1.o
     
    1213
    1314GENOCONV_GDK_OBJS=frams/genetics/defgenoconv.o $(CONVF1) $(CONVF9) $(CONVFF)
    14 GENMAN_GDK_OBJS=$(GENMAN_COMMON_OBJS) $(GENMANF9) $(GENMANFF)
     15GENMAN_GDK_OBJS=$(GENMAN_COMMON_OBJS) $(GENMANF9) $(GENMANFF) $(GENMANFT)
    1516
    1617GENMAN_COMMON_OBJS=frams/genetics/genman.o frams/param/mutableparam.o frams/param/mutparamlist.o frams/neuro/geneticneuroparam.o frams/neuro/neurolibparam.o frams/vm/classes/genoobj.o frams/model/autoname.o
     
    3435GENO_TEST_OBJS= frams/_demos/geno_test.o frams/virtfile/virtfile.o   $(GDK_OBJS) $(GENOCONV_GDK_OBJS) $(GENMAN_GDK_OBJS)
    3536
     37GENOOPER_TEST_OBJS=frams/_demos/genooper_test.o frams/virtfile/virtfile.o frams/errmgr/stdouterr.o $(GDK_OBJS) $(GENOCONV_GDK_OBJS) $(GENMAN_GDK_OBJS)
     38
     39GENOOPER_TEST_FTEST_OBJS=frams/_demos/genooper_test_fTest.o frams/virtfile/virtfile.o $(GDK_OBJS) $(GENOCONV_GDK_OBJS) $(GENMAN_GDK_OBJS)
     40
    3641NEURO_TEST_OBJS= frams/_demos/neuro_test.o frams/errmgr/stdouterr.o frams/virtfile/virtfile.o \
    3742        frams/neuro/neuroimpl.o frams/neuro/neurofactory.o frams/neuro/impl/neuroimpl-simple.o frams/neuro/impl/neuroimpl-channels.o \
     
    4651NEURO_LAYOUT_TEST_OBJS= frams/_demos/neuro_layout_test.o frams/virtfile/virtfile.o frams/errmgr/stdouterr.o $(GDK_OBJS) $(GENOCONV_GDK_OBJS) $(GENMAN_GDK_OBJS) frams/canvas/nn_layout_model.o frams/canvas/nn_simple_layout.o frams/canvas/nn_smart_layout.o
    4752
    48 GENOOPER_TEST_OBJS=frams/_demos/genooper_test.o frams/virtfile/virtfile.o frams/errmgr/stdouterr.o $(GDK_OBJS) $(GENOCONV_GDK_OBJS) $(GENMAN_GDK_OBJS)
    49 
    5053GEOMETRY_APICES_TEST_OBJS=frams/_demos/geometry/apices_test.o frams/_demos/geometry/geometrytestutils.o frams/_demos/genotypeloader.o frams/virtfile/virtfile.o frams/param/multiparamload.o frams/virtfile/stdiofile-autoinit.o frams/virtfile/stdiofile.o common/nonstd_stdio.o $(GDK_OBJS) $(GENOCONV_GDK_OBJS) $(GEOMETRY_OBJS)
    5154
  • cpp/frams/genetics/fT/oper_fTest.cpp

    r193 r194  
    1 // This file is a part of the Framsticks GenoFX library.
    2 // Copyright (C) 2002-2011  Maciej Komosinski.  See LICENSE.txt for details.
     1// This file is a part of the Framsticks GDK.
     2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
    33// Refer to http://www.framsticks.com/ for further information.
    44
    5 #include "geno_ftest.h"
    6 #include "nonstd.h" //randomN, rnd01
     5#include "oper_fTest.h"
     6#include <common/nonstd.h> //randomN, rnd01
    77
    8 /** \file
    9 Sample output (simple examples of various genetic operations):
    10 \anchor geno_ftest_example
    11 \include geno_ftest_example
    12 Produced by the source:
    13 \include geno_ftest.cpp
     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?
    1437*/
    1538
    16 #define FIELDSTRUCT Geno_ftest
    17 static ParamEntry GENOtestparam_tab[]=   //external access to ftest genetic parameters
     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
    1847{
    19 {"Genetics: ftest",1,1,},
    20 {"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)",},
    21 {0,},
     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, },
    2251};
    2352#undef FIELDSTRUCT
    2453
    25 Geno_ftest::Geno_ftest()
     54GenoOper_fTest::GenoOper_fTest()
    2655{
    27    par.setParamTab(GENOtestparam_tab);
    28    par.select(this);
    29    supported_format='t'; //'0' for f0, '1' for f1, etc.
    30    prob=0.1;
     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;
    3160}
    3261
    3362///The only letters allowed are A,T,G,C
    34 int Geno_ftest::checkValidity(const char* gene)
     63int GenoOper_fTest::checkValidity(const char* gene)
    3564{
    36    if (!gene[0]) return 1; //empty is not valid
    37    bool ok=true;
    38    int i;
    39    for(i=0;i<strlen(gene);i++) if (!strchr("ATGC",gene[i])) {ok=false; break;}
    40    return ok ? GENOPER_OK : i+1;
     65        if (!gene[0]) return 1; //empty is not valid
     66        bool ok = true;
     67        int i;
     68        for (i = 0; i < strlen(gene); i++) if (!strchr("ATGC", gene[i])) { ok = false; break; }
     69        return ok ? GENOPER_OK : i + 1;
    4170}
    4271
    4372///Remove all invalid letters from the genotype
    44 int Geno_ftest::validate(char *&gene)
     73int GenoOper_fTest::validate(char *&gene)
    4574{
    46    SString validated; //new genotype (everything except ATGC is skipped)
    47    for(int i=0;i<strlen(gene);i++)
    48       if (strchr("ATGC",gene[i])) validated+=gene[i];  //validated contains only ATGC
    49    free(gene);
    50    gene=strdup(validated); //reallocate
    51    return GENOPER_OK;
     75        SString validated; //new genotype (everything except ATGC is skipped)
     76        for (int i = 0; i < strlen(gene); i++)
     77                if (strchr("ATGC", gene[i])) validated += gene[i];  //validated contains only ATGC
     78        free(gene);
     79        gene = strdup(validated); //reallocate
     80        return GENOPER_OK;
    5281}
    5382
    54 ///Very simple mutation
    55 int Geno_ftest::mutate(char *&gene,float &chg)
     83///Very simple mutation; should be improved to guarantee at least one gene changed
     84int GenoOper_fTest::mutate(char *&geno, float &chg, int &method)
    5685{
    57    static char a[]="ATGC";
    58    int changes=0,len=strlen(gene);
    59    for(int i=0;i<len;i++)
    60       if (rnd01<prob) //normalize prob with length of genotype
    61          {gene[i]=a[randomN(4)]; changes++;}
    62    chg=(float)changes/len;
    63    return GENOPER_OK;
     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;
    6497}
    6598
    6699///A simple one-point crossover
    67 int Geno_ftest::crossOver(char *&g1,char *&g2,float& chg1,float& chg2)
     100int GenoOper_fTest::crossOver(char *&g1, char *&g2, float& chg1, float& chg2)
    68101{
    69    int len1=strlen(g1),len2=strlen(g2);
    70    int p1=randomN(len1);  //random cut point for first genotype
    71    int p2=randomN(len2);  //random cut point for second genotype
    72    char *child1=(char*)malloc(p1+len2-p2+1);
    73    char *child2=(char*)malloc(p2+len1-p1+1);
    74    strncpy(child1,g1,p1);   strcpy(child1+p1,g2+p2);
    75    strncpy(child2,g2,p2);   strcpy(child2+p2,g1+p1);
    76    free(g1); g1=child1;
    77    free(g2); g2=child2;
    78    chg1=(float)p1/strlen(child1);
    79    chg2=(float)p2/strlen(child2);
    80    return GENOPER_OK;
     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;
    81114}
    82115
    83116///Applying some colors and font styles...
    84 unsigned long Geno_ftest::style(const char *g, int pos)
     117unsigned long GenoOper_fTest::style(const char *g, int pos)
    85118{
    86    char ch=g[pos];
    87    unsigned long style=GENSTYLE_CS(0,GENSTYLE_INVALID); //default, should be changed below
    88    if (ch=='A') style=GENSTYLE_RGBS(200,0,0,GENSTYLE_BOLD);
    89    if (ch=='T') style=GENSTYLE_RGBS(0,200,0,GENSTYLE_BOLD);
    90    if (ch=='G') style=GENSTYLE_RGBS(0,0,200,GENSTYLE_NONE);
    91    if (ch=='C') style=GENSTYLE_RGBS(200,200,0,GENSTYLE_NONE);
    92    return style;
     119        char ch = g[pos];
     120        unsigned long style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below
     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;
    93126}
    94 
    95 
    96 #ifdef GENO_FTEST_APPL //define this macro to compile a simple testing main function
    97 
    98 Geno_ftest gft;
    99 
    100 void main()
    101 {
    102    float chg;
    103    char *g=strdup(gft.getSimplest());
    104    for(int i=0;i<10;i++)
    105    {
    106       int result=gft.mutate(g,chg);
    107       printf("%s   [mutated %.1f%%]\n",g,chg*100);
    108    }
    109 
    110    char *g2=strdup(gft.getSimplest());
    111    float chg2;
    112    printf("\nCrossing over the last mutant, \n\t%s\nand the simplest genotype\n\t%s\n:\n",g,g2);
    113    gft.crossOver(g,g2,chg,chg2);
    114    printf("Offspring 1:\n\t%s  (%.1f%% genes from parent1)\n",g,chg*100);
    115    printf("Offspring 2:\n\t%s  (%.1f%% genes from parent2)\n",g2,chg2*100);
    116    free(g);
    117    free(g2);
    118 
    119    g=strdup("ATGsomethingCG");
    120    printf("\nChecking genotype:\n\t%s... error at position %d.\n",g,gft.checkValidity(g));
    121    gft.validate(g);
    122    printf("After validation:\n\t%s\n",g);
    123    free(g);
    124    printf("...and is YOUR genotype O.K.?\n\n");
    125 }
    126 
    127 #endif
    128 
  • cpp/frams/genetics/fT/oper_fTest.h

    r191 r194  
    1 // This file is a part of the Framsticks GenoFX library.
    2 // Copyright (C) 2002-2011  Maciej Komosinski.  See LICENSE.txt for details.
     1// This file is a part of the Framsticks GDK.
     2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
    33// Refer to http://www.framsticks.com/ for further information.
    44
    5 #ifndef _GENO_FTEST_H_
    6 #define _GENO_FTEST_H_
     5#ifndef _GENOOPER_FTEST_H_
     6#define _GENOOPER_FTEST_H_
    77
    8 #include "param.h"
    9 #include "sstring.h"
    10 #include "geno_fx.h"
     8#include "../oper_fx.h"
    119
    1210/** \file */
     
    1614\author Maciej Komosinski
    1715
    18 This is a very simple class to illustrate basic genetic operations.
    19 To compile this code, you may need some GDK files.
    20 For a more sophisticated example of Geno_f4 derived from Geno_fx, refer to
    21 the available source on developmental encoding and f4 genotype format.
     16This is a very simple class that illustrates basic genetic operations performed on ATGC sequences.
     17For a more sophisticated and realistic examples of genetic formats and operators derived from GenoOperators,
     18refer to the available source for genetic formats f9, fF, and f4.
    2219
    2320\sa \ref geno_ftest_example
    24 */     
     21*/
    2522
    26 class Geno_ftest : public Geno_fx
     23class GenoOper_fTest : public GenoOperators
    2724{
    28  public:
    29    Geno_ftest();
    30    int checkValidity(const char *);
    31    int validate(char *&);
    32    int mutate(char *&g,float& chg);
    33    int crossOver(char *&g1,char *&g2,float& chg1,float& chg2);
    34    unsigned long style(const char *g, int pos);
    35    char* getSimplest() {return "GTTCAGATC";}
     25public:
     26        GenoOper_fTest();
     27        int checkValidity(const char *);
     28        int validate(char *&);
     29        int mutate(char *&geno, float& chg, int &method);
     30        int crossOver(char *&g1, char *&g2, float& chg1, float& chg2);
     31        unsigned long style(const char *g, int pos);
     32        const char* getSimplest() { return "GATCGATTACA"; }
    3633
    37    double prob;
     34        double prob;
    3835};
    3936
    4037#endif
    41 
  • cpp/frams/genetics/gen-config-GDK.h

    r140 r194  
    2929#define USE_GENMAN_f9
    3030#define USE_GENMAN_fF
     31#define USE_GENMAN_fT
     32
  • cpp/frams/genetics/genman.cpp

    r168 r194  
    4646#ifdef USE_GENMAN_fF
    4747#include "fF/oper_fF.h"
     48#endif
     49#ifdef USE_GENMAN_fT
     50#include "fT/oper_fTest.h"
    4851#endif
    4952
     
    139142#ifdef USE_GENMAN_fF
    140143        oper_fx_list.push_back(new GenoOper_fF);
     144#endif
     145#ifdef USE_GENMAN_fT
     146        oper_fx_list.push_back(new GenoOper_fTest);
    141147#endif
    142148
Note: See TracChangeset for help on using the changeset viewer.