1 | // This file is a part of Framsticks GDK library. |
---|
2 | // Copyright (C) 2002-2006 Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.frams.alife.pl/ 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 | int main(int argc,char*argv[]) |
---|
252 | { |
---|
253 | srand(time(0)); |
---|
254 | printNiceBanner("Welcome to GDK test application!"); |
---|
255 | |
---|
256 | //Geno g(argc>1?argv[1]:"X[|G:1.23]"); |
---|
257 | Geno g(argc>1?argv[1]:"//0\np:\nn:\nc:0,0,i=\"abc=7\"\n"); |
---|
258 | printf("\nSource genotype: '%s'\n",(const char*)g.getGene()); |
---|
259 | printf(" ( format %c %s)\n", |
---|
260 | g.getFormat(), (const char*)g.getComment()); |
---|
261 | |
---|
262 | Model m(g);//.getConverted('0')); |
---|
263 | |
---|
264 | if (!m.isValid()) |
---|
265 | { |
---|
266 | printf("Cannot build Model from this genotype!\n"); |
---|
267 | return 2; |
---|
268 | } |
---|
269 | printf("Converted to f0:\n%s\n",(const char*)m.getF0Geno().getGene()); |
---|
270 | |
---|
271 | printf("Model contains: %d part(s)\n" |
---|
272 | " %d joint(s)\n" |
---|
273 | " %d neuron(s)\n",m.getPartCount(),m.getJointCount(),m.getNeuroCount()); |
---|
274 | |
---|
275 | printf("\nInvestigating details...\n"); |
---|
276 | |
---|
277 | if (m.getPartCount()>0) |
---|
278 | { |
---|
279 | int p=rand()%m.getPartCount(); |
---|
280 | printNiceBanner("P A R T O B J E C T"); |
---|
281 | printf(" (part # %d)\n",p); |
---|
282 | moreAboutPart(m.getPart(p)); |
---|
283 | } |
---|
284 | |
---|
285 | if (m.getJointCount()>0) |
---|
286 | { |
---|
287 | int j=rand()%m.getJointCount(); |
---|
288 | printNiceBanner("J O I N T O B J E C T"); |
---|
289 | printf(" (joint # %d)\n",j); |
---|
290 | moreAboutJoint(m.getJoint(j)); |
---|
291 | } |
---|
292 | |
---|
293 | if (m.getNeuroCount()>0) |
---|
294 | { |
---|
295 | int n=rand()%m.getNeuroCount(); |
---|
296 | printNiceBanner("N E U R O O B J E C T"); |
---|
297 | printf(" (neuro # %d)\n",n); |
---|
298 | moreAboutNeuro(m.getNeuro(n)); |
---|
299 | } |
---|
300 | |
---|
301 | #ifdef MODEL_V1_COMPATIBLE |
---|
302 | printNiceBanner("Old Neuro/NeuroItem view"); |
---|
303 | int nc=m.old_getNeuroCount(); |
---|
304 | printf("Model::old_getNeuroCount() = %d\n",nc); |
---|
305 | for (int i=0;i<nc;i++) |
---|
306 | { |
---|
307 | Neuro *n=m.old_getNeuro(i); |
---|
308 | printf("neuron #%d: p=%d, j=%d, force=%g, inertia=%g, sigmoid=%g\n", |
---|
309 | i,n->part_refno,n->joint_refno, |
---|
310 | n->force,n->inertia,n->sigmo); |
---|
311 | int nicount=n->getItemCount(); |
---|
312 | printf(" %d items\n",nicount); |
---|
313 | for (int j=0;j<nicount;j++) |
---|
314 | { |
---|
315 | NeuroItem *ni=n->getNeuroItem(j); |
---|
316 | printf(" item #%d - '%s', conn=%d, weight=%g\n", |
---|
317 | j,(const char*)ni->getDetails(),ni->conn_refno,ni->weight); |
---|
318 | } |
---|
319 | } |
---|
320 | printf("end.\n"); |
---|
321 | #endif |
---|
322 | |
---|
323 | printf("\n######### THE END ###########\n\n" |
---|
324 | "Hints:\n" |
---|
325 | " 1. You can redirect output: gdktest >filename.txt\n" |
---|
326 | " 2. Each run can yield different results because some\n" |
---|
327 | " values are randomly generated.\n" |
---|
328 | " 3. Application will use custom genotype passed as\n" |
---|
329 | " a commandline parameter: gdktest X\n" |
---|
330 | "\n"); |
---|
331 | return 0; |
---|
332 | } |
---|