123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- /**
- \brief minimal.cpp is a minimal reference simulation to show how to use the objsimlibrary
- \author Frank Michler
- */
- #include "sys.hpp"
- #include "debug.hpp"
- #include <iostream>
- #include <cmath>
- #include <cstdio>
- #include <cstdlib>
- #include <objsimlibrary.hpp>
- #include <layerfactory.hpp>
- #include <anyoption.h>
- #include <valarray>
- #include <ctime>
- #include "version.h"
- #include <fstream>
- #include "layer/iaf_psc_exp.h"
- ////////////////////
- int main(int argc, char** argv)
- {
- std::string SimName("compnest_two_layers");
- cout << testfunction();
- clock_t cpu_start, cpu_end;
- double cpu_time_used;
- ////////////////Begin//ProcessCommandLineArguments//////////////
- cpu_start = clock();
- // define some directories
- std::string HomeDir(getenv("HOME"));
- std::string BaseDir(HomeDir+"/prog/objsim");
- std::string ConfigDir(HomeDir + "/.objsim");
- std::string ConfigFile(ConfigDir+"/settings_"+SimName+".cfg");
- // 'a' = dummy; do not use -a !! (setOption without second para doesn't work!!)
- /**
- AnyOptionWrapper takes care of simulation parameters
- 1. names of variables and default parameters are defined.
- 2. if the config file "settings_<SimName>" has an entry with the same name as one of the defined variables,
- the variable is set to the value in the config file.
- 3. if a command line parameter with the same name as one of the variables is given, the corresponding variable
- is set to the command line value value.
- As an example look at "AnyWrapParaInt(InputNx, 10);"
- AnyWrapParaInt is a macro for defining an integer parameter
- InputNx is used as the name of the variable, as the identifyer string in the config file settings_minimal.cfg
- and as the identifyer for the command line argument.
- The default value for InputNx is 10. This value is used when no config file entry and no command line argument
- with this name is given. If the config file contains the line "InputNx : 8", InputNx is set to 8.
- If furthermore, the executable is called with InputNx as command line parameter:
- "simminimal --InputNx 12", InputNx is set to 12.
- If command line option MakeDefaultConfig is set (it is a flag, so you don't give a value):
- "simminimal --MakeDefaultConfig --InputNx 12"
- then the values of all parameters will be written in the config file settings_minimal.cfg.
- (fm) 10.11.2008
- */
- AnyOptionWrapper *myAnyWrap = new AnyOptionWrapper(argc, argv, ConfigFile.c_str());
- CSimStandardOptions StdOpt(myAnyWrap);
- AnyWrapParaStringCmdOnly(SimControlName, "");
- AnyWrapParaFloat(WeightStrength, 0.5);
- AnyWrapParaFloat(Connectivity, 0.1);
- AnyWrapParaString(StimFileName, "");
- AnyWrapParaFloat(StimDur, 20);
- AnyWrapParaFloat(TestStimDur, 100);
- AnyWrapParaFloat(ConInpL2Sigma, 0.1);
- AnyWrapParaFloat(ConInpL2MaxWeight, 0.4);
- AnyWrapParaInt(L2Nx, 5);
- AnyWrapParaInt(L2Ny, 4);
- AnyWrapParaInt(L1MemRecIndex, 0);
- AnyWrapParaInt(L2MemRecIndex, 0);
- myAnyWrap->process();
- ////////////////End//ProcessCommandLineArguments//////////////
- ////////////////Begin//InitializeGlobalSimLoop
- SimLoop* MainSimLoop = InitLibCSim();
- MainSimLoop->SetDataDirectory(StdOpt.DataDirectory.c_str());
- float dt=MainSimLoop->GetDeltaT(); // milli sec
- cout << "int main(): dt = " << dt << "\n";
- myAnyWrap->Save((MainSimLoop->GetDataDirectory() + "/backup_settings").c_str());
- delete myAnyWrap;
- system(("cp progversion.txt "+MainSimLoop->GetDataDirectory()).c_str());
- ////////////////End//InitializeGlobalSimLoop
- int i, j, k, sec, t;
- ////////////////End//ProcessCommandLineArguments//////////////
- //////////////////////////////////////////////
- // load movie
- std::string MetaFileName = HomeDir+"/prog/objsim/data/movies/" + StimFileName + ".meta.xml";
- MovieMetaFile MetaFile(MetaFileName.c_str());
- std::string MovieFileName = MetaFile.MovieFileName;
- std::string StimName = HomeDir+"/prog/objsim/data/movies/" + MovieFileName;
- cout << "Load movie: " << StimName << "\n";
- ObjMovie* MyMovie = new ObjMovie(StimName.c_str());
- int InputNx = MyMovie->GetOutputWidth(0);
- int InputNy = MyMovie->GetOutputHeight(0);
- int NStimuli = MyMovie->GetNFrames();
- int NFilters = MyMovie->GetNFilters();
- int NXParas = int(sqrt(float(NStimuli))); // assume square number of stimuli
- int NYParas = NXParas;
- if (MetaFile.Loaded) {
- NXParas = MetaFile.NXParas;
- NYParas = MetaFile.NYParas;
- }
- //////// Setup Neuron Layer
- // layer* MyLayer = createLayer(InputNx*InputNy, NMType_DecoLif, NPType_Excitatory);
- layer* MyLayer = createLayer(InputNx*InputNy, NMType_iaf_psc_exp, NPType_Excitatory);
- MyLayer->SetName("InputLayer");
- MyLayer->SetupPositions(InputNx, InputNy, true);
- MainSimLoop->AddSimElement(MyLayer);
- iaf_psc_exp* MyIafPscLayer = dynamic_cast<iaf_psc_exp*>(MyLayer);
- size_t Layer2Nx = L2Nx;
- size_t Layer2Ny = L2Ny;
- layer* Layer2 = createLayer(Layer2Nx*Layer2Ny, NMType_iaf_psc_exp, NPType_Excitatory);
- Layer2->SetName("SecondLayer");
- Layer2->SetupPositions(Layer2Nx, Layer2Ny, true);
- MainSimLoop->AddSimElement(Layer2);
- iaf_psc_exp* MyIafLayer2 = dynamic_cast<iaf_psc_exp*>(Layer2);
- ////////// Setup connections
- VecConnection *MyConnection = new VecConnection(MyLayer, Layer2, csimInputChannel_AMPA);
- MyConnection->SetName("MyConnection");
- // if (!StdOpt.LoadWeights || (MyConnection->Load() != 0)) {
- /*
- MyConnection->SetMinMaxDelay(1.0, 0.0);
- for (size_t i_target=0; i_target<Layer2->N/2; ++i_target)
- {
- for (size_t i_source=0; i_source<MyLayer->N/2; ++i_source)
- {
- MyConnection->PushBackNewSynapse(i_source, i_target,
- ConInpL2MaxWeight*float(i_target)/float(Layer2->N), 2);
- }
- }
- for (size_t i_target=i_target<Layer2->N/2; i_target<Layer2->N; ++i_target)
- {
- for (size_t i_source=MyLayer->N/2; i_source<MyLayer->N; ++i_source)
- {
- MyConnection->PushBackNewSynapse(i_source, i_target,
- ConInpL2MaxWeight*float(i_target)/float(Layer2->N), 2);
- }
- }
- MyConnection->SetupDelaysArray();
- */
- MyConnection->ConnectGaussian(ConInpL2Sigma, ConInpL2MaxWeight, 1, 0, true);
- // }
- MyConnection->Save();
- MainSimLoop->AddSimElement(MyConnection);
- ////////// Setup Input
- int FltNr=0;
- float IsiDur=0;
- float Isi2Dur=0;
- int TestIsiDur= 0;
- bool XContinuous = true;
- cout << "NX=" << NXParas << "NY=" << NYParas << "\n";
- ScanObjMovieInput* MovieInput = new ScanObjMovieInput(
- MyLayer, csimInputChannel_Membrane, MyMovie, FltNr, StdOpt.InputStrength, StimDur, NXParas, NYParas,XContinuous, NXParas, IsiDur, Isi2Dur);
- MovieInput->SetXCircle(true);
- MovieInput->SetYCircle(true);
- MovieInput->SetName("MovieScanInput");
- cout << "before adding to mainsimloop simmodules.cpp\n";
- MainSimLoop->AddSimElement(MovieInput);
- cout << "after adding to mainsimloop simmodules.cpp\n";
- XYpairList StimList;
- for (int x=0;x<NXParas;++x) for (int y=0;y<NYParas;++y) {
- // cout << "x=" << 4*x << " y=" << 4*y << "\n";
- if (!XContinuous) {
- StimList.push_back(XYpair(x,y));
- } else {
- StimList.push_back(XYpair(y,x));
- }
- }
- MovieInput->InitializeTestMode(&StimList, TestStimDur,TestIsiDur);
- MovieInput->SetMode(csimInputPlayMovie);
- ////////////////////////////////////
- int trial, step;
- sec =0;
- int TotalTime=0;
- MainSimLoop->Hallo();
- MainSimLoop->SaveSimInfo();
- cout << "StdOpt.NTrials" << StdOpt.NTrials << "StdOpt.NSteps" << StdOpt.NSteps << "\n";
- fflush(stdout);
- std::fstream f("output.txt", ios_base::out);
- std::fstream f2("output2.txt", ios_base::out);
- size_t l1_rec_index = 0;
- if (L1MemRecIndex < MyLayer->N)
- l1_rec_index = L1MemRecIndex;
- size_t l2_rec_index = 0;
- if (L2MemRecIndex < Layer2->N)
- l2_rec_index = L2MemRecIndex;
- for (trial=0; trial<StdOpt.NTrials; ++trial)
- {
- cout << "TrialNr=" << trial << "\n";fflush(stdout);
- // learn
- for(step=0;step<StdOpt.NSteps;++step)
- {
- cpu_end=clock();
- cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
- cout << "T" << trial << " Step=" << step
- << " sec= " << dt*(sec++) << " cputime="
- << cpu_time_used << " sec \n "; fflush(stdout);
- for (t=0;t<1000;t++) // simulation of 1 sec
- {
- MainSimLoop->proceede(1000*step+t);
- f << MyIafPscLayer->pS_[l1_rec_index].V_m_ << "\n";
- f2 << MyIafLayer2->pS_[l2_rec_index].V_m_ << "\n";
- }
- MainSimLoop->prepare(step);
- }
- if (SimControlName != "") system((BaseDir + "/scripts/mytksend " + SimControlName + " SetTrialNr " +stringify(trial)+ " &").c_str());
- }
- f.close();
- f2.close();
- cpu_end=clock();
- cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
- cout << "\n\n***********CpuTimeUsed= " << cpu_time_used << " seconds \n";
- if (StdOpt.SimFinishButton) system((BaseDir + "/scripts/bluetclbutton Simulation finished &").c_str());
- }
- ObjMovie* LoadMovie(const std::string& StimFileName, const std::string& HomeDir)
- {
- }
|