source: cpp/frams/_demos/f0_variants_test.cpp @ 934

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

Param::save2() renamed to saveSingleLine(); unified Param::load() so that it gets a single-line/multi-line format selector

  • Property svn:eol-style set to native
File size: 4.2 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[534]2// Copyright (C) 1999-2016  Maciej Komosinski and Szymon Ulatowski.
[286]3// See LICENSE.txt for details.
[121]4
[109]5#include <stdlib.h>
6#include <stdio.h>
7#include <time.h>
[382]8#include <common/virtfile/stdiofile.h>
[109]9
[145]10#include <frams/genetics/defgenoconv.h>
[109]11#include <frams/model/model.h>
[391]12#include <common/loggers/loggertostdout.h>
[109]13
14void save_as_f0(SString &gen,Model &m,bool omit_default_values)
15{
16// copied from Model::makeGeno() (with small changes)
17
18static Param modelparam(f0_model_paramtab);
19static Param partparam(f0_part_paramtab);
20static Param jointparam(f0_joint_paramtab);
21static Param neuroparam(f0_neuro_paramtab);
22static Param connparam(f0_neuroconn_paramtab);
23
24static Part defaultpart;
25static Joint defaultjoint;
26static Neuro defaultneuro;
27static Model defaultmodel;
28static NeuroConn defaultconn;
29
30modelparam.select(&m);
31gen+="m:";
[720]32modelparam.saveSingleLine(gen,omit_default_values ? &defaultmodel : NULL);
[109]33
34Part *p;
35Joint *j;
36Neuro *n;
37
38for (int i=0;p=(Part*)m.getPart(i);i++)
39        {
40        partparam.select(p);
41        gen+="p:";
[720]42        partparam.saveSingleLine(gen,omit_default_values ? &defaultpart : NULL);
[109]43        }
44for (int i=0;j=(Joint*)m.getJoint(i);i++)
45        {
46        jointparam.select(j);
47        jointparam.setParamTab(j->usedelta?f0_joint_paramtab:f0_nodeltajoint_paramtab);
48        gen+="j:";
[720]49        jointparam.saveSingleLine(gen,omit_default_values ? &defaultjoint : NULL);
[109]50        }
51for (int i=0;n=(Neuro*)m.getNeuro(i);i++)
52        {
53        neuroparam.select(n);
54        gen+="n:";
[720]55        neuroparam.saveSingleLine(gen,omit_default_values ? &defaultneuro : NULL);
[109]56        }
57for (int a=0;n=(Neuro*)m.getNeuro(a);a++)
58        { // inputs
59        for (int b=0;b<n->getInputCount();b++)
60                {
61                double w;
62                NeuroConn nc;
63                Neuro* n2=n->getInput(b,w);
64                nc.n1_refno=n->refno; nc.n2_refno=n2->refno;
65                nc.weight=w;
66                nc.info=n->getInputInfo(b);
67                connparam.select(&nc);
68                gen+="c:";
[720]69                connparam.saveSingleLine(gen,omit_default_values ? &defaultconn : NULL);
[109]70                }
71        }
72}
73
74int main(int argc,char*argv[])
75{
[375]76LoggerToStdout messages_to_stdout(LoggerBase::Enable);
[145]77
78//without converters the application would only handle "format 0" genotypes
79DefaultGenoConvManager gcm;
80gcm.addDefaultConverters();
[348]81Geno::useConverters(&gcm);
[145]82
[348]83Geno::Validators validators;
84Geno::useValidators(&validators);
[145]85ModelGenoValidator model_validator;
[348]86validators+=&model_validator; //This simple validator handles all cases where a converter for a particular format is available but there is no genetic operator. Converters may be less strict in detecting invalid genotypes but it is better than nothing
[145]87
[109]88SString gen(argc>1?argv[1]:"X[|G:1.23]");
[348]89if (!strcmp(gen.c_str(),"-"))
[109]90        {
91        gen=0;
92        StdioFILEDontClose in(stdin);
93        loadSString(&in,gen);
94        }
95Geno g(gen);
[534]96printf("\nSource genotype: '%s'\n",g.getGenes().c_str());
[109]97printf("                  ( format %c %s)\n",
[348]98       g.getFormat(), g.getComment().c_str());
[109]99
100Model m(g);//.getConverted('0'));
101
102if (!m.isValid())
103        {
104        printf("Cannot build Model from this genotype!\n");
105        return 2;       
106        }
107
[720]108printf("\nthis example shows how to save a f0 genotype using low-level ParamInterface::saveSingleLine() calls\n");
[109]109
110SString f0_skipping_defaults;
111SString f0_no_skipping_defaults;
112
113save_as_f0(f0_skipping_defaults,m,true);
114save_as_f0(f0_no_skipping_defaults,m,false);
115
[348]116printf("\n==== with defdata (skips default values) ======\n%s\n",f0_skipping_defaults.c_str());
117printf("\n==== without defdata (saves all fields) ======\n%s\n",f0_no_skipping_defaults.c_str());
[109]118
119return 0;
120}
121
122/*********************** EXAMPLE OUTPUT *********************************
123
124Source genotype: 'X[|G:1.23]'
125                  ( format 1 )
126
[720]127this example shows how to save a f0 genotype using low-level ParamInterface::saveSingleLine() calls
[109]128
129==== with defdata (skips default values) ======
130m:
131p:
132p:1
133j:0, 1, dx=1
134n:p=1, d=N
135n:j=0, d="|:p=0.25,r=1"
136n:j=0, d=G
137c:0, 2, 1.23
138c:1, 0
139
140
141==== without defdata (saves all fields) ======
142m:se=1, Vstyle=
143p:0, 0, 0, m=1, s=1, dn=1, fr=0.4, ing=0.25, as=0.25, rx=0, 0, 0, i=, Vstyle=part
144p:1, 0, 0, m=1, s=1, dn=1, fr=0.4, ing=0.25, as=0.25, rx=0, 0, 0, i=, Vstyle=part
145j:0, 1, rx=0, 0, 0, dx=1, 0, 0, stif=1, rotstif=1, stam=0.25, i=, Vstyle=joint
146n:p=1, j=-1, d=N, i=, Vstyle=neuro
147n:p=-1, j=0, d="|:p=0.25,r=1", i=, Vstyle=neuro
148n:p=-1, j=0, d=G, i=, Vstyle=neuro
149c:0, 2, 1.23, i=
150c:1, 0, 1, i=
151
152*************************************************************************/
153
Note: See TracBrowser for help on using the repository browser.