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 |
|
---|
18 | StdoutErrorHandler err; //redirect model-related errors to stdout
|
---|
19 | DefaultGenoConvManager gcm; //without this object the application would only handle "format 0" genotypes
|
---|
20 |
|
---|
21 | void printNiceBanner(const char* title)
|
---|
22 | {
|
---|
23 | printf(" ########################################\n"
|
---|
24 | " ## ##\n"
|
---|
25 | " ## %-32s ##\n"
|
---|
26 | " ## ##\n"
|
---|
27 | " ########################################\n",title);
|
---|
28 | }
|
---|
29 | void printProperties(ParamInterface &pi)
|
---|
30 | {
|
---|
31 | printf(" # id type name group (%d properties)\n",pi.getPropCount());
|
---|
32 | for (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 |
|
---|
38 | void changeOneProperty(ParamInterface &pi)
|
---|
39 | {
|
---|
40 | if (pi.getPropCount()<=0) return;
|
---|
41 | int i=rand() % pi.getPropCount();
|
---|
42 | double maxprop=1,minprop=0,def;
|
---|
43 | pi.getMinMax(i,minprop,maxprop,def);
|
---|
44 | printf(" Change property #%d to random value from range [%g..%g]\n",i,minprop,maxprop);
|
---|
45 | printf(" Current value of '%s' (%s) is '%s'\n",pi.id(i),pi.name(i),(const char*)pi.get(i));
|
---|
46 | char t[100];
|
---|
47 | sprintf(t,"%g",minprop+(rnd01)*(maxprop-minprop));
|
---|
48 | printf(" Setting new value... [ using ParamInterface::set() ]\n");
|
---|
49 | pi.set(i,t);
|
---|
50 | printf(" The value is now '%s'\n",(const char*)pi.get(i));
|
---|
51 | }
|
---|
52 |
|
---|
53 | void moreAboutPart(Part* p)
|
---|
54 | {
|
---|
55 | printf("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");
|
---|
60 | printProperties(p->properties());
|
---|
61 | printf("\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");
|
---|
66 | printProperties(p->extraProperties());
|
---|
67 | printf("\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");
|
---|
72 | p->getModel().open();
|
---|
73 | changeOneProperty(p->extraProperties());
|
---|
74 | p->getModel().close();
|
---|
75 | printf("\nLet's see f0... (check out part #%d !)\n\n%s\n", p->refno, (const char*)p->getModel().getF0Geno().getGene());
|
---|
76 | }
|
---|
77 |
|
---|
78 | void playWithAbsolute(Joint *j)
|
---|
79 | {
|
---|
80 | printf("\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");
|
---|
83 | j->getModel().open();
|
---|
84 | j->part1->p.y-=0.1;
|
---|
85 | j->getModel().close();
|
---|
86 | printf("The Part's position is changed, but everything else stays intact:\n\n%s\n",
|
---|
87 | (const char*)j->getModel().getF0Geno().getGene());
|
---|
88 | }
|
---|
89 |
|
---|
90 | void playWithDelta(Joint *j)
|
---|
91 | {
|
---|
92 | printf("\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);
|
---|
95 | j->getModel().open();
|
---|
96 | j->part1->p.y+=0.1;
|
---|
97 | j->d.z+=0.1;
|
---|
98 | j->getModel().close();
|
---|
99 | printf("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());
|
---|
101 | printf("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");
|
---|
104 | j->getModel().open();
|
---|
105 | j->part2->p.z-=0.2;
|
---|
106 | j->resetDelta();
|
---|
107 | j->getModel().close();
|
---|
108 | printf("As you can see, Joint's delta fields have altered:\n\n%s\n", (const char*)j->getModel().getF0Geno().getGene());
|
---|
109 | }
|
---|
110 |
|
---|
111 | void switchDelta(Joint *j)
|
---|
112 | {
|
---|
113 | int option=! j->isDelta();
|
---|
114 | printf("How would this joint look like with delta option %s?\n[ by calling Joint::useDelta(%d) ]\n",option?"enabled":"disabled",option);
|
---|
115 | j->getModel().open();
|
---|
116 | j->useDelta( ! j->isDelta() );
|
---|
117 | j->getModel().close();
|
---|
118 | printf("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 |
|
---|
123 | void moreAboutJoint(Joint* j)
|
---|
124 | {
|
---|
125 | printf("Similarly as with Part, the full list of properties comes first:\n\n");
|
---|
126 | printProperties(j->properties());
|
---|
127 | printf("\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");
|
---|
130 | if (j->isDelta())
|
---|
131 | {
|
---|
132 | playWithDelta(j);
|
---|
133 | switchDelta(j);
|
---|
134 | playWithAbsolute(j);
|
---|
135 | }
|
---|
136 | else
|
---|
137 | {
|
---|
138 | playWithAbsolute(j);
|
---|
139 | switchDelta(j);
|
---|
140 | playWithDelta(j);
|
---|
141 | }
|
---|
142 |
|
---|
143 | printf("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");
|
---|
146 | printProperties(j->extraProperties());
|
---|
147 | printf("\nThey can be changed just like Part's extra properties:\n");
|
---|
148 | j->getModel().open();
|
---|
149 | changeOneProperty(j->extraProperties());
|
---|
150 | j->getModel().close();
|
---|
151 | printf("And after that we have this genotype:\n\n%s\n", (const char*)j->getModel().getF0Geno().getGene());
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 |
|
---|
156 | void moreAboutNeuro(Neuro* n)
|
---|
157 | {
|
---|
158 | printf("Basic features of Neuro object are similar to those of Part and Joint.\n"
|
---|
159 | "We can request a property list:\n\n");
|
---|
160 | printProperties(n->properties());
|
---|
161 | printf("\n...and extra properties (which are designed to be always valid and easy to change):\n\n");
|
---|
162 | printProperties(n->extraProperties());
|
---|
163 | printf("\nAs usual, we will change something:\n");
|
---|
164 | n->getModel().open();
|
---|
165 | changeOneProperty(n->extraProperties());
|
---|
166 | n->getModel().close();
|
---|
167 | printf("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());
|
---|
169 | printf("Standard API is provided for accessing those inputs (getInput(int)),\n"
|
---|
170 | "adding inputs (addInput(Neuro*)) and removing them (removeInput(int)).\n\n");
|
---|
171 |
|
---|
172 | printf("\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 | {
|
---|
177 | NeuroClass* cl=n->getClass();
|
---|
178 | if (!cl)
|
---|
179 | printf("It should contain the class name but the meaning of '%s' is unknown\n",(const char*)n->getDetails());
|
---|
180 | else
|
---|
181 | {
|
---|
182 |
|
---|
183 | printf("'%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());
|
---|
185 | printf("Neuro::getClass() gives you information about basic characteristic\n"
|
---|
186 | "of the class, that can be analyzed automatically.\n");
|
---|
187 | printf("For the current object we can get know, that this object likes to have ");
|
---|
188 | if (cl->getPreferredInputs()<0) printf("any number of inputs");
|
---|
189 | else if (cl->getPreferredInputs()==0) printf("no inputs");
|
---|
190 | else printf("%d inputs",cl->getPreferredInputs());
|
---|
191 | printf(" (getPreferredInputs()) ");
|
---|
192 | printf(cl->getPreferredOutput()?"and provides meaningful output signal (getPreferredOutput==1).\n":"and doesn't provide useful output signal (getPreferredOutput==0).\n");
|
---|
193 |
|
---|
194 | SyntParam p=n->classProperties();
|
---|
195 | if (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 | }
|
---|
206 | else
|
---|
207 | printf("(This class does not have its own properties\n"
|
---|
208 | " - Neuro::classProperties().getPropCount()==0)\n");
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | printf("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");
|
---|
215 | printf(" # class description properties\n");
|
---|
216 | for (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 | }
|
---|
222 | int cl=rand() % n->getClassCount();
|
---|
223 | printf("\nLet's change the NeuroItem's class to '%s'...\n",(const char*)n->getClassName(cl));
|
---|
224 | n->getModel().open();
|
---|
225 | n->setClass(n->getClass(cl));
|
---|
226 | {
|
---|
227 | SyntParam p=n->classProperties();
|
---|
228 | if (p.getPropCount()>0)
|
---|
229 | {
|
---|
230 | printProperties(p);
|
---|
231 | changeOneProperty(p);
|
---|
232 | p.update();
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (n->getInputCount()>0)
|
---|
237 | {
|
---|
238 | printf("input info 0 = \"%s\"\n",(const char*)n->getInputInfo(0));
|
---|
239 | printf("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 |
|
---|
244 | n->getModel().close();
|
---|
245 | printf("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 |
|
---|
251 | void findingConverters()
|
---|
252 | {
|
---|
253 | GenoConverter *gc=gcm.findConverters(0,'1');
|
---|
254 | if (gc) printf("found converter accepting f1: \"%s\"\n",gc->name);
|
---|
255 | SListTempl<GenoConverter*> found;
|
---|
256 | gcm.findConverters(&found,-1,'0');
|
---|
257 | printf("found %d converter(s) producing f0\n",found.size());
|
---|
258 | }
|
---|
259 |
|
---|
260 | int main(int argc,char*argv[])
|
---|
261 | {
|
---|
262 | srand(time(0));
|
---|
263 | printNiceBanner("Welcome to GDK test application!");
|
---|
264 |
|
---|
265 | findingConverters();
|
---|
266 |
|
---|
267 | //Geno g(argc>1?argv[1]:"X[|G:1.23]");
|
---|
268 | Geno g(argc>1?argv[1]:"//0\np:\nn:\nc:0,0,i=\"abc=7\"\n");
|
---|
269 | printf("\nSource genotype: '%s'\n",(const char*)g.getGene());
|
---|
270 | printf(" ( format %c %s)\n",
|
---|
271 | g.getFormat(), (const char*)g.getComment());
|
---|
272 |
|
---|
273 | Model m(g);//.getConverted('0'));
|
---|
274 |
|
---|
275 | if (!m.isValid())
|
---|
276 | {
|
---|
277 | printf("Cannot build Model from this genotype!\n");
|
---|
278 | return 2;
|
---|
279 | }
|
---|
280 | printf("Converted to f0:\n%s\n",(const char*)m.getF0Geno().getGene());
|
---|
281 |
|
---|
282 | printf("Model contains: %d part(s)\n"
|
---|
283 | " %d joint(s)\n"
|
---|
284 | " %d neuron(s)\n",m.getPartCount(),m.getJointCount(),m.getNeuroCount());
|
---|
285 |
|
---|
286 | printf("\nInvestigating details...\n");
|
---|
287 |
|
---|
288 | if (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 |
|
---|
296 | if (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 |
|
---|
304 | if (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
|
---|
313 | printNiceBanner("Old Neuro/NeuroItem view");
|
---|
314 | int nc=m.old_getNeuroCount();
|
---|
315 | printf("Model::old_getNeuroCount() = %d\n",nc);
|
---|
316 | for (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 | }
|
---|
331 | printf("end.\n");
|
---|
332 | #endif
|
---|
333 |
|
---|
334 | printf("\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. Application will use custom genotype passed as\n"
|
---|
340 | " a commandline parameter: gdktest X\n"
|
---|
341 | "\n");
|
---|
342 | return 0;
|
---|
343 | }
|
---|