CARLsim  5.0.0
CARLsim: a GPU-accelerated SNN simulator
simple_weight_tuner.cpp
Go to the documentation of this file.
1 /* * Copyright (c) 2016 Regents of the University of California. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. The names of its contributors may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * *********************************************************************************************** *
31 * CARLsim
32 * created by: (MDR) Micah Richert, (JN) Jayram M. Nageswaran
33 * maintained by:
34 * (MA) Mike Avery <averym@uci.edu>
35 * (MB) Michael Beyeler <mbeyeler@uci.edu>,
36 * (KDC) Kristofor Carlson <kdcarlso@uci.edu>
37 * (TSC) Ting-Shuo Chou <tingshuc@uci.edu>
38 * (HK) Hirak J Kashyap <kashyaph@uci.edu>
39 *
40 * CARLsim v1.0: JM, MDR
41 * CARLsim v2.0/v2.1/v2.2: JM, MDR, MA, MB, KDC
42 * CARLsim3: MB, KDC, TSC
43 * CARLsim4: TSC, HK
44 * CARLsim5: HK, JX, KC
45 *
46 * CARLsim available from http://socsci.uci.edu/~jkrichma/CARLsim/
47 * Ver 12/31/2016
48 */
49 #include "simple_weight_tuner.h"
50 
51 #include <carlsim.h> // CARLsim, SpikeMonitor
52 #include <math.h> // fabs
53 #include <stdio.h> // printf
54 #include <limits> // double::max
55 #include <assert.h> // assert
56 
57 // ****************************************************************************************************************** //
58 // SIMPLEWEIGHTTUNER UTILITY PRIVATE IMPLEMENTATION
59 // ****************************************************************************************************************** //
60 
69 public:
70  // +++++ PUBLIC METHODS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
71 
72  Impl(CARLsim *sim, double errorMargin, int maxIter, double stepSizeFraction) {
73  assert(sim!=NULL);
74  assert(errorMargin>0);
75  assert(maxIter>0);
76  assert(stepSizeFraction>0.0f && stepSizeFraction<=1.0f);
77 
78  sim_ = sim;
79  assert(sim_->getCARLsimState()!=RUN_STATE);
80 
81  errorMargin_ = errorMargin;
82  stepSizeFraction_ = stepSizeFraction;
83  maxIter_ = maxIter;
84 
85  connId_ = -1;
86  wtRange_ = NULL;
87  wtInit_ = -1.0;
88 
89  grpId_ = -1;
90  targetRate_ = -1.0;
91 
92  wtStepSize_ = -1.0;
93  cntIter_ = 0;
94 
95  wtShouldIncrease_ = true;
96  adjustRange_ = true;
97 
98  needToInitConnection_ = true;
99  needToInitTargetFiring_ = true;
100 
101  needToInitAlgo_ = true;
102  }
103 
104  ~Impl() {
105  if (wtRange_!=NULL)
106  delete wtRange_;
107  wtRange_=NULL;
108  }
109 
110 // user function to reset algo
111 void reset() {
112  needToInitAlgo_ = true;
113  initAlgo();
114 }
115 
116 bool done(bool printMessage) {
117  // algo not initalized: we're not done
118  if (needToInitConnection_ || needToInitTargetFiring_ || needToInitAlgo_)
119  return false;
120 
121  // success: margin reached
122  if (fabs(currentError_) < errorMargin_) {
123  if (printMessage) {
124  printf("SimpleWeightTuner successful: Error margin reached in %d iterations.\n",cntIter_);
125  }
126  return true;
127  }
128 
129  // failure: max iter reached
130  if (cntIter_ >= maxIter_) {
131  if (printMessage) {
132  printf("SimpleWeightTuner failed: Max number of iterations (%d) reached.\n",maxIter_);
133  }
134  return true;
135  }
136 
137  // else we're not done
138  return false;
139 }
140 
141 void setConnectionToTune(short int connId, double initWt, bool adjustRange) {
142  assert(connId>=0 && connId<sim_->getNumConnections());
143 
144  connId_ = connId;
145  wtInit_ = initWt;
146  adjustRange_ = adjustRange;
147 
148  needToInitConnection_ = false;
149  needToInitAlgo_ = true;
150 }
151 
152 void setTargetFiringRate(int grpId, double targetRate) {
153  grpId_ = grpId;
154  targetRate_ = targetRate;
155  currentError_ = targetRate;
156 
157  // check whether group has SpikeMonitor
158  SM_ = sim_->getSpikeMonitor(grpId);
159  if (SM_==NULL) {
160  // setSpikeMonitor has not been called yet
161  SM_ = sim_->setSpikeMonitor(grpId,"NULL");
162  }
163 
164  needToInitTargetFiring_ = false;
165  needToInitAlgo_ = true;
166 }
167 
168 void iterate(int runDurationMs, bool printStatus) {
169  assert(runDurationMs>0);
170 
171  // if we're done, don't iterate
172  if (done(printStatus)) {
173  return;
174  }
175 
176  // make sure we have initialized algo
177  assert(!needToInitConnection_);
178  assert(!needToInitTargetFiring_);
179  if (needToInitAlgo_)
180  initAlgo();
181 
182  // in case the user has already been messing with the SpikeMonitor, we need to make sure that
183  // PersistentMode is off
184  SM_->setPersistentData(false);
185 
186  // now iterate
187  SM_->startRecording();
188  sim_->runNetwork(runDurationMs/1000, runDurationMs%1000, false);
189  SM_->stopRecording();
190 
191  double thisRate = SM_->getPopMeanFiringRate();
192  if (printStatus) {
193  printf("#%d: rate=%.4fHz, target=%.4fHz, error=%.7f, errorMargin=%.7f\n", cntIter_, thisRate, targetRate_,
194  thisRate-targetRate_, errorMargin_);
195  }
196 
197  currentError_ = thisRate - targetRate_;
198  cntIter_++;
199 
200  // check if we're done now
201  if (done(printStatus)) {
202  return;
203  }
204 
205  // else update parameters
206  if (wtStepSize_>0 && thisRate>targetRate_ || wtStepSize_<0 && thisRate<targetRate_) {
207  // we stepped too far to the right or too far to the left
208  // turn around and cut step size in half
209  // note that this should work for inhibitory connections, too: they have negative weights, so adding
210  // to the weight will actually decrease it (make it less negative)
211  wtStepSize_ = -wtStepSize_/2.0;
212  }
213 
214  // find new weight
215  sim_->biasWeights(connId_, wtStepSize_, adjustRange_);
216 }
217 
218 private:
219  // +++++ PRIVATE METHODS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
220 
221 // need to call this whenever connection or target firing changes
222 // or when user calls reset
223 void initAlgo() {
224  if (!needToInitAlgo_)
225  return;
226 
227  // make sure we have all the data structures we need
228  assert(!needToInitConnection_);
229  assert(!needToInitTargetFiring_);
230 
231  // update weight ranges
232  RangeWeight wt = sim_->getWeightRange(connId_);
233  wtRange_ = new RangeWeight(wt.min, wt.init, wt.max);
234 
235  // reset algo
236  wtShouldIncrease_ = true;
237  wtStepSize_ = stepSizeFraction_ * (wtRange_->max - wtRange_->min);
238 #if defined(WIN32) || defined(WIN64)
239  currentError_ = DBL_MAX;
240 #else
241  currentError_ = std::numeric_limits<double>::max();
242 #endif
243 
244  // make sure we're in the right CARLsim state
245  if (sim_->getCARLsimState()!=RUN_STATE)
246  sim_->runNetwork(0,0,false);
247 
248  // initialize weights
249  if (wtInit_>=0) {
250  // start at some specified initWt
251  if (wt.init != wtInit_) {
252  // specified starting point is not what is specified in connect
253 
254  sim_->biasWeights(connId_, wtInit_ - wt.init, adjustRange_);
255  }
256  }
257 
258  needToInitAlgo_ = false;
259 }
260 
261 
262  // +++++ PRIVATE STATIC PROPERTIES ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
263 
264  // +++++ PRIVATE PROPERTIES +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
265 
266  // flags that manage state
267  bool needToInitConnection_;
268  bool needToInitTargetFiring_;
269  bool needToInitAlgo_;
270 
271  // CARLsim data structures
272  CARLsim *sim_;
273  SpikeMonitor *SM_;
274  int grpId_;
275  short int connId_;
276  RangeWeight* wtRange_;
277 
278  // termination condition params
279  int maxIter_;
280  double errorMargin_;
281  double targetRate_;
282 
283  // params that are updated every iteration step
284  int cntIter_;
285  double wtStepSize_;
286  bool wtShouldIncrease_;
287  double currentError_;
288 
289  // options
290  bool adjustRange_;
291  double wtInit_;
292  double stepSizeFraction_;
293 };
294 
295 
296 // ****************************************************************************************************************** //
297 // SIMPLEWEIGHTTUNER API IMPLEMENTATION
298 // ****************************************************************************************************************** //
299 
300 // create and destroy a pImpl instance
301 SimpleWeightTuner::SimpleWeightTuner(CARLsim* sim, double errorMargin, int maxIter, double stepSizeFraction) :
302  _impl( new Impl(sim, errorMargin, maxIter, stepSizeFraction) ) {}
304 
305 void SimpleWeightTuner::setConnectionToTune(short int connId, double initWt, bool adjustRange) {
306  _impl->setConnectionToTune(connId, initWt, adjustRange);
307 }
308 void SimpleWeightTuner::setTargetFiringRate(int grpId, double targetRate) {
309  _impl->setTargetFiringRate(grpId, targetRate);
310 }
311 void SimpleWeightTuner::iterate(int runDurationMs, bool printStatus) { _impl->iterate(runDurationMs, printStatus); }
312 bool SimpleWeightTuner::done(bool printMessage) { return _impl->done(printMessage); }
313 void SimpleWeightTuner::reset() { _impl->reset(); }
RangeWeight::init
double init
Definition: carlsim_datastructures.h:335
RUN_STATE
@ RUN_STATE
run state, where the model is stepped
Definition: carlsim_datastructures.h:263
SimpleWeightTuner::Impl::setTargetFiringRate
void setTargetFiringRate(int grpId, double targetRate)
Definition: simple_weight_tuner.cpp:152
SimpleWeightTuner::Impl::Impl
Impl(CARLsim *sim, double errorMargin, int maxIter, double stepSizeFraction)
Definition: simple_weight_tuner.cpp:72
RangeWeight
a range struct for synaptic weight magnitudes
Definition: carlsim_datastructures.h:312
SpikeMonitor
Class SpikeMonitor.
Definition: spike_monitor.h:120
SimpleWeightTuner::setConnectionToTune
void setConnectionToTune(short int connId, double initWt=-1.0, bool adjustRange=true)
Sets up the connection to tune.
Definition: simple_weight_tuner.cpp:305
SpikeMonitor::setPersistentData
void setPersistentData(bool persistentData)
Sets PersistentMode either on (true) or off (false)
Definition: spike_monitor.cpp:247
SimpleWeightTuner::Impl::reset
void reset()
Definition: simple_weight_tuner.cpp:111
CARLsim::runNetwork
int runNetwork(int nSec, int nMsec=0, bool printRunSummary=true)
run the simulation for time=(nSec*seconds + nMsec*milliseconds)
Definition: carlsim.cpp:1910
CARLsim::biasWeights
void biasWeights(short int connId, float bias, bool updateWeightRange=false)
Adds a constant bias to the weight of every synapse in the connection.
Definition: carlsim.cpp:1937
SpikeMonitor::stopRecording
void stopRecording()
Ends a recording period.
Definition: spike_monitor.cpp:208
SimpleWeightTuner::Impl::setConnectionToTune
void setConnectionToTune(short int connId, double initWt, bool adjustRange)
Definition: simple_weight_tuner.cpp:141
SimpleWeightTuner::Impl::iterate
void iterate(int runDurationMs, bool printStatus)
Definition: simple_weight_tuner.cpp:168
RangeWeight::max
double max
Definition: carlsim_datastructures.h:335
SimpleWeightTuner::~SimpleWeightTuner
~SimpleWeightTuner()
Destructor.
Definition: simple_weight_tuner.cpp:303
SimpleWeightTuner::Impl
Private implementation of the Stopwatch Utility.
Definition: simple_weight_tuner.cpp:68
simple_weight_tuner.h
SpikeMonitor::getPopMeanFiringRate
float getPopMeanFiringRate()
Returns the mean firing rate of the entire neuronal population.
Definition: spike_monitor.cpp:77
SimpleWeightTuner::setTargetFiringRate
void setTargetFiringRate(int grpId, double targetRate)
Sets up the target firing rate of a specific group.
Definition: simple_weight_tuner.cpp:308
SimpleWeightTuner::reset
void reset()
Resets the algorithm to initial conditions.
Definition: simple_weight_tuner.cpp:313
SimpleWeightTuner::SimpleWeightTuner
SimpleWeightTuner(CARLsim *sim, double errorMargin=1e-3, int maxIter=100, double stepSizeFraction=0.5)
Creates a new instance of class SimpleWeightTuner.
Definition: simple_weight_tuner.cpp:301
SimpleWeightTuner::done
bool done(bool printMessage=false)
Determines whether a termination criterion has been met.
Definition: simple_weight_tuner.cpp:312
CARLsim::getSpikeMonitor
SpikeMonitor * getSpikeMonitor(int grpId)
Returns the number of spikes per neuron for a certain group.
Definition: carlsim.cpp:2094
CARLsim
CARLsim User Interface This class provides a user interface to the public sections of CARLsimCore sou...
Definition: carlsim.h:138
CARLsim::getWeightRange
RangeWeight getWeightRange(short int connId)
returns the RangeWeight struct for a specific connection ID
Definition: carlsim.cpp:2097
SpikeMonitor::startRecording
void startRecording()
Starts a new recording period.
Definition: spike_monitor.cpp:201
SimpleWeightTuner::Impl::~Impl
~Impl()
Definition: simple_weight_tuner.cpp:104
CARLsim::getCARLsimState
CARLsimState getCARLsimState()
Writes population weights from gIDpre to gIDpost to file fname in binary.
Definition: carlsim.cpp:1999
carlsim.h
SimpleWeightTuner::iterate
void iterate(int runDurationMs=1000, bool printStatus=true)
Performs an iteration step of the tuning algorithm.
Definition: simple_weight_tuner.cpp:311
RangeWeight::min
double min
Definition: carlsim_datastructures.h:335
CARLsim::setSpikeMonitor
SpikeMonitor * setSpikeMonitor(int grpId, const std::string &fileName)
Sets a Spike Monitor for a groups, prints spikes to binary file.
Definition: carlsim.cpp:1973
SimpleWeightTuner::Impl::done
bool done(bool printMessage)
Definition: simple_weight_tuner.cpp:116