Changeset 972 for cpp/frams/_demos
- Timestamp:
- 07/03/20 00:32:23 (5 years ago)
- Location:
- cpp/frams/_demos
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/_demos/f0_variants_test.cpp
r955 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 16Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 12 12 #include <common/loggers/loggertostdout.h> 13 13 14 void save_as_f0(SString &gen, Model &m,bool omit_default_values)14 void save_as_f0(SString &gen, Model &m, bool omit_default_values) 15 15 { 16 // copied from Model::makeGeno() (with small changes)16 // copied from Model::makeGeno() (with small changes) 17 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);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 23 24 static Part defaultpart;25 static Joint defaultjoint;26 static Neuro defaultneuro;27 static Model defaultmodel;28 static NeuroConn defaultconn;24 static Part defaultpart; 25 static Joint defaultjoint; 26 static Neuro defaultneuro; 27 static Model defaultmodel; 28 static NeuroConn defaultconn; 29 29 30 modelparam.select(&m);31 gen+="m:";32 modelparam.saveSingleLine(gen,omit_default_values ? &defaultmodel : NULL);30 modelparam.select(&m); 31 gen += "m:"; 32 modelparam.saveSingleLine(gen, omit_default_values ? &defaultmodel : NULL); 33 33 34 Part *p;35 Joint *j;36 Neuro *n;34 Part *p; 35 Joint *j; 36 Neuro *n; 37 37 38 for (int i=0;p=(Part*)m.getPart(i);i++)38 for (int i = 0; p = (Part*)m.getPart(i); i++) 39 39 { 40 partparam.select(p);41 gen+="p:";42 partparam.saveSingleLine(gen,omit_default_values ? &defaultpart : NULL);40 partparam.select(p); 41 gen += "p:"; 42 partparam.saveSingleLine(gen, omit_default_values ? &defaultpart : NULL); 43 43 } 44 for (int i=0;j=(Joint*)m.getJoint(i);i++)44 for (int i = 0; j = (Joint*)m.getJoint(i); i++) 45 45 { 46 jointparam.select(j);47 jointparam.setParamTab(j->usedelta?f0_joint_paramtab:f0_nodeltajoint_paramtab);48 gen+="j:";49 jointparam.saveSingleLine(gen,omit_default_values ? &defaultjoint : NULL);46 jointparam.select(j); 47 jointparam.setParamTab(j->usedelta ? f0_joint_paramtab : f0_nodeltajoint_paramtab); 48 gen += "j:"; 49 jointparam.saveSingleLine(gen, omit_default_values ? &defaultjoint : NULL); 50 50 } 51 for (int i=0;n=(Neuro*)m.getNeuro(i);i++)51 for (int i = 0; n = (Neuro*)m.getNeuro(i); i++) 52 52 { 53 neuroparam.select(n);54 gen+="n:";55 neuroparam.saveSingleLine(gen,omit_default_values ? &defaultneuro : NULL);53 neuroparam.select(n); 54 gen += "n:"; 55 neuroparam.saveSingleLine(gen, omit_default_values ? &defaultneuro : NULL); 56 56 } 57 for (int a=0;n=(Neuro*)m.getNeuro(a);a++)57 for (int a = 0; n = (Neuro*)m.getNeuro(a); a++) 58 58 { // inputs 59 for (int b=0;b<n->getInputCount();b++)59 for (int b = 0; b < n->getInputCount(); b++) 60 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.saveSingleLine(gen,omit_default_values ? &defaultconn : NULL);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.saveSingleLine(gen, omit_default_values ? &defaultconn : NULL); 70 70 } 71 71 } 72 72 } 73 73 74 int main(int argc, char*argv[])74 int main(int argc, char*argv[]) 75 75 { 76 LoggerToStdout messages_to_stdout(LoggerBase::Enable);76 LoggerToStdout messages_to_stdout(LoggerBase::Enable); 77 77 78 //without converters the application would only handle "format 0" genotypes79 DefaultGenoConvManager gcm;80 gcm.addDefaultConverters();81 Geno::useConverters(&gcm);78 //without converters the application would only handle "format 0" genotypes 79 DefaultGenoConvManager gcm; 80 gcm.addDefaultConverters(); 81 Geno::useConverters(&gcm); 82 82 83 Geno::Validators validators;84 Geno::useValidators(&validators);85 ModelGenoValidator model_validator;86 validators+=&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 nothing83 Geno::Validators validators; 84 Geno::useValidators(&validators); 85 ModelGenoValidator model_validator; 86 validators += &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 87 87 88 SString gen(argc>1?argv[1]:"X[|G:1.23]");89 if (!strcmp(gen.c_str(),"-"))88 SString gen(argc > 1 ? argv[1] : "X[|G:1.23]"); 89 if (!strcmp(gen.c_str(), "-")) 90 90 { 91 gen=0;92 StdioFILEDontClose in(stdin);93 loadSString(&in,gen);91 gen = 0; 92 StdioFILEDontClose in(stdin); 93 loadSString(&in, gen); 94 94 } 95 Geno g(gen);96 printf("\nSource genotype: '%s'\n",g.getGenes().c_str());97 printf(" ( format %s %s)\n",98 95 Geno g(gen); 96 printf("\nSource genotype: '%s'\n", g.getGenes().c_str()); 97 printf(" ( format %s %s)\n", 98 g.getFormat().c_str(), g.getComment().c_str()); 99 99 100 Model m(g);//.getConverted('0'));100 Model m(g, Model::SHAPE_UNKNOWN);//.getConverted('0')); 101 101 102 if (!m.isValid())102 if (!m.isValid()) 103 103 { 104 printf("Cannot build Model from this genotype!\n");105 return 2;104 printf("Cannot build Model from this genotype!\n"); 105 return 2; 106 106 } 107 107 108 printf("\nthis example shows how to save a f0 genotype using low-level ParamInterface::saveSingleLine() calls\n");108 printf("\nthis example shows how to save a f0 genotype using low-level ParamInterface::saveSingleLine() calls\n"); 109 109 110 SString f0_skipping_defaults;111 SString f0_no_skipping_defaults;110 SString f0_skipping_defaults; 111 SString f0_no_skipping_defaults; 112 112 113 save_as_f0(f0_skipping_defaults,m,true);114 save_as_f0(f0_no_skipping_defaults,m,false);113 save_as_f0(f0_skipping_defaults, m, true); 114 save_as_f0(f0_no_skipping_defaults, m, false); 115 115 116 printf("\n==== with defdata (skips default values) ======\n%s\n",f0_skipping_defaults.c_str());117 printf("\n==== without defdata (saves all fields) ======\n%s\n",f0_no_skipping_defaults.c_str());116 printf("\n==== with defdata (skips default values) ======\n%s\n", f0_skipping_defaults.c_str()); 117 printf("\n==== without defdata (saves all fields) ======\n%s\n", f0_no_skipping_defaults.c_str()); 118 118 119 return 0;119 return 0; 120 120 } 121 121 … … 123 123 124 124 Source genotype: 'X[|G:1.23]' 125 125 ( format 1 ) 126 126 127 127 this example shows how to save a f0 genotype using low-level ParamInterface::saveSingleLine() calls -
cpp/frams/_demos/full_props.cpp
r534 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 16Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 47 47 */ 48 48 49 int main(int argc, char*argv[])49 int main(int argc, char*argv[]) 50 50 { 51 StdioFILE::setStdio();//setup VirtFILE::Vstdin/out/err52 LoggerToStdout messages_to_stderr(LoggerBase::Enable | LoggerBase::DontBlock,VirtFILE::Vstderr); //errors -> stderr, don't interfere with stdout51 StdioFILE::setStdio();//setup VirtFILE::Vstdin/out/err 52 LoggerToStdout messages_to_stderr(LoggerBase::Enable | LoggerBase::DontBlock, VirtFILE::Vstderr); //errors -> stderr, don't interfere with stdout 53 53 54 PreconfiguredGenetics genetics;54 PreconfiguredGenetics genetics; 55 55 56 bool reverse=false;57 char* gen_arg=0;58 for(int i=1;i<argc;i++)56 bool reverse = false; 57 char* gen_arg = 0; 58 for (int i = 1; i < argc; i++) 59 59 { 60 char* ar=argv[i];61 if (ar[0]=='-')62 switch(ar[1])60 char* ar = argv[i]; 61 if (ar[0] == '-') 62 switch (ar[1]) 63 63 { 64 case 'r': reverse =true; break;64 case 'r': reverse = true; break; 65 65 case 'h': puts("usage: full_props [-r[everse]] [genotype_or_stdin]\n"); break; 66 66 } 67 else 68 if (!gen_arg) 69 gen_arg = ar; 70 } 71 SString gen; 72 if (gen_arg) 73 gen = gen_arg; 67 74 else 68 if (!gen_arg) 69 gen_arg=ar; 70 } 71 SString gen; 72 if (gen_arg) 73 gen=gen_arg; 74 else 75 loadSString(VirtFILE::Vstdin,gen); 76 Geno g(gen); 77 Model m(g); 75 loadSString(VirtFILE::Vstdin, gen); 76 Geno g(gen); 77 Model m(g, Model::SHAPE_UNKNOWN); 78 78 79 if (!m.isValid())79 if (!m.isValid()) 80 80 { 81 logPrintf("","full_props",LOG_ERROR,"Cannot build Model from the supplied genotype\n");82 return 2;81 logPrintf("", "full_props", LOG_ERROR, "Cannot build Model from the supplied genotype\n"); 82 return 2; 83 83 } 84 84 85 m.open();86 for(int i=0;i<m.getNeuroCount();i++)85 m.open(); 86 for (int i = 0; i < m.getNeuroCount(); i++) 87 87 { 88 Neuro *n=m.getNeuro(i);89 SyntParam p=n->classProperties(reverse);90 p.update();// ...so everyone reading the source can recognize that p was created to update the neuro d field91 // but actually, calling update() here is not necessary, because ~SyntParam() would do it anyway88 Neuro *n = m.getNeuro(i); 89 SyntParam p = n->classProperties(reverse); 90 p.update();// ...so everyone reading the source can recognize that p was created to update the neuro d field 91 // but actually, calling update() here is not necessary, because ~SyntParam() would do it anyway 92 92 } 93 m.close();93 m.close(); 94 94 95 // normal f0 (omitting default values) would be retrieved using m.getF0Geno()96 // the following form allows for more control:97 Geno f0_g;98 m.makeGeno(f0_g,NULL,reverse);//third arg is "handle_defaults" == whether f0 should omit default property values99 puts(f0_g.getGenesAndFormat().c_str());95 // normal f0 (omitting default values) would be retrieved using m.getF0Geno() 96 // the following form allows for more control: 97 Geno f0_g; 98 m.makeGeno(f0_g, NULL, reverse);//third arg is "handle_defaults" == whether f0 should omit default property values 99 puts(f0_g.getGenesAndFormat().c_str()); 100 100 101 return 0;101 return 0; 102 102 } 103 103 -
cpp/frams/_demos/genoconv_test.cpp
r955 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 18Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 16 16 */ 17 17 18 /// Sample Geno converter not using Model class.19 /// (This converter generates the same output for each input).20 /// Such a converter is responsible for doing valid f0 (or other format) output and storing temporary data.18 /// Sample Geno converter not using Model class. 19 /// (This converter generates the same output for each input). 20 /// Such a converter is responsible for doing valid f0 (or other format) output and storing temporary data. 21 21 class GenoConv_Test : public GenoConverter 22 22 { … … 84 84 for (t = src; *t; t++) 85 85 { 86 if (insideneuron &&*t == ']') insideneuron = 0;86 if (insideneuron && *t == ']') insideneuron = 0; 87 87 if (*t == '[') insideneuron = 1; 88 88 if ((!insideneuron) && isdigit(*t) && t[1]) … … 93 93 dst += *t; 94 94 if (map) // fill in the map only if requested 95 map->add(t - src, t - src, dst.len () - n, dst.len() - 1);95 map->add(t - src, t - src, dst.length() - n, dst.length() - 1); 96 96 // meaning: source character (t-src) becomes (dst.len()-n ... dst.len()-1) 97 97 } … … 100 100 dst += *t; 101 101 if (map) 102 map->add(t - src, t - src, dst.len () - 1, dst.len() - 1);102 map->add(t - src, t - src, dst.length() - 1, dst.length() - 1); 103 103 // meaning: map single to single character: (t-src) into (dst.len()-1) 104 104 } … … 113 113 { 114 114 printf("Genotype:\n%s\nFormat: %s\nValid: %s\nComment: %s\n", 115 115 g.getGenes().c_str(), g.getFormat().c_str(), g.isValid() ? "yes" : "no", g.getComment().c_str()); 116 116 } 117 117 … … 167 167 if (using_checkpoints) 168 168 { // using Model with checkpoints 169 Model m1(g2, false, true);//true=using_checkpoints169 Model m1(g2, Model::SHAPE_UNKNOWN, false, true);//true=using_checkpoints 170 170 printf("\nModel built from the converted f%s genotype has %d checkpoints\n", g2.getFormat().c_str(), m1.getCheckpointCount()); 171 Model m2(g1, false, true);//true=using_checkpoints171 Model m2(g1, Model::SHAPE_UNKNOWN, false, true);//true=using_checkpoints 172 172 printf("Model built from the source f%s genotype has %d checkpoints\n", g1.getFormat().c_str(), m2.getCheckpointCount()); 173 173 // accessing individual checkpoint models (if available) … … 195 195 } 196 196 197 Model mod1(g1, 1);197 Model mod1(g1, Model::SHAPE_UNKNOWN, 1); 198 198 printf("\nModel map for f%s genotype:\n", g1.getFormat().c_str()); 199 199 ModelDisplayMap dm1(mod1); … … 202 202 mod1combined.addCombined(mod1.getMap(), dm1.getMap()); 203 203 mod1combined.print(); 204 Model mod2(g2, 1);204 Model mod2(g2, Model::SHAPE_UNKNOWN, 1); 205 205 printf("\nModel map for f%s genotype:\n", g2.getFormat().c_str()); 206 206 ModelDisplayMap dm2(mod2); -
cpp/frams/_demos/genomanipulation.cpp
r955 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 19Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 210 210 else 211 211 printf("(This class does not have its own properties\n" 212 " - Neuro::classProperties().getPropCount()==0)\n");212 " - Neuro::classProperties().getPropCount()==0)\n"); 213 213 } 214 214 } … … 230 230 { 231 231 SyntParam p = n->classProperties(); 232 if (p.getPropCount() >0)232 if (p.getPropCount() > 0) 233 233 { 234 234 printProperties(p); … … 284 284 g.getFormat().c_str(), g.getComment().c_str()); 285 285 286 Model m(g );//.getConverted('0'));286 Model m(g, Model::SHAPE_UNKNOWN);//.getConverted('0')); 287 287 288 288 if (!m.isValid()) -
cpp/frams/_demos/geometry/geometrytestutils.cpp
r737 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 17Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 24 24 { 25 25 count++; 26 totalSize += genotype->genotype.len ();27 28 fprintf(stderr, "%d. (%6d chars) %s\n", count, genotype->genotype.len (),26 totalSize += genotype->genotype.length(); 27 28 fprintf(stderr, "%d. (%6d chars) %s\n", count, genotype->genotype.length(), 29 29 genotype->name.c_str()); 30 30 } … … 63 63 if ((genoIndex == count) || (strcmp(genotype->name.c_str(), genoName) == 0)) 64 64 { 65 Model model(genotype->genotype );65 Model model(genotype->genotype, Model::SHAPE_UNKNOWN); 66 66 67 67 if (!model.isValid()) -
cpp/frams/_demos/multiline_f0_test.cpp
r955 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 15Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 13 13 #include <common/virtfile/stringfile.h> 14 14 15 int main(int argc, char*argv[])15 int main(int argc, char*argv[]) 16 16 { 17 LoggerToStdout messages_to_stdout(LoggerBase::Enable);18 PreconfiguredGenetics genetics;17 LoggerToStdout messages_to_stdout(LoggerBase::Enable); 18 PreconfiguredGenetics genetics; 19 19 20 SString gen(argc>1?argv[1]:"X[|G:1.23]");21 if (!strcmp(gen.c_str(),"-"))20 SString gen(argc > 1 ? argv[1] : "X[|G:1.23]"); 21 if (!strcmp(gen.c_str(), "-")) 22 22 { 23 gen=0;24 StdioFILEDontClose in(stdin);25 loadSString(&in,gen);23 gen = 0; 24 StdioFILEDontClose in(stdin); 25 loadSString(&in, gen); 26 26 } 27 Geno g(gen);28 printf("\nSource genotype: '%s'\n",g.getGenes().c_str());29 printf(" ( format %s %s)\n",30 27 Geno g(gen); 28 printf("\nSource genotype: '%s'\n", g.getGenes().c_str()); 29 printf(" ( format %s %s)\n", 30 g.getFormat().c_str(), g.getComment().c_str()); 31 31 32 Model m(g);//.getConverted('0'));32 Model m(g, Model::SHAPE_UNKNOWN);//.getConverted('0')); 33 33 34 if (!m.isValid())34 if (!m.isValid()) 35 35 { 36 printf("Cannot build Model from this genotype!\n");37 return 2;36 printf("Cannot build Model from this genotype!\n"); 37 return 2; 38 38 } 39 printf("Converted to f0:\n%s\n",m.getF0Geno().getGenes().c_str());39 printf("Converted to f0:\n%s\n", m.getF0Geno().getGenes().c_str()); 40 40 41 printf("\nusing Param::saveMultiLine() to create the \"expanded\" form of the f0 genotype...\n(MultiParamLoader should be able to load this)");41 printf("\nusing Param::saveMultiLine() to create the \"expanded\" form of the f0 genotype...\n(MultiParamLoader should be able to load this)"); 42 42 43 StringFILE2 f;43 StringFILE2 f; 44 44 45 static Param modelparam(f0_model_paramtab);46 static Param partparam(f0_part_paramtab);47 static Param jointparam(f0_joint_paramtab);48 static Param neuroparam(f0_neuro_paramtab);49 static Param connparam(f0_neuroconn_paramtab);45 static Param modelparam(f0_model_paramtab); 46 static Param partparam(f0_part_paramtab); 47 static Param jointparam(f0_joint_paramtab); 48 static Param neuroparam(f0_neuro_paramtab); 49 static Param connparam(f0_neuroconn_paramtab); 50 50 51 modelparam.select(&m);52 modelparam.saveMultiLine(&f,"m");51 modelparam.select(&m); 52 modelparam.saveMultiLine(&f, "m"); 53 53 54 Part *p;55 Joint *j;56 Neuro *n;54 Part *p; 55 Joint *j; 56 Neuro *n; 57 57 58 for (int i=0;p=(Part*)m.getPart(i);i++)58 for (int i = 0; p = (Part*)m.getPart(i); i++) 59 59 { 60 partparam.select(p);61 partparam.saveMultiLine(&f,"p");60 partparam.select(p); 61 partparam.saveMultiLine(&f, "p"); 62 62 } 63 for (int i=0;j=(Joint*)m.getJoint(i);i++)63 for (int i = 0; j = (Joint*)m.getJoint(i); i++) 64 64 { 65 jointparam.select(j);66 jointparam.setParamTab(j->usedelta?f0_joint_paramtab:f0_nodeltajoint_paramtab);67 jointparam.saveMultiLine(&f,"j");65 jointparam.select(j); 66 jointparam.setParamTab(j->usedelta ? f0_joint_paramtab : f0_nodeltajoint_paramtab); 67 jointparam.saveMultiLine(&f, "j"); 68 68 } 69 for (int i=0;n=(Neuro*)m.getNeuro(i);i++)69 for (int i = 0; n = (Neuro*)m.getNeuro(i); i++) 70 70 { 71 neuroparam.select(n);72 neuroparam.saveMultiLine(&f,"n");71 neuroparam.select(n); 72 neuroparam.saveMultiLine(&f, "n"); 73 73 } 74 for (int a=0;n=(Neuro*)m.getNeuro(a);a++)74 for (int a = 0; n = (Neuro*)m.getNeuro(a); a++) 75 75 { // inputs 76 for (int b=0;b<n->getInputCount();b++)76 for (int b = 0; b < n->getInputCount(); b++) 77 77 { 78 double w;79 NeuroConn nc;80 Neuro* n2=n->getInput(b,w);81 nc.n1_refno=n->refno; nc.n2_refno=n2->refno;82 nc.weight=w;83 nc.info=n->getInputInfo(b);84 connparam.select(&nc);85 connparam.saveMultiLine(&f,"c");78 double w; 79 NeuroConn nc; 80 Neuro* n2 = n->getInput(b, w); 81 nc.n1_refno = n->refno; nc.n2_refno = n2->refno; 82 nc.weight = w; 83 nc.info = n->getInputInfo(b); 84 connparam.select(&nc); 85 connparam.saveMultiLine(&f, "c"); 86 86 } 87 87 } 88 88 89 printf("\n============================\n%s\n",f.getString().c_str());89 printf("\n============================\n%s\n", f.getString().c_str()); 90 90 91 return 0;91 return 0; 92 92 } 93 93 … … 95 95 96 96 Source genotype: 'X[|G:1.23]' 97 97 ( format 1 ) 98 98 Converted to f0: 99 99 p: -
cpp/frams/_demos/neuro_layout_test.cpp
r408 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 15Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 21 21 22 22 // stl is fun? ;-) ForwardIterator implementation for element coordinates (required by min_element/max_element) 23 template <int MEMBER> struct NNIter : public std::iterator<std::forward_iterator_tag,int> //MEMBER: 0..3=x/y/w/h23 template <int MEMBER> struct NNIter : public std::iterator<std::forward_iterator_tag, int> //MEMBER: 0..3=x/y/w/h 24 24 { 25 NNLayoutState *nn; int index;26 NNIter() {}27 NNIter(NNLayoutState *_nn, int _index):nn(_nn),index(_index) {}28 int operator*() {return nn->GetXYWH(index)[MEMBER];}29 NNIter& operator++() {index++; return *this;}30 bool operator!=(const NNIter& it) {return index!=it.index;}31 bool operator==(const NNIter& it) {return index==it.index;}25 NNLayoutState *nn; int index; 26 NNIter() {} 27 NNIter(NNLayoutState *_nn, int _index) :nn(_nn), index(_index) {} 28 int operator*() { return nn->GetXYWH(index)[MEMBER]; } 29 NNIter& operator++() { index++; return *this; } 30 bool operator!=(const NNIter& it) { return index != it.index; } 31 bool operator==(const NNIter& it) { return index == it.index; } 32 32 33 static NNIter begin(NNLayoutState *_nn) {return NNIter(_nn,0);}34 static NNIter end(NNLayoutState *_nn) {return NNIter(_nn,_nn->GetElements());}33 static NNIter begin(NNLayoutState *_nn) { return NNIter(_nn, 0); } 34 static NNIter end(NNLayoutState *_nn) { return NNIter(_nn, _nn->GetElements()); } 35 35 }; 36 36 37 37 class Screen 38 38 { 39 int min_x,max_x,min_y,max_y,scale_x,scale_y;40 int rows,columns;41 char* screen;39 int min_x, max_x, min_y, max_y, scale_x, scale_y; 40 int rows, columns; 41 char* screen; 42 42 43 43 public: 44 44 45 Screen(int _min_x,int _max_x,int _min_y,int _max_y,int _scale_x,int _scale_y) 46 :min_x(_min_x),max_x(_max_x),min_y(_min_y),max_y(_max_y),scale_x(_scale_x),scale_y(_scale_y) 45 Screen(int _min_x, int _max_x, int _min_y, int _max_y, int _scale_x, int _scale_y) 46 :min_x(_min_x), max_x(_max_x), min_y(_min_y), max_y(_max_y), scale_x(_scale_x), scale_y(_scale_y) 47 { 48 columns = (max_x - min_x + scale_x - 1) / scale_x; 49 rows = (max_y - min_y + scale_y - 1) / scale_y; 50 screen = new char[rows * columns]; 51 memset(screen, ' ', rows * columns); 52 } 53 54 ~Screen() 55 { 56 delete[] screen; 57 } 58 59 void put(int x, int y, const char *str) 60 { 61 x = (x - min_x) / scale_x; 62 y = (y - min_y) / scale_y; 63 if (x < 0) return; 64 if (y < 0) return; 65 if (y >= rows) return; 66 for (; *str; str++, x++) 47 67 { 48 columns=(max_x-min_x+scale_x-1)/scale_x; 49 rows=(max_y-min_y+scale_y-1)/scale_y; 50 screen=new char[rows*columns]; 51 memset(screen,' ',rows*columns); 52 } 53 54 ~Screen() 55 { 56 delete[] screen; 57 } 58 59 void put(int x,int y,const char *str) 60 { 61 x=(x-min_x)/scale_x; 62 y=(y-min_y)/scale_y; 63 if (x<0) return; 64 if (y<0) return; 65 if (y>=rows) return; 66 for(;*str;str++,x++) 67 { 68 if (x>=columns) return; 69 screen[columns*y+x]=*str; 68 if (x >= columns) return; 69 screen[columns * y + x] = *str; 70 70 } 71 71 } 72 72 73 void print()73 void print() 74 74 { 75 for(int y=0;y<rows;y++)75 for (int y = 0; y < rows; y++) 76 76 { 77 fwrite(&screen[columns*y],1,columns,stdout);78 printf("\n");77 fwrite(&screen[columns * y], 1, columns, stdout); 78 printf("\n"); 79 79 } 80 80 } 81 81 }; 82 82 83 int main(int argc, char*argv[])83 int main(int argc, char*argv[]) 84 84 { 85 LoggerToStdout messages_to_stdout(LoggerBase::Enable);86 PreconfiguredGenetics genetics;85 LoggerToStdout messages_to_stdout(LoggerBase::Enable); 86 PreconfiguredGenetics genetics; 87 87 88 if (argc<=1)88 if (argc <= 1) 89 89 { 90 90 puts("Parameters:\n" 91 92 93 91 " 1. Genotype (or - character indicating the genotype will be read from stdin)\n" 92 " 2. (Optional) layout type (the only useful layout is 2, which is the default, see nn_simple_layout.cpp"); 93 return 10; 94 94 } 95 SString gen(argv[1]);96 if (!strcmp(gen.c_str(),"-"))95 SString gen(argv[1]); 96 if (!strcmp(gen.c_str(), "-")) 97 97 { 98 gen=0;99 StdioFILEDontClose in(stdin);100 loadSString(&in,gen);98 gen = 0; 99 StdioFILEDontClose in(stdin); 100 loadSString(&in, gen); 101 101 } 102 int layout_type=2;103 if (argc>2) layout_type=atol(argv[2]);104 Geno g(gen);105 if (!g.isValid()) {puts("invalid genotype");return 5;}106 Model m(g);107 if (!m.getNeuroCount()) {puts("no neural network");return 1;}108 printf("%d neurons,",m.getNeuroCount());102 int layout_type = 2; 103 if (argc > 2) layout_type = atol(argv[2]); 104 Geno g(gen); 105 if (!g.isValid()) { puts("invalid genotype"); return 5; } 106 Model m(g, Model::SHAPE_UNKNOWN); 107 if (!m.getNeuroCount()) { puts("no neural network"); return 1; } 108 printf("%d neurons,", m.getNeuroCount()); 109 109 110 NNLayoutState_Model nn_layout(&m);111 struct NNLayoutFunction &nnfun=nn_layout_functions[layout_type];112 printf(" using layout type=%d (%s)\n",layout_type,nnfun.name);113 nnfun.doLayout(&nn_layout);110 NNLayoutState_Model nn_layout(&m); 111 struct NNLayoutFunction &nnfun = nn_layout_functions[layout_type]; 112 printf(" using layout type=%d (%s)\n", layout_type, nnfun.name); 113 nnfun.doLayout(&nn_layout); 114 114 115 for(int i=0;i<nn_layout.GetElements();i++)115 for (int i = 0; i < nn_layout.GetElements(); i++) 116 116 { 117 int *xywh=nn_layout.GetXYWH(i);118 printf("#%-3d %s\t%d,%d\t%dx%d\n",i,m.getNeuro(i)->getClassName().c_str(),119 xywh[0],xywh[1],xywh[2],xywh[3]);117 int *xywh = nn_layout.GetXYWH(i); 118 printf("#%-3d %s\t%d,%d\t%dx%d\n", i, m.getNeuro(i)->getClassName().c_str(), 119 xywh[0], xywh[1], xywh[2], xywh[3]); 120 120 } 121 121 122 Screen screen(*std::min_element(NNIter<0>::begin(&nn_layout),NNIter<0>::end(&nn_layout))-30,123 *std::max_element(NNIter<0>::begin(&nn_layout),NNIter<0>::end(&nn_layout))+70,124 *std::min_element(NNIter<1>::begin(&nn_layout),NNIter<1>::end(&nn_layout)),125 *std::max_element(NNIter<1>::begin(&nn_layout),NNIter<1>::end(&nn_layout))+30,126 10,35);122 Screen screen(*std::min_element(NNIter<0>::begin(&nn_layout), NNIter<0>::end(&nn_layout)) - 30, 123 *std::max_element(NNIter<0>::begin(&nn_layout), NNIter<0>::end(&nn_layout)) + 70, 124 *std::min_element(NNIter<1>::begin(&nn_layout), NNIter<1>::end(&nn_layout)), 125 *std::max_element(NNIter<1>::begin(&nn_layout), NNIter<1>::end(&nn_layout)) + 30, 126 10, 35); 127 127 128 printf("===========================================\n");129 for(int i=0;i<nn_layout.GetElements();i++)128 printf("===========================================\n"); 129 for (int i = 0; i < nn_layout.GetElements(); i++) 130 130 { 131 int *xywh=nn_layout.GetXYWH(i);132 SString label=SString::sprintf("%d:%s",i,m.getNeuro(i)->getClassName().c_str());133 screen.put(xywh[0],xywh[1],label.c_str());131 int *xywh = nn_layout.GetXYWH(i); 132 SString label = SString::sprintf("%d:%s", i, m.getNeuro(i)->getClassName().c_str()); 133 screen.put(xywh[0], xywh[1], label.c_str()); 134 134 } 135 screen.print();136 printf("===========================================\n");135 screen.print(); 136 printf("===========================================\n"); 137 137 138 138 } -
cpp/frams/_demos/neuro_test.cpp
r391 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 15Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 18 18 #ifndef SDK_WITHOUT_FRAMS 19 19 #include <frams/mech/creatmechobj.h> 20 int CreatMechObject::modeltags_id =0;21 int CreatMechObject::mechtags_id =0;20 int CreatMechObject::modeltags_id = 0; 21 int CreatMechObject::mechtags_id = 0; 22 22 #endif 23 23 24 ParamEntry creature_paramtab[] ={0};24 ParamEntry creature_paramtab[] = { 0 }; 25 25 26 26 #ifdef VEYETEST … … 33 33 #define LEARNINGSTEPS 50 34 34 35 void veyeStep(Model &m, int step)35 void veyeStep(Model &m, int step) 36 36 { 37 static float angle=0;37 static float angle = 0; 38 38 39 NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->setState(step>=LEARNINGSTEPS); //0 (learning) or 1 (normal)39 NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->setState(step >= LEARNINGSTEPS); //0 (learning) or 1 (normal) 40 40 41 NeuroImpl *ni =NeuroNetImpl::getImpl(m.getNeuro(N_VEye));42 ((NI_VectorEye*)ni)->relpos.y=0;43 ((NI_VectorEye*)ni)->relpos.z=0;44 if (NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->getNewState()<0.5)45 46 ((NI_VectorEye*)ni)->relpos.x=5.0*sin(2*M_PI*step/LEARNINGSTEPS);47 48 49 50 angle+=NeuroNetImpl::getImpl(m.getNeuro(N_VMotor))->getState();51 angle=fmod((double)angle,M_PI*2.0);52 ((NI_VectorEye*)ni)->relpos.x=5*sin(angle);53 41 NeuroImpl *ni = NeuroNetImpl::getImpl(m.getNeuro(N_VEye)); 42 ((NI_VectorEye*)ni)->relpos.y = 0; 43 ((NI_VectorEye*)ni)->relpos.z = 0; 44 if (NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->getNewState() < 0.5) 45 { //learning 46 ((NI_VectorEye*)ni)->relpos.x = 5.0 * sin(2 * M_PI * step / LEARNINGSTEPS); 47 } 48 else 49 { //VMotor controls location of VEye 50 angle += NeuroNetImpl::getImpl(m.getNeuro(N_VMotor))->getState(); 51 angle = fmod((double)angle, M_PI * 2.0); 52 ((NI_VectorEye*)ni)->relpos.x = 5 * sin(angle); 53 } 54 54 55 56 55 NeuroNetImpl::getImpl(m.getNeuro(N_Fitness))->setState(angle); //wymaga poprawy 56 //oraz trzeba przemyslec kolejnosc get/set'ow neuronow zeby sygnal sie dobrze propagowal. 57 57 } 58 58 #endif 59 59 60 int main(int argc, char*argv[])60 int main(int argc, char*argv[]) 61 61 { 62 LoggerToStdout messages_to_stdout(LoggerBase::Enable);63 PreconfiguredGenetics genetics;62 LoggerToStdout messages_to_stdout(LoggerBase::Enable); 63 PreconfiguredGenetics genetics; 64 64 65 if (argc<=1)65 if (argc <= 1) 66 66 { 67 67 puts("Parameters: <genotype> [number of simulation steps]"); 68 68 return 10; 69 69 } 70 SString gen(argv[1]);71 if (!strcmp(gen.c_str(),"-"))70 SString gen(argv[1]); 71 if (!strcmp(gen.c_str(), "-")) 72 72 { 73 gen=0;74 StdioFILEDontClose in(stdin);75 loadSString(&in,gen);73 gen = 0; 74 StdioFILEDontClose in(stdin); 75 loadSString(&in, gen); 76 76 } 77 Geno g(gen);78 if (!g.isValid()) {puts("invalid genotype");return 5;}79 Model m(g);80 if (!m.getNeuroCount()) {puts("no neural network");return 1;}81 printf("%d neurons,",m.getNeuroCount());82 NeuroFactory neurofac;83 neurofac.setStandardImplementation();84 NeuroNetConfig nn_config(&neurofac);85 NeuroNetImpl *nn=new NeuroNetImpl(m,nn_config);86 int i; Neuro *n;87 if (!nn->getErrorCount()) printf(" no errors\n");88 else77 Geno g(gen); 78 if (!g.isValid()) { puts("invalid genotype"); return 5; } 79 Model m(g, Model::SHAPE_UNKNOWN); 80 if (!m.getNeuroCount()) { puts("no neural network"); return 1; } 81 printf("%d neurons,", m.getNeuroCount()); 82 NeuroFactory neurofac; 83 neurofac.setStandardImplementation(); 84 NeuroNetConfig nn_config(&neurofac); 85 NeuroNetImpl *nn = new NeuroNetImpl(m, nn_config); 86 int i; Neuro *n; 87 if (!nn->getErrorCount()) printf(" no errors\n"); 88 else 89 89 { 90 printf(" %d errors:",nn->getErrorCount());91 int no_impl=0; SString no_impl_names;92 int init_err=0; SString init_err_names;93 for(i=0;i<m.getNeuroCount();i++)90 printf(" %d errors:", nn->getErrorCount()); 91 int no_impl = 0; SString no_impl_names; 92 int init_err = 0; SString init_err_names; 93 for (i = 0; i < m.getNeuroCount(); i++) 94 94 { 95 n=m.getNeuro(i);96 NeuroImpl *ni=NeuroNetImpl::getImpl(n);97 if (!ni)95 n = m.getNeuro(i); 96 NeuroImpl *ni = NeuroNetImpl::getImpl(n); 97 if (!ni) 98 98 { 99 if (no_impl) no_impl_names+=',';100 no_impl_names+=SString::sprintf("#%d.%s",i,n->getClassName().c_str());101 no_impl++;99 if (no_impl) no_impl_names += ','; 100 no_impl_names += SString::sprintf("#%d.%s", i, n->getClassName().c_str()); 101 no_impl++; 102 102 } 103 else if (ni->status==NeuroImpl::InitError)103 else if (ni->status == NeuroImpl::InitError) 104 104 { 105 if (init_err) init_err_names+=',';106 init_err_names+=SString::sprintf("#%d.%s",i,n->getClassName().c_str());107 init_err++;105 if (init_err) init_err_names += ','; 106 init_err_names += SString::sprintf("#%d.%s", i, n->getClassName().c_str()); 107 init_err++; 108 108 } 109 109 } 110 printf("\n"); 111 if (no_impl) printf("%d x missing implementation (%s)\n", no_impl, no_impl_names.c_str()); 112 if (init_err) printf("%d x failed initialization (%s)\n", init_err, init_err_names.c_str()); 113 } 114 int steps = 1; 115 if (argc > 2) steps = atol(argv[2]); 116 int st; 117 printf("step"); 118 for (i = 0; i < m.getNeuroCount(); i++) 119 { 120 n = m.getNeuro(i); 121 printf("\t#%d.%s", i, n->getClassName().c_str()); 122 } 110 123 printf("\n"); 111 if (no_impl) printf("%d x missing implementation (%s)\n",no_impl,no_impl_names.c_str()); 112 if (init_err) printf("%d x failed initialization (%s)\n",init_err,init_err_names.c_str()); 113 } 114 int steps=1; 115 if (argc>2) steps=atol(argv[2]); 116 int st; 117 printf("step"); 118 for(i=0;i<m.getNeuroCount();i++) 119 { 120 n=m.getNeuro(i); 121 printf("\t#%d.%s",i,n->getClassName().c_str()); 122 } 123 printf("\n"); 124 for(st=0;st<=steps;st++) 124 for (st = 0; st <= steps; st++) 125 125 { 126 126 #ifdef VEYETEST 127 veyeStep(m,st);127 veyeStep(m, st); 128 128 #endif 129 printf("%d",st);130 for(i=0;i<m.getNeuroCount();i++)129 printf("%d", st); 130 for (i = 0; i < m.getNeuroCount(); i++) 131 131 { 132 n=m.getNeuro(i);133 printf("\t%g",n->state);132 n = m.getNeuro(i); 133 printf("\t%g", n->state); 134 134 } 135 printf("\n");136 nn->simulateNeuroNet();135 printf("\n"); 136 nn->simulateNeuroNet(); 137 137 } 138 neurofac.freeImplementation();138 neurofac.freeImplementation(); 139 139 } -
cpp/frams/_demos/shapeconvert.cpp
r546 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 16Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 46 46 if (ar[0] == '-') 47 47 switch (ar[1]) 48 {48 { 49 49 case 's': shape = (Part::Shape)atol(ar + 2); 50 50 if ((shape != Part::SHAPE_ELLIPSOID) && (shape != Part::SHAPE_CUBOID) && (shape != Part::SHAPE_CYLINDER)) … … 56 56 case 't': thickness = atof(ar + 2); break; 57 57 case 'h': puts("Usage: shapeconvert [-sSHAPE] [-tTHICKNESS] [genotype_or_stdin]\n\tSHAPE: 1=ellipsoid, 2=cuboid, 3(default)=cylinder\n\tTHICKNESS: used for Part.sy/sz (default=0.2)"); break; 58 }58 } 59 59 else 60 60 if (!gen_arg) … … 67 67 loadSString(VirtFILE::Vstdin, gen); 68 68 Geno g(gen); 69 Model m(g );69 Model m(g, Model::SHAPE_UNKNOWN); 70 70 71 71 if (!m.isValid())
Note: See TracChangeset
for help on using the changeset viewer.