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 "geno.h" |
---|
6 | #include "stdiofile.h" |
---|
7 | #include "sstringutils.h" |
---|
8 | #include "defgenoconv.h" |
---|
9 | #include "neuroimpl.h" |
---|
10 | #include "neurofactory.h" |
---|
11 | #include "stdouterr.h" |
---|
12 | |
---|
13 | /** |
---|
14 | @file |
---|
15 | Sample code: Neural network tester (can run your custom neurons) |
---|
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 | #ifndef GDK_WITHOUT_FRAMS |
---|
22 | #include "creatmechobj.h" |
---|
23 | int CreatMechObject::modeltags_id=0; |
---|
24 | int CreatMechObject::mechtags_id=0; |
---|
25 | #endif |
---|
26 | |
---|
27 | ParamEntry creature_paramtab[]={0}; |
---|
28 | |
---|
29 | #ifdef VEYETEST |
---|
30 | #include "neuroimpl-vectoreye.h" |
---|
31 | |
---|
32 | #define N_VEye 0 |
---|
33 | #define N_VMotor 1 |
---|
34 | #define N_Mode 2 |
---|
35 | #define N_Fitness 3 |
---|
36 | #define LEARNINGSTEPS 50 |
---|
37 | |
---|
38 | void veyeStep(Model &m,int step) |
---|
39 | { |
---|
40 | static float angle=0; |
---|
41 | |
---|
42 | NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->setState(step>=LEARNINGSTEPS); //0 (learning) or 1 (normal) |
---|
43 | |
---|
44 | NeuroImpl *ni=NeuroNetImpl::getImpl(m.getNeuro(N_VEye)); |
---|
45 | ((NI_VectorEye*)ni)->relpos.y=0; |
---|
46 | ((NI_VectorEye*)ni)->relpos.z=0; |
---|
47 | if (NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->getNewState()<0.5) |
---|
48 | { //learning |
---|
49 | ((NI_VectorEye*)ni)->relpos.x=5.0*sin(2*M_PI*step/LEARNINGSTEPS); |
---|
50 | } |
---|
51 | else |
---|
52 | { //VMotor controls location of VEye |
---|
53 | angle+=NeuroNetImpl::getImpl(m.getNeuro(N_VMotor))->getState(); |
---|
54 | angle=fmod((double)angle,M_PI*2.0); |
---|
55 | ((NI_VectorEye*)ni)->relpos.x=5*sin(angle); |
---|
56 | } |
---|
57 | |
---|
58 | NeuroNetImpl::getImpl(m.getNeuro(N_Fitness))->setState(angle); //wymaga poprawy |
---|
59 | //oraz trzeba przemyslec kolejnosc get/set'ow neuronow zeby sygnal sie dobrze propagowal. |
---|
60 | } |
---|
61 | #endif |
---|
62 | |
---|
63 | int main(int argc,char*argv[]) |
---|
64 | { |
---|
65 | if (argc<=1) |
---|
66 | { |
---|
67 | puts("Parameters: <genotype> [number of simulation steps]"); |
---|
68 | return 10; |
---|
69 | } |
---|
70 | SString gen(argv[1]); |
---|
71 | if (!strcmp(gen,"-")) |
---|
72 | { |
---|
73 | gen=0; |
---|
74 | StdioFILEDontClose in(stdin); |
---|
75 | loadSString(&in,gen); |
---|
76 | } |
---|
77 | Geno g(gen); |
---|
78 | if (!g.isValid()) {puts("invalid genotype");return 5;} |
---|
79 | Model m(g); |
---|
80 | if (!m.getNeuroCount()) {puts("no neural network");return 1;} |
---|
81 | printf("%d neurons,",m.getNeuroCount()); |
---|
82 | NeuroFactory neurofac; |
---|
83 | neurofac.setStandardImplementation(); |
---|
84 | NeuroNetConfig nn_config(&neurofac); |
---|
85 | NeuroNetImpl *nn=new NeuroNetImpl(m,nn_config); |
---|
86 | int i; Neuro *n; |
---|
87 | if (!nn->getErrorCount()) printf(" no errors\n"); |
---|
88 | else |
---|
89 | { |
---|
90 | printf(" %d errors:",nn->getErrorCount()); |
---|
91 | int no_impl=0; SString no_impl_names; |
---|
92 | int init_err=0; SString init_err_names; |
---|
93 | for(i=0;i<m.getNeuroCount();i++) |
---|
94 | { |
---|
95 | n=m.getNeuro(i); |
---|
96 | NeuroImpl *ni=NeuroNetImpl::getImpl(n); |
---|
97 | if (!ni) |
---|
98 | { |
---|
99 | if (no_impl) no_impl_names+=','; |
---|
100 | sprintf(no_impl_names.directAppend(100),"#%d.%s", |
---|
101 | i,(const char*)n->getClassName()); |
---|
102 | no_impl_names.endAppend(); |
---|
103 | no_impl++; |
---|
104 | } |
---|
105 | else if (ni->status==NeuroImpl::InitError) |
---|
106 | { |
---|
107 | if (init_err) init_err_names+=','; |
---|
108 | sprintf(init_err_names.directAppend(100),"#%d.%s", |
---|
109 | i,(const char*)n->getClassName()); |
---|
110 | init_err_names.endAppend(); |
---|
111 | init_err++; |
---|
112 | } |
---|
113 | } |
---|
114 | printf("\n"); |
---|
115 | if (no_impl) printf("%d x missing implementation (%s)\n",no_impl,(const char*)no_impl_names); |
---|
116 | if (init_err) printf("%d x failed initialization (%s)\n",init_err,(const char*)init_err_names); |
---|
117 | } |
---|
118 | int steps=1; |
---|
119 | if (argc>2) steps=atol(argv[2]); |
---|
120 | int st; |
---|
121 | printf("step"); |
---|
122 | for(i=0;i<m.getNeuroCount();i++) |
---|
123 | { |
---|
124 | n=m.getNeuro(i); |
---|
125 | printf("\t#%d.%s",i,(const char*)n->getClassName()); |
---|
126 | } |
---|
127 | printf("\n"); |
---|
128 | for(st=0;st<=steps;st++) |
---|
129 | { |
---|
130 | #ifdef VEYETEST |
---|
131 | veyeStep(m,st); |
---|
132 | #endif |
---|
133 | printf("%d",st); |
---|
134 | for(i=0;i<m.getNeuroCount();i++) |
---|
135 | { |
---|
136 | n=m.getNeuro(i); |
---|
137 | printf("\t%g",n->state); |
---|
138 | } |
---|
139 | printf("\n"); |
---|
140 | nn->simulateNeuroNet(); |
---|
141 | } |
---|
142 | neurofac.freeImplementation(); |
---|
143 | } |
---|