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 | |
---|
14 | StdoutErrorHandler err; //redirect model-related errors to stdout |
---|
15 | DefaultGenoConvManager gcm; //without this object the application would only handle "format 0" genotypes |
---|
16 | |
---|
17 | void save_as_f0(SString &gen,Model &m,bool omit_default_values) |
---|
18 | { |
---|
19 | // copied from Model::makeGeno() (with small changes) |
---|
20 | |
---|
21 | static Param modelparam(f0_model_paramtab); |
---|
22 | static Param partparam(f0_part_paramtab); |
---|
23 | static Param jointparam(f0_joint_paramtab); |
---|
24 | static Param neuroparam(f0_neuro_paramtab); |
---|
25 | static Param connparam(f0_neuroconn_paramtab); |
---|
26 | |
---|
27 | static Part defaultpart; |
---|
28 | static Joint defaultjoint; |
---|
29 | static Neuro defaultneuro; |
---|
30 | static Model defaultmodel; |
---|
31 | static NeuroConn defaultconn; |
---|
32 | |
---|
33 | modelparam.select(&m); |
---|
34 | gen+="m:"; |
---|
35 | modelparam.save2(gen,omit_default_values ? &defaultmodel : NULL); |
---|
36 | |
---|
37 | Part *p; |
---|
38 | Joint *j; |
---|
39 | Neuro *n; |
---|
40 | |
---|
41 | for (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 | } |
---|
47 | for (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 | } |
---|
54 | for (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 | } |
---|
60 | for (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 | |
---|
77 | int main(int argc,char*argv[]) |
---|
78 | { |
---|
79 | SString gen(argc>1?argv[1]:"X[|G:1.23]"); |
---|
80 | if (!strcmp(gen,"-")) |
---|
81 | { |
---|
82 | gen=0; |
---|
83 | StdioFILEDontClose in(stdin); |
---|
84 | loadSString(&in,gen); |
---|
85 | } |
---|
86 | Geno g(gen); |
---|
87 | printf("\nSource genotype: '%s'\n",(const char*)g.getGene()); |
---|
88 | printf(" ( format %c %s)\n", |
---|
89 | g.getFormat(), (const char*)g.getComment()); |
---|
90 | |
---|
91 | Model m(g);//.getConverted('0')); |
---|
92 | |
---|
93 | if (!m.isValid()) |
---|
94 | { |
---|
95 | printf("Cannot build Model from this genotype!\n"); |
---|
96 | return 2; |
---|
97 | } |
---|
98 | |
---|
99 | printf("\nthis example shows how to save a f0 genotype using low-level ParamInterface::save2() calls\n"); |
---|
100 | |
---|
101 | SString f0_skipping_defaults; |
---|
102 | SString f0_no_skipping_defaults; |
---|
103 | |
---|
104 | save_as_f0(f0_skipping_defaults,m,true); |
---|
105 | save_as_f0(f0_no_skipping_defaults,m,false); |
---|
106 | |
---|
107 | printf("\n==== with defdata (skips default values) ======\n%s\n",(const char*)f0_skipping_defaults); |
---|
108 | printf("\n==== without defdata (saves all fields) ======\n%s\n",(const char*)f0_no_skipping_defaults); |
---|
109 | |
---|
110 | return 0; |
---|
111 | } |
---|
112 | |
---|
113 | /*********************** EXAMPLE OUTPUT ********************************* |
---|
114 | |
---|
115 | Source genotype: 'X[|G:1.23]' |
---|
116 | ( format 1 ) |
---|
117 | |
---|
118 | this example shows how to save a f0 genotype using low-level ParamInterface::save2() calls |
---|
119 | |
---|
120 | ==== with defdata (skips default values) ====== |
---|
121 | m: |
---|
122 | p: |
---|
123 | p:1 |
---|
124 | j:0, 1, dx=1 |
---|
125 | n:p=1, d=N |
---|
126 | n:j=0, d="|:p=0.25,r=1" |
---|
127 | n:j=0, d=G |
---|
128 | c:0, 2, 1.23 |
---|
129 | c:1, 0 |
---|
130 | |
---|
131 | |
---|
132 | ==== without defdata (saves all fields) ====== |
---|
133 | m:se=1, Vstyle= |
---|
134 | p: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 |
---|
135 | p: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 |
---|
136 | j:0, 1, rx=0, 0, 0, dx=1, 0, 0, stif=1, rotstif=1, stam=0.25, i=, Vstyle=joint |
---|
137 | n:p=1, j=-1, d=N, i=, Vstyle=neuro |
---|
138 | n:p=-1, j=0, d="|:p=0.25,r=1", i=, Vstyle=neuro |
---|
139 | n:p=-1, j=0, d=G, i=, Vstyle=neuro |
---|
140 | c:0, 2, 1.23, i= |
---|
141 | c:1, 0, 1, i= |
---|
142 | |
---|
143 | *************************************************************************/ |
---|
144 | |
---|