source: cpp/gdk/gdktest.cpp @ 68

Last change on this file since 68 was 68, checked in by Maciej Komosinski, 13 years ago

added missing sources; updated sources for compatibility with vs2008 and added project files

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