Kaspersky Neuromorphic Platform  1.0.0
API Reference
Loading...
Searching...
No Matches
network.h
Go to the documentation of this file.
1
22#pragma once
23
24#include <knp/core/backend.h>
25#include <knp/core/core.h>
26#include <knp/core/impexp.h>
27#include <knp/core/population.h>
28#include <knp/core/projection.h>
34
35#include <string>
36#include <tuple>
37#include <type_traits>
38#include <utility>
39#include <variant>
40#include <vector>
41
42
46namespace knp::framework
47{
48
52class KNP_DECLSPEC Network
53{
54public:
62 using AllPopulations = boost::mp11::mp_transform<knp::core::Population, knp::neuron_traits::AllNeurons>;
63
72 using AllProjections = boost::mp11::mp_transform<knp::core::Projection, knp::synapse_traits::AllSynapses>;
73
83 using AllPopulationVariants = boost::mp11::mp_rename<AllPopulations, std::variant>;
93 using AllProjectionVariants = boost::mp11::mp_rename<AllProjections, std::variant>;
94
95public:
99 using PopulationContainer = std::vector<core::AllPopulationsVariant>;
103 using ProjectionContainer = std::vector<core::AllProjectionsVariant>;
104
108 using PopulationIterator = PopulationContainer::iterator;
112 using PopulationConstIterator = PopulationContainer::const_iterator;
113
117 using ProjectionIterator = ProjectionContainer::iterator;
121 using ProjectionConstIterator = ProjectionContainer::const_iterator;
122
123public:
127 Network() = default;
128
133 explicit Network(const core::UID &uid) : base_({uid, {}}) {}
134
135public:
140 void add_population(core::AllPopulationsVariant &&population);
146 template <typename PopulationType>
147 void add_population(typename std::decay<PopulationType>::type &&population);
153 template <typename PopulationType>
154 void add_population(typename std::decay<PopulationType>::type &population);
155
161 [[nodiscard]] bool is_population_exists(const knp::core::UID &population_uid) const;
162
170 template <typename NeuronType>
171 [[nodiscard]] knp::core::Population<NeuronType> &get_population(const knp::core::UID &population_uid);
172
181 template <typename NeuronType>
182 [[nodiscard]] const knp::core::Population<NeuronType> &get_population(const knp::core::UID &population_uid) const;
183
190 [[nodiscard]] core::AllPopulationsVariant &get_population(const knp::core::UID &population_uid);
191
196 void remove_population(const knp::core::UID &population_uid);
197
198public:
203 void add_projection(core::AllProjectionsVariant &&projection);
209 template <typename ProjectionType>
210 void add_projection(typename std::decay<ProjectionType>::type &&projection);
216 template <typename ProjectionType>
217 void add_projection(typename std::decay<ProjectionType>::type &projection);
227 template <typename SynapseType>
229 knp::core::UID projection_uid, knp::core::UID pre_population_uid, knp::core::UID post_population_uid,
230 typename knp::core::Projection<SynapseType>::SynapseGenerator generator, size_t synapse_count);
231
237 [[nodiscard]] bool is_projection_exists(const knp::core::UID &projection_uid) const;
238
246 template <typename SynapseType>
247 [[nodiscard]] knp::core::Projection<SynapseType> &get_projection(const knp::core::UID &projection_uid);
256 template <typename SynapseType>
257 [[nodiscard]] const knp::core::Projection<SynapseType> &get_projection(const knp::core::UID &projection_uid) const;
264 [[nodiscard]] core::AllProjectionsVariant &get_projection(const knp::core::UID &projection_uid);
269 void remove_projection(const knp::core::UID &projection_uid);
270
271public:
284 template <typename SynapseType, typename SourceNeuronType, typename DestinationNeuronType>
285 knp::core::UID connect_populations(
286 const core::Population<SourceNeuronType> &src, const core::Population<DestinationNeuronType> &dst,
287 typename projection::parameters_generators::SynGen2ParamsType<SynapseType> syn_gen =
288 projection::parameters_generators::default_synapse_gen<SynapseType>)
289 {
290 const auto &[src_uid, dst_uid] = get_populations_uid(src, dst);
291 auto new_proj =
292 projection::connect_populations<SynapseType, SourceNeuronType, DestinationNeuronType>(src, dst, syn_gen);
293 const auto proj_uid = new_proj.get_uid();
294
295 add_projection(std::move(new_proj));
296
297 return proj_uid;
298 }
299
311 template <typename SynapseType, typename SourceNeuronType, typename DestinationNeuronType>
312 knp::core::UID connect_populations(
313 const core::Population<SourceNeuronType> &src, const core::Population<DestinationNeuronType> &dst,
314 typename knp::core::Projection<SynapseType>::SynapseGenerator syn_gen, size_t num_iterations)
315 {
316 const auto &[src_uid, dst_uid] = get_populations_uid(src, dst);
317 auto new_proj = projection::connect_populations<SynapseType, SourceNeuronType, DestinationNeuronType>(
318 src, dst, syn_gen, num_iterations);
319 const auto proj_uid = new_proj.get_uid();
320
321 add_projection(std::move(new_proj));
322
323 return proj_uid;
324 }
325
326public:
331 [[nodiscard]] PopulationIterator begin_populations();
336 [[nodiscard]] PopulationConstIterator begin_populations() const;
341 [[nodiscard]] PopulationIterator end_populations();
346 [[nodiscard]] PopulationConstIterator end_populations() const;
351 [[nodiscard]] ProjectionIterator begin_projections();
356 [[nodiscard]] ProjectionConstIterator begin_projections() const;
361 [[nodiscard]] ProjectionIterator end_projections();
366 [[nodiscard]] ProjectionConstIterator end_projections() const;
367
368public:
373 [[nodiscard]] const PopulationContainer &get_populations() const { return populations_; }
378 [[nodiscard]] const ProjectionContainer &get_projections() const { return projections_; }
379
380public:
385 [[nodiscard]] size_t populations_count() const { return populations_.size(); }
390 [[nodiscard]] size_t projections_count() const { return projections_.size(); }
391
392public:
397 [[nodiscard]] const knp::core::UID &get_uid() const { return base_.uid_; }
398
404 [[nodiscard]] auto &get_tags() { return base_.tags_; }
405
406private:
407 template <typename SourceNeuronType, typename DestinationNeuronType>
408 [[nodiscard]] std::tuple<core::UID, core::UID> get_populations_uid(
409 const core::Population<SourceNeuronType> &src, const core::Population<DestinationNeuronType> &dst) const
410 {
411 const auto src_uid = src.get_uid();
412 if (!is_population_exists(src_uid))
413 {
414 throw std::logic_error("Source population with UID = " + std::string(src_uid) + " doesn't exist.");
415 }
416
417 const auto dst_uid = dst.get_uid();
418 if (!is_population_exists(dst_uid))
419 {
420 throw std::logic_error("Source population with UID = " + std::string(dst_uid) + " doesn't exist.");
421 }
422
423 return std::make_tuple(src_uid, dst_uid);
424 }
425
426 template <typename PopulationType>
427 void check_population_constraints(const PopulationType &population) const;
428 template <typename ProjectionType>
429 void check_projection_constraints(const ProjectionType &projection) const;
430
431private:
432 knp::core::BaseData base_;
433 PopulationContainer populations_;
434 ProjectionContainer projections_;
435};
436
437
438} // namespace knp::framework
const UID & get_uid() const
Get population UID.
Definition population.h:88
std::function< std::optional< Synapse >(size_t)> SynapseGenerator
Synapse generation function type.
Definition projection.h:97
The Network class is a definition of a neural network that contains populations and projections.
Definition network.h:53
void add_projection(core::AllProjectionsVariant &&projection)
Add a projection to the network.
const PopulationContainer & get_populations() const
Get population container from the network.
Definition network.h:373
size_t populations_count() const
Count populations in the network.
Definition network.h:385
core::AllPopulationsVariant & get_population(const knp::core::UID &population_uid)
Get a population with the given UID from the network.
const knp::core::Population< NeuronType > & get_population(const knp::core::UID &population_uid) const
Get a population with the given UID from the network.
void add_population(core::AllPopulationsVariant &&population)
Add a population to the network.
ProjectionConstIterator end_projections() const
Get an iterator pointing to the last element of the projection.
const ProjectionContainer & get_projections() const
Get projection container from the network.
Definition network.h:378
PopulationIterator begin_populations()
Get an iterator pointing to the first element of the population.
void add_projection(knp::core::UID projection_uid, knp::core::UID pre_population_uid, knp::core::UID post_population_uid, typename knp::core::Projection< SynapseType >::SynapseGenerator generator, size_t synapse_count)
Add a projection to the network.
knp::core::Projection< SynapseType > & get_projection(const knp::core::UID &projection_uid)
Get a projection with the given UID from the network.
PopulationConstIterator begin_populations() const
Get an iterator pointing to the first element of the population.
Network()=default
Default network constructor.
knp::core::UID connect_populations(const core::Population< SourceNeuronType > &src, const core::Population< DestinationNeuronType > &dst, typename projection::parameters_generators::SynGen2ParamsType< SynapseType > syn_gen=projection::parameters_generators::default_synapse_gen< SynapseType >)
Connect presynaptic and postsynaptic populations and add projection to the network.
Definition network.h:285
ProjectionConstIterator begin_projections() const
Get an iterator pointing to the first element of the projection.
bool is_population_exists(const knp::core::UID &population_uid) const
Check if population exists.
const knp::core::UID & get_uid() const
Get network UID.
Definition network.h:397
void add_projection(typename std::decay< ProjectionType >::type &projection)
Add a projection to the network.
size_t projections_count() const
Count projections in the network.
Definition network.h:390
PopulationConstIterator end_populations() const
Get an iterator pointing to the last element of the population.
auto & get_tags()
Get tags used by the network.
Definition network.h:404
knp::core::Population< NeuronType > & get_population(const knp::core::UID &population_uid)
Get a population with the given UID from the network.
const knp::core::Projection< SynapseType > & get_projection(const knp::core::UID &projection_uid) const
Get a projection with the given UID from the network.
ProjectionIterator end_projections()
Get an iterator pointing to the last element of the projection.
core::AllProjectionsVariant & get_projection(const knp::core::UID &projection_uid)
Get a projection with the given UID from the network.
knp::core::UID connect_populations(const core::Population< SourceNeuronType > &src, const core::Population< DestinationNeuronType > &dst, typename knp::core::Projection< SynapseType >::SynapseGenerator syn_gen, size_t num_iterations)
Connect presynaptic and postsynaptic populations and add projection to the network.
Definition network.h:312
void add_population(typename std::decay< PopulationType >::type &population)
Add a population to the network.
void add_projection(typename std::decay< ProjectionType >::type &&projection)
Add a projection to the network.
void remove_projection(const knp::core::UID &projection_uid)
Remove a projection with the given UID from the network.
void add_population(typename std::decay< PopulationType >::type &&population)
Add a population to the network.
void remove_population(const knp::core::UID &population_uid)
Remove a population with the given UID from the network.
Network(const core::UID &uid)
Network constructor with pre-defined UID.
Definition network.h:133
bool is_projection_exists(const knp::core::UID &projection_uid) const
Check if projection exists.
PopulationIterator end_populations()
Get an iterator pointing to the last element of the population.
ProjectionIterator begin_projections()
Get an iterator pointing to the first element of the projection.
Population connector.
Class definition for backend base.
Class definition for core library basic entities.
Coordinate generator.
Framework namespace.
List of all neuron type traits.
General population interface.
General projection interface.
Delta synapse type traits.
Parameters generators.