[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <stdio.h> |
---|
| 7 | #include <time.h> |
---|
[382] | 8 | #include <common/virtfile/stdiofile.h> |
---|
[109] | 9 | |
---|
| 10 | #include <frams/model/model.h> |
---|
[145] | 11 | #include <frams/genetics/preconfigured.h> |
---|
[391] | 12 | #include <common/loggers/loggertostdout.h> |
---|
[109] | 13 | |
---|
| 14 | /** |
---|
| 15 | @file |
---|
| 16 | Sample code: Accessing model elements |
---|
| 17 | */ |
---|
| 18 | |
---|
[145] | 19 | PreconfiguredGenetics genetics; |
---|
[109] | 20 | |
---|
| 21 | void printNiceBanner(const char* title) |
---|
| 22 | { |
---|
[288] | 23 | printf(" #############################################\n" |
---|
| 24 | " ## ##\n" |
---|
| 25 | " ## %-37s ##\n" |
---|
| 26 | " ## ##\n" |
---|
| 27 | " #############################################\n",title); |
---|
[109] | 28 | } |
---|
| 29 | void printProperties(Param &pi) |
---|
| 30 | { |
---|
| 31 | printf(" # id type name group (%d properties)\n",pi.getPropCount()); |
---|
| 32 | for (int i=0;i<pi.getPropCount();i++) |
---|
| 33 | { |
---|
| 34 | const char* type=pi.type(i); |
---|
| 35 | if (*type=='p') continue; |
---|
[348] | 36 | printf("%2d. %8s = %-20s %-3s %-10s %-10s\n",i,pi.id(i),pi.get(i).c_str(),pi.type(i),pi.name(i),pi.grname(pi.group(i))); |
---|
[109] | 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | #define PRINT_PROPERTIES(p) {Param tmp_param(p); printProperties(tmp_param);} |
---|
| 41 | |
---|
| 42 | void changeOneProperty(Param &pi) |
---|
| 43 | { |
---|
| 44 | if (pi.getPropCount()<=0) return; |
---|
| 45 | int i=rand() % pi.getPropCount(); |
---|
| 46 | double maxprop=1,minprop=0,def; |
---|
| 47 | pi.getMinMax(i,minprop,maxprop,def); |
---|
| 48 | printf(" Change property #%d to random value from range [%g..%g]\n",i,minprop,maxprop); |
---|
[348] | 49 | printf(" Current value of '%s' (%s) is '%s'\n",pi.id(i),pi.name(i),pi.get(i).c_str()); |
---|
[109] | 50 | char t[100]; |
---|
| 51 | sprintf(t,"%g",minprop+(rnd01)*(maxprop-minprop)); |
---|
| 52 | printf(" Setting new value... [ using ParamInterface::set() ]\n"); |
---|
| 53 | pi.set(i,t); |
---|
[348] | 54 | printf(" The value is now '%s'\n",pi.get(i).c_str()); |
---|
[109] | 55 | } |
---|
| 56 | |
---|
| 57 | #define CHANGE_ONE_PROPERTY(p) {Param tmp_param(p); changeOneProperty(tmp_param);} |
---|
| 58 | |
---|
| 59 | void moreAboutPart(Part* p) |
---|
| 60 | { |
---|
| 61 | printf("Here is the full listing of properties as they are printed in f0\n" |
---|
| 62 | " (please compare with f0 genotype).\n" |
---|
| 63 | "Some properties have special meaning (eg. geometry and connections groups)\n" |
---|
| 64 | "and should be handled with care, because they influence other elements of the model.\n\n" |
---|
| 65 | " [this data is provided by Part::properties() ]\n"); |
---|
| 66 | PRINT_PROPERTIES(p->properties()); |
---|
| 67 | printf("\nHowever, there is a subset of properties which may be modified more freely.\n" |
---|
| 68 | "Properties on this list are related only to this part and can be changed\n" |
---|
| 69 | "without much consideration. They are guaranteed to be always valid; any inconsistencies\n" |
---|
| 70 | "will be silently repaired.\n" |
---|
| 71 | "\n [this data is provided by Part::extraProperties() ]\n"); |
---|
| 72 | PRINT_PROPERTIES(p->extraProperties()); |
---|
| 73 | printf("\nThis set of properties can vary from release to release,\n" |
---|
| 74 | "but can be safely accessed by using extraProperties() call.\n" |
---|
| 75 | "This method accesses the full set of properies (even those\n" |
---|
| 76 | "which appear in future releases).\n" |
---|
| 77 | "Now we will try to change some of properties:\n\n"); |
---|
| 78 | p->getModel().open(); |
---|
| 79 | CHANGE_ONE_PROPERTY(p->extraProperties()); |
---|
| 80 | p->getModel().close(); |
---|
[348] | 81 | printf("\nLet's see f0... (check out part #%d !)\n\n%s\n", p->refno, p->getModel().getF0Geno().getGene().c_str()); |
---|
[109] | 82 | } |
---|
| 83 | |
---|
| 84 | void playWithAbsolute(Joint *j) |
---|
| 85 | { |
---|
| 86 | printf("\nAbsolute Joints adapt to its Parts' positions.\n" |
---|
| 87 | "We can move a Part, and it does not influence the second part, nor the Joint.\n" |
---|
| 88 | "Let's move the first Part along y axis by -0.1...\n"); |
---|
| 89 | j->getModel().open(); |
---|
| 90 | j->part1->p.y-=0.1; |
---|
| 91 | j->getModel().close(); |
---|
| 92 | printf("The Part's position is changed, but everything else stays intact:\n\n%s\n", |
---|
[348] | 93 | j->getModel().getF0Geno().getGene().c_str()); |
---|
[109] | 94 | } |
---|
| 95 | |
---|
| 96 | void playWithDelta(Joint *j) |
---|
| 97 | { |
---|
| 98 | printf("\nDelta fields (dx,dy,dz) describe relative location of the second part.\n" |
---|
| 99 | "This joint will change the second Part's positions to preserve delta distance.\n" |
---|
| 100 | "Let's move the first Part (#%d) along y axis (+0.1) and change delta.z (dz) by 0.1.\n",j->part1->refno); |
---|
| 101 | j->getModel().open(); |
---|
| 102 | j->part1->p.y+=0.1; |
---|
| 103 | j->d.z+=0.1; |
---|
| 104 | j->getModel().close(); |
---|
| 105 | printf("Position of the second Part referenced by this joint (part #%d) is now changed:\n\n%s\n", |
---|
[348] | 106 | j->part2->refno, j->getModel().getF0Geno().getGene().c_str()); |
---|
[109] | 107 | printf("If no delta fields are defined, they will be computed automatically.\n" |
---|
| 108 | "You can always delete existing delta values by using Joint::resetDelta().\n" |
---|
| 109 | "Now we will change the second Part's z position by -0.2 and call resetDelta()...\n"); |
---|
| 110 | j->getModel().open(); |
---|
| 111 | j->part2->p.z-=0.2; |
---|
| 112 | j->resetDelta(); |
---|
| 113 | j->getModel().close(); |
---|
[348] | 114 | printf("As you can see, Joint's delta fields have altered:\n\n%s\n", j->getModel().getF0Geno().getGene().c_str()); |
---|
[109] | 115 | } |
---|
| 116 | |
---|
| 117 | void switchDelta(Joint *j) |
---|
| 118 | { |
---|
| 119 | int option=! j->isDelta(); |
---|
| 120 | printf("How would this joint look like with delta option %s?\n[ by calling Joint::useDelta(%d) ]\n",option?"enabled":"disabled",option); |
---|
| 121 | j->getModel().open(); |
---|
| 122 | j->useDelta( ! j->isDelta() ); |
---|
| 123 | j->getModel().close(); |
---|
| 124 | printf("f0 is now:\n\n%s\n...so this is %s joint.\n", |
---|
[348] | 125 | j->getModel().getF0Geno().getGene().c_str(), option?"a delta":"an absolute"); |
---|
[109] | 126 | |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void moreAboutJoint(Joint* j) |
---|
| 130 | { |
---|
| 131 | printf("Similarly as with Part, the full list of properties comes first:\n\n"); |
---|
[291] | 132 | PRINT_PROPERTIES(j->properties()); |
---|
[109] | 133 | printf("\nActually, there are two kinds of Joints: delta and absolute.\n" |
---|
| 134 | "For this object, Joint::isDelta() returns %d, so this is the %s Joint.\n", |
---|
| 135 | j->isDelta(),j->isDelta()?"delta":"absolute"); |
---|
| 136 | if (j->isDelta()) |
---|
| 137 | { |
---|
| 138 | playWithDelta(j); |
---|
| 139 | switchDelta(j); |
---|
| 140 | playWithAbsolute(j); |
---|
| 141 | } |
---|
| 142 | else |
---|
| 143 | { |
---|
| 144 | playWithAbsolute(j); |
---|
| 145 | switchDelta(j); |
---|
| 146 | playWithDelta(j); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | printf("Part references and delta fields are the 'core' properties of the Joint.\n" |
---|
| 150 | "The other properties are available from Joint::extraProperties()\n" |
---|
| 151 | "and at the moment are defined as follows:\n\n"); |
---|
| 152 | PRINT_PROPERTIES(j->extraProperties()); |
---|
| 153 | printf("\nThey can be changed just like Part's extra properties:\n"); |
---|
| 154 | j->getModel().open(); |
---|
| 155 | CHANGE_ONE_PROPERTY(j->extraProperties()); |
---|
| 156 | j->getModel().close(); |
---|
[348] | 157 | printf("And after that we have this genotype:\n\n%s\n", j->getModel().getF0Geno().getGene().c_str()); |
---|
[109] | 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | void moreAboutNeuro(Neuro* n) |
---|
| 163 | { |
---|
| 164 | printf("Basic features of Neuro object are similar to those of Part and Joint.\n" |
---|
| 165 | "We can request a property list:\n\n"); |
---|
| 166 | PRINT_PROPERTIES(n->properties()); |
---|
| 167 | printf("\n...and extra properties (which are designed to be always valid and easy to change):\n\n"); |
---|
| 168 | PRINT_PROPERTIES(n->extraProperties()); |
---|
| 169 | printf("\nAs usual, we will change something:\n"); |
---|
| 170 | n->getModel().open(); |
---|
| 171 | CHANGE_ONE_PROPERTY(n->extraProperties()); |
---|
| 172 | n->getModel().close(); |
---|
| 173 | printf("Each neuron can have any number of inputs = weighted connections\n with other neurons.\n" |
---|
| 174 | "According to Neuro::getInputCount(), this one has %d inputs.\n",n->getInputCount()); |
---|
| 175 | printf("Standard API is provided for accessing those inputs (getInput(int)),\n" |
---|
| 176 | "adding inputs (addInput(Neuro*)) and removing them (removeInput(int)).\n\n"); |
---|
| 177 | |
---|
[289] | 178 | printf("\nThe most unusual thing is 'details' field (d).\n" |
---|
[109] | 179 | "It is something like separate object with its own set of properties.\n" |
---|
[348] | 180 | "Currently the value of 'd' is '%s'.\n",n->getDetails().c_str()); |
---|
[109] | 181 | |
---|
| 182 | { |
---|
| 183 | NeuroClass* cl=n->getClass(); |
---|
| 184 | if (!cl) |
---|
[348] | 185 | printf("It should contain the class name but the meaning of '%s' is unknown\n",n->getDetails().c_str()); |
---|
[109] | 186 | else |
---|
| 187 | { |
---|
| 188 | |
---|
[289] | 189 | printf("'%s' is the class name (Neuro::getClassName() == '%s') and means '%s'.\n", |
---|
[348] | 190 | cl->getName().c_str(),cl->getName().c_str(),cl->getLongName().c_str()); |
---|
[109] | 191 | printf("Neuro::getClass() gives you information about basic characteristic\n" |
---|
| 192 | "of the class, that can be analyzed automatically.\n"); |
---|
| 193 | printf("For the current object we can learn that it supports "); |
---|
| 194 | if (cl->getPreferredInputs()<0) printf("any number of inputs"); |
---|
| 195 | else if (cl->getPreferredInputs()==0) printf("no inputs"); |
---|
| 196 | else printf("%d inputs",cl->getPreferredInputs()); |
---|
| 197 | printf(" (getPreferredInputs()) "); |
---|
| 198 | printf(cl->getPreferredOutput()?"and provides meaningful output signal (getPreferredOutput()==1).\n":"and doesn't provide useful output signal (getPreferredOutput()==0).\n"); |
---|
| 199 | |
---|
| 200 | SyntParam p=n->classProperties(); |
---|
| 201 | if (p.getPropCount()>0) |
---|
| 202 | { |
---|
| 203 | printf("The class defines its own properties:\n\n [ data provided by Neuro::classProperties() ]\n"); |
---|
| 204 | printProperties(p); |
---|
| 205 | printf("and they can be changed:\n"); |
---|
| 206 | n->getModel().open(); |
---|
| 207 | changeOneProperty(p); |
---|
| 208 | p.update(); |
---|
| 209 | n->getModel().close(); |
---|
[348] | 210 | printf("After that, 'details' contains the new object: '%s'.\n",n->getDetails().c_str()); |
---|
[109] | 211 | } |
---|
| 212 | else |
---|
| 213 | printf("(This class does not have its own properties\n" |
---|
| 214 | " - Neuro::classProperties().getPropCount()==0)\n"); |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | printf("The class of this object can be changed using Neuro::setClassName()\n" |
---|
| 219 | "The following classes are available:\n" |
---|
| 220 | " [ data provided by Neuro::getClassInfo()->getProperties() ]\n\n"); |
---|
| 221 | printf(" # class description properties\n"); |
---|
| 222 | for (int i=0;i<n->getClassCount();i++) |
---|
| 223 | { |
---|
| 224 | NeuroClass* cl=n->getClass(i); |
---|
| 225 | Param p=cl->getProperties(); |
---|
[348] | 226 | printf("%2d.%6s %-20s %2d\n",i,cl->getName().c_str(),cl->getLongName().c_str(),p.getPropCount()); |
---|
[109] | 227 | } |
---|
| 228 | int cl=rand() % n->getClassCount(); |
---|
[348] | 229 | printf("\nLet's change the Neuro's class to '%s'...\n",n->getClassName(cl).c_str()); |
---|
[109] | 230 | n->getModel().open(); |
---|
| 231 | n->setClass(n->getClass(cl)); |
---|
| 232 | { |
---|
| 233 | SyntParam p=n->classProperties(); |
---|
| 234 | if (p.getPropCount()>0) |
---|
| 235 | { |
---|
| 236 | printProperties(p); |
---|
| 237 | changeOneProperty(p); |
---|
| 238 | p.update(); |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | if (n->getInputCount()>0) |
---|
| 243 | { |
---|
[348] | 244 | printf("Info for input #0 = \"%s\"\n",n->getInputInfo(0).c_str()); |
---|
| 245 | printf("Info for input #0, field \"%s\" = \"%s\"\n", "abc", n->getInputInfo(0,"abc").c_str()); |
---|
[109] | 246 | n->setInputInfo(0,"test",44); |
---|
| 247 | n->setInputInfo(0,"abc","yeah"); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | n->getModel().close(); |
---|
| 251 | printf("The final object description will be then: '%s'\nAnd the full f0 genotype:\n\n%s\n", |
---|
[348] | 252 | n->getDetails().c_str(), n->getModel().getF0Geno().getGene().c_str()); |
---|
[109] | 253 | |
---|
| 254 | |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | void findingConverters() |
---|
| 258 | { |
---|
[348] | 259 | GenoConverter *gc=Geno::getConverters()->findConverters(0,'1'); |
---|
[109] | 260 | if (gc) printf("found converter accepting f1: \"%s\"\n",gc->name); |
---|
| 261 | SListTempl<GenoConverter*> found; |
---|
[348] | 262 | Geno::getConverters()->findConverters(&found,-1,'0'); |
---|
[109] | 263 | printf("found %d converter(s) producing f0\n",found.size()); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | int main(int argc,char*argv[]) |
---|
| 267 | { |
---|
[375] | 268 | LoggerToStdout messages_to_stdout(LoggerBase::Enable); //redirect model-related errors to stdout |
---|
[291] | 269 | |
---|
[109] | 270 | srand(time(0)); |
---|
[288] | 271 | printNiceBanner("Welcome to Genotype Manipulation App!"); |
---|
[109] | 272 | |
---|
| 273 | findingConverters(); |
---|
| 274 | |
---|
| 275 | SString gen(argc>1?argv[1]:"X[|G:1.23]"); |
---|
[348] | 276 | if (!strcmp(gen.c_str(),"-")) |
---|
[109] | 277 | { |
---|
| 278 | gen=0; |
---|
| 279 | StdioFILEDontClose in(stdin); |
---|
| 280 | loadSString(&in,gen); |
---|
| 281 | } |
---|
| 282 | Geno g(gen); |
---|
[348] | 283 | printf("\nSource genotype: '%s'\n",g.getGene().c_str()); |
---|
[109] | 284 | printf(" ( format %c %s)\n", |
---|
[348] | 285 | g.getFormat(), g.getComment().c_str()); |
---|
[109] | 286 | |
---|
| 287 | Model m(g);//.getConverted('0')); |
---|
| 288 | |
---|
| 289 | if (!m.isValid()) |
---|
| 290 | { |
---|
| 291 | printf("Cannot build Model from this genotype!\n"); |
---|
| 292 | return 2; |
---|
| 293 | } |
---|
[348] | 294 | printf("Converted to f0:\n%s\n",m.getF0Geno().getGene().c_str()); |
---|
[109] | 295 | |
---|
| 296 | printf("Model contains: %d part(s)\n" |
---|
| 297 | " %d joint(s)\n" |
---|
| 298 | " %d neuron(s)\n",m.getPartCount(),m.getJointCount(),m.getNeuroCount()); |
---|
| 299 | |
---|
| 300 | printf("\nInvestigating details...\n"); |
---|
| 301 | |
---|
| 302 | if (m.getPartCount()>0) |
---|
| 303 | { |
---|
| 304 | int p=rand()%m.getPartCount(); |
---|
| 305 | printNiceBanner("P A R T O B J E C T"); |
---|
| 306 | printf(" (part # %d)\n",p); |
---|
| 307 | moreAboutPart(m.getPart(p)); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | if (m.getJointCount()>0) |
---|
| 311 | { |
---|
| 312 | int j=rand()%m.getJointCount(); |
---|
| 313 | printNiceBanner("J O I N T O B J E C T"); |
---|
| 314 | printf(" (joint # %d)\n",j); |
---|
| 315 | moreAboutJoint(m.getJoint(j)); |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | if (m.getNeuroCount()>0) |
---|
| 319 | { |
---|
| 320 | int n=rand()%m.getNeuroCount(); |
---|
| 321 | printNiceBanner("N E U R O O B J E C T"); |
---|
| 322 | printf(" (neuro # %d)\n",n); |
---|
| 323 | moreAboutNeuro(m.getNeuro(n)); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | #ifdef MODEL_V1_COMPATIBLE |
---|
| 327 | printNiceBanner("Old Neuro/NeuroItem view"); |
---|
| 328 | int nc=m.old_getNeuroCount(); |
---|
| 329 | printf("Model::old_getNeuroCount() = %d\n",nc); |
---|
| 330 | for (int i=0;i<nc;i++) |
---|
| 331 | { |
---|
| 332 | Neuro *n=m.old_getNeuro(i); |
---|
| 333 | printf("neuron #%d: p=%d, j=%d, force=%g, inertia=%g, sigmoid=%g\n", |
---|
| 334 | i,n->part_refno,n->joint_refno, |
---|
| 335 | n->force,n->inertia,n->sigmo); |
---|
| 336 | int nicount=n->getItemCount(); |
---|
| 337 | printf(" %d items\n",nicount); |
---|
| 338 | for (int j=0;j<nicount;j++) |
---|
| 339 | { |
---|
| 340 | NeuroItem *ni=n->getNeuroItem(j); |
---|
| 341 | printf(" item #%d - '%s', conn=%d, weight=%g\n", |
---|
[348] | 342 | j,ni->getDetails().c_str(),ni->conn_refno,ni->weight); |
---|
[109] | 343 | } |
---|
| 344 | } |
---|
| 345 | printf("end.\n"); |
---|
| 346 | #endif |
---|
| 347 | |
---|
| 348 | printf("\n######### THE END ###########\n\n" |
---|
| 349 | "Hints:\n" |
---|
[288] | 350 | " 1. You can redirect output: genomanipulation >filename.txt\n" |
---|
[109] | 351 | " 2. Each run can yield different results, because some\n" |
---|
| 352 | " values are randomly generated.\n" |
---|
| 353 | " 3. This application will use custom genotype passed as\n" |
---|
[288] | 354 | " a commandline parameter: genomanipulation XX\n" |
---|
[109] | 355 | "\n"); |
---|
| 356 | return 0; |
---|
| 357 | } |
---|