source: cpp/frams/_demos/genomanipulation.cpp @ 1058

Last change on this file since 1058 was 1018, checked in by Maciej Komosinski, 4 years ago

Make genomanipulation.cpp results consistent across platforms by using our random number generator instead of stdlib's

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