compnest_two_layers.cpp.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. \brief minimal.cpp is a minimal reference simulation to show how to use the objsimlibrary
  3. \author Frank Michler
  4. */
  5. #include "sys.hpp"
  6. #include "debug.hpp"
  7. #include <iostream>
  8. #include <cmath>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <objsimlibrary.hpp>
  12. #include <layerfactory.hpp>
  13. #include <anyoption.h>
  14. #include <valarray>
  15. #include <ctime>
  16. #include "version.h"
  17. #include <fstream>
  18. #include "layer/iaf_psc_exp.h"
  19. ////////////////////
  20. int main(int argc, char** argv)
  21. {
  22. std::string SimName("compnest_two_layers");
  23. cout << testfunction();
  24. clock_t cpu_start, cpu_end;
  25. double cpu_time_used;
  26. ////////////////Begin//ProcessCommandLineArguments//////////////
  27. cpu_start = clock();
  28. // define some directories
  29. std::string HomeDir(getenv("HOME"));
  30. std::string BaseDir(HomeDir+"/prog/objsim");
  31. std::string ConfigDir(HomeDir + "/.objsim");
  32. std::string ConfigFile(ConfigDir+"/settings_"+SimName+".cfg");
  33. // 'a' = dummy; do not use -a !! (setOption without second para doesn't work!!)
  34. /**
  35. AnyOptionWrapper takes care of simulation parameters
  36. 1. names of variables and default parameters are defined.
  37. 2. if the config file "settings_<SimName>" has an entry with the same name as one of the defined variables,
  38. the variable is set to the value in the config file.
  39. 3. if a command line parameter with the same name as one of the variables is given, the corresponding variable
  40. is set to the command line value value.
  41. As an example look at "AnyWrapParaInt(InputNx, 10);"
  42. AnyWrapParaInt is a macro for defining an integer parameter
  43. InputNx is used as the name of the variable, as the identifyer string in the config file settings_minimal.cfg
  44. and as the identifyer for the command line argument.
  45. The default value for InputNx is 10. This value is used when no config file entry and no command line argument
  46. with this name is given. If the config file contains the line "InputNx : 8", InputNx is set to 8.
  47. If furthermore, the executable is called with InputNx as command line parameter:
  48. "simminimal --InputNx 12", InputNx is set to 12.
  49. If command line option MakeDefaultConfig is set (it is a flag, so you don't give a value):
  50. "simminimal --MakeDefaultConfig --InputNx 12"
  51. then the values of all parameters will be written in the config file settings_minimal.cfg.
  52. (fm) 10.11.2008
  53. */
  54. AnyOptionWrapper *myAnyWrap = new AnyOptionWrapper(argc, argv, ConfigFile.c_str());
  55. CSimStandardOptions StdOpt(myAnyWrap);
  56. AnyWrapParaStringCmdOnly(SimControlName, "");
  57. AnyWrapParaFloat(WeightStrength, 0.5);
  58. AnyWrapParaFloat(Connectivity, 0.1);
  59. AnyWrapParaString(StimFileName, "");
  60. AnyWrapParaFloat(StimDur, 20);
  61. AnyWrapParaFloat(TestStimDur, 100);
  62. AnyWrapParaFloat(ConInpL2Sigma, 0.1);
  63. AnyWrapParaFloat(ConInpL2MaxWeight, 0.4);
  64. AnyWrapParaInt(L2Nx, 5);
  65. AnyWrapParaInt(L2Ny, 4);
  66. AnyWrapParaInt(L1MemRecIndex, 0);
  67. AnyWrapParaInt(L2MemRecIndex, 0);
  68. myAnyWrap->process();
  69. ////////////////End//ProcessCommandLineArguments//////////////
  70. ////////////////Begin//InitializeGlobalSimLoop
  71. SimLoop* MainSimLoop = InitLibCSim();
  72. MainSimLoop->SetDataDirectory(StdOpt.DataDirectory.c_str());
  73. float dt=MainSimLoop->GetDeltaT(); // milli sec
  74. cout << "int main(): dt = " << dt << "\n";
  75. myAnyWrap->Save((MainSimLoop->GetDataDirectory() + "/backup_settings").c_str());
  76. delete myAnyWrap;
  77. system(("cp progversion.txt "+MainSimLoop->GetDataDirectory()).c_str());
  78. ////////////////End//InitializeGlobalSimLoop
  79. int i, j, k, sec, t;
  80. ////////////////End//ProcessCommandLineArguments//////////////
  81. //////////////////////////////////////////////
  82. // load movie
  83. std::string MetaFileName = HomeDir+"/prog/objsim/data/movies/" + StimFileName + ".meta.xml";
  84. MovieMetaFile MetaFile(MetaFileName.c_str());
  85. std::string MovieFileName = MetaFile.MovieFileName;
  86. std::string StimName = HomeDir+"/prog/objsim/data/movies/" + MovieFileName;
  87. cout << "Load movie: " << StimName << "\n";
  88. ObjMovie* MyMovie = new ObjMovie(StimName.c_str());
  89. int InputNx = MyMovie->GetOutputWidth(0);
  90. int InputNy = MyMovie->GetOutputHeight(0);
  91. int NStimuli = MyMovie->GetNFrames();
  92. int NFilters = MyMovie->GetNFilters();
  93. int NXParas = int(sqrt(float(NStimuli))); // assume square number of stimuli
  94. int NYParas = NXParas;
  95. if (MetaFile.Loaded) {
  96. NXParas = MetaFile.NXParas;
  97. NYParas = MetaFile.NYParas;
  98. }
  99. //////// Setup Neuron Layer
  100. // layer* MyLayer = createLayer(InputNx*InputNy, NMType_DecoLif, NPType_Excitatory);
  101. layer* MyLayer = createLayer(InputNx*InputNy, NMType_iaf_psc_exp, NPType_Excitatory);
  102. MyLayer->SetName("InputLayer");
  103. MyLayer->SetupPositions(InputNx, InputNy, true);
  104. MainSimLoop->AddSimElement(MyLayer);
  105. iaf_psc_exp* MyIafPscLayer = dynamic_cast<iaf_psc_exp*>(MyLayer);
  106. size_t Layer2Nx = L2Nx;
  107. size_t Layer2Ny = L2Ny;
  108. layer* Layer2 = createLayer(Layer2Nx*Layer2Ny, NMType_iaf_psc_exp, NPType_Excitatory);
  109. Layer2->SetName("SecondLayer");
  110. Layer2->SetupPositions(Layer2Nx, Layer2Ny, true);
  111. MainSimLoop->AddSimElement(Layer2);
  112. iaf_psc_exp* MyIafLayer2 = dynamic_cast<iaf_psc_exp*>(Layer2);
  113. ////////// Setup connections
  114. VecConnection *MyConnection = new VecConnection(MyLayer, Layer2, csimInputChannel_AMPA);
  115. MyConnection->SetName("MyConnection");
  116. // if (!StdOpt.LoadWeights || (MyConnection->Load() != 0)) {
  117. /*
  118. MyConnection->SetMinMaxDelay(1.0, 0.0);
  119. for (size_t i_target=0; i_target<Layer2->N/2; ++i_target)
  120. {
  121. for (size_t i_source=0; i_source<MyLayer->N/2; ++i_source)
  122. {
  123. MyConnection->PushBackNewSynapse(i_source, i_target,
  124. ConInpL2MaxWeight*float(i_target)/float(Layer2->N), 2);
  125. }
  126. }
  127. for (size_t i_target=i_target<Layer2->N/2; i_target<Layer2->N; ++i_target)
  128. {
  129. for (size_t i_source=MyLayer->N/2; i_source<MyLayer->N; ++i_source)
  130. {
  131. MyConnection->PushBackNewSynapse(i_source, i_target,
  132. ConInpL2MaxWeight*float(i_target)/float(Layer2->N), 2);
  133. }
  134. }
  135. MyConnection->SetupDelaysArray();
  136. */
  137. MyConnection->ConnectGaussian(ConInpL2Sigma, ConInpL2MaxWeight, 1, 0, true);
  138. // }
  139. MyConnection->Save();
  140. MainSimLoop->AddSimElement(MyConnection);
  141. ////////// Setup Input
  142. int FltNr=0;
  143. float IsiDur=0;
  144. float Isi2Dur=0;
  145. int TestIsiDur= 0;
  146. bool XContinuous = true;
  147. cout << "NX=" << NXParas << "NY=" << NYParas << "\n";
  148. ScanObjMovieInput* MovieInput = new ScanObjMovieInput(
  149. MyLayer, csimInputChannel_Membrane, MyMovie, FltNr, StdOpt.InputStrength, StimDur, NXParas, NYParas,XContinuous, NXParas, IsiDur, Isi2Dur);
  150. MovieInput->SetXCircle(true);
  151. MovieInput->SetYCircle(true);
  152. MovieInput->SetName("MovieScanInput");
  153. cout << "before adding to mainsimloop simmodules.cpp\n";
  154. MainSimLoop->AddSimElement(MovieInput);
  155. cout << "after adding to mainsimloop simmodules.cpp\n";
  156. XYpairList StimList;
  157. for (int x=0;x<NXParas;++x) for (int y=0;y<NYParas;++y) {
  158. // cout << "x=" << 4*x << " y=" << 4*y << "\n";
  159. if (!XContinuous) {
  160. StimList.push_back(XYpair(x,y));
  161. } else {
  162. StimList.push_back(XYpair(y,x));
  163. }
  164. }
  165. MovieInput->InitializeTestMode(&StimList, TestStimDur,TestIsiDur);
  166. MovieInput->SetMode(csimInputPlayMovie);
  167. ////////////////////////////////////
  168. int trial, step;
  169. sec =0;
  170. int TotalTime=0;
  171. MainSimLoop->Hallo();
  172. MainSimLoop->SaveSimInfo();
  173. cout << "StdOpt.NTrials" << StdOpt.NTrials << "StdOpt.NSteps" << StdOpt.NSteps << "\n";
  174. fflush(stdout);
  175. std::fstream f("output.txt", ios_base::out);
  176. std::fstream f2("output2.txt", ios_base::out);
  177. size_t l1_rec_index = 0;
  178. if (L1MemRecIndex < MyLayer->N)
  179. l1_rec_index = L1MemRecIndex;
  180. size_t l2_rec_index = 0;
  181. if (L2MemRecIndex < Layer2->N)
  182. l2_rec_index = L2MemRecIndex;
  183. for (trial=0; trial<StdOpt.NTrials; ++trial)
  184. {
  185. cout << "TrialNr=" << trial << "\n";fflush(stdout);
  186. // learn
  187. for(step=0;step<StdOpt.NSteps;++step)
  188. {
  189. cpu_end=clock();
  190. cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
  191. cout << "T" << trial << " Step=" << step
  192. << " sec= " << dt*(sec++) << " cputime="
  193. << cpu_time_used << " sec \n "; fflush(stdout);
  194. for (t=0;t<1000;t++) // simulation of 1 sec
  195. {
  196. MainSimLoop->proceede(1000*step+t);
  197. f << MyIafPscLayer->pS_[l1_rec_index].V_m_ << "\n";
  198. f2 << MyIafLayer2->pS_[l2_rec_index].V_m_ << "\n";
  199. }
  200. MainSimLoop->prepare(step);
  201. }
  202. if (SimControlName != "") system((BaseDir + "/scripts/mytksend " + SimControlName + " SetTrialNr " +stringify(trial)+ " &").c_str());
  203. }
  204. f.close();
  205. f2.close();
  206. cpu_end=clock();
  207. cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
  208. cout << "\n\n***********CpuTimeUsed= " << cpu_time_used << " seconds \n";
  209. if (StdOpt.SimFinishButton) system((BaseDir + "/scripts/bluetclbutton Simulation finished &").c_str());
  210. }
  211. ObjMovie* LoadMovie(const std::string& StimFileName, const std::string& HomeDir)
  212. {
  213. }