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