Changeset 972
- Timestamp:
- 07/03/20 00:32:23 (4 years ago)
- Location:
- cpp/frams
- Files:
-
- 20 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()) -
cpp/frams/genetics/geno.cpp
r955 r972 24 24 } 25 25 26 bool Geno::formatIsOneOf(const SString& format, const SString& format_list) 27 { 28 if (strchr(format_list.c_str(), ',') == NULL) 29 return format == format_list; 30 else 31 { 32 SString item; int pos = 0; 33 while (format_list.getNextToken(pos, item, ',')) 34 if (item == format) 35 return true; 36 } 37 return false; 38 } 39 26 40 const SString Geno::INVALID_FORMAT = "invalid"; 27 41 const SString Geno::UNKNOWN_FORMAT = ""; 42 const SString Geno::F0_FORMAT_LIST = "0,0s"; 43 44 bool Geno::isF0Format(const SString& format_list) 45 { 46 if (strchr(format_list.c_str(), ',') == NULL) 47 return formatIsOneOf(format_list, F0_FORMAT_LIST); 48 SString item; int pos = 0; 49 while (format_list.getNextToken(pos, item, ',')) 50 if (!formatIsOneOf(item, F0_FORMAT_LIST)) 51 return false; 52 return true; 53 } 28 54 29 55 void Geno::init(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment) … … 41 67 { 42 68 SString format = trim(input); 43 if (format.len () == 0 || strContainsOneOf(format.c_str(), " \r\n\t"))69 if (format.length() == 0 || strContainsOneOf(format.c_str(), " \r\n\t")) 44 70 return Geno::INVALID_FORMAT; 45 71 return format; … … 73 99 genformat = trimAndValidateFormat(genstring.substr(2)); 74 100 gencopy = ""; 75 mapinshift = genstring.len ();101 mapinshift = genstring.length(); 76 102 } 77 103 break; … … 88 114 genformat = trimAndValidateFormat(genstring.substr(2)); 89 115 gencopy = ""; 90 mapinshift = genstring.len ();116 mapinshift = genstring.length(); 91 117 } 92 118 break; … … 95 121 { 96 122 SString cut; 97 if (error_end < 0) error_end = genstring.len ();123 if (error_end < 0) error_end = genstring.length(); 98 124 static const int MAX_ERROR = 20; 99 125 if (error_end > MAX_ERROR) … … 104 130 if (lf >= 0) { if ((lf > 0) && (cut[lf - 1] == '\r')) lf--; cut = cut.substr(0, lf); } 105 131 sstringQuote(cut); 106 logPrintf("Geno", "init", LOG_ERROR, "Invalid genotype format declaration: '%s'%s", cut.c_str(), name.len () ? SString::sprintf(" in '%s'", name.c_str()).c_str() : "");132 logPrintf("Geno", "init", LOG_ERROR, "Invalid genotype format declaration: '%s'%s", cut.c_str(), name.length() ? SString::sprintf(" in '%s'", name.c_str()).c_str() : ""); 107 133 } 108 134 … … 212 238 int Geno::mapGenToString(int genpos) const 213 239 { 214 if (genpos > gen.len ()) return -2;240 if (genpos > gen.length()) return -2; 215 241 if (genpos < 0) return -1; 216 242 return mapinshift + genpos; … … 220 246 { 221 247 stringpos -= mapinshift; 222 if (stringpos > gen.len ()) return -2;248 if (stringpos > gen.length()) return -2; 223 249 if (stringpos < 0) return -1; 224 250 return stringpos; … … 232 258 int ModelGenoValidator::testGenoValidity(Geno& g) 233 259 { 234 if ( g.getFormat() == "0")235 { 236 Model mod(g );260 if (Geno::formatIsOneOf(g.getFormat(), Geno::F0_FORMAT_LIST)) 261 { 262 Model mod(g, Model::SHAPE_UNKNOWN); 237 263 return mod.isValid(); 238 264 } … … 240 266 { 241 267 bool converter_missing; 242 Geno f0geno = g.getConverted( "0", NULL, false, &converter_missing);268 Geno f0geno = g.getConverted(Geno::F0_FORMAT_LIST, NULL, false, &converter_missing); 243 269 if (converter_missing) 244 270 return -1;//no result … … 250 276 { 251 277 if (isvalid >= 0) return; 252 if (gen.len () == 0) { isvalid = 0; return; }278 if (gen.length() == 0) { isvalid = 0; return; } 253 279 if (format == INVALID_FORMAT) { isvalid = 0; return; } 254 280 Validators* vals = getValidators(); … … 309 335 } 310 336 311 Geno Geno::getConverted(SString otherformat , MultiMap *m, bool using_checkpoints, bool *converter_missing)312 { 313 if ( otherformat == getFormat()) { if (converter_missing) *converter_missing = false; return *this; }337 Geno Geno::getConverted(SString otherformat_list, MultiMap *m, bool using_checkpoints, bool *converter_missing) 338 { 339 if (formatIsOneOf(getFormat(), otherformat_list)) { if (converter_missing) *converter_missing = false; return *this; } 314 340 #ifndef NO_GENOCONVMANAGER 315 341 GenoConvManager *converters = getConverters(); 316 342 if (converters) 317 343 { 318 if (( otherformat == "0") && (!m) && (!using_checkpoints))344 if ((isF0Format(otherformat_list)) && (!m) && (!using_checkpoints)) 319 345 { 320 346 if (!f0gen) 321 f0gen = new Geno(converters->convert(*this, otherformat , NULL, using_checkpoints, converter_missing));347 f0gen = new Geno(converters->convert(*this, otherformat_list, NULL, using_checkpoints, converter_missing)); 322 348 else 323 349 { … … 327 353 } 328 354 else 329 return converters->convert(*this, otherformat , m, using_checkpoints, converter_missing);355 return converters->convert(*this, otherformat_list, m, using_checkpoints, converter_missing); 330 356 } 331 357 #endif 332 358 if (converter_missing) *converter_missing = true; 333 return ( otherformat == getFormat()) ? *this : Geno("", "", "", "GenConvManager not available");359 return (formatIsOneOf(getFormat(), otherformat_list)) ? *this : Geno("", "", "", "GenConvManager not available"); 334 360 } 335 361 -
cpp/frams/genetics/geno.h
r955 r972 129 129 static GenoConvManager* useConverters(GenoConvManager* gcm); 130 130 static GenoConvManager* getConverters(); 131 132 static bool formatIsOneOf(const SString& format, const SString& format_list); 133 static bool isF0Format(const SString& format_list); 134 static const SString F0_FORMAT_LIST; 131 135 }; 132 136 -
cpp/frams/genetics/genoconv.cpp
r955 r972 123 123 /// (can be NULL if you don't need this information) 124 124 125 GenoConverter **GenoConvManager::getPath(const SString& in , const SString& out, GenoConverter **path, int maxlen, int *mapavailable)125 GenoConverter **GenoConvManager::getPath(const SString& in_format, const SString& out_format_list, GenoConverter **path, int maxlen, int *mapavailable) 126 126 { 127 127 if (!maxlen) return 0; … … 130 130 for (; gk = (GenoConverter*)converters(i); i++) 131 131 { 132 if ((gk->enabled) && (gk->in_format == in ))132 if ((gk->enabled) && (gk->in_format == in_format)) 133 133 { 134 134 *path = gk; 135 if ( gk->out_format == out)135 if (Geno::formatIsOneOf(gk->out_format, out_format_list)) 136 136 { 137 137 if (mapavailable) … … 142 142 { 143 143 int mapavail; 144 GenoConverter **ret = getPath(gk->out_format, out , path + 1, maxlen - 1, &mapavail);144 GenoConverter **ret = getPath(gk->out_format, out_format_list, path + 1, maxlen - 1, &mapavail); 145 145 if (ret) 146 146 { … … 155 155 } 156 156 157 Geno GenoConvManager::convert(Geno &in, SString format, MultiMap *map, bool using_checkpoints, bool *converter_missing) 158 { 159 if (in.getFormat() == format) { if (converter_missing) *converter_missing = false; return in; } 157 Geno GenoConvManager::convert(Geno &in, SString format_list, MultiMap *map, bool using_checkpoints, bool *converter_missing) 158 { 159 if (Geno::formatIsOneOf(in.getFormat(), format_list)) 160 { 161 if (converter_missing) *converter_missing = false; return in; 162 } 160 163 GenoConverter *path[10]; 161 164 int dep; … … 164 167 int mapavail; 165 168 for (dep = 1; dep < (int)sizeof(path); dep++) //iterative deepening 166 if (ret = getPath(in.getFormat(), format , path, dep, &mapavail)) break;169 if (ret = getPath(in.getFormat(), format_list, path, dep, &mapavail)) break; 167 170 if (!ret) { if (converter_missing) *converter_missing = true; return Geno("", Geno::INVALID_FORMAT, "", "converter not found"); } 168 171 if (converter_missing) *converter_missing = false; 169 172 if (!map) mapavail = 0; 170 173 GenoConverter **t = path; 171 SString tmp ;174 SString tmp, out_format; 172 175 tmp = in.getGenes(); 173 176 MultiMap lastmap, tmpmap; … … 177 180 GenoConverter *gk = *t; 178 181 tmp = gk->convert(tmp, mapavail ? &tmpmap : 0, using_checkpoints); 179 if (!tmp.len()) 182 out_format = gk->out_format; 183 if (!tmp.length()) 180 184 { 181 185 string t = ssprintf("f%s->f%s conversion failed (%s)", gk->in_format.c_str(), gk->out_format.c_str(), gk->name); … … 200 204 if (map) 201 205 *map = lastmap; 202 return Geno(tmp, format, in.getName(), in.getComment());203 } 206 return Geno(tmp, out_format, in.getName(), in.getComment()); 207 } -
cpp/frams/genetics/genoconv.h
r955 r972 77 77 /// make a genotype in other format. genotype will be invalid 78 78 /// if GenoConvManager cannot convert it. 79 Geno convert(Geno &in, SString format , MultiMap *map = 0, bool using_checkpoints = false, bool *converter_missing = NULL);79 Geno convert(Geno &in, SString format_list, MultiMap *map = 0, bool using_checkpoints = false, bool *converter_missing = NULL); 80 80 /// register GenoConverter, the added object will be automatically deleted when GenoConvManager is destructed (call removeConverter() if this is not desirable) 81 81 void addConverter(GenoConverter *conv); -
cpp/frams/model/model.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 … … 11 11 #define F0_CHECKPOINT_LINE "checkpoint:" 12 12 13 Model::Model( )13 Model::Model(ShapeType sh) 14 14 { 15 15 autobuildmaps = false; 16 init( );17 } 18 19 void Model::init( )16 init(sh); 17 } 18 19 void Model::init(ShapeType sh) 20 20 { 21 21 partmappingchanged = 0; … … 30 30 f0genoknown = 1; 31 31 shape = SHAPE_UNKNOWN; 32 declared_shape = sh; 33 } 34 35 void Model::declareShapeType(ShapeType sh) 36 { 37 declared_shape = sh; 32 38 } 33 39 … … 51 57 f0genoknown = 0; 52 58 shape = mod.shape; 59 declared_shape = mod.declared_shape; 53 60 startenergy = mod.startenergy; 54 61 modelfromgenotype = mod.modelfromgenotype; … … 93 100 94 101 95 Model::Model(const Geno &src, bool buildmaps, bool _using_checkpoints, bool _is_checkpoint)102 Model::Model(const Geno &src, ShapeType st, bool buildmaps, bool _using_checkpoints, bool _is_checkpoint) 96 103 :autobuildmaps(buildmaps) 97 104 { 98 init(src, _using_checkpoints, _is_checkpoint);105 init(src, st, _using_checkpoints, _is_checkpoint); 99 106 } 100 107 … … 110 117 :autobuildmaps(buildmaps) 111 118 { 112 init( );119 init(mod.declared_shape); 113 120 open(_using_checkpoints, _is_checkpoint); 114 121 internalCopy(mod); … … 123 130 } 124 131 125 void Model::init(const Geno &src, bool _using_checkpoints, bool _is_checkpoint)126 { 127 init( );132 void Model::init(const Geno &src, ShapeType sh, bool _using_checkpoints, bool _is_checkpoint) 133 { 134 init(sh); 128 135 using_checkpoints = _using_checkpoints; 129 136 is_checkpoint = _is_checkpoint; … … 162 169 delMap(); 163 170 delF0Map(); 164 init( );171 init(declared_shape); 165 172 geno = Geno(); 166 173 f0geno = Geno(); … … 333 340 f0warnposition = -1; 334 341 MultiMap *convmap = autobuildmaps ? new MultiMap() : NULL; 335 f0geno = (geno.getFormat() == "0") ? geno : geno.getConverted("0", convmap, using_checkpoints); 342 if (declared_shape == SHAPE_UNKNOWN) 343 f0geno = geno.getConverted(Geno::F0_FORMAT_LIST, convmap, using_checkpoints); 344 else 345 f0geno = geno.getConverted(genoFormatForShapeType(declared_shape), convmap, using_checkpoints); 336 346 f0genoknown = 1; 337 347 if (f0geno.isInvalid()) … … 467 477 modelparam.select(this); 468 478 modelparam.saveSingleLine(mod_props, handle_defaults ? &defaultmodel : NULL, true, !handle_defaults); 469 if (mod_props.len () > 1) //are there any non-default values? ("\n" is empty)479 if (mod_props.length() > 1) //are there any non-default values? ("\n" is empty) 470 480 { 471 481 gen += "m:"; … … 476 486 { 477 487 partparam.select(p); 478 len = gen.len ();488 len = gen.length(); 479 489 gen += "p:"; 480 490 partparam.saveSingleLine(gen, handle_defaults ? &defaultpart : NULL, true, !handle_defaults); 481 491 if (map) 482 map->add(len, gen.len () - 1, partToMap(i));492 map->add(len, gen.length() - 1, partToMap(i)); 483 493 } 484 494 for (i = 0; j = (Joint *)joints(i); i++) 485 495 { 486 496 jointparam.select(j); 487 len = gen.len ();497 len = gen.length(); 488 498 jointparam.setParamTab(j->usedelta ? f0_joint_paramtab : f0_nodeltajoint_paramtab); 489 499 gen += "j:"; 490 500 jointparam.saveSingleLine(gen, handle_defaults ? &defaultjoint : NULL, true, !handle_defaults); 491 501 if (map) 492 map->add(len, gen.len () - 1, jointToMap(i));502 map->add(len, gen.length() - 1, jointToMap(i)); 493 503 } 494 504 for (i = 0; n = (Neuro *)neurons(i); i++) 495 505 { 496 506 neuroparam.select(n); 497 len = gen.len ();507 len = gen.length(); 498 508 gen += "n:"; 499 509 neuroparam.saveSingleLine(gen, handle_defaults ? &defaultneuro : NULL, true, !handle_defaults); 500 510 if (map) 501 map->add(len, gen.len () - 1, neuroToMap(i));511 map->add(len, gen.length() - 1, neuroToMap(i)); 502 512 } 503 513 for (a = 0; a < neurons.size(); a++) … … 526 536 nc.info = **s; 527 537 connparam.select(&nc); 528 len = gen.len ();538 len = gen.length(); 529 539 gen += "c:"; 530 540 connparam.saveSingleLine(gen, handle_defaults ? &defaultconn : NULL, true, !handle_defaults); 531 541 if (map) 532 map->add(len, gen.len () - 1, neuroToMap(n->refno));542 map->add(len, gen.length() - 1, neuroToMap(n->refno)); 533 543 } 534 544 } … … 541 551 } 542 552 543 g = Geno(gen.c_str(), '0'); 553 g = Geno(gen.c_str(), genoFormatForShapeType(getShapeType()), "", ""); 554 } 555 556 SString Model::genoFormatForShapeType(ShapeType st) 557 { 558 switch (st) 559 { 560 case SHAPE_BALL_AND_STICK: 561 return "0"; 562 case SHAPE_SOLIDS: 563 return "0s"; 564 default: 565 return Geno::INVALID_FORMAT; 566 } 567 } 568 569 Model::ShapeType Model::shapeTypeForGenoFormat(const SString& format) 570 { 571 if (format == "0") 572 return SHAPE_BALL_AND_STICK; 573 else if (format == "0s") 574 return SHAPE_SOLIDS; 575 else 576 return SHAPE_UNKNOWN; 577 578 } 579 580 const char* Model::getShapeTypeName(ShapeType sh) 581 { 582 switch (sh) 583 { 584 case SHAPE_BALL_AND_STICK: return "ball-and-stick"; 585 case SHAPE_SOLIDS: return "solid shapes"; 586 case SHAPE_UNKNOWN: return "unknown"; 587 588 case SHAPE_ILLEGAL: 589 default: 590 return "illegal"; 591 } 544 592 } 545 593 … … 653 701 if (result < 0) 654 702 { 655 if (error_message.len () == 0) // generic error when no detailed message is available703 if (error_message.length() == 0) // generic error when no detailed message is available 656 704 error_message = "Invalid f0 code"; 657 705 if (line_num > 0) … … 731 779 { 732 780 // default class for unparented units: standard neuron 733 if (nu->getClassName().len () == 0) nu->setClassName("N");781 if (nu->getClassName().length() == 0) nu->setClassName("N"); 734 782 } 735 783 /* … … 832 880 SString Model::nameForErrors() const 833 881 { 834 if (geno.getName().len () > 0)882 if (geno.getName().length() > 0) 835 883 return SString::sprintf(" in '%s'", geno.getName().c_str()); 836 884 return SString::empty(); … … 1061 1109 if (shape == SHAPE_ILLEGAL) 1062 1110 ret = 0; 1111 else if ((declared_shape != SHAPE_UNKNOWN) && (declared_shape != shape)) 1112 { 1113 logPrintf("Model", "internalCheck", LOG_ERROR, "Model shape type '%s' does not match the declared type '%s'", getShapeTypeName(shape), getShapeTypeName(declared_shape)); 1114 ret = 0; 1115 } 1116 1063 1117 return ret; 1064 1118 } -
cpp/frams/model/model.h
r935 r972 102 102 protected: 103 103 ShapeType shape; 104 ShapeType declared_shape; 104 105 105 106 SString nameForErrors() const; 106 107 int internalcheck(CheckType check); 107 108 108 void init(const Geno &srcgen, bool _using_checkpoints, bool _is_checkpoint);109 void init( );109 void init(const Geno &srcgen, ShapeType sh, bool _using_checkpoints, bool _is_checkpoint); 110 void init(ShapeType sh); 110 111 111 112 void delMap(); … … 136 137 bool isUsingCheckpoints() const { return using_checkpoints; } 137 138 bool isCheckpoint() const { return is_checkpoint; } 139 static SString genoFormatForShapeType(ShapeType st); 140 static ShapeType shapeTypeForGenoFormat(const SString& format); 141 static const char* getShapeTypeName(ShapeType sh); 138 142 139 143 void updateRefno(); // set ::refno for all elements … … 150 154 ModelUserTags userdata; 151 155 152 /// Create empty model with invalid empty genotype 153 Model(); 156 /// Create empty model with invalid empty genotype, declaring the shape type for later operations 157 Model(ShapeType sh = SHAPE_UNKNOWN); 158 159 /// Change the declared shape type of the Model 160 void declareShapeType(ShapeType sh); 154 161 155 162 /** Create a model based on provided genotype … … 158 165 @see getMap() 159 166 */ 160 Model(const Geno &src, bool buildmaps = false, bool _using_checkpoints = false, bool _is_checkpoint = false);167 Model(const Geno &src, ShapeType sh, bool buildmaps = false, bool _using_checkpoints = false, bool _is_checkpoint = false); 161 168 Model(const Model &mod, bool buildmaps = false, bool _using_checkpoints = false, bool _is_checkpoint = false); 162 169 /** duplicate the model. -
cpp/frams/model/modelobj.cpp
r732 r972 58 58 void ModelObj::p_newfromstring(ExtValue *args, ExtValue *ret) 59 59 { 60 *ret = makeDynamicObject(new Model(Geno(args[0].getString()) ));60 *ret = makeDynamicObject(new Model(Geno(args[0].getString()), Model::SHAPE_UNKNOWN)); 61 61 } 62 62 … … 65 65 Geno *g = GenoObj::fromObject(args[0].getObject()); 66 66 if (g) 67 *ret = makeDynamicObject(new Model(*g ));67 *ret = makeDynamicObject(new Model(*g, Model::SHAPE_UNKNOWN)); 68 68 else 69 69 ret->setEmpty(); … … 74 74 Model *m = NULL; 75 75 if (args[0].getType() == TString) 76 m = new Model(Geno(args[0].getString()), false, true);76 m = new Model(Geno(args[0].getString()), Model::SHAPE_UNKNOWN, false, true); 77 77 else 78 78 { 79 79 Geno *g = GenoObj::fromObject(args[0].getObject(), false); 80 80 if (g) 81 m = new Model(*g, false, true);81 m = new Model(*g, Model::SHAPE_UNKNOWN, false, true); 82 82 else 83 83 logPrintf("Model", "newWithCheckpoints", LOG_ERROR, "Geno or string expected, %s found", args[0].typeDescription().c_str()); -
cpp/frams/model/modelparts.h
r952 r972 34 34 { 35 35 public: 36 enum ShapeType { SHAPE_BALL_AND_STICK , SHAPE_SOLIDS, SHAPE_UNKNOWN, SHAPE_ILLEGAL };36 enum ShapeType { SHAPE_BALL_AND_STICK = 0, SHAPE_SOLIDS = 1, SHAPE_UNKNOWN, SHAPE_ILLEGAL }; ///< 0 and 1 have special significance - these values allow for bit operations. 37 37 }; 38 38 -
cpp/frams/model/similarity/simil_model.cpp
r877 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 … … 122 122 123 123 // assign matching function 124 int (ModelSimil:: * pfMatchingFunction) () = NULL;124 int (ModelSimil:: * pfMatchingFunction) () = NULL; 125 125 126 126 pfMatchingFunction = &ModelSimil::MatchPartsGeometry; … … 534 534 return NULL; 535 535 } 536 Model *m = new Model(*g );536 Model *m = new Model(*g, Model::SHAPE_UNKNOWN); 537 537 if (!m->isValid()) 538 538 { … … 2058 2058 { 2059 2059 if (geo) 2060 dist[i *bigger + j] += m_adFactors[3] * m_aPositions[1 - m_iSmaller][i].length();2060 dist[i * bigger + j] += m_adFactors[3] * m_aPositions[1 - m_iSmaller][i].length(); 2061 2061 else 2062 dist[i *bigger + j] = m_adFactors[1] * m_aDegrees[1 - m_iSmaller][i][DEGREE] + m_adFactors[2] * m_aDegrees[1 - m_iSmaller][i][NEURONS];2062 dist[i * bigger + j] = m_adFactors[1] * m_aDegrees[1 - m_iSmaller][i][DEGREE] + m_adFactors[2] * m_aDegrees[1 - m_iSmaller][i][NEURONS]; 2063 2063 } 2064 2064 // compute distance between parts … … 2066 2066 { 2067 2067 if (geo) 2068 dist[i *bigger + j] += m_adFactors[3] * m_aPositions[1 - m_iSmaller][i].distanceTo(m_aPositions[m_iSmaller][j]);2068 dist[i * bigger + j] += m_adFactors[3] * m_aPositions[1 - m_iSmaller][i].distanceTo(m_aPositions[m_iSmaller][j]); 2069 2069 else 2070 dist[i *bigger + j] = m_adFactors[1] * abs(m_aDegrees[1 - m_iSmaller][i][DEGREE] - m_aDegrees[m_iSmaller][j][DEGREE])2070 dist[i * bigger + j] = m_adFactors[1] * abs(m_aDegrees[1 - m_iSmaller][i][DEGREE] - m_aDegrees[m_iSmaller][j][DEGREE]) 2071 2071 + m_adFactors[2] * abs(m_aDegrees[1 - m_iSmaller][i][NEURONS] - m_aDegrees[m_iSmaller][j][NEURONS]); 2072 2072 } … … 2104 2104 int nBigger = m_Mod[1 - m_iSmaller]->getPartCount(); 2105 2105 2106 double* partsDistances = new double[nBigger *nBigger]();2106 double* partsDistances = new double[nBigger * nBigger](); 2107 2107 FillPartsDistances(partsDistances, nBigger, nSmaller, false); 2108 2108 int *assignment = new int[nBigger](); … … 2138 2138 // - models (m_Mod) exist and are available 2139 2139 // - all properties are created and available (m_aDegrees and m_aPositions) 2140 double* tmpPartsDistances = new double[nBigger *nBigger]();2140 double* tmpPartsDistances = new double[nBigger * nBigger](); 2141 2141 std::copy(partsDistances, partsDistances + nBigger * nBigger, tmpPartsDistances); 2142 2142 // recompute geometric properties according to the transformation iTransform -
cpp/frams/userinput/modelcheckpoints.cpp
r741 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 … … 45 45 freeModel(); 46 46 if (g != NULL) 47 mod = new Model(*g, true, false);47 mod = new Model(*g, Model::SHAPE_UNKNOWN, true, false); 48 48 toguiCheckpoints(use_checkpoints ? EnabledUnknown : Disabled, 1, 1); 49 49 int ret = model_view.showModel(mod); … … 58 58 { 59 59 if (p == current_value) return; 60 mod_checkpoints = new Model(mod->getGeno(), false, true);60 mod_checkpoints = new Model(mod->getGeno(), Model::SHAPE_UNKNOWN, false, true); 61 61 if (mod_checkpoints->getCheckpointCount() > 1) 62 62 { -
cpp/frams/vm/classes/genoobj.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 … … 64 64 void GenoObj::get_autoname(ExtValue *ret) 65 65 { 66 Model m(*this );66 Model m(*this, Model::SHAPE_UNKNOWN); 67 67 ret->setString(AutoName::makeName(m)); 68 68 } … … 97 97 void GenoObj::get_f0genotype(ExtValue *ret) 98 98 { 99 ret->setString(getConverted( '0').getGenes());99 ret->setString(getConverted(Geno::F0_FORMAT_LIST).getGenes()); 100 100 } 101 101 … … 105 105 { 106 106 char ch = v.getInt(); 107 return SString(&ch, 1);107 return SString(&ch, 1); 108 108 } 109 109 if (v.getType() == TString) … … 144 144 #ifdef __CODEGUARD__ 145 145 static GenoObj static_genoobj; 146 static Param static_genoparam(geno_paramtab, &static_genoobj);146 static Param static_genoparam(geno_paramtab, &static_genoobj); 147 147 #else 148 148 static Param static_genoparam(geno_paramtab);
Note: See TracChangeset
for help on using the changeset viewer.