source: cpp/frams/_demos/genoconv_test.cpp @ 732

Last change on this file since 732 was 732, checked in by Maciej Komosinski, 6 years ago

Added support for "checkpoints" (intermediate phases of development of the Model when converting between genetic encodings). See Model.checkpoint() and conv_f1.cpp for an example.

  • Property svn:eol-style set to native
File size: 5.9 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[727]2// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
[286]3// See LICENSE.txt for details.
[109]4
5#include <ctype.h>
[129]6#include <frams/genetics/defgenoconv.h>
[109]7#include <frams/model/model.h>
8#include <frams/util/multimap.h>
[527]9#include <common/virtfile/stdiofile.h>
[109]10#include "printconvmap.h"
[391]11#include <common/loggers/loggertostdout.h>
[109]12
13/**
14 @file
15 Sample code: Genotype converter class
[727]16 */
[109]17
18/// Sample Geno converter not using Model class.
[727]19/// (This converter generates the same output for each input).
20/// Such a converter is responsible for doing valid f0 (or other format) output and storing temporary data.
21class GenoConv_Test : public GenoConverter
[109]22{
23public:
[727]24        GenoConv_Test()
[109]25        {
[727]26                name = "Test Converter";
27                in_format = 'x';
[109]28        }
[732]29        SString convert(SString &i, MultiMap *map, bool using_checkpoints) { return SString("after conversion..."); }
[727]30        ~GenoConv_Test() {}
[109]31};
32
33/// Sample Geno converter using Model class.
[727]34/// (This converter generates the same output for each input).
35class GenoConv_Test2 : public GenoConverter
[109]36{
37public:
[727]38        GenoConv_Test2()
[109]39        {
[727]40                name = "Test Converter #2";
41                in_format = 'y';
[109]42        }
[727]43
[732]44        SString convert(SString &i, MultiMap *map, bool using_checkpoints)
[727]45        {
46                Model mod;
47                mod.open();
48                mod.addFromString(Model::PartType, "0,0,0");
49                mod.addFromString(Model::PartType, "0,0,-1");
50                mod.addFromString(Model::JointType, "0,1");
51                mod.getPart(1)->p.y += 0.2; //as an example, directly modify position of part #1
52                mod.close();
53                return mod.getF0Geno().getGenes();
54        }
[732]55
[727]56        ~GenoConv_Test2() {}
[109]57};
58
59/// Sample Geno converter supporting conversion mapping.
[727]60/// The conversion is very simple: any sequence of <digit><character>
61/// (but not inside neurons) is replaced by the repeated sequence of the character.
62class GenoConv_Test3 : public GenoConverter
[109]63{
64public:
[727]65        GenoConv_Test3()
[109]66        {
[727]67                name = "Test Converter #3";
68                in_format = 'z';
69                out_format = '1';
70                mapsupport = 1;
[109]71        }
[732]72        SString convert(SString &in, MultiMap *map, bool using_checkpoints);
[727]73        ~GenoConv_Test3() {}
[109]74};
75
76/** main converting routine - most important: direct conversion map example */
[732]77SString GenoConv_Test3::convert(SString &in, MultiMap *map, bool using_checkpoints)
[109]78{
[727]79        SString dst;
80        const char* src = in.c_str();
81        const char* t;
82        int insideneuron = 0;
83        int n;
84        for (t = src; *t; t++)
[109]85        {
[727]86                if (insideneuron&&*t == ']') insideneuron = 0;
87                if (*t == '[') insideneuron = 1;
88                if ((!insideneuron) && isdigit(*t) && t[1])
[109]89                { // special sequence detected!
[727]90                        n = *t - '0';
91                        t++; // *t will be repeated 'n' times
92                        for (int i = 0; i < n; i++)
93                                dst += *t;
94                        if (map) // fill in the map only if requested
95                                map->add(t - src, t - src, dst.len() - n, dst.len() - 1);
96                        // meaning: source character (t-src) becomes (dst.len()-n ... dst.len()-1)
[109]97                }
[727]98                else
[109]99                {
[727]100                        dst += *t;
101                        if (map)
102                                map->add(t - src, t - src, dst.len() - 1, dst.len() - 1);
103                        // meaning: map single to single character: (t-src) into (dst.len()-1)
[109]104                }
105        }
[727]106        return dst;
[109]107}
108
109
110///////////////////////////////////////////////
111
112void printGen(Geno &g)
113{
[727]114        printf("Genotype:\n%s\nFormat: %c\nValid: %s\nComment: %s\n",
115                g.getGenes().c_str(), g.getFormat(), g.isValid() ? "yes" : "no", g.getComment().c_str());
[109]116}
117
[732]118// arguments:
119//     genotype (or - meaning "read from stdin") [default: X]
120//     target format [default: 0]
121//     "checkpoints" (just write this exact word) [default: not using checkpoints]
[727]122int main(int argc, char *argv[])
[109]123{
[727]124        LoggerToStdout messages_to_stdout(LoggerBase::Enable);
[145]125
[727]126        DefaultGenoConvManager gcm;
127        gcm.addDefaultConverters();
128        gcm.addConverter(new GenoConv_Test());
129        gcm.addConverter(new GenoConv_Test2());
130        gcm.addConverter(new GenoConv_Test3());
131        Geno::useConverters(&gcm);
[109]132
[727]133        Geno::Validators validators;
134        ModelGenoValidator model_validator;
135        validators += &model_validator;
136        Geno::useValidators(&validators);
[145]137
[727]138        SString src;
139        if (argc > 1)
[527]140        {
[727]141                src = argv[1];
142                if (src == "-")
[527]143                {
[727]144                        StdioFILEDontClose in(stdin);
145                        src = "";
146                        loadSString(&in, src, false);
[527]147                }
148        }
[727]149        else
150                src = "X";
151        char dst = (argc > 2) ? *argv[2] : '0';
[732]152        bool using_checkpoints = (argc > 3) ? (strcmp(argv[3], "checkpoints") == 0) : false;
[109]153
[727]154        printf("*** Source genotype:\n");
155        Geno g1(src);
156        printGen(g1);
157        MultiMap m;
[732]158        Geno g2 = g1.getConverted(dst, &m, using_checkpoints);
[727]159        printf("*** Converted to f%c:\n", dst);
160        printGen(g2);
[732]161
162        if (using_checkpoints)
163        { // using Model with checkpoints
164                Model m1(g2, false, true);//true=using_checkpoints
165                printf("\nModel built from the converted f%c genotype has %d checkpoints\n", g2.getFormat(), m1.getCheckpointCount());
166                Model m2(g1, false, true);//true=using_checkpoints
167                printf("Model built from the source f%c genotype has %d checkpoints\n", g1.getFormat(), m2.getCheckpointCount());
168                // accessing individual checkpoint models (if available)
169                if (m1.getCheckpointCount() > 0)
170                {
171                        int c = m1.getCheckpointCount() / 2;
172                        Model *cm = m1.getCheckpoint(c);
173                        printf("Checkpoint #%d (%d parts, %d joint, %d neurons)\n%s", c, cm->getPartCount(), cm->getJointCount(), cm->getNeuroCount(), cm->getF0Geno().getGenesAndFormat().c_str());
174                }
175        }
[727]176        else
[732]177        { // there is no mapping for checkpoints so it's nothing interesting to see here in the checkpoints mode
178                if (m.isEmpty())
179                        printf("(conversion map not available)\n");
180                else
181                {
182                        printf("Conversion map:\n");
183                        m.print();
184                        printConvMap(g1.getGenes(), g2.getGenes(), m);
185                        printf("Reverse conversion map:\n");
186                        MultiMap rm;
187                        rm.addReversed(m);
188                        rm.print();
189                        printConvMap(g2.getGenes(), g1.getGenes(), rm);
190                }
191
192                Model mod1(g1, 1);
193                printf("\nModel map for f%c genotype:\n", g1.getFormat());
194                printModelMap(g1.getGenes(), mod1.getMap());
195                MultiMap mod1combined;
196                mod1combined.addCombined(mod1.getMap(), getModelDisplayMap());
197                mod1combined.print();
198                Model mod2(g2, 1);
199                printf("\nModel map for f%c genotype:\n", g2.getFormat());
200                printModelMap(g2.getGenes(), mod2.getMap());
201                MultiMap mod2combined;
202                mod2combined.addCombined(mod2.getMap(), getModelDisplayMap());
203                mod2combined.print();
[109]204        }
[727]205        return 0;
[109]206}
Note: See TracBrowser for help on using the repository browser.