Kaspersky Neuromorphic Platform  1.0.0
API Reference
Loading...
Searching...
No Matches
projection.h
Go to the documentation of this file.
1
22#pragma once
23
24#include <knp/core/core.h>
25#include <knp/core/uid.h>
27
28#include <algorithm>
29#include <functional>
30#include <optional>
31#include <tuple>
32#include <unordered_map>
33#include <utility>
34#include <vector>
35
36#include <boost/multi_index/hashed_index.hpp>
37#include <boost/multi_index/member.hpp>
38#include <boost/multi_index_container.hpp>
39
40
44namespace knp::core
45{
64
65
72template <class SynapseType>
73class Projection final
74{
75public:
79 using ProjectionSynapseType = SynapseType;
87 using SynapseParameters = typename synapse_traits::synapse_parameters<SynapseType>;
88
92 using Synapse = std::tuple<SynapseParameters, size_t, size_t>;
93
97 using SynapseGenerator = std::function<std::optional<Synapse>(size_t)>;
98
99public:
104 template <typename SynapseT>
106 {
110 knp::synapse_traits::shared_synapse_parameters<SynapseT> synapses_parameters_;
111 };
112
118 template <template <typename> typename Rule, typename SynapseT>
119 struct SharedSynapseParametersT<knp::synapse_traits::STDP<Rule, SynapseT>>
120 {
124 enum class ProcessingType
125 {
129 STDPOnly,
133 STDPAndSpike
134 };
135
139 using ContainerType = std::unordered_map<core::UID, ProcessingType, core::uid_hash>;
143 ContainerType stdp_populations_;
144
148 knp::synapse_traits::shared_synapse_parameters<knp::synapse_traits::STDP<Rule, SynapseT>> synapses_parameters_;
149 };
150
155
156public:
162 Projection(UID presynaptic_uid, UID postsynaptic_uid);
169 Projection(UID uid, UID presynaptic_uid, UID postsynaptic_uid);
170
178 Projection(UID presynaptic_uid, UID postsynaptic_uid, SynapseGenerator generator, size_t num_iterations);
179
188 Projection(UID uid, UID presynaptic_uid, UID postsynaptic_uid, SynapseGenerator generator, size_t num_iterations);
189
190public:
195 [[nodiscard]] const UID &get_uid() const { return base_.uid_; }
196
202 [[nodiscard]] auto &get_tags() { return base_.tags_; }
203
209 [[nodiscard]] const auto &get_tags() const { return base_.tags_; }
210
211public:
217 [[nodiscard]] Synapse &operator[](size_t index) { return parameters_[index]; }
218
225 [[nodiscard]] const Synapse &operator[](size_t index) const { return parameters_[index]; }
226
231 [[nodiscard]] auto begin() const { return parameters_.cbegin(); }
232
237 [[nodiscard]] auto begin() { return parameters_.begin(); }
238
243 [[nodiscard]] auto end() const { return parameters_.cend(); }
244
249 [[nodiscard]] auto end() { return parameters_.end(); }
250
251public:
256 [[nodiscard]] size_t size() const { return parameters_.size(); }
257
262 [[nodiscard]] const UID &get_presynaptic() const { return presynaptic_uid_; }
263
268 [[nodiscard]] const UID &get_postsynaptic() const { return postsynaptic_uid_; }
269
284
291 [[nodiscard]] std::vector<size_t> find_synapses(size_t neuron_index, Search search_method) const;
292
299 size_t add_synapses(SynapseGenerator generator, size_t num_iterations);
300
304 void clear();
305
310 void remove_synapse(size_t index);
311
317 size_t remove_synapse_if(std::function<bool(const Synapse &)> predicate);
318
324 size_t remove_postsynaptic_neuron_synapses(size_t neuron_index);
325
331 size_t remove_presynaptic_neuron_synapses(size_t neuron_index);
332
333public:
337 void lock_weights() { is_locked_ = true; }
338
342 void unlock_weights() { is_locked_ = false; }
343
348 bool is_locked() const { return is_locked_; }
349
350public:
355 SharedSynapseParameters &get_shared_parameters() { return shared_parameters_; }
361 const SharedSynapseParameters &get_shared_parameters() const { return shared_parameters_; }
362
363private:
364 void reindex() const;
365
366 BaseData base_;
367
371 UID presynaptic_uid_;
372
376 UID postsynaptic_uid_;
377
381 bool is_locked_ = true;
382
386 std::vector<Synapse> parameters_;
387 // So far the index is mutable so we can reindex a const object that has a non-updated index.
388 struct Connection
389 {
390 size_t from_;
391 size_t to_;
392 size_t index_;
393 bool operator==(const Connection &connection) const
394 {
395 return from_ == connection.from_ && to_ == connection.to_ && index_ == connection.index_;
396 }
397 };
398
399 typedef boost::multi_index::multi_index_container<
400 Connection,
401 boost::multi_index::indexed_by<
402 boost::multi_index::hashed_non_unique<
403 boost::multi_index::tag<struct mi_presynaptic>, BOOST_MULTI_INDEX_MEMBER(Connection, size_t, from_)>,
404 boost::multi_index::hashed_non_unique<
405 boost::multi_index::tag<struct mi_postsynaptic>, BOOST_MULTI_INDEX_MEMBER(Connection, size_t, to_)>,
406 boost::multi_index::hashed_unique<
407 boost::multi_index::tag<struct mi_synapse_index>,
408 BOOST_MULTI_INDEX_MEMBER(Connection, size_t, index_)>>>
409 Index;
410 using ByPresynaptic = mi_presynaptic;
411 using ByPostsynaptic = mi_postsynaptic;
412
413 mutable Index index_;
414 mutable bool is_index_updated_ = false;
415
416 SharedSynapseParameters shared_parameters_;
417};
418
419
427using AllProjections = boost::mp11::mp_transform<knp::core::Projection, knp::synapse_traits::AllSynapses>;
428
438using AllProjectionsVariant = boost::mp11::mp_rename<AllProjections, std::variant>;
439
440
441} // namespace knp::core
The Projection class is a definition of similar connections between the neurons of two populations.
Definition projection.h:74
Synapse & operator[](size_t index)
Get parameter values of a synapse with the given index.
Definition projection.h:217
const UID & get_postsynaptic() const
Get UID of the associated population to which this projection sends signals.
Definition projection.h:268
Projection(UID presynaptic_uid, UID postsynaptic_uid, SynapseGenerator generator, size_t num_iterations)
Construct a projection by running a synapse generator a given number of times.
std::vector< size_t > find_synapses(size_t neuron_index, Search search_method) const
Find synapses that originate from a neuron with the given index.
const UID & get_presynaptic() const
Get UID of the associated population from which this projection receives spikes.
Definition projection.h:262
auto begin()
Get an iterator pointing to the first element of the projection.
Definition projection.h:237
void remove_synapse(size_t index)
Remove a synapse with the given index from the projection.
auto end() const
Get an iterator pointing to the last element of the projection.
Definition projection.h:243
void clear()
Remove all synapses from the projection.
SynapseType ProjectionSynapseType
Type of the projection synapses.
Definition projection.h:79
const SharedSynapseParameters & get_shared_parameters() const
Get parameters shared between all synapses.
Definition projection.h:361
Projection< SynapseType > ProjectionType
Projection of synapses with the specified synapse type.
Definition projection.h:83
auto begin() const
Get an iterator pointing to the first element of the projection.
Definition projection.h:231
size_t remove_synapse_if(std::function< bool(const Synapse &)> predicate)
Remove synapses according to a given criterion.
size_t remove_presynaptic_neuron_synapses(size_t neuron_index)
Remove all synapses that receive signals from a neuron with the given index.
size_t size() const
Count number of synapses in the projection.
Definition projection.h:256
auto end()
Get an iterator pointing to the last element of the projection.
Definition projection.h:249
typename synapse_traits::synapse_parameters< SynapseType > SynapseParameters
Parameters of the specified synapse type.
Definition projection.h:87
size_t remove_postsynaptic_neuron_synapses(size_t neuron_index)
Remove all synapses that lead to a neuron with the given index.
std::function< std::optional< Synapse >(size_t)> SynapseGenerator
Synapse generation function type.
Definition projection.h:97
void lock_weights()
Lock the possibility to change synapses weights.
Definition projection.h:337
size_t add_synapses(SynapseGenerator generator, size_t num_iterations)
Append connections to the existing projection.
Projection(UID presynaptic_uid, UID postsynaptic_uid)
Construct an empty projection.
SharedSynapseParameters & get_shared_parameters()
Get parameters shared between all synapses.
Definition projection.h:355
std::tuple< SynapseParameters, size_t, size_t > Synapse
Synapse description structure that contains synapse parameters and indexes of the associated neurons.
Definition projection.h:92
void unlock_weights()
Unlock the possibility to change synapses weights.
Definition projection.h:342
Search
Types of synapse search.
Definition projection.h:274
@ by_presynaptic
Search by presynaptic neuron index.
Definition projection.h:278
@ by_postsynaptic
Search by postsynaptic neuron index.
Definition projection.h:282
Projection(UID uid, UID presynaptic_uid, UID postsynaptic_uid, SynapseGenerator generator, size_t num_iterations)
Construct a projection by running a synapse generator a given number of times.
const Synapse & operator[](size_t index) const
Get parameter values of a synapse with the given index.
Definition projection.h:225
const UID & get_uid() const
Get projection UID.
Definition projection.h:195
Projection(UID uid, UID presynaptic_uid, UID postsynaptic_uid)
Construct an empty projection.
const auto & get_tags() const
Get tags used by the projection.
Definition projection.h:209
auto & get_tags()
Get tags used by the projection.
Definition projection.h:202
SharedSynapseParametersT< SynapseType > SharedSynapseParameters
Type of shared synapse parameters.
Definition projection.h:154
bool is_locked() const
Determine if the synapse weight change is locked.
Definition projection.h:348
Class definition for core library basic entities.
Core library namespace.
Definition backend.h:50
boost::mp11::mp_transform< knp::core::Projection, knp::synapse_traits::AllSynapses > AllProjections
List of projection types based on synapse types specified in knp::synapse_traits::AllSynapses.
Definition projection.h:427
boost::mp11::mp_rename< AllProjections, std::variant > AllProjectionsVariant
Projection variant that contains any projection type specified in AllProjections.
Definition projection.h:438
SynapseElementAccess
Enumeration used to access connection.
Definition projection.h:50
@ target_neuron_id
Getting target neuron index.
Definition projection.h:62
@ synapse_data
Getting synapse parameters.
Definition projection.h:54
@ source_neuron_id
Getting source neuron index.
Definition projection.h:58
Shared synapse parameters for the non-STDP variant of the projection.
Definition projection.h:106
knp::synapse_traits::shared_synapse_parameters< SynapseT > synapses_parameters_
Shared synapse parameters.
Definition projection.h:110
Delta synapse type traits.
UID class and routines.