compnest.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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");
  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. myAnyWrap->process();
  63. ////////////////End//ProcessCommandLineArguments//////////////
  64. ////////////////Begin//InitializeGlobalSimLoop
  65. SimLoop* MainSimLoop = InitLibCSim();
  66. MainSimLoop->SetDataDirectory(StdOpt.DataDirectory.c_str());
  67. float dt=MainSimLoop->GetDeltaT(); // milli sec
  68. cout << "int main(): dt = " << dt << "\n";
  69. myAnyWrap->Save((MainSimLoop->GetDataDirectory() + "/backup_settings").c_str());
  70. delete myAnyWrap;
  71. system(("cp progversion.txt "+MainSimLoop->GetDataDirectory()).c_str());
  72. ////////////////End//InitializeGlobalSimLoop
  73. int i, j, k, sec, t;
  74. ////////////////End//ProcessCommandLineArguments//////////////
  75. //////////////////////////////////////////////
  76. // load movie
  77. std::string MetaFileName = HomeDir+"/prog/objsim/data/movies/" + StimFileName + ".meta.xml";
  78. MovieMetaFile MetaFile(MetaFileName.c_str());
  79. std::string MovieFileName = MetaFile.MovieFileName;
  80. std::string StimName = HomeDir+"/prog/objsim/data/movies/" + MovieFileName;
  81. cout << "Load movie: " << StimName << "\n";
  82. ObjMovie* MyMovie = new ObjMovie(StimName.c_str());
  83. int InputNx = MyMovie->GetOutputWidth(0);
  84. int InputNy = MyMovie->GetOutputHeight(0);
  85. int NStimuli = MyMovie->GetNFrames();
  86. int NFilters = MyMovie->GetNFilters();
  87. int NXParas = int(sqrt(float(NStimuli))); // assume square number of stimuli
  88. int NYParas = NXParas;
  89. if (MetaFile.Loaded) {
  90. NXParas = MetaFile.NXParas;
  91. NYParas = MetaFile.NYParas;
  92. }
  93. //////// Setup Neuron Layer
  94. // layer* MyLayer = createLayer(InputNx*InputNy, NMType_DecoLif, NPType_Excitatory);
  95. layer* MyLayer = createLayer(InputNx*InputNy, NMType_iaf_psc_exp, NPType_Excitatory);
  96. MyLayer->SetName("MyLayer");
  97. MyLayer->SetupPositions(InputNx, InputNy, true);
  98. MainSimLoop->AddSimElement(MyLayer);
  99. iaf_psc_exp* MyIafPscLayer = dynamic_cast<iaf_psc_exp*>(MyLayer);
  100. ////////// Setup connections
  101. VecConnection *MyConnection = new VecConnection(MyLayer, MyLayer, csimInputChannel_Membrane);
  102. MyConnection->SetName("MyConnection");
  103. if (!StdOpt.LoadWeights || (MyConnection->Load() != 0)) {
  104. MyConnection->ConnectRandomIncomming(Connectivity, WeightStrength);
  105. }
  106. MyConnection->Save();
  107. // MainSimLoop->AddSimElement(MyConnection);
  108. ////////// Setup Input
  109. int FltNr=0;
  110. float IsiDur=0;
  111. float Isi2Dur=0;
  112. int TestIsiDur= 0;
  113. bool XContinuous = true;
  114. cout << "NX=" << NXParas << "NY=" << NYParas << "\n";
  115. ScanObjMovieInput* MovieInput = new ScanObjMovieInput(
  116. MyLayer, csimInputChannel_Membrane, MyMovie, FltNr, StdOpt.InputStrength, StimDur, NXParas, NYParas,XContinuous, NXParas, IsiDur, Isi2Dur);
  117. MovieInput->SetXCircle(true);
  118. MovieInput->SetYCircle(true);
  119. MovieInput->SetName("MovieScanInput");
  120. cout << "before adding to mainsimloop simmodules.cpp\n";
  121. MainSimLoop->AddSimElement(MovieInput);
  122. cout << "after adding to mainsimloop simmodules.cpp\n";
  123. XYpairList StimList;
  124. for (int x=0;x<NXParas;++x) for (int y=0;y<NYParas;++y) {
  125. // cout << "x=" << 4*x << " y=" << 4*y << "\n";
  126. if (!XContinuous) {
  127. StimList.push_back(XYpair(x,y));
  128. } else {
  129. StimList.push_back(XYpair(y,x));
  130. }
  131. }
  132. MovieInput->InitializeTestMode(&StimList, TestStimDur,TestIsiDur);
  133. MovieInput->SetMode(csimInputPlayMovie);
  134. ////////////////////////////////////
  135. int trial, step;
  136. sec =0;
  137. int TotalTime=0;
  138. MainSimLoop->Hallo();
  139. MainSimLoop->SaveSimInfo();
  140. cout << "StdOpt.NTrials" << StdOpt.NTrials << "StdOpt.NSteps" << StdOpt.NSteps << "\n";
  141. fflush(stdout);
  142. std::fstream f("output.txt", ios_base::out);
  143. for (trial=0; trial<StdOpt.NTrials; ++trial)
  144. {
  145. cout << "TrialNr=" << trial << "\n";fflush(stdout);
  146. // learn
  147. for(step=0;step<StdOpt.NSteps;++step)
  148. {
  149. cpu_end=clock();
  150. cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
  151. cout << "T" << trial << " Step=" << step
  152. << " sec= " << dt*(sec++) << " cputime="
  153. << cpu_time_used << " sec \n "; fflush(stdout);
  154. for (t=0;t<1000;t++) // simulation of 1 sec
  155. {
  156. MainSimLoop->proceede(1000*step+t);
  157. f << MyIafPscLayer->pS_[0].V_m_ << "\n";
  158. }
  159. MainSimLoop->prepare(step);
  160. }
  161. if (SimControlName != "") system((BaseDir + "/scripts/mytksend " + SimControlName + " SetTrialNr " +stringify(trial)+ " &").c_str());
  162. }
  163. cpu_end=clock();
  164. cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
  165. cout << "\n\n***********CpuTimeUsed= " << cpu_time_used << " seconds \n";
  166. if (StdOpt.SimFinishButton) system((BaseDir + "/scripts/bluetclbutton Simulation finished &").c_str());
  167. }
  168. ObjMovie* LoadMovie(const std::string& StimFileName, const std::string& HomeDir)
  169. {
  170. }