Changeset 972 for cpp/frams/_demos/neuro_test.cpp
- Timestamp:
- 07/03/20 00:32:23 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/_demos/neuro_test.cpp
r391 r972 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 15Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 18 18 #ifndef SDK_WITHOUT_FRAMS 19 19 #include <frams/mech/creatmechobj.h> 20 int CreatMechObject::modeltags_id =0;21 int CreatMechObject::mechtags_id =0;20 int CreatMechObject::modeltags_id = 0; 21 int CreatMechObject::mechtags_id = 0; 22 22 #endif 23 23 24 ParamEntry creature_paramtab[] ={0};24 ParamEntry creature_paramtab[] = { 0 }; 25 25 26 26 #ifdef VEYETEST … … 33 33 #define LEARNINGSTEPS 50 34 34 35 void veyeStep(Model &m, int step)35 void veyeStep(Model &m, int step) 36 36 { 37 static float angle=0;37 static float angle = 0; 38 38 39 NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->setState(step>=LEARNINGSTEPS); //0 (learning) or 1 (normal)39 NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->setState(step >= LEARNINGSTEPS); //0 (learning) or 1 (normal) 40 40 41 NeuroImpl *ni =NeuroNetImpl::getImpl(m.getNeuro(N_VEye));42 ((NI_VectorEye*)ni)->relpos.y=0;43 ((NI_VectorEye*)ni)->relpos.z=0;44 if (NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->getNewState()<0.5)45 46 ((NI_VectorEye*)ni)->relpos.x=5.0*sin(2*M_PI*step/LEARNINGSTEPS);47 48 49 50 angle+=NeuroNetImpl::getImpl(m.getNeuro(N_VMotor))->getState();51 angle=fmod((double)angle,M_PI*2.0);52 ((NI_VectorEye*)ni)->relpos.x=5*sin(angle);53 41 NeuroImpl *ni = NeuroNetImpl::getImpl(m.getNeuro(N_VEye)); 42 ((NI_VectorEye*)ni)->relpos.y = 0; 43 ((NI_VectorEye*)ni)->relpos.z = 0; 44 if (NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->getNewState() < 0.5) 45 { //learning 46 ((NI_VectorEye*)ni)->relpos.x = 5.0 * sin(2 * M_PI * step / LEARNINGSTEPS); 47 } 48 else 49 { //VMotor controls location of VEye 50 angle += NeuroNetImpl::getImpl(m.getNeuro(N_VMotor))->getState(); 51 angle = fmod((double)angle, M_PI * 2.0); 52 ((NI_VectorEye*)ni)->relpos.x = 5 * sin(angle); 53 } 54 54 55 56 55 NeuroNetImpl::getImpl(m.getNeuro(N_Fitness))->setState(angle); //wymaga poprawy 56 //oraz trzeba przemyslec kolejnosc get/set'ow neuronow zeby sygnal sie dobrze propagowal. 57 57 } 58 58 #endif 59 59 60 int main(int argc, char*argv[])60 int main(int argc, char*argv[]) 61 61 { 62 LoggerToStdout messages_to_stdout(LoggerBase::Enable);63 PreconfiguredGenetics genetics;62 LoggerToStdout messages_to_stdout(LoggerBase::Enable); 63 PreconfiguredGenetics genetics; 64 64 65 if (argc<=1)65 if (argc <= 1) 66 66 { 67 67 puts("Parameters: <genotype> [number of simulation steps]"); 68 68 return 10; 69 69 } 70 SString gen(argv[1]);71 if (!strcmp(gen.c_str(),"-"))70 SString gen(argv[1]); 71 if (!strcmp(gen.c_str(), "-")) 72 72 { 73 gen=0;74 StdioFILEDontClose in(stdin);75 loadSString(&in,gen);73 gen = 0; 74 StdioFILEDontClose in(stdin); 75 loadSString(&in, gen); 76 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 else77 Geno g(gen); 78 if (!g.isValid()) { puts("invalid genotype"); return 5; } 79 Model m(g, Model::SHAPE_UNKNOWN); 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 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++)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 94 { 95 n=m.getNeuro(i);96 NeuroImpl *ni=NeuroNetImpl::getImpl(n);97 if (!ni)95 n = m.getNeuro(i); 96 NeuroImpl *ni = NeuroNetImpl::getImpl(n); 97 if (!ni) 98 98 { 99 if (no_impl) no_impl_names+=',';100 no_impl_names+=SString::sprintf("#%d.%s",i,n->getClassName().c_str());101 no_impl++;99 if (no_impl) no_impl_names += ','; 100 no_impl_names += SString::sprintf("#%d.%s", i, n->getClassName().c_str()); 101 no_impl++; 102 102 } 103 else if (ni->status==NeuroImpl::InitError)103 else if (ni->status == NeuroImpl::InitError) 104 104 { 105 if (init_err) init_err_names+=',';106 init_err_names+=SString::sprintf("#%d.%s",i,n->getClassName().c_str());107 init_err++;105 if (init_err) init_err_names += ','; 106 init_err_names += SString::sprintf("#%d.%s", i, n->getClassName().c_str()); 107 init_err++; 108 108 } 109 109 } 110 printf("\n"); 111 if (no_impl) printf("%d x missing implementation (%s)\n", no_impl, no_impl_names.c_str()); 112 if (init_err) printf("%d x failed initialization (%s)\n", init_err, init_err_names.c_str()); 113 } 114 int steps = 1; 115 if (argc > 2) steps = atol(argv[2]); 116 int st; 117 printf("step"); 118 for (i = 0; i < m.getNeuroCount(); i++) 119 { 120 n = m.getNeuro(i); 121 printf("\t#%d.%s", i, n->getClassName().c_str()); 122 } 110 123 printf("\n"); 111 if (no_impl) printf("%d x missing implementation (%s)\n",no_impl,no_impl_names.c_str()); 112 if (init_err) printf("%d x failed initialization (%s)\n",init_err,init_err_names.c_str()); 113 } 114 int steps=1; 115 if (argc>2) steps=atol(argv[2]); 116 int st; 117 printf("step"); 118 for(i=0;i<m.getNeuroCount();i++) 119 { 120 n=m.getNeuro(i); 121 printf("\t#%d.%s",i,n->getClassName().c_str()); 122 } 123 printf("\n"); 124 for(st=0;st<=steps;st++) 124 for (st = 0; st <= steps; st++) 125 125 { 126 126 #ifdef VEYETEST 127 veyeStep(m,st);127 veyeStep(m, st); 128 128 #endif 129 printf("%d",st);130 for(i=0;i<m.getNeuroCount();i++)129 printf("%d", st); 130 for (i = 0; i < m.getNeuroCount(); i++) 131 131 { 132 n=m.getNeuro(i);133 printf("\t%g",n->state);132 n = m.getNeuro(i); 133 printf("\t%g", n->state); 134 134 } 135 printf("\n");136 nn->simulateNeuroNet();135 printf("\n"); 136 nn->simulateNeuroNet(); 137 137 } 138 neurofac.freeImplementation();138 neurofac.freeImplementation(); 139 139 }
Note: See TracChangeset
for help on using the changeset viewer.