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

Last change on this file since 113 was 109, checked in by sz, 10 years ago

source reorganization (see README)
new feature added: part/joint shapes (see frams/_demos/part_shapes.cpp)

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <time.h>
4#include <frams/virtfile/stdiofile.h>
5
6#include <frams/model/model.h>
7#include <frams/genetic/defgenoconv.h>
8#include <frams/errmgr/stdouterr.h>
9
10StdoutErrorHandler err; //redirect model-related errors to stdout
11DefaultGenoConvManager gcm; //without this object the application would only handle "format 0" genotypes
12
13void save_as_f0(SString &gen,Model &m,bool omit_default_values)
14{
15// copied from Model::makeGeno() (with small changes)
16
17static Param modelparam(f0_model_paramtab);
18static Param partparam(f0_part_paramtab);
19static Param jointparam(f0_joint_paramtab);
20static Param neuroparam(f0_neuro_paramtab);
21static Param connparam(f0_neuroconn_paramtab);
22
23static Part defaultpart;
24static Joint defaultjoint;
25static Neuro defaultneuro;
26static Model defaultmodel;
27static NeuroConn defaultconn;
28
29modelparam.select(&m);
30gen+="m:";
31modelparam.save2(gen,omit_default_values ? &defaultmodel : NULL);
32
33Part *p;
34Joint *j;
35Neuro *n;
36
37for (int i=0;p=(Part*)m.getPart(i);i++)
38        {
39        partparam.select(p);
40        gen+="p:";
41        partparam.save2(gen,omit_default_values ? &defaultpart : NULL);
42        }
43for (int i=0;j=(Joint*)m.getJoint(i);i++)
44        {
45        jointparam.select(j);
46        jointparam.setParamTab(j->usedelta?f0_joint_paramtab:f0_nodeltajoint_paramtab);
47        gen+="j:";
48        jointparam.save2(gen,omit_default_values ? &defaultjoint : NULL);
49        }
50for (int i=0;n=(Neuro*)m.getNeuro(i);i++)
51        {
52        neuroparam.select(n);
53        gen+="n:";
54        neuroparam.save2(gen,omit_default_values ? &defaultneuro : NULL);
55        }
56for (int a=0;n=(Neuro*)m.getNeuro(a);a++)
57        { // inputs
58        for (int b=0;b<n->getInputCount();b++)
59                {
60                double w;
61                NeuroConn nc;
62                Neuro* n2=n->getInput(b,w);
63                nc.n1_refno=n->refno; nc.n2_refno=n2->refno;
64                nc.weight=w;
65                nc.info=n->getInputInfo(b);
66                connparam.select(&nc);
67                gen+="c:";
68                connparam.save2(gen,omit_default_values ? &defaultconn : NULL);
69                }
70        }
71}
72
73int main(int argc,char*argv[])
74{
75SString gen(argc>1?argv[1]:"X[|G:1.23]");
76if (!strcmp(gen,"-"))
77        {
78        gen=0;
79        StdioFILEDontClose in(stdin);
80        loadSString(&in,gen);
81        }
82Geno g(gen);
83printf("\nSource genotype: '%s'\n",(const char*)g.getGene());
84printf("                  ( format %c %s)\n",
85       g.getFormat(), (const char*)g.getComment());
86
87Model m(g);//.getConverted('0'));
88
89if (!m.isValid())
90        {
91        printf("Cannot build Model from this genotype!\n");
92        return 2;       
93        }
94
95printf("\nthis example shows how to save a f0 genotype using low-level ParamInterface::save2() calls\n");
96
97SString f0_skipping_defaults;
98SString f0_no_skipping_defaults;
99
100save_as_f0(f0_skipping_defaults,m,true);
101save_as_f0(f0_no_skipping_defaults,m,false);
102
103printf("\n==== with defdata (skips default values) ======\n%s\n",(const char*)f0_skipping_defaults);
104printf("\n==== without defdata (saves all fields) ======\n%s\n",(const char*)f0_no_skipping_defaults);
105
106return 0;
107}
108
109/*********************** EXAMPLE OUTPUT *********************************
110
111Source genotype: 'X[|G:1.23]'
112                  ( format 1 )
113
114this example shows how to save a f0 genotype using low-level ParamInterface::save2() calls
115
116==== with defdata (skips default values) ======
117m:
118p:
119p:1
120j:0, 1, dx=1
121n:p=1, d=N
122n:j=0, d="|:p=0.25,r=1"
123n:j=0, d=G
124c:0, 2, 1.23
125c:1, 0
126
127
128==== without defdata (saves all fields) ======
129m:se=1, Vstyle=
130p: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
131p: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
132j:0, 1, rx=0, 0, 0, dx=1, 0, 0, stif=1, rotstif=1, stam=0.25, i=, Vstyle=joint
133n:p=1, j=-1, d=N, i=, Vstyle=neuro
134n:p=-1, j=0, d="|:p=0.25,r=1", i=, Vstyle=neuro
135n:p=-1, j=0, d=G, i=, Vstyle=neuro
136c:0, 2, 1.23, i=
137c:1, 0, 1, i=
138
139*************************************************************************/
140
Note: See TracBrowser for help on using the repository browser.