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

Last change on this file since 132 was 121, checked in by sz, 11 years ago

updated file headers and makefiles

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