Miind
WilsonCowanAlgorithm.cpp
Go to the documentation of this file.
1 // Copyright (c) 2005 - 2012 Marc de Kamps
2 // 2012 David-Matthias Sichau
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
9 // and/or other materials provided with the distribution.
10 // * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software
11 // without specific prior written permission.
12 //
13 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
15 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
16 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
17 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18 //
24 
25 #include <NumtoolsLib/NumtoolsLib.h>
26 #include <gsl/gsl_matrix.h>
27 #include <gsl/gsl_errno.h>
28 
29 #include <functional>
30 #include <numeric>
31 
32 namespace {
33 
34 int sigmoid(double, const double y[], double f[], void *params) {
35  auto p_parameter = (MPILib::WilsonCowanParameter *) params;
36 
37  f[0] = (-y[0]
38  + p_parameter->_rate_maximum
39  / (1 + exp(-p_parameter->_f_noise * p_parameter->_f_input - p_parameter->_f_bias)))
40  / p_parameter->_time_membrane;
41 
42  return GSL_SUCCESS;
43 }
44 
45 int sigmoidprime(double , const double[], double *dfdy, double dfdt[],
46  void *params) {
47  auto p_parameter = (MPILib::WilsonCowanParameter *) params;
48  gsl_matrix_view dfdy_mat = gsl_matrix_view_array(dfdy, 1, 1);
49 
50  gsl_matrix * m = &dfdy_mat.matrix;
51 
52  gsl_matrix_set(m, 0, 0, -1 / p_parameter->_time_membrane);
53 
54  dfdt[0] = 0.0;
55 
56  return GSL_SUCCESS;
57 
58 }
59 }
60 
61 
62 using namespace MPILib;
63 
65  MPILib::AlgorithmInterface<double>(), _integrator(0, getInitialState(), 0, 0,
68 
69 }
70 
72  MPILib::AlgorithmInterface<double>(), _parameter(parameter), _integrator(0,
73  getInitialState(), 0, 0,
76  _integrator.Parameter() = _parameter;
77 }
78 
80 }
81 
83  return new WilsonCowanAlgorithm(*this);
84 }
85 
87 
88  NumtoolsLib::DVIntegratorStateParameter<WilsonCowanParameter> parameter_dv;
89 
90  parameter_dv._vector_state = std::vector<double>(1, 0);
91  parameter_dv._time_begin = simParam.getTBegin();
92  parameter_dv._time_end = simParam.getTEnd();
93  parameter_dv._time_step = simParam.getTStep();
94  parameter_dv._time_current = simParam.getTBegin();
95 
96  parameter_dv._parameter_space = _parameter;
97 
98  parameter_dv._number_maximum_iterations =
99  simParam.getMaximumNumberIterations();
100 
101  _integrator.Reconfigure(parameter_dv);
102 }
103 
104 void WilsonCowanAlgorithm::evolveNodeState(const std::vector<Rate>& nodeVector,
105  const std::vector<double>& weightVector, Time time) {
106 
107  double f_inner_product = innerProduct(nodeVector, weightVector);
108 
109  _integrator.Parameter()._f_input = f_inner_product;
110 
111  try {
112  while (_integrator.Evolve(time) < time)
113  ;
114  } catch (NumtoolsLib::DVIntegratorException& except) {
115  if (except.Code() == NumtoolsLib::NUMBER_ITERATIONS_EXCEEDED)
117  else
118  throw except;
119  }
120 }
121 
123  return _integrator.CurrentTime();
124 
125 }
126 
128  return *_integrator.BeginState();
129 
130 }
131 
132 double WilsonCowanAlgorithm::innerProduct(const std::vector<Rate>& nodeVector,
133  const std::vector<double>& weightVector) {
134 
135  assert(nodeVector.size()==weightVector.size());
136 
137  if (nodeVector.begin() == nodeVector.end())
138  return 0;
139 
140  return std::inner_product(nodeVector.begin(), nodeVector.end(),
141  weightVector.begin(), 0.0);
142 
143 }
144 
145 std::vector<double> WilsonCowanAlgorithm::getInitialState() const {
146  std::vector<double> array_return(WILSON_COWAN_STATE_DIMENSION);
147  array_return[0] = 0;
148  return array_return;
149 }
150 
152  return _integrator.State();
153 }
154 
double innerProduct(const std::vector< Rate > &nodeVector, const std::vector< double > &weightVector)
int sigmoidprime(double, const double[], double *dfdy, double dfdt[], void *params)
virtual void evolveNodeState(const std::vector< Rate > &nodeVector, const std::vector< double > &weightVector, Time time)
double Time
The interface for all algorithm classes.
const double WC_ABSOLUTE_PRECISION
virtual Time getCurrentTime() const
const std::string STR_NUMBER_ITERATIONS_EXCEEDED("The predetermined number of iterations is exceeded in Evolve()")
virtual Rate getCurrentRate() const
NumtoolsLib::DVIntegrator< WilsonCowanParameter > _integrator
virtual WilsonCowanAlgorithm * clone() const
unsigned int NodeId
const double WC_RELATIVE_PRECISION
Parameter determining how a simulation is run. Specifiying begin and end time, log file names...
virtual AlgorithmGrid getGrid(NodeId, bool b_state=true) const
const int WILSON_COWAN_STATE_DIMENSION
Wilson Cowan nodes have single double as state.
int sigmoid(double, const double y[], double f[], void *params)
virtual void configure(const SimulationRunParameter &simParam)
std::vector< double > getInitialState() const
The background of this algorithm is described on page The Wilson-Cowan Algorithm. An example of a ful...
double Rate