[121] | 1 | // This file is a part of the Framsticks GDK. |
---|
[197] | 2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
[121] | 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 4 | |
---|
[109] | 5 | #include <stdlib.h> |
---|
| 6 | #include <stdio.h> |
---|
| 7 | #include <time.h> |
---|
| 8 | #include <frams/virtfile/stdiofile.h> |
---|
| 9 | |
---|
[145] | 10 | #include <frams/genetics/defgenoconv.h> |
---|
[109] | 11 | #include <frams/model/model.h> |
---|
| 12 | #include <frams/errmgr/stdouterr.h> |
---|
| 13 | |
---|
| 14 | void save_as_f0(SString &gen,Model &m,bool omit_default_values) |
---|
| 15 | { |
---|
| 16 | // copied from Model::makeGeno() (with small changes) |
---|
| 17 | |
---|
| 18 | static Param modelparam(f0_model_paramtab); |
---|
| 19 | static Param partparam(f0_part_paramtab); |
---|
| 20 | static Param jointparam(f0_joint_paramtab); |
---|
| 21 | static Param neuroparam(f0_neuro_paramtab); |
---|
| 22 | static Param connparam(f0_neuroconn_paramtab); |
---|
| 23 | |
---|
| 24 | static Part defaultpart; |
---|
| 25 | static Joint defaultjoint; |
---|
| 26 | static Neuro defaultneuro; |
---|
| 27 | static Model defaultmodel; |
---|
| 28 | static NeuroConn defaultconn; |
---|
| 29 | |
---|
| 30 | modelparam.select(&m); |
---|
| 31 | gen+="m:"; |
---|
| 32 | modelparam.save2(gen,omit_default_values ? &defaultmodel : NULL); |
---|
| 33 | |
---|
| 34 | Part *p; |
---|
| 35 | Joint *j; |
---|
| 36 | Neuro *n; |
---|
| 37 | |
---|
| 38 | for (int i=0;p=(Part*)m.getPart(i);i++) |
---|
| 39 | { |
---|
| 40 | partparam.select(p); |
---|
| 41 | gen+="p:"; |
---|
| 42 | partparam.save2(gen,omit_default_values ? &defaultpart : NULL); |
---|
| 43 | } |
---|
| 44 | for (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:"; |
---|
| 49 | jointparam.save2(gen,omit_default_values ? &defaultjoint : NULL); |
---|
| 50 | } |
---|
| 51 | for (int i=0;n=(Neuro*)m.getNeuro(i);i++) |
---|
| 52 | { |
---|
| 53 | neuroparam.select(n); |
---|
| 54 | gen+="n:"; |
---|
| 55 | neuroparam.save2(gen,omit_default_values ? &defaultneuro : NULL); |
---|
| 56 | } |
---|
| 57 | for (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:"; |
---|
| 69 | connparam.save2(gen,omit_default_values ? &defaultconn : NULL); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | int main(int argc,char*argv[]) |
---|
| 75 | { |
---|
[145] | 76 | StdoutErrorHandler err;//the default ErrorHandler constructor automatically registers this object to receive framsg messages (and in this case, redirect them to standard output) |
---|
| 77 | |
---|
| 78 | //without converters the application would only handle "format 0" genotypes |
---|
| 79 | DefaultGenoConvManager gcm; |
---|
| 80 | gcm.addDefaultConverters(); |
---|
| 81 | Geno::useConverters(gcm); |
---|
| 82 | |
---|
| 83 | ModelGenoValidator model_validator; |
---|
| 84 | Geno::addValidator(&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 |
---|
| 85 | |
---|
[109] | 86 | SString gen(argc>1?argv[1]:"X[|G:1.23]"); |
---|
| 87 | if (!strcmp(gen,"-")) |
---|
| 88 | { |
---|
| 89 | gen=0; |
---|
| 90 | StdioFILEDontClose in(stdin); |
---|
| 91 | loadSString(&in,gen); |
---|
| 92 | } |
---|
| 93 | Geno g(gen); |
---|
| 94 | printf("\nSource genotype: '%s'\n",(const char*)g.getGene()); |
---|
| 95 | printf(" ( format %c %s)\n", |
---|
| 96 | g.getFormat(), (const char*)g.getComment()); |
---|
| 97 | |
---|
| 98 | Model m(g);//.getConverted('0')); |
---|
| 99 | |
---|
| 100 | if (!m.isValid()) |
---|
| 101 | { |
---|
| 102 | printf("Cannot build Model from this genotype!\n"); |
---|
| 103 | return 2; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | printf("\nthis example shows how to save a f0 genotype using low-level ParamInterface::save2() calls\n"); |
---|
| 107 | |
---|
| 108 | SString f0_skipping_defaults; |
---|
| 109 | SString f0_no_skipping_defaults; |
---|
| 110 | |
---|
| 111 | save_as_f0(f0_skipping_defaults,m,true); |
---|
| 112 | save_as_f0(f0_no_skipping_defaults,m,false); |
---|
| 113 | |
---|
| 114 | printf("\n==== with defdata (skips default values) ======\n%s\n",(const char*)f0_skipping_defaults); |
---|
| 115 | printf("\n==== without defdata (saves all fields) ======\n%s\n",(const char*)f0_no_skipping_defaults); |
---|
| 116 | |
---|
| 117 | return 0; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /*********************** EXAMPLE OUTPUT ********************************* |
---|
| 121 | |
---|
| 122 | Source genotype: 'X[|G:1.23]' |
---|
| 123 | ( format 1 ) |
---|
| 124 | |
---|
| 125 | this example shows how to save a f0 genotype using low-level ParamInterface::save2() calls |
---|
| 126 | |
---|
| 127 | ==== with defdata (skips default values) ====== |
---|
| 128 | m: |
---|
| 129 | p: |
---|
| 130 | p:1 |
---|
| 131 | j:0, 1, dx=1 |
---|
| 132 | n:p=1, d=N |
---|
| 133 | n:j=0, d="|:p=0.25,r=1" |
---|
| 134 | n:j=0, d=G |
---|
| 135 | c:0, 2, 1.23 |
---|
| 136 | c:1, 0 |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | ==== without defdata (saves all fields) ====== |
---|
| 140 | m:se=1, Vstyle= |
---|
| 141 | 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 |
---|
| 142 | 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 |
---|
| 143 | j:0, 1, rx=0, 0, 0, dx=1, 0, 0, stif=1, rotstif=1, stam=0.25, i=, Vstyle=joint |
---|
| 144 | n:p=1, j=-1, d=N, i=, Vstyle=neuro |
---|
| 145 | n:p=-1, j=0, d="|:p=0.25,r=1", i=, Vstyle=neuro |
---|
| 146 | n:p=-1, j=0, d=G, i=, Vstyle=neuro |
---|
| 147 | c:0, 2, 1.23, i= |
---|
| 148 | c:1, 0, 1, i= |
---|
| 149 | |
---|
| 150 | *************************************************************************/ |
---|
| 151 | |
---|