├── 3rdparty ├── httplib.h └── json.hpp ├── CMakeLists.txt ├── LICENSE.md ├── README.md ├── RELEASES.md ├── docs ├── Doxyfile ├── README.md ├── doxygen-awesome-darkmode-toggle.js ├── doxygen-awesome-fragment-copy-button.js ├── doxygen-awesome-interactive-toc.js ├── doxygen-awesome-paragraph-link.js ├── doxygen-awesome-sidebar-only-darkmode-toggle.css ├── doxygen-awesome-sidebar-only.css ├── doxygen-awesome.css ├── header.html └── logo.png ├── include └── indk │ ├── backends │ ├── default.h │ ├── multithread.h │ └── opencl.h │ ├── computer.h │ ├── error.h │ ├── interlink.h │ ├── neuralnet.h │ ├── neuron.h │ ├── position.h │ ├── profiler.h │ └── system.h ├── samples ├── lang │ ├── CMakeLists.txt │ ├── main.cpp │ ├── structures │ │ └── structure.json │ └── texts │ │ └── vocab.txt ├── multimodal │ ├── CMakeLists.txt │ ├── bmp.hpp │ ├── images │ │ ├── 1.bmp │ │ ├── 2.bmp │ │ └── 3.bmp │ ├── main.cpp │ └── structures │ │ └── structure.json ├── test │ ├── CMakeLists.txt │ ├── main.cpp │ └── structures │ │ ├── structure_bench.json │ │ └── structure_general.json └── vision │ ├── CMakeLists.txt │ ├── bmp.hpp │ ├── images │ ├── learn │ │ ├── 1.bmp │ │ ├── 10.bmp │ │ ├── 2.bmp │ │ ├── 3.bmp │ │ ├── 4.bmp │ │ ├── 5.bmp │ │ ├── 6.bmp │ │ ├── 7.bmp │ │ ├── 8.bmp │ │ └── 9.bmp │ └── test │ │ ├── 1-1.bmp │ │ ├── 1-10.bmp │ │ ├── 1-2.bmp │ │ ├── 1-3.bmp │ │ ├── 1-4.bmp │ │ ├── 1-5.bmp │ │ ├── 1-6.bmp │ │ ├── 1-7.bmp │ │ ├── 1-8.bmp │ │ ├── 1-9.bmp │ │ ├── 10-1.bmp │ │ ├── 10-10.bmp │ │ ├── 10-2.bmp │ │ ├── 10-3.bmp │ │ ├── 10-4.bmp │ │ ├── 10-5.bmp │ │ ├── 10-6.bmp │ │ ├── 10-7.bmp │ │ ├── 10-8.bmp │ │ ├── 10-9.bmp │ │ ├── 2-1.bmp │ │ ├── 2-10.bmp │ │ ├── 2-2.bmp │ │ ├── 2-3.bmp │ │ ├── 2-4.bmp │ │ ├── 2-5.bmp │ │ ├── 2-6.bmp │ │ ├── 2-7.bmp │ │ ├── 2-8.bmp │ │ ├── 2-9.bmp │ │ ├── 3-1.bmp │ │ ├── 3-10.bmp │ │ ├── 3-2.bmp │ │ ├── 3-3.bmp │ │ ├── 3-4.bmp │ │ ├── 3-5.bmp │ │ ├── 3-6.bmp │ │ ├── 3-7.bmp │ │ ├── 3-8.bmp │ │ ├── 3-9.bmp │ │ ├── 4-1.bmp │ │ ├── 4-10.bmp │ │ ├── 4-2.bmp │ │ ├── 4-3.bmp │ │ ├── 4-4.bmp │ │ ├── 4-5.bmp │ │ ├── 4-6.bmp │ │ ├── 4-7.bmp │ │ ├── 4-8.bmp │ │ ├── 4-9.bmp │ │ ├── 5-1.bmp │ │ ├── 5-10.bmp │ │ ├── 5-2.bmp │ │ ├── 5-3.bmp │ │ ├── 5-4.bmp │ │ ├── 5-5.bmp │ │ ├── 5-6.bmp │ │ ├── 5-7.bmp │ │ ├── 5-8.bmp │ │ ├── 5-9.bmp │ │ ├── 6-1.bmp │ │ ├── 6-10.bmp │ │ ├── 6-2.bmp │ │ ├── 6-3.bmp │ │ ├── 6-4.bmp │ │ ├── 6-5.bmp │ │ ├── 6-6.bmp │ │ ├── 6-7.bmp │ │ ├── 6-8.bmp │ │ ├── 6-9.bmp │ │ ├── 7-1.bmp │ │ ├── 7-10.bmp │ │ ├── 7-2.bmp │ │ ├── 7-3.bmp │ │ ├── 7-4.bmp │ │ ├── 7-5.bmp │ │ ├── 7-6.bmp │ │ ├── 7-7.bmp │ │ ├── 7-8.bmp │ │ ├── 7-9.bmp │ │ ├── 8-1.bmp │ │ ├── 8-10.bmp │ │ ├── 8-2.bmp │ │ ├── 8-3.bmp │ │ ├── 8-4.bmp │ │ ├── 8-5.bmp │ │ ├── 8-6.bmp │ │ ├── 8-7.bmp │ │ ├── 8-8.bmp │ │ ├── 8-9.bmp │ │ ├── 9-1.bmp │ │ ├── 9-10.bmp │ │ ├── 9-2.bmp │ │ ├── 9-3.bmp │ │ ├── 9-4.bmp │ │ ├── 9-5.bmp │ │ ├── 9-6.bmp │ │ ├── 9-7.bmp │ │ ├── 9-8.bmp │ │ └── 9-9.bmp │ ├── main.cpp │ └── structures │ └── structure.json └── src ├── backends ├── default.cpp ├── multithread.cpp └── opencl.cpp ├── computer.cpp ├── error.cpp ├── interlink.cpp ├── neuralnet └── neuralnet.cpp ├── neuron ├── entry.cpp ├── neuron.cpp ├── receptor.cpp └── synapse.cpp ├── position.cpp ├── profiler.cpp └── system.cpp /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | project(interference VERSION 2.1.0 DESCRIPTION "Interference library") 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | set(TARGET_SOURCE 7 | src/neuron/neuron.cpp include/indk/neuron.h src/neuron/entry.cpp src/neuron/synapse.cpp src/neuron/receptor.cpp 8 | src/neuralnet/neuralnet.cpp include/indk/neuralnet.h 9 | src/error.cpp include/indk/error.h src/system.cpp include/indk/system.h src/position.cpp include/indk/position.h 10 | src/computer.cpp include/indk/computer.h 11 | src/backends/default.cpp include/indk/backends/default.h 12 | src/backends/multithread.cpp include/indk/backends/multithread.h 13 | src/backends/opencl.cpp include/indk/backends/opencl.h src/interlink.cpp include/indk/interlink.h 14 | src/profiler.cpp include/indk/profiler.h) 15 | 16 | add_library(objlib OBJECT ${TARGET_SOURCE}) 17 | set_property(TARGET objlib PROPERTY POSITION_INDEPENDENT_CODE ON) 18 | 19 | function(build_indk LIBTYPE) 20 | message("Building ${LIBTYPE} variant") 21 | 22 | if(${LIBTYPE} STREQUAL shared) 23 | set(LIBTYPE "") 24 | add_library(${PROJECT_NAME} SHARED $) 25 | elseif(${LIBTYPE} STREQUAL static) 26 | set(LIBTYPE "_static") 27 | add_library(${PROJECT_NAME}${LIBTYPE} STATIC $) 28 | endif() 29 | 30 | set_target_properties(${PROJECT_NAME}${LIBTYPE} PROPERTIES 31 | VERSION ${PROJECT_VERSION} 32 | SOVERSION ${PROJECT_VERSION_MAJOR}) 33 | 34 | include_directories(include/) 35 | include_directories(3rdparty/) 36 | 37 | find_package(Threads REQUIRED) 38 | target_link_libraries(${PROJECT_NAME}${LIBTYPE} Threads::Threads -latomic) 39 | 40 | if (WIN32) 41 | target_link_libraries(${PROJECT_NAME}${LIBTYPE} -lws2_32) 42 | ENDIF() 43 | 44 | OPTION(INDK_OPENCL_SUPPORT "Interference NDK OpenCL compute backend support" OFF) 45 | 46 | if(INDK_OPENCL_SUPPORT) 47 | find_package(OpenCL REQUIRED) 48 | target_link_libraries(${PROJECT_NAME}${LIBTYPE} ${OpenCL_LIBRARIES}) 49 | 50 | include_directories(${OpenCL_INCLUDE_DIRS}) 51 | 52 | target_compile_definitions(${PROJECT_NAME}${LIBTYPE} PRIVATE CL_HPP_TARGET_OPENCL_VERSION=120) 53 | target_compile_definitions(${PROJECT_NAME}${LIBTYPE} PRIVATE CL_HPP_MINIMUM_OPENCL_VERSION=120) 54 | target_compile_definitions(${PROJECT_NAME}${LIBTYPE} PRIVATE CL_TARGET_OPENCL_VERSION=120) 55 | 56 | add_compile_definitions(INDK_OPENCL_SUPPORT) 57 | message(STATUS "Interference NDK OpenCL compute backend support is enabled") 58 | endif() 59 | endfunction() 60 | 61 | build_indk(shared) 62 | build_indk(static) 63 | 64 | if (WIN32) 65 | install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_SOURCE_DIR}/dist/lib NAMELINK_COMPONENT) 66 | else() 67 | install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_SOURCE_DIR}/dist/lib NAMELINK_COMPONENT) 68 | endif() 69 | 70 | install(TARGETS ${PROJECT_NAME}_static ARCHIVE DESTINATION ${CMAKE_SOURCE_DIR}/dist/lib NAMELINK_COMPONENT) 71 | install(DIRECTORY include DESTINATION ${CMAKE_SOURCE_DIR}/dist) 72 | 73 | add_subdirectory(samples/test) 74 | add_subdirectory(samples/vision) 75 | add_subdirectory(samples/multimodal) 76 | add_subdirectory(samples/lang) 77 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 NickWare Group 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 |

3 | Cross-platform C++ library - universal neurobiology-based machine learning framework.
4 | Version 2.1.0 5 |

6 | 7 | 8 | ---------------------------------------------------------------- 9 | ### PLATFORMS 10 | | | x86 | aarch64 | armle.v7 | PowerPC | e2k | MIPS | 11 | |:------------------------------:|:---:|:-------:|:--------:|:-------:|:---:|:----:| 12 | | **Windows** | + | | | | | | 13 | | **Linux** | + | + | + | | + | | 14 | | **QNX** | + | | + | | | | 15 | | **Secured RTOS KPDA 10964-01** | + | + | + | + | + | + | 16 | 17 | ---------------------------------------------------------------- 18 | ### COMPUTING 19 | - Native CPU (single thread) 20 | - Native CPU (multithread) 21 | - OpenCL 22 | 23 | ---------------------------------------------------------------- 24 | ### REQUIREMENTS 25 | - CMake 3.12 or newer 26 | - g++ 8.3.0 or newer (MinGW under Windows) 27 | 28 | ---------------------------------------------------------------- 29 | ### LICENCE 30 | Interference library is distributed under the MIT Licence. 31 | 32 | interference_vision example uses the part of COIL-100 dataset. 33 | 34 | "Columbia Object Image Library (COIL-100)," S. A. Nene, S. K. Nayar and H. Murase, Technical Report CUCS-006-96, February 1996. 35 | http://www1.cs.columbia.edu/CAVE/software/softlib/coil-100.php 36 | 37 | ---------------------------------------------------------------- 38 | ### SAMPLES 39 | - [Test](samples/test) - benchmark 40 | - [Vision](samples/vision) - example of image recognition system 41 | - [Multimodal](samples/multimodal) - example of multimodal (image+text) data processing 42 | - [Lang](samples/lang) - example of natural language processing (NLP) 43 | 44 | ---------------------------------------------------------------- 45 | ### HOW TO BUILD 46 | Note that %INTERFERENCE_ROOT% is root directory of Interference library files, %BUILD_TYPE% is "debug" or "release". After the last step, library and binaries will be in %INTERFERENCE_ROOT%/lib and %INTERFERENCE_ROOT%/bin respectively. 47 | #### Building for Windows (MinGW) 48 | Run CMD and follow this steps: 49 | 1. Prepare build directory 50 | ``` 51 | cd %INTERFERENCE_ROOT% 52 | mkdir cmake-build-%BUILD_TYPE% 53 | cd cmake-build-%BUILD_TYPE% 54 | ``` 55 | 2. Configure build files 56 | ``` 57 | cmake -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DCMAKE_MAKE_PROGRAM=make.exe -G "MinGW Makefiles" .. 58 | ``` 59 | 3. Build and install the library 60 | ``` 61 | make install 62 | ``` 63 | #### Building for Linux 64 | Run terminal and follow this steps: 65 | 1. Prepare build directory 66 | ``` 67 | cd %INTERFERENCE_ROOT% 68 | mkdir cmake-build-%BUILD_TYPE% 69 | cd cmake-build-%BUILD_TYPE% 70 | ``` 71 | 2. Configure build files 72 | ``` 73 | cmake -DCMAKE_BUILD_TYPE=%BUILD_TYPE% .. 74 | ``` 75 | 3. Build and install the library 76 | ``` 77 | make install 78 | ``` 79 | -------------------------------------------------------------------------------- /RELEASES.md: -------------------------------------------------------------------------------- 1 | ### RELEASE NOTES 2 | #### 1.1.0 3 | - Interference library performance increased about 15 times 4 | - Added multithreading support for multicore CPUs 5 | - Added experimental support of ARM architecture 6 | - Added ultimate example of image recognition system 7 | - Exceptions raised by inn::Error class are more informative now 8 | - Added doCheckSignal(), doResetSignalController(), getSignal() and getTime() methods for inn::NeuralNet::Link class 9 | - Fixed doAddNeuron(...) method in inn::NeuralNet class (now allowed feedback links) 10 | - Added doComparePatterns() and getNeuronCount() methods for inn::NeuralNet class 11 | - Added new modes of neuron link definition: LinkDefinitionRange and LinkDefinitionRangeNext 12 | - Now getEntries() and getReceptors() methods in inn::Neuron are named as getEntry(...) and getReceptor(...) 13 | - Added doPrepare(), setk1(...) and setk2(...) methods for inn::Neuron, inn::Neuron::Entry and inn::Neuron::Synaps classes 14 | - Added setNeurotransmitterType(...) method for inn::Neuron::Entry and inn::Neuron::Synaps classes 15 | - Added getQSize() method for inn::Neuron::Synaps class 16 | - Added setk3(...) and getdFi() methods for inn::Neuron::Receptor class 17 | - Added getReceptorInfluenceValue(...) and getSynapticSensitivityValue(...) static methods for inn::Neuron::System class 18 | - Added new arithmetic operations methods for inn::Position class 19 | - Added doZeroPosition() and getDistanceFrom(...) methods for inn::Position class 20 | 21 | #### 1.0.8 22 | - Fixed inn::Position operators definition 23 | - Fixed neurons priority assignment algorithm in inn::NeuralNet 24 | - Added getXm() method for inn::Neuron 25 | - Added destructors for inn::NeuralNet, inn::Neuron and inn::Neuron::Entry 26 | - Added doFinalize() method for inn::NeuralNet 27 | - Improved data finalization process 28 | - Type fixes in inn::NeuralNet 29 | - inn::Neuron synapse and receptor position definition fixes (doCreateNewSynaps() and doCreateNewReceptor() methods) 30 | - Added new inn::Position constructor 31 | 32 | #### 1.0.3 33 | - Fixed file headers 34 | - Added implementation of inn::Error (exception system) 35 | - Updated library structure 36 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # Getting started 2 | 3 |

4 |

5 |

6 | 7 | 8 | ## Platforms 9 | | | x86 | aarch64 | armle.v7 | PowerPC | e2k | MIPS | 10 | |:------------------------------:|:---:|:-------:|:--------:|:-------:|:---:|:----:| 11 | | **Windows** | + | | | | | | 12 | | **Linux** | + | + | + | | + | | 13 | | **QNX** | + | | + | | | | 14 | | **Secured RTOS KPDA 10964-01** | + | + | + | + | + | + | 15 | 16 | 17 | ## Computing 18 | - Native CPU (single thread) 19 | - Native CPU (multithread) 20 | - OpenCL 21 | 22 | 23 | ## Requirements 24 | - CMake 3.12 or newer 25 | - g++ 8.3.0 or newer (MinGW under Windows) 26 | 27 | ---------------------------------------------------------------- 28 | ## Building library and samples 29 | Note that %INTERFERENCE_ROOT% is root directory of Interference library files, %BUILD_TYPE% is "debug" or "release". After the last step, library and binaries will be in %INTERFERENCE_ROOT%/lib and %INTERFERENCE_ROOT%/bin respectively. 30 | ### Building for Windows (MinGW) 31 | Run CMD and follow this steps: 32 | 1. Prepare build directory 33 | ``` 34 | cd %INTERFERENCE_ROOT% 35 | mkdir cmake-build-%BUILD_TYPE% 36 | cd cmake-build-%BUILD_TYPE% 37 | ``` 38 | 2. Configure build files 39 | ``` 40 | cmake -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DCMAKE_MAKE_PROGRAM=make.exe -G "MinGW Makefiles" .. 41 | ``` 42 | 3. Build and install the library 43 | ``` 44 | make install 45 | ``` 46 | ### Building for Linux 47 | Run terminal and follow this steps: 48 | 1. Prepare build directory 49 | ``` 50 | cd %INTERFERENCE_ROOT% 51 | mkdir cmake-build-%BUILD_TYPE% 52 | cd cmake-build-%BUILD_TYPE% 53 | ``` 54 | 2. Configure build files 55 | ``` 56 | cmake -DCMAKE_BUILD_TYPE=%BUILD_TYPE% .. 57 | ``` 58 | 3. Build and install the library 59 | ``` 60 | make install 61 | ``` 62 | -------------------------------------------------------------------------------- /docs/doxygen-awesome-darkmode-toggle.js: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | Doxygen Awesome 4 | https://github.com/jothepro/doxygen-awesome-css 5 | 6 | MIT License 7 | 8 | Copyright (c) 2021 - 2022 jothepro 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | 28 | */ 29 | 30 | class DoxygenAwesomeDarkModeToggle extends HTMLElement { 31 | // SVG icons from https://fonts.google.com/icons 32 | // Licensed under the Apache 2.0 license: 33 | // https://www.apache.org/licenses/LICENSE-2.0.html 34 | static lightModeIcon = `` 35 | static darkModeIcon = `` 36 | static title = "Toggle Light/Dark Mode" 37 | 38 | static prefersLightModeInDarkModeKey = "prefers-light-mode-in-dark-mode" 39 | static prefersDarkModeInLightModeKey = "prefers-dark-mode-in-light-mode" 40 | 41 | static _staticConstructor = function() { 42 | DoxygenAwesomeDarkModeToggle.enableDarkMode(DoxygenAwesomeDarkModeToggle.userPreference) 43 | // Update the color scheme when the browsers preference changes 44 | // without user interaction on the website. 45 | window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { 46 | DoxygenAwesomeDarkModeToggle.onSystemPreferenceChanged() 47 | }) 48 | // Update the color scheme when the tab is made visible again. 49 | // It is possible that the appearance was changed in another tab 50 | // while this tab was in the background. 51 | document.addEventListener("visibilitychange", visibilityState => { 52 | if (document.visibilityState === 'visible') { 53 | DoxygenAwesomeDarkModeToggle.onSystemPreferenceChanged() 54 | } 55 | }); 56 | }() 57 | 58 | static init() { 59 | $(function() { 60 | $(document).ready(function() { 61 | const toggleButton = document.createElement('doxygen-awesome-dark-mode-toggle') 62 | toggleButton.title = DoxygenAwesomeDarkModeToggle.title 63 | toggleButton.updateIcon() 64 | 65 | window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { 66 | toggleButton.updateIcon() 67 | }) 68 | document.addEventListener("visibilitychange", visibilityState => { 69 | if (document.visibilityState === 'visible') { 70 | toggleButton.updateIcon() 71 | } 72 | }); 73 | 74 | $(document).ready(function(){ 75 | document.getElementById("MSearchBox").parentNode.appendChild(toggleButton) 76 | }) 77 | $(window).resize(function(){ 78 | document.getElementById("MSearchBox").parentNode.appendChild(toggleButton) 79 | }) 80 | }) 81 | }) 82 | } 83 | 84 | constructor() { 85 | super(); 86 | this.onclick=this.toggleDarkMode 87 | } 88 | 89 | /** 90 | * @returns `true` for dark-mode, `false` for light-mode system preference 91 | */ 92 | static get systemPreference() { 93 | return window.matchMedia('(prefers-color-scheme: dark)').matches 94 | } 95 | 96 | /** 97 | * @returns `true` for dark-mode, `false` for light-mode user preference 98 | */ 99 | static get userPreference() { 100 | return (!DoxygenAwesomeDarkModeToggle.systemPreference && localStorage.getItem(DoxygenAwesomeDarkModeToggle.prefersDarkModeInLightModeKey)) || 101 | (DoxygenAwesomeDarkModeToggle.systemPreference && !localStorage.getItem(DoxygenAwesomeDarkModeToggle.prefersLightModeInDarkModeKey)) 102 | } 103 | 104 | static set userPreference(userPreference) { 105 | DoxygenAwesomeDarkModeToggle.darkModeEnabled = userPreference 106 | if(!userPreference) { 107 | if(DoxygenAwesomeDarkModeToggle.systemPreference) { 108 | localStorage.setItem(DoxygenAwesomeDarkModeToggle.prefersLightModeInDarkModeKey, true) 109 | } else { 110 | localStorage.removeItem(DoxygenAwesomeDarkModeToggle.prefersDarkModeInLightModeKey) 111 | } 112 | } else { 113 | if(!DoxygenAwesomeDarkModeToggle.systemPreference) { 114 | localStorage.setItem(DoxygenAwesomeDarkModeToggle.prefersDarkModeInLightModeKey, true) 115 | } else { 116 | localStorage.removeItem(DoxygenAwesomeDarkModeToggle.prefersLightModeInDarkModeKey) 117 | } 118 | } 119 | DoxygenAwesomeDarkModeToggle.onUserPreferenceChanged() 120 | } 121 | 122 | static enableDarkMode(enable) { 123 | if(enable) { 124 | DoxygenAwesomeDarkModeToggle.darkModeEnabled = true 125 | document.documentElement.classList.add("dark-mode") 126 | document.documentElement.classList.remove("light-mode") 127 | } else { 128 | DoxygenAwesomeDarkModeToggle.darkModeEnabled = false 129 | document.documentElement.classList.remove("dark-mode") 130 | document.documentElement.classList.add("light-mode") 131 | } 132 | } 133 | 134 | static onSystemPreferenceChanged() { 135 | DoxygenAwesomeDarkModeToggle.darkModeEnabled = DoxygenAwesomeDarkModeToggle.userPreference 136 | DoxygenAwesomeDarkModeToggle.enableDarkMode(DoxygenAwesomeDarkModeToggle.darkModeEnabled) 137 | } 138 | 139 | static onUserPreferenceChanged() { 140 | DoxygenAwesomeDarkModeToggle.enableDarkMode(DoxygenAwesomeDarkModeToggle.darkModeEnabled) 141 | } 142 | 143 | toggleDarkMode() { 144 | DoxygenAwesomeDarkModeToggle.userPreference = !DoxygenAwesomeDarkModeToggle.userPreference 145 | this.updateIcon() 146 | } 147 | 148 | updateIcon() { 149 | if(DoxygenAwesomeDarkModeToggle.darkModeEnabled) { 150 | this.innerHTML = DoxygenAwesomeDarkModeToggle.darkModeIcon 151 | } else { 152 | this.innerHTML = DoxygenAwesomeDarkModeToggle.lightModeIcon 153 | } 154 | } 155 | } 156 | 157 | customElements.define("doxygen-awesome-dark-mode-toggle", DoxygenAwesomeDarkModeToggle); 158 | -------------------------------------------------------------------------------- /docs/doxygen-awesome-fragment-copy-button.js: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | Doxygen Awesome 4 | https://github.com/jothepro/doxygen-awesome-css 5 | 6 | MIT License 7 | 8 | Copyright (c) 2022 jothepro 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | 28 | */ 29 | 30 | class DoxygenAwesomeFragmentCopyButton extends HTMLElement { 31 | constructor() { 32 | super(); 33 | this.onclick=this.copyContent 34 | } 35 | static title = "Copy to clipboard" 36 | static copyIcon = `` 37 | static successIcon = `` 38 | static successDuration = 980 39 | static init() { 40 | $(function() { 41 | $(document).ready(function() { 42 | if(navigator.clipboard) { 43 | const fragments = document.getElementsByClassName("fragment") 44 | for(const fragment of fragments) { 45 | const fragmentWrapper = document.createElement("div") 46 | fragmentWrapper.className = "doxygen-awesome-fragment-wrapper" 47 | const fragmentCopyButton = document.createElement("doxygen-awesome-fragment-copy-button") 48 | fragmentCopyButton.innerHTML = DoxygenAwesomeFragmentCopyButton.copyIcon 49 | fragmentCopyButton.title = DoxygenAwesomeFragmentCopyButton.title 50 | 51 | fragment.parentNode.replaceChild(fragmentWrapper, fragment) 52 | fragmentWrapper.appendChild(fragment) 53 | fragmentWrapper.appendChild(fragmentCopyButton) 54 | 55 | } 56 | } 57 | }) 58 | }) 59 | } 60 | 61 | 62 | copyContent() { 63 | const content = this.previousSibling.cloneNode(true) 64 | // filter out line number from file listings 65 | content.querySelectorAll(".lineno, .ttc").forEach((node) => { 66 | node.remove() 67 | }) 68 | let textContent = content.textContent 69 | // remove trailing newlines that appear in file listings 70 | let numberOfTrailingNewlines = 0 71 | while(textContent.charAt(textContent.length - (numberOfTrailingNewlines + 1)) == '\n') { 72 | numberOfTrailingNewlines++; 73 | } 74 | textContent = textContent.substring(0, textContent.length - numberOfTrailingNewlines) 75 | navigator.clipboard.writeText(textContent); 76 | this.classList.add("success") 77 | this.innerHTML = DoxygenAwesomeFragmentCopyButton.successIcon 78 | window.setTimeout(() => { 79 | this.classList.remove("success") 80 | this.innerHTML = DoxygenAwesomeFragmentCopyButton.copyIcon 81 | }, DoxygenAwesomeFragmentCopyButton.successDuration); 82 | } 83 | } 84 | 85 | customElements.define("doxygen-awesome-fragment-copy-button", DoxygenAwesomeFragmentCopyButton) 86 | -------------------------------------------------------------------------------- /docs/doxygen-awesome-interactive-toc.js: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | Doxygen Awesome 4 | https://github.com/jothepro/doxygen-awesome-css 5 | 6 | MIT License 7 | 8 | Copyright (c) 2022 jothepro 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | 28 | */ 29 | 30 | class DoxygenAwesomeInteractiveToc { 31 | static topOffset = 38 32 | static hideMobileMenu = true 33 | static headers = [] 34 | 35 | static init() { 36 | window.addEventListener("load", () => { 37 | let toc = document.querySelector(".contents > .toc") 38 | if(toc) { 39 | toc.classList.add("interactive") 40 | if(!DoxygenAwesomeInteractiveToc.hideMobileMenu) { 41 | toc.classList.add("open") 42 | } 43 | document.querySelector(".contents > .toc > h3")?.addEventListener("click", () => { 44 | if(toc.classList.contains("open")) { 45 | toc.classList.remove("open") 46 | } else { 47 | toc.classList.add("open") 48 | } 49 | }) 50 | 51 | document.querySelectorAll(".contents > .toc > ul a").forEach((node) => { 52 | let id = node.getAttribute("href").substring(1) 53 | DoxygenAwesomeInteractiveToc.headers.push({ 54 | node: node, 55 | headerNode: document.getElementById(id) 56 | }) 57 | 58 | document.getElementById("doc-content")?.addEventListener("scroll", () => { 59 | DoxygenAwesomeInteractiveToc.update() 60 | }) 61 | }) 62 | DoxygenAwesomeInteractiveToc.update() 63 | } 64 | }) 65 | } 66 | 67 | static update() { 68 | let active = DoxygenAwesomeInteractiveToc.headers[0]?.node 69 | DoxygenAwesomeInteractiveToc.headers.forEach((header) => { 70 | let position = header.headerNode.getBoundingClientRect().top 71 | header.node.classList.remove("active") 72 | header.node.classList.remove("aboveActive") 73 | if(position < DoxygenAwesomeInteractiveToc.topOffset) { 74 | active = header.node 75 | active?.classList.add("aboveActive") 76 | } 77 | }) 78 | active?.classList.add("active") 79 | active?.classList.remove("aboveActive") 80 | } 81 | } -------------------------------------------------------------------------------- /docs/doxygen-awesome-paragraph-link.js: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | Doxygen Awesome 4 | https://github.com/jothepro/doxygen-awesome-css 5 | 6 | MIT License 7 | 8 | Copyright (c) 2022 jothepro 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | 28 | */ 29 | 30 | class DoxygenAwesomeParagraphLink { 31 | // Icon from https://fonts.google.com/icons 32 | // Licensed under the Apache 2.0 license: 33 | // https://www.apache.org/licenses/LICENSE-2.0.html 34 | static icon = `` 35 | static title = "Permanent Link" 36 | static init() { 37 | $(function() { 38 | $(document).ready(function() { 39 | document.querySelectorAll(".contents a.anchor[id], .contents .groupheader > a[id]").forEach((node) => { 40 | let anchorlink = document.createElement("a") 41 | anchorlink.setAttribute("href", `#${node.getAttribute("id")}`) 42 | anchorlink.setAttribute("title", DoxygenAwesomeParagraphLink.title) 43 | anchorlink.classList.add("anchorlink") 44 | node.classList.add("anchor") 45 | anchorlink.innerHTML = DoxygenAwesomeParagraphLink.icon 46 | node.parentElement.appendChild(anchorlink) 47 | }) 48 | }) 49 | }) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /docs/doxygen-awesome-sidebar-only-darkmode-toggle.css: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | 4 | Doxygen Awesome 5 | https://github.com/jothepro/doxygen-awesome-css 6 | 7 | MIT License 8 | 9 | Copyright (c) 2021 jothepro 10 | 11 | Permission is hereby granted, free of charge, to any person obtaining a copy 12 | of this software and associated documentation files (the "Software"), to deal 13 | in the Software without restriction, including without limitation the rights 14 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 | copies of the Software, and to permit persons to whom the Software is 16 | furnished to do so, subject to the following conditions: 17 | 18 | The above copyright notice and this permission notice shall be included in all 19 | copies or substantial portions of the Software. 20 | 21 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27 | SOFTWARE. 28 | 29 | */ 30 | 31 | @media screen and (min-width: 768px) { 32 | 33 | #MSearchBox { 34 | width: calc(var(--side-nav-fixed-width) - calc(2 * var(--spacing-medium)) - var(--searchbar-height) - 1px); 35 | } 36 | 37 | #MSearchField { 38 | width: calc(var(--side-nav-fixed-width) - calc(2 * var(--spacing-medium)) - 66px - var(--searchbar-height)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /docs/doxygen-awesome-sidebar-only.css: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | Doxygen Awesome 4 | https://github.com/jothepro/doxygen-awesome-css 5 | 6 | MIT License 7 | 8 | Copyright (c) 2021 jothepro 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | 28 | */ 29 | 30 | html { 31 | /* side nav width. MUST be = `TREEVIEW_WIDTH`. 32 | * Make sure it is wide enough to contain the page title (logo + title + version) 33 | */ 34 | --side-nav-fixed-width: 335px; 35 | --menu-display: none; 36 | 37 | --top-height: 120px; 38 | --toc-sticky-top: -25px; 39 | --toc-max-height: calc(100vh - 2 * var(--spacing-medium) - 25px); 40 | } 41 | 42 | #projectname { 43 | white-space: nowrap; 44 | } 45 | 46 | 47 | @media screen and (min-width: 768px) { 48 | html { 49 | --searchbar-background: var(--page-background-color); 50 | } 51 | 52 | #side-nav { 53 | min-width: var(--side-nav-fixed-width); 54 | max-width: var(--side-nav-fixed-width); 55 | top: var(--top-height); 56 | overflow: visible; 57 | } 58 | 59 | #nav-tree, #side-nav { 60 | height: calc(100vh - var(--top-height)) !important; 61 | } 62 | 63 | #nav-tree { 64 | padding: 0; 65 | } 66 | 67 | #top { 68 | display: block; 69 | border-bottom: none; 70 | height: var(--top-height); 71 | margin-bottom: calc(0px - var(--top-height)); 72 | max-width: var(--side-nav-fixed-width); 73 | overflow: hidden; 74 | background: var(--side-nav-background); 75 | } 76 | #main-nav { 77 | float: left; 78 | padding-right: 0; 79 | } 80 | 81 | .ui-resizable-handle { 82 | cursor: default; 83 | width: 1px !important; 84 | box-shadow: 0 calc(-2 * var(--top-height)) 0 0 var(--separator-color); 85 | } 86 | 87 | #nav-path { 88 | position: fixed; 89 | right: 0; 90 | left: var(--side-nav-fixed-width); 91 | bottom: 0; 92 | width: auto; 93 | } 94 | 95 | #doc-content { 96 | height: calc(100vh - 31px) !important; 97 | padding-bottom: calc(3 * var(--spacing-large)); 98 | padding-top: calc(var(--top-height) - 80px); 99 | box-sizing: border-box; 100 | margin-left: var(--side-nav-fixed-width) !important; 101 | } 102 | 103 | #MSearchBox { 104 | width: calc(var(--side-nav-fixed-width) - calc(2 * var(--spacing-medium))); 105 | } 106 | 107 | #MSearchField { 108 | width: calc(var(--side-nav-fixed-width) - calc(2 * var(--spacing-medium)) - 65px); 109 | } 110 | 111 | #MSearchResultsWindow { 112 | left: var(--spacing-medium) !important; 113 | right: auto; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /docs/header.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | $projectname: $title 10 | $title 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 25 | $treeview 26 | $search 27 | $mathjax 28 | 29 | $extrastylesheet 30 | 31 | 32 | 33 |
34 | 35 | 36 |
37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 49 | 50 | 51 | 52 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 |
45 |
$projectname $projectnumber 46 |
47 |
$projectbrief
48 |
53 |
$projectbrief
54 |
$searchbox
$searchbox
72 |
73 | 74 | 75 | -------------------------------------------------------------------------------- /docs/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/docs/logo.png -------------------------------------------------------------------------------- /include/indk/backends/default.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 18.10.22 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | #ifndef INTERFERENCE_DEFAULT_H 10 | #define INTERFERENCE_DEFAULT_H 11 | 12 | #include 13 | 14 | namespace indk { 15 | class ComputeBackendDefault : public Computer { 16 | private: 17 | indk::Position *dRPos, *nRPos, *zPos; 18 | public: 19 | ComputeBackendDefault(); 20 | void doRegisterHost(const std::vector&) override; 21 | void doUnregisterHost() override; 22 | void doWaitTarget() override; 23 | void doProcess(void*) override; 24 | }; 25 | } 26 | 27 | #endif //INTERFERENCE_DEFAULT_H 28 | -------------------------------------------------------------------------------- /include/indk/backends/multithread.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 18.10.22 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | #ifndef INTERFERENCE_MULTITHREAD_H 10 | #define INTERFERENCE_MULTITHREAD_H 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #define indk_MULTITHREAD_DEFAULT_NUM 2 20 | 21 | namespace indk { 22 | /// \private 23 | typedef struct worker { 24 | std::vector objects; 25 | std::thread thread; 26 | void *event; 27 | std::atomic done; 28 | std::mutex m; 29 | std::condition_variable cv; 30 | 31 | } Worker; 32 | 33 | class ComputeBackendMultithread : public Computer { 34 | private: 35 | std::vector Workers; 36 | std::map ObjectTable; 37 | unsigned int WorkerCount; 38 | 39 | [[noreturn]] static void tWorker(void*); 40 | public: 41 | explicit ComputeBackendMultithread(int); 42 | void doRegisterHost(const std::vector&) override; 43 | void doUnregisterHost() override; 44 | void doWaitTarget() override; 45 | void doProcess(void*) override; 46 | }; 47 | } 48 | 49 | #endif //INTERFERENCE_MULTITHREAD_H 50 | -------------------------------------------------------------------------------- /include/indk/backends/opencl.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 06.04.23 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | #ifndef INTERFERENCE_OPENCL_H 10 | #define INTERFERENCE_OPENCL_H 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #ifdef INDK_OPENCL_SUPPORT 18 | #include 19 | #endif 20 | 21 | namespace indk { 22 | class ComputeBackendOpenCL : public Computer { 23 | private: 24 | #ifdef INDK_OPENCL_SUPPORT 25 | cl::Context Context; 26 | cl::Kernel KernelPairs; 27 | cl::Kernel KernelReceptors; 28 | cl::Kernel KernelNeurons; 29 | cl::CommandQueue Queue; 30 | 31 | cl_float16 *PairsInfo; 32 | cl_float8 *ReceptorsInfo; 33 | cl_float3 *NeuronsInfo; 34 | cl_float2 *Inputs; 35 | cl_float *Outputs; 36 | 37 | cl::Buffer PairsBuffer; 38 | cl::Buffer ReceptorsBuffer; 39 | cl::Buffer NeuronsBuffer; 40 | cl::Buffer InputsBuffer; 41 | cl::Buffer OutputsBuffer; 42 | #endif 43 | uint64_t PairPoolSize; 44 | uint64_t ReceptorPoolSize; 45 | uint64_t NeuronPoolSize; 46 | uint64_t InputPoolSize; 47 | std::vector Objects; 48 | public: 49 | ComputeBackendOpenCL(); 50 | void doRegisterHost(const std::vector&) override; 51 | void doUnregisterHost() override; 52 | void doWaitTarget() override; 53 | void doProcess(void*) override; 54 | }; 55 | } 56 | 57 | #endif //INTERFERENCE_OPENCL_H 58 | -------------------------------------------------------------------------------- /include/indk/computer.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 17.10.22 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | #ifndef INTERFERENCE_COMPUTER_H 10 | #define INTERFERENCE_COMPUTER_H 11 | 12 | #include 13 | #include 14 | 15 | namespace indk { 16 | class Computer { 17 | public: 18 | Computer(); 19 | virtual void doRegisterHost(const std::vector&) = 0; 20 | virtual void doUnregisterHost() = 0; 21 | virtual void doWaitTarget() = 0; 22 | virtual void doProcess(void*) = 0; 23 | static std::vector doCompareCPFunction(std::vector, std::vector); 24 | static float doCompareCPFunctionD(std::vector, std::vector); 25 | static float doCompareFunction(indk::Position*, indk::Position*); 26 | static float getGammaFunctionValue(float, float, float, float); 27 | static std::pair getFiFunctionValue(float, float, float, float); 28 | static float getReceptorInfluenceValue(bool, float, indk::Position*, indk::Position*); 29 | static float getRcValue(float, float, float, float); 30 | static void getNewPosition(indk::Position*, indk::Position*, indk::Position*, float, float); 31 | static float getLambdaValue(unsigned int); 32 | static float getFiVectorLength(float); 33 | static float getSynapticSensitivityValue(unsigned int, unsigned int); 34 | }; 35 | } 36 | 37 | #endif //INTERFERENCE_COMPUTER_H 38 | -------------------------------------------------------------------------------- /include/indk/error.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: indk/error.h 3 | // Purpose: Exception system class header 4 | // Author: Nickolay Babbysh 5 | // Created: 07.05.2019 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #ifndef INTERFERENCE_ERROR_H 11 | #define INTERFERENCE_ERROR_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace indk { 19 | /// Error handler class. 20 | class Error: public std::exception { 21 | public: 22 | typedef unsigned int ExceptionType; 23 | /** 24 | * Exception types. 25 | */ 26 | typedef enum { 27 | /// Out of neuron list. 28 | EX_NEURALNET_NEURONS, 29 | /// The number of input signals does not match the neural net entries count. 30 | EX_NEURALNET_INPUT, 31 | /// Out of neural net entries list. 32 | EX_NEURALNET_ENTRIES, 33 | /// The number of links more than the neuron entries count. 34 | EX_NEURALNET_NEURON_ENTRIES, 35 | EX_NEURALNET_LINKTYPE, 36 | /// The number of input signals does not match the neuron entries count. 37 | EX_NEURON_INPUT, 38 | /// Out of entry list. 39 | EX_NEURON_ENTRIES, 40 | /// Out of receptor list. 41 | EX_NEURON_RECEPTORS, 42 | /// Coordinates out of range. 43 | EX_POSITION_OUT_RANGES, 44 | /// Not equal coordinates ranges. 45 | EX_POSITION_RANGES, 46 | /// Not equal space dimensions of positions. 47 | EX_POSITION_DIMENSIONS 48 | } Exceptions; 49 | 50 | Error(); 51 | explicit Error(ExceptionType); 52 | explicit Error(ExceptionType, std::vector); 53 | const char* what() const noexcept override; 54 | private: 55 | ExceptionType ET; 56 | std::vector ED; 57 | }; 58 | } 59 | 60 | #endif //INTERFERENCE_ERROR_H 61 | -------------------------------------------------------------------------------- /include/indk/interlink.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: indk/interlink.h 3 | // Purpose: Interlink service class header 4 | // Author: Nickolay Babbysh 5 | // Created: 19.07.2023 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #ifndef INTERFERENCE_INTERLINK_H 11 | #define INTERFERENCE_INTERLINK_H 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | namespace indk { 18 | /** 19 | * Interlink class. 20 | */ 21 | class Interlink { 22 | private: 23 | // void *LinkedObject; 24 | void *Input; 25 | void *Output; 26 | std::string Host; 27 | std::string InputPort, OutputPort; 28 | std::thread Thread; 29 | std::atomic Interlinked; 30 | std::string Structure; 31 | 32 | void doInitInput(int, int); 33 | void doInitOutput(); 34 | 35 | void doSend(const std::string&, const std::string&); 36 | public: 37 | Interlink(); 38 | Interlink(int, int timeout); 39 | void doUpdateStructure(const std::string&); 40 | void doUpdateModelData(const std::string&); 41 | void doUpdateMetrics(const std::string&); 42 | void setStructure(const std::string&); 43 | std::string getStructure(); 44 | bool isInterlinked(); 45 | ~Interlink(); 46 | }; 47 | } 48 | 49 | #endif //INTERFERENCE_INTERLINK_H 50 | -------------------------------------------------------------------------------- /include/indk/neuralnet.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: indk/neuralnet.h 3 | // Purpose: Neural net classes header 4 | // Author: Nickolay Babbysh 5 | // Created: 12.05.2019 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #ifndef INTERFERENCE_NEURALNET_H 11 | #define INTERFERENCE_NEURALNET_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace indk { 23 | typedef enum { 24 | CompareDefault, 25 | CompareNormalized 26 | } PatternCompareFlags; 27 | 28 | typedef std::queue> NQueue; 29 | typedef std::vector>> EntryList; 30 | typedef std::pair OutputValue; 31 | 32 | /** 33 | * Main neural net class. 34 | */ 35 | class NeuralNet { 36 | private: 37 | std::string Name, Description, Version; 38 | int64_t t; 39 | 40 | EntryList Entries; 41 | std::map> Ensembles; 42 | std::map Neurons; 43 | std::map Latencies; 44 | std::vector Outputs; 45 | 46 | std::map> StateSyncList; 47 | 48 | int64_t doFindEntry(const std::string&); 49 | void doParseLinks(const EntryList&, const std::string&); 50 | void doSignalProcessStart(const std::vector>&, const EntryList&); 51 | void doSyncNeuronStates(const std::string&); 52 | 53 | indk::LinkList Links; 54 | std::string PrepareID; 55 | bool StateSyncEnabled; 56 | int LastUsedComputeBackend; 57 | 58 | indk::Interlink *InterlinkService; 59 | std::vector> InterlinkDataBuffer; 60 | 61 | public: 62 | NeuralNet(); 63 | explicit NeuralNet(const std::string &path); 64 | void doInterlinkInit(int port, int timeout = 5); 65 | void doInterlinkSyncStructure(); 66 | void doInterlinkSyncData(); 67 | std::vector doComparePatterns(int CompareFlag = indk::PatternCompareFlags::CompareDefault, 68 | int ProcessingMethod = indk::ScopeProcessingMethods::ProcessMin); 69 | std::vector doComparePatterns(const std::string& ename, 70 | int CompareFlag = indk::PatternCompareFlags::CompareDefault, 71 | int ProcessingMethod = indk::ScopeProcessingMethods::ProcessMin); 72 | std::vector doComparePatterns(std::vector nnames, 73 | int CompareFlag = indk::PatternCompareFlags::CompareDefault, 74 | int ProcessingMethod = indk::ScopeProcessingMethods::ProcessMin); 75 | void doCreateNewScope(); 76 | void doChangeScope(uint64_t); 77 | void doAddNewOutput(const std::string&); 78 | void doIncludeNeuronToEnsemble(const std::string&, const std::string&); 79 | void doReset(); 80 | void doPrepare(); 81 | void doStructurePrepare(); 82 | std::vector doSignalTransfer(const std::vector>& X, const std::vector& inputs = {}); 83 | void doSignalTransferAsync(const std::vector>&, const std::function)>& Callback = nullptr, const std::vector& inputs = {}); 84 | std::vector doLearn(const std::vector>&, bool prepare = true, const std::vector& inputs = {}); 85 | std::vector doRecognise(const std::vector>&, bool prepare = true, const std::vector& inputs = {}); 86 | void doLearnAsync(const std::vector>&, const std::function)>& Callback = nullptr, bool prepare = true, const std::vector& inputs = {}); 87 | void doRecogniseAsync(const std::vector>&, const std::function)>& Callback = nullptr, bool prepare = true, const std::vector& inputs = {}); 88 | std::vector doSignalReceive(const std::string& ensemble = ""); 89 | indk::Neuron* doReplicateNeuron(const std::string& from, const std::string& to, bool integrate); 90 | void doDeleteNeuron(const std::string& name); 91 | void doReplicateEnsemble(const std::string& From, const std::string& To, bool CopyEntries = false); 92 | void doReserveSignalBuffer(int64_t L); 93 | void doClearCache(); 94 | void setStructure(std::ifstream&); 95 | void setStructure(const std::string &Str); 96 | void setLearned(bool); 97 | void setStateSyncEnabled(bool enabled = true); 98 | bool isLearned(); 99 | std::string getStructure(bool minimized = true); 100 | std::string getName(); 101 | std::string getDescription(); 102 | std::string getVersion(); 103 | std::vector getEnsemble(const std::string&); 104 | indk::Neuron* getNeuron(const std::string&); 105 | std::vector getNeurons(); 106 | uint64_t getNeuronCount(); 107 | int64_t getSignalBufferSize(); 108 | ~NeuralNet(); 109 | }; 110 | } 111 | 112 | #endif //INTERFERENCE_NEURALNET_H 113 | -------------------------------------------------------------------------------- /include/indk/neuron.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: indk/neuron.h 3 | // Purpose: Neuron classes header 4 | // Author: Nickolay Babbysh 5 | // Created: 29.04.2019 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #ifndef INTERFERENCE_NEURON_H 11 | #define INTERFERENCE_NEURON_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | namespace indk { 21 | typedef enum { 22 | ProcessMin, 23 | ProcessAverage, 24 | } ScopeProcessingMethods; 25 | 26 | typedef unsigned int TopologyID; 27 | 28 | class Neuron { 29 | private: 30 | class Entry; 31 | class Synapse; 32 | class Receptor; 33 | std::vector> Entries; 34 | std::vector Links; 35 | std::vector Receptors; 36 | std::atomic t; 37 | int64_t Tlo; 38 | unsigned int Xm, DimensionsCount; 39 | float *OutputSignal; 40 | int64_t OutputSignalSize; 41 | int64_t OutputSignalPointer; 42 | int NID, ProcessingMode, OutputMode; 43 | bool Learned; 44 | std::vector OutputsPredefined; 45 | std::string Name; 46 | public: 47 | /** 48 | * Neuron states. 49 | */ 50 | typedef enum { 51 | /// Neuron not processed (initial state). 52 | NotProcessed, 53 | /// Neuron is processing now. 54 | Pending, 55 | /// Processing of neuron is done. 56 | Computed, 57 | } States; 58 | 59 | /** 60 | * Neuron recognition processing modes. 61 | */ 62 | typedef enum { 63 | /// During recognition, the neuron receptors work as usual. 64 | ProcessingModeDefault, 65 | /// During recognition, the neuron receptors work in auto reset mode - receptors automatically return to their default position after each movement. 66 | ProcessingModeAutoReset, 67 | /// During recognition, the neuron receptors work in auto rollback mode - receptors automatically return to their previous position after movement only if output signal (for current tick) is 0. 68 | ProcessingModeAutoRollback, 69 | } ProcessingModes; 70 | 71 | /** 72 | * Neuron recognition output modes. 73 | */ 74 | typedef enum { 75 | /// During recognition, the neuron works in stream mode - the output signal is available at every tick. 76 | OutputModeStream, 77 | /// During recognition, the neuron works in latch mode - the output signal is available only if pattern difference is 0. 78 | OutputModeLatch, 79 | /// During recognition, the neuron works in predefined mode - the predefined output signal is available only if pattern difference is 0. 80 | OutputModePredefined 81 | } OutputModes; 82 | 83 | typedef std::tuple PatternDefinition; 84 | 85 | Neuron(); 86 | Neuron(const indk::Neuron&); 87 | Neuron(unsigned int, unsigned int, int64_t, const std::vector& InputSignals); 88 | void doCreateNewSynapse(const std::string&, std::vector, float, int64_t, int); 89 | void doCreateNewSynapseCluster(const std::vector& PosVector, unsigned R, float k1, int64_t Tl, int NT); 90 | void doCreateNewReceptor(std::vector); 91 | void doCreateNewReceptorCluster(const std::vector& PosVector, unsigned R, unsigned C); 92 | bool doSignalSendEntry(const std::string&, float, int64_t); 93 | std::pair doSignalReceive(int64_t tT = -1); 94 | void doFinalizeInput(float); 95 | void doPrepare(); 96 | void doFinalize(); 97 | void doCreateNewScope(float output = 0); 98 | void doChangeScope(uint64_t); 99 | void doReset(); 100 | indk::Neuron::PatternDefinition doComparePattern(int ProcessingMethod = indk::ScopeProcessingMethods::ProcessMin) const; 101 | void doLinkOutput(const std::string&); 102 | void doClearOutputLinks(); 103 | void doClearEntries(); 104 | void doAddEntryName(const std::string&); 105 | void doCopyEntry(const std::string&, const std::string&); 106 | void doReplaceEntryName(const std::string&, const std::string&); 107 | void doReserveSignalBuffer(int64_t); 108 | void setTime(int64_t); 109 | void setEntries(const std::vector& inputs); 110 | void setLambda(float); 111 | void setk1(float); 112 | void setk2(float); 113 | void setk3(float); 114 | void setNID(int); 115 | void setProcessingMode(int); 116 | void setOutputMode(int); 117 | void setName(const std::string&); 118 | void setLearned(bool LearnedFlag); 119 | bool isLearned() const; 120 | std::vector getLinkOutput() const; 121 | std::vector getEntries() const; 122 | indk::Neuron::Entry* getEntry(int64_t) const; 123 | indk::Neuron::Receptor* getReceptor(int64_t) const; 124 | std::vector getWaitingEntries(); 125 | int64_t getEntriesCount() const; 126 | unsigned int getSynapsesCount() const; 127 | int64_t getReceptorsCount() const; 128 | int64_t getTime() const; 129 | unsigned int getXm() const; 130 | unsigned int getDimensionsCount() const; 131 | int64_t getTlo() const; 132 | int getNID() const; 133 | std::string getName(); 134 | int64_t getSignalBufferSize() const; 135 | int getState(int64_t) const; 136 | int getProcessingMode() const; 137 | int getOutputMode() const; 138 | ~Neuron(); 139 | }; 140 | 141 | class Neuron::Entry { 142 | private: 143 | std::vector Synapses; 144 | int64_t t, tm; 145 | float *Signal; 146 | int64_t SignalSize; 147 | int64_t SignalPointer; 148 | public: 149 | Entry(); 150 | Entry(const indk::Neuron::Entry&); 151 | bool doCheckState(int64_t) const; 152 | void doAddSynapse(indk::Position*, unsigned int, float, int64_t, int); 153 | void doIn(float, int64_t); 154 | void doProcess(); 155 | void doPrepare(); 156 | void doFinalize(); 157 | void doRollback(); 158 | void doReserveSignalBuffer(uint64_t); 159 | void setLambda(float); 160 | void setk1(float); 161 | void setk2(float); 162 | indk::Neuron::Synapse* getSynapse(int64_t) const; 163 | int64_t getSynapsesCount() const; 164 | float getIn(); 165 | ~Entry(); 166 | }; 167 | 168 | class Neuron::Synapse { 169 | private: 170 | indk::Position* SPos; 171 | float ok1, ok2, k1, k2; 172 | float Lambda; 173 | int NeurotransmitterType; 174 | int64_t Tl; 175 | float Gamma, dGamma; 176 | float lGamma, ldGamma; 177 | long long QCounter; 178 | std::vector GammaQ; 179 | std::atomic QSize; 180 | public: 181 | Synapse(); 182 | Synapse(const indk::Neuron::Synapse&); 183 | Synapse(indk::Position*, float, float, int64_t, int); 184 | void doIn(float); 185 | void doSendToQueue(float, float); 186 | bool doInFromQueue(int64_t); 187 | void doPrepare(); 188 | void doReset(); 189 | void doRollback(); 190 | void setGamma(float); 191 | void setk1(float); 192 | void setk2(float); 193 | void setLambda(float); 194 | indk::Position* getPos() const; 195 | float getk1() const; 196 | float getk2() const; 197 | float getLambda() const; 198 | int64_t getTl() const; 199 | float getGamma() const; 200 | float getdGamma() const; 201 | int getNeurotransmitterType() const; 202 | int64_t getQSize(); 203 | ~Synapse() = default; 204 | }; 205 | 206 | class Neuron::Receptor { 207 | private: 208 | std::vector CP, CPf; 209 | //indk::Position *RPos, *RPos0, *RPosf; 210 | std::vector ReferencePos; 211 | indk::Position* DefaultPos; 212 | indk::Position* PhantomPos; 213 | float k3; 214 | float Rs; 215 | bool Locked; 216 | float L, Lf; 217 | float Fi, dFi; 218 | uint64_t Scope; 219 | public: 220 | Receptor(); 221 | Receptor(const indk::Neuron::Receptor&); 222 | Receptor(indk::Position*, float); 223 | bool doCheckActive() const; 224 | void doLock(); 225 | void doUnlock(); 226 | void doCreateNewScope(); 227 | void doChangeScope(uint64_t); 228 | void doReset(); 229 | void doPrepare(); 230 | void doSavePos(); 231 | void doUpdateSensitivityValue(); 232 | void doUpdatePos(indk::Position*); 233 | void setPos(indk::Position*); 234 | void setRs(float); 235 | void setk3(float); 236 | void setFi(float); 237 | std::vector getCP() const; 238 | std::vector getCPf() const; 239 | indk::Position* getPos() const; 240 | indk::Position* getPos0() const; 241 | indk::Position* getPosf() const; 242 | std::vector getReferencePosScopes(); 243 | float getRs() const; 244 | float getk3() const; 245 | float getFi(); 246 | float getdFi(); 247 | float getSensitivityValue() const; 248 | bool isLocked() const; 249 | float getL() const; 250 | float getLf() const; 251 | ~Receptor() = default; 252 | }; 253 | } 254 | 255 | #endif //INTERFERENCE_NEURON_H 256 | -------------------------------------------------------------------------------- /include/indk/position.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: indk/position.h 3 | // Purpose: Multidimensional position class header 4 | // Author: Nickolay Babbysh 5 | // Created: 07.05.2019 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #ifndef INTERFERENCE_POSITION_H 11 | #define INTERFERENCE_POSITION_H 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | namespace indk { 18 | /// Object position class. Provides the ability to store Cartesian 19 | /// coordinates of object positions in n-dimensional space, 20 | /// and also allows you to perform basic arithmetic operations on them: 21 | /// add, subtract, multiply, and divide. 22 | class Position { 23 | private: 24 | unsigned int Xm; 25 | unsigned int DimensionsCount; 26 | float *X; 27 | public: 28 | Position(); 29 | Position(const indk::Position&); 30 | Position(unsigned int, unsigned int); 31 | Position(unsigned int, std::vector); 32 | void doAdd(const indk::Position*); 33 | void doSubtract(const indk::Position*); 34 | void doDivide(float); 35 | void doMultiply(float); 36 | void doZeroPosition(); 37 | void setPosition(const indk::Position&); 38 | void setPosition(const indk::Position*); 39 | void setPosition(std::vector); 40 | void setDimensionsCount(unsigned int); 41 | void setXm(unsigned int); 42 | unsigned int getDimensionsCount() const; 43 | unsigned int getXm() const; 44 | float getPositionValue(unsigned int) const; 45 | float getDistanceFrom(const indk::Position*); 46 | indk::Position& operator= (const indk::Position&); 47 | 48 | static float getDistance(const indk::Position&, const indk::Position&); 49 | static float getDistance(const indk::Position*, const indk::Position*); 50 | 51 | static indk::Position* getSum(const indk::Position*, const indk::Position*); 52 | static indk::Position* getDiff(const indk::Position*, const indk::Position*); 53 | static indk::Position* getQuotient(const indk::Position*, float); 54 | static indk::Position* getProduct(const indk::Position*, float); 55 | ~Position(); 56 | }; 57 | 58 | indk::Position operator+(const indk::Position&, const indk::Position&); 59 | indk::Position operator-(const indk::Position&, const indk::Position&); 60 | indk::Position operator/(const indk::Position&, float); 61 | indk::Position operator*(const indk::Position&, float); 62 | } 63 | 64 | #endif //INTERFERENCE_POSITION_H 65 | -------------------------------------------------------------------------------- /include/indk/profiler.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 23.08.23 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #ifndef INTERFERENCE_PROFILER_H 11 | #define INTERFERENCE_PROFILER_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace indk { 19 | typedef std::function ProfilerCallback; 20 | 21 | class Profiler { 22 | private: 23 | public: 24 | typedef enum { 25 | EventProcessed, 26 | EventTick 27 | } EventFlags; 28 | 29 | Profiler(); 30 | static void doAttachCallback(indk::NeuralNet *object, int flag, indk::ProfilerCallback callback); 31 | static void doEmit(indk::NeuralNet *object, int flag); 32 | ~Profiler(); 33 | }; 34 | } 35 | 36 | #endif //INTERFERENCE_PROFILER_H 37 | -------------------------------------------------------------------------------- /include/indk/system.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 18.10.22 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | #ifndef INTERFERENCE_SYSTEM_H 10 | #define INTERFERENCE_SYSTEM_H 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | namespace indk { 17 | typedef std::tuple LinkDefinition; 18 | typedef std::vector LinkList; 19 | 20 | class System { 21 | public: 22 | static bool isSynchronizationNeeded(); 23 | 24 | /** 25 | * Set compute backend. 26 | * @param Backend Compute backend value. 27 | * @param Parameter Custom parameter. 28 | */ 29 | static void setComputeBackend(int Backend, int Parameter = 0); 30 | 31 | /** 32 | * Set library verbosity level. 33 | * @param VL New verbosity level value. 34 | */ 35 | static void setVerbosityLevel(int); 36 | 37 | /** 38 | * Get current compute backend. 39 | * @return Pointer of current compute backend object. 40 | */ 41 | static indk::Computer* getComputeBackend(); 42 | 43 | /** 44 | * Get current compute backend. 45 | * @return ID of current compute backend. 46 | */ 47 | static int getComputeBackendKind(); 48 | 49 | /** 50 | * Get current verbosity level. 51 | * @return Verbosity level value. 52 | */ 53 | static int getVerbosityLevel(); 54 | 55 | /** 56 | * Get current compute backend parameter. The parameter can be set as an argument to the setComputeBackend method. It is always zero for indk::ComputeBackends::Default backend. 57 | * @return Backend parameter. 58 | */ 59 | static int getComputeBackendParameter(); 60 | 61 | /** 62 | * Compute backends enum. 63 | */ 64 | typedef enum { 65 | /// Native CPU compute backend. 66 | Default, 67 | /// Native CPU multithread compute backend. You can set the number of threads by `parameter` argument of setComputeBackend method. 68 | Multithread, 69 | /// OpenCL compute backend. 70 | OpenCL 71 | } ComputeBackends; 72 | }; 73 | 74 | class Event { 75 | public: 76 | Event(): m_bEvent(false) {} 77 | ~Event() = default; 78 | bool doWaitTimed(int); 79 | bool doWait(); 80 | void doNotifyOne(); 81 | private: 82 | std::mutex m_oMutex; 83 | std::condition_variable m_oConditionVariable; 84 | bool m_bEvent; 85 | }; 86 | } 87 | 88 | #endif //INTERFERENCE_SYSTEM_H 89 | -------------------------------------------------------------------------------- /samples/lang/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | project(interference_lang) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | add_executable(${PROJECT_NAME} main.cpp) 7 | 8 | include_directories(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/include) 9 | target_link_libraries(${PROJECT_NAME} interference_static) 10 | 11 | set(SAMPLE_INSTALL_DIR ${CMAKE_SOURCE_DIR}/dist/bin/lang) 12 | install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${SAMPLE_INSTALL_DIR}) 13 | install(DIRECTORY texts DESTINATION ${SAMPLE_INSTALL_DIR}) 14 | install(DIRECTORY structures DESTINATION ${SAMPLE_INSTALL_DIR}) 15 | -------------------------------------------------------------------------------- /samples/lang/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 31.10.23 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #define DEFINITIONS_COUNT 5 20 | 21 | uint64_t getTimestampMS() { 22 | return std::chrono::duration_cast(std::chrono::system_clock::now(). 23 | time_since_epoch()).count(); 24 | } 25 | 26 | inline std::vector doStrSplit(std::string str, const std::string& div, bool last) { 27 | size_t pos; 28 | std::vector R; 29 | std::string token; 30 | while ((pos = str.find(div)) != std::string::npos) { 31 | token = str.substr(0, pos); 32 | R.push_back(token); 33 | str.erase(0, pos+div.length()); 34 | } 35 | if (last) R.push_back(str); 36 | return R; 37 | } 38 | 39 | auto doLoadVocabulary(const std::string& path) { 40 | std::vector data; 41 | 42 | std::ifstream f(path); 43 | if (!f.is_open()) { 44 | std::cerr << "Can't load the vocabulary file" << std::endl; 45 | return std::vector(); 46 | } 47 | while (!f.eof()) { 48 | std::string rstr; 49 | getline(f, rstr); 50 | 51 | data.push_back(rstr); 52 | } 53 | 54 | return data; 55 | } 56 | 57 | auto doLearnVocabulary(indk::NeuralNet *NN, 58 | const std::array& definitions, 59 | const std::vector& vocab) { 60 | for (int i = 0; i < definitions.size(); i++) 61 | NN -> getNeuron("N"+std::to_string(i+1)) -> setOutputMode(indk::Neuron::OutputModes::OutputModeLatch); 62 | 63 | // learn the vocabulary 64 | for (int i = 1; i <= vocab.size(); i++) { 65 | const auto& item = vocab[i-1]; 66 | 67 | std::string word = item.substr(0, item.find(';')); 68 | std::string definition = item.substr(item.find(';')+1); 69 | std::string source, destination; 70 | 71 | for (int d = 0; d < definitions.size(); d++) { 72 | if (definition == definitions[d]) { 73 | source = "N"+std::to_string(d+1); 74 | destination = "N"+std::to_string(definitions.size()+i); 75 | NN -> doReplicateNeuron(source, destination, true); 76 | break; 77 | } 78 | } 79 | 80 | auto n = NN -> getNeuron(destination); 81 | if (!n) break; 82 | 83 | NN -> doIncludeNeuronToEnsemble(n->getName(), "TEXT"); 84 | auto dn = NN -> getNeuron(n->getLinkOutput()[0]); 85 | dn -> doCopyEntry(source, n->getName()); 86 | n -> doLinkOutput(dn->getName()); 87 | 88 | for (const auto& ch: word) { 89 | n -> doSignalSendEntry("ET", (float)ch, n->getTime()); 90 | } 91 | n -> doFinalize(); 92 | n -> setOutputMode(indk::Neuron::OutputModes::OutputModeLatch); 93 | } 94 | } 95 | 96 | auto doRecognizeInput(indk::NeuralNet *NN, const std::string& sequence, int type) { 97 | std::vector> encoded; 98 | auto stripped = sequence.substr(0, sequence.size()-1); 99 | auto words = doStrSplit(stripped, " ", true); 100 | 101 | // recognize sequence 102 | for (const auto &word: words) { 103 | std::vector> data; 104 | 105 | for (const auto& ch: word) { 106 | data.emplace_back(); 107 | data.back().push_back(ch); 108 | } 109 | 110 | auto Y = NN -> doRecognise(data, true, {"ET"}); 111 | auto patterns = NN -> doComparePatterns("DEFINITION"); 112 | for (int i = 0; i < patterns.size(); i++) { 113 | if (Y[i].first > 0) encoded.push_back({Y[i].first, static_cast(i)}); 114 | } 115 | } 116 | return encoded; 117 | } 118 | 119 | void doCreateContextSpace(indk::NeuralNet *NN, const std::vector>& encoded, int space) { 120 | NN -> doReplicateNeuron("_SPACE_INIT", "_SPACE_"+std::to_string(space), true); 121 | auto n = NN -> getNeuron("_SPACE_"+std::to_string(space)); 122 | if (!n) return; 123 | 124 | n -> setLambda(1); 125 | NN -> doIncludeNeuronToEnsemble(n->getName(), "CONTEXT"); 126 | n -> doReset(); 127 | 128 | int nstart = 0; 129 | while (nstart < encoded.size()) { 130 | indk::Position *pos = nullptr; 131 | 132 | for (int r = nstart; r < encoded.size(); r++) { 133 | n -> doCreateNewScope(); 134 | n -> doPrepare(); 135 | if (pos) n -> getReceptor(0) -> getPos() -> setPosition(pos); 136 | for (int j = 0; j < DEFINITIONS_COUNT; j++) { 137 | if (j == (int)encoded[r][1]) { 138 | n -> doSignalSendEntry("SPACE_E"+std::to_string(j+1), encoded[r][0], n->getTime()); 139 | } else { 140 | n -> doSignalSendEntry("SPACE_E"+std::to_string(j+1), 0, n->getTime()); 141 | } 142 | } 143 | pos = n -> getReceptor(0) -> getPos(); 144 | } 145 | nstart++; 146 | } 147 | n -> doFinalize(); 148 | } 149 | 150 | bool doReceiveResponse(indk::NeuralNet *NN, const std::vector>& encoded) { 151 | bool found = false; 152 | std::vector> marks; 153 | 154 | for (int r = 0; r < encoded.size(); r++) { 155 | marks.emplace_back(); 156 | for (int j = 0; j < DEFINITIONS_COUNT; j++) { 157 | if (j == (int)encoded[r][1]) marks.back().push_back(encoded[r][0]); 158 | else marks.back().push_back(0); 159 | } 160 | } 161 | 162 | NN -> doRecognise(marks, true, {"SPACE_E1", "SPACE_E2", "SPACE_E3", "SPACE_E4", "SPACE_E5"}); 163 | auto patterns = NN -> doComparePatterns( "CONTEXT"); 164 | auto r = std::min_element(patterns.begin(), patterns.end()); 165 | if (r != patterns.end() && *r >= 0 && *r < 10e-6) found = true; 166 | 167 | return found; 168 | } 169 | 170 | void doProcessTextSequence(indk::NeuralNet *NN, std::string sequence, int &space) { 171 | // parse sequence 172 | bool qflag = sequence.back() == '?'; 173 | auto encoded = doRecognizeInput(NN, sequence, 0); 174 | 175 | if (!qflag) { 176 | // create new space in the context 177 | std::cout << sequence << std::endl; 178 | doCreateContextSpace(NN, encoded, space); 179 | space++; 180 | } else { 181 | // check info in the context if this is a question 182 | auto response = doReceiveResponse(NN, encoded); 183 | std::cout << std::setw(50) << std::left << sequence; 184 | if (response) 185 | std::cout << " [ YES ]" << std::endl; 186 | else 187 | std::cout << " [ NO ]" << std::endl; 188 | } 189 | } 190 | 191 | int main() { 192 | constexpr char STRUCTURE_PATH[128] = "structures/structure.json"; 193 | constexpr char VOCAB_PATH[128] = "texts/vocab.txt"; 194 | std::array definitions = {"STATE", "OBJECT", "PROCESS", "PLACE", "PROPERTY"}; 195 | 196 | // load vocabulary from text file 197 | auto vocab = doLoadVocabulary(VOCAB_PATH); 198 | 199 | // load neural network structure from file 200 | auto NN = new indk::NeuralNet(STRUCTURE_PATH); 201 | // NN -> doInterlinkInit(4408, 1); 202 | // indk::System::setVerbosityLevel(2); 203 | NN -> doPrepare(); 204 | 205 | std::cout << "Threads : " << indk::System::getComputeBackendParameter() << std::endl; 206 | std::cout << "Model name : " << NN->getName() << std::endl; 207 | std::cout << "Model desc : " << NN->getDescription() << std::endl; 208 | std::cout << "Model ver : " << NN->getVersion() << std::endl; 209 | std::cout << std::endl; 210 | 211 | for (const auto& d: definitions) { 212 | NN -> doIncludeNeuronToEnsemble(d, "DEFINITION"); 213 | NN -> getNeuron(d) -> setLambda(0.1); 214 | } 215 | 216 | int space = 1; 217 | auto T = getTimestampMS(); 218 | doLearnVocabulary(NN, definitions, vocab); 219 | 220 | // creating context 221 | doProcessTextSequence(NN, "The cat siting on the table.", space); 222 | doProcessTextSequence(NN, "The cat is black and the table is wooden.", space); 223 | doProcessTextSequence(NN, "Blue light falls from the window.", space); 224 | doProcessTextSequence(NN, "The cat is also half blue.", space); 225 | doProcessTextSequence(NN, "The cat is alien.", space); 226 | doProcessTextSequence(NN, "Other aliens are coming for the cat.", space); 227 | std::cout << std::endl; 228 | 229 | // checking 230 | doProcessTextSequence(NN, "Is the cat gray?", space); 231 | doProcessTextSequence(NN, "Is the cat black?", space); 232 | doProcessTextSequence(NN, "Is the cat blue?", space); 233 | doProcessTextSequence(NN, "Is the table wooden?", space); 234 | doProcessTextSequence(NN, "Is the table black?", space); 235 | doProcessTextSequence(NN, "Is the cat lying on the table?", space); 236 | doProcessTextSequence(NN, "Is the cat siting on the chair?", space); 237 | doProcessTextSequence(NN, "Is the cat lying under the table?", space); 238 | doProcessTextSequence(NN, "Is the cat siting on the table?", space); 239 | doProcessTextSequence(NN, "Is the light falls from the monitor?", space); 240 | doProcessTextSequence(NN, "Is the light falls from the window?", space); 241 | doProcessTextSequence(NN, "Is the cat alien?", space); 242 | doProcessTextSequence(NN, "Are the other aliens coming?", space); 243 | doProcessTextSequence(NN, "Are the other aliens coming for human?", space); 244 | doProcessTextSequence(NN, "Are the other aliens coming for the cat?", space); 245 | std::cout << std::endl; 246 | 247 | // NN -> doInterlinkSyncStructure(); 248 | // NN -> doInterlinkSyncData(); 249 | 250 | T = getTimestampMS() - T; 251 | std::cout << "Done in " << T << " ms" << std::endl; 252 | return 0; 253 | } 254 | -------------------------------------------------------------------------------- /samples/lang/structures/structure.json: -------------------------------------------------------------------------------- 1 | { 2 | "desc": "Natural language processing neural network", 3 | "entries": [ 4 | "ET", 5 | "SPACE_E1", 6 | "SPACE_E2", 7 | "SPACE_E3", 8 | "SPACE_E4", 9 | "SPACE_E5" 10 | ], 11 | "name": "lang model", 12 | "neurons": [ 13 | { 14 | "dimensions": 3, 15 | "ensemble": "", 16 | "input_signals": [ 17 | "ET" 18 | ], 19 | "latency": 0, 20 | "name": "N1", 21 | "receptors": [ 22 | { 23 | "count": 2, 24 | "data_scopes": [], 25 | "position": [ 26 | 50, 27 | 50, 28 | 0 29 | ], 30 | "radius": 20, 31 | "type": "cluster" 32 | } 33 | ], 34 | "size": 100, 35 | "synapses": [ 36 | { 37 | "entry": 0, 38 | "k1": 4, 39 | "neurotransmitter": "activation", 40 | "position": [ 41 | 50, 42 | 50, 43 | 0 44 | ], 45 | "radius": 10, 46 | "type": "cluster" 47 | } 48 | ] 49 | }, 50 | { 51 | "dimensions": 3, 52 | "ensemble": "", 53 | "input_signals": [ 54 | "ET" 55 | ], 56 | "latency": 0, 57 | "name": "N2", 58 | "receptors": [ 59 | { 60 | "count": 2, 61 | "data_scopes": [], 62 | "position": [ 63 | 50, 64 | 50, 65 | 0 66 | ], 67 | "radius": 20, 68 | "type": "cluster" 69 | } 70 | ], 71 | "size": 100, 72 | "synapses": [ 73 | { 74 | "entry": 0, 75 | "k1": 4, 76 | "neurotransmitter": "activation", 77 | "position": [ 78 | 50, 79 | 50, 80 | 0 81 | ], 82 | "radius": 10, 83 | "type": "cluster" 84 | } 85 | ] 86 | }, 87 | { 88 | "dimensions": 3, 89 | "ensemble": "", 90 | "input_signals": [ 91 | "ET" 92 | ], 93 | "latency": 0, 94 | "name": "N3", 95 | "receptors": [ 96 | { 97 | "count": 2, 98 | "data_scopes": [], 99 | "position": [ 100 | 50, 101 | 50, 102 | 0 103 | ], 104 | "radius": 20, 105 | "type": "cluster" 106 | } 107 | ], 108 | "size": 100, 109 | "synapses": [ 110 | { 111 | "entry": 0, 112 | "k1": 4, 113 | "neurotransmitter": "activation", 114 | "position": [ 115 | 50, 116 | 50, 117 | 0 118 | ], 119 | "radius": 10, 120 | "type": "cluster" 121 | } 122 | ] 123 | }, 124 | { 125 | "dimensions": 3, 126 | "ensemble": "", 127 | "input_signals": [ 128 | "ET" 129 | ], 130 | "latency": 0, 131 | "name": "N4", 132 | "receptors": [ 133 | { 134 | "count": 2, 135 | "data_scopes": [], 136 | "position": [ 137 | 50, 138 | 50, 139 | 0 140 | ], 141 | "radius": 20, 142 | "type": "cluster" 143 | } 144 | ], 145 | "size": 100, 146 | "synapses": [ 147 | { 148 | "entry": 0, 149 | "k1": 4, 150 | "neurotransmitter": "activation", 151 | "position": [ 152 | 50, 153 | 50, 154 | 0 155 | ], 156 | "radius": 10, 157 | "type": "cluster" 158 | } 159 | ] 160 | }, 161 | { 162 | "dimensions": 3, 163 | "ensemble": "", 164 | "input_signals": [ 165 | "ET" 166 | ], 167 | "latency": 0, 168 | "name": "N5", 169 | "receptors": [ 170 | { 171 | "count": 2, 172 | "data_scopes": [], 173 | "position": [ 174 | 50, 175 | 50, 176 | 0 177 | ], 178 | "radius": 20, 179 | "type": "cluster" 180 | } 181 | ], 182 | "size": 100, 183 | "synapses": [ 184 | { 185 | "entry": 0, 186 | "k1": 4, 187 | "neurotransmitter": "activation", 188 | "position": [ 189 | 50, 190 | 50, 191 | 0 192 | ], 193 | "radius": 10, 194 | "type": "cluster" 195 | } 196 | ] 197 | }, 198 | { 199 | "dimensions": 3, 200 | "ensemble": "", 201 | "input_signals": [ 202 | "N2" 203 | ], 204 | "latency": 0, 205 | "name": "OBJECT", 206 | "receptors": [ 207 | { 208 | "count": 10, 209 | "data_scopes": [], 210 | "position": [ 211 | 4, 212 | 4, 213 | 0 214 | ], 215 | "radius": 10, 216 | "type": "single" 217 | } 218 | ], 219 | "size": 10, 220 | "synapses": [ 221 | { 222 | "entry": 0, 223 | "k1": 100, 224 | "neurotransmitter": "activation", 225 | "position": [ 226 | 5, 227 | 5, 228 | 0 229 | ], 230 | "radius": 10, 231 | "type": "single" 232 | } 233 | ] 234 | }, 235 | { 236 | "dimensions": 3, 237 | "ensemble": "", 238 | "input_signals": [ 239 | "N4" 240 | ], 241 | "latency": 0, 242 | "name": "PLACE", 243 | "receptors": [ 244 | { 245 | "count": 10, 246 | "data_scopes": [], 247 | "position": [ 248 | 4, 249 | 4, 250 | 0 251 | ], 252 | "radius": 10, 253 | "type": "single" 254 | } 255 | ], 256 | "size": 10, 257 | "synapses": [ 258 | { 259 | "entry": 0, 260 | "k1": 100, 261 | "neurotransmitter": "activation", 262 | "position": [ 263 | 5, 264 | 5, 265 | 0 266 | ], 267 | "radius": 10, 268 | "type": "single" 269 | } 270 | ] 271 | }, 272 | { 273 | "dimensions": 3, 274 | "ensemble": "", 275 | "input_signals": [ 276 | "N3" 277 | ], 278 | "latency": 0, 279 | "name": "PROCESS", 280 | "receptors": [ 281 | { 282 | "count": 10, 283 | "data_scopes": [], 284 | "position": [ 285 | 4, 286 | 4, 287 | 0 288 | ], 289 | "radius": 10, 290 | "type": "single" 291 | } 292 | ], 293 | "size": 10, 294 | "synapses": [ 295 | { 296 | "entry": 0, 297 | "k1": 100, 298 | "neurotransmitter": "activation", 299 | "position": [ 300 | 5, 301 | 5, 302 | 0 303 | ], 304 | "radius": 10, 305 | "type": "single" 306 | } 307 | ] 308 | }, 309 | { 310 | "dimensions": 3, 311 | "ensemble": "", 312 | "input_signals": [ 313 | "N5" 314 | ], 315 | "latency": 0, 316 | "name": "PROPERTY", 317 | "receptors": [ 318 | { 319 | "count": 10, 320 | "data_scopes": [], 321 | "position": [ 322 | 4, 323 | 4, 324 | 0 325 | ], 326 | "radius": 10, 327 | "type": "single" 328 | } 329 | ], 330 | "size": 10, 331 | "synapses": [ 332 | { 333 | "entry": 0, 334 | "k1": 100, 335 | "neurotransmitter": "activation", 336 | "position": [ 337 | 5, 338 | 5, 339 | 0 340 | ], 341 | "radius": 10, 342 | "type": "single" 343 | } 344 | ] 345 | }, 346 | { 347 | "dimensions": 3, 348 | "ensemble": "", 349 | "input_signals": [ 350 | "N1" 351 | ], 352 | "latency": 0, 353 | "name": "STATE", 354 | "receptors": [ 355 | { 356 | "count": 10, 357 | "data_scopes": [], 358 | "position": [ 359 | 4, 360 | 4, 361 | 0 362 | ], 363 | "radius": 10, 364 | "type": "single" 365 | } 366 | ], 367 | "size": 10, 368 | "synapses": [ 369 | { 370 | "entry": 0, 371 | "k1": 100, 372 | "neurotransmitter": "activation", 373 | "position": [ 374 | 5, 375 | 5, 376 | 0 377 | ], 378 | "radius": 10, 379 | "type": "single" 380 | } 381 | ] 382 | }, 383 | { 384 | "dimensions": 3, 385 | "ensemble": "", 386 | "input_signals": [ 387 | "SPACE_E1", 388 | "SPACE_E2", 389 | "SPACE_E3", 390 | "SPACE_E4", 391 | "SPACE_E5" 392 | ], 393 | "latency": 0, 394 | "name": "_SPACE_INIT", 395 | "receptors": [ 396 | { 397 | "count": 10, 398 | "data_scopes": [], 399 | "position": [ 400 | 5, 401 | 5, 402 | 0 403 | ], 404 | "radius": 10, 405 | "type": "single" 406 | } 407 | ], 408 | "size": 10, 409 | "synapses": [ 410 | { 411 | "entry": 0, 412 | "k1": 100, 413 | "neurotransmitter": "activation", 414 | "position": [ 415 | 0, 416 | 0, 417 | 0 418 | ], 419 | "radius": 10, 420 | "type": "single" 421 | }, 422 | { 423 | "entry": 1, 424 | "k1": 100, 425 | "neurotransmitter": "activation", 426 | "position": [ 427 | 0, 428 | 10, 429 | 0 430 | ], 431 | "radius": 10, 432 | "type": "single" 433 | }, 434 | { 435 | "entry": 2, 436 | "k1": 100, 437 | "neurotransmitter": "activation", 438 | "position": [ 439 | 7, 440 | 0, 441 | 0 442 | ], 443 | "radius": 10, 444 | "type": "single" 445 | }, 446 | { 447 | "entry": 3, 448 | "k1": 100, 449 | "neurotransmitter": "activation", 450 | "position": [ 451 | 3, 452 | 10, 453 | 0 454 | ], 455 | "radius": 10, 456 | "type": "single" 457 | }, 458 | { 459 | "entry": 4, 460 | "k1": 100, 461 | "neurotransmitter": "activation", 462 | "position": [ 463 | 10, 464 | 5, 465 | 0 466 | ], 467 | "radius": 10, 468 | "type": "single" 469 | } 470 | ] 471 | } 472 | ], 473 | "output_signals": [ 474 | "STATE", 475 | "OBJECT", 476 | "PROCESS", 477 | "PLACE", 478 | "PROPERTY" 479 | ], 480 | "version": "1.1" 481 | } -------------------------------------------------------------------------------- /samples/lang/texts/vocab.txt: -------------------------------------------------------------------------------- 1 | color;PROPERTY 2 | black;PROPERTY 3 | gray;PROPERTY 4 | white;PROPERTY 5 | orange;PROPERTY 6 | blue;PROPERTY 7 | wooden;PROPERTY 8 | cat;OBJECT 9 | table;OBJECT 10 | chair;OBJECT 11 | animal;OBJECT 12 | wood;OBJECT 13 | monitor;OBJECT 14 | window;OBJECT 15 | light;OBJECT 16 | human;OBJECT 17 | alien;OBJECT 18 | aliens;OBJECT 19 | sit;PROCESS 20 | siting;PROCESS 21 | lying;PROCESS 22 | come;PROCESS 23 | coming;PROCESS 24 | falls;PROCESS 25 | in;PLACE 26 | on;PLACE 27 | under;PLACE 28 | at;PLACE 29 | from;PLACE -------------------------------------------------------------------------------- /samples/multimodal/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | project(interference_multimodal) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | add_executable(${PROJECT_NAME} main.cpp bmp.hpp) 7 | 8 | include_directories(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/include) 9 | target_link_libraries(${PROJECT_NAME} interference_static) 10 | 11 | set(SAMPLE_INSTALL_DIR ${CMAKE_SOURCE_DIR}/dist/bin/multimodal) 12 | install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${SAMPLE_INSTALL_DIR}) 13 | install(DIRECTORY images DESTINATION ${SAMPLE_INSTALL_DIR}) 14 | install(DIRECTORY structures DESTINATION ${SAMPLE_INSTALL_DIR}) -------------------------------------------------------------------------------- /samples/multimodal/bmp.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by nickware on 30.01.2023. 3 | // 4 | 5 | #ifndef INTERFERENCE_BMP_HPP 6 | #define INTERFERENCE_BMP_HPP 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | typedef std::vector> BMPImage; 16 | 17 | inline BMPImage doReadBMP(const std::string& filename) { 18 | std::ifstream fbmp(filename, std::ios::binary); 19 | unsigned char info[54]; 20 | std::vector> dbmp; 21 | 22 | if(!fbmp.is_open()) return {}; 23 | fbmp.read(reinterpret_cast(info), 54); 24 | 25 | int width = *(int*)&info[18]; 26 | int height = *(int*)&info[22]; 27 | auto datasize = width * height * 3; 28 | auto* data = new uint8_t[datasize]; 29 | memset(data, 0x0, datasize); 30 | fbmp.read(reinterpret_cast(data), datasize); 31 | for (int i = 0; i < datasize; i+=3) { 32 | dbmp.push_back({data[i+2], data[i+1], data[i]}); 33 | } 34 | 35 | fbmp.close(); 36 | delete [] data; 37 | return dbmp; 38 | } 39 | 40 | inline std::vector RGB2CMYK(float r, float g, float b) { 41 | auto rgb = std::vector({r, g, b}); 42 | auto max = std::max_element(rgb.begin(), rgb.end()); 43 | 44 | auto k = 1 - rgb[std::distance(rgb.begin(), max)]; 45 | auto c = (1-rgb[0]-k) / (1-k); 46 | auto m = (1-rgb[1]-k) / (1-k); 47 | auto y = (1-rgb[2]-k) / (1-k); 48 | 49 | return {c, m, y, k}; 50 | } 51 | 52 | inline std::vector RGB2HSI(float r, float g, float b) { 53 | auto rgb = std::vector({r, g, b}); 54 | auto min = std::min_element(rgb.begin(), rgb.end()); 55 | 56 | float h = 0; 57 | float s = 0; 58 | float i = (r + g + b) / 3; 59 | 60 | float rn = r / (r + g + b); 61 | float gn = g / (r + g + b); 62 | float bn = b / (r + g + b); 63 | 64 | if (i > 0) s = 1 - (rgb[std::distance(rgb.begin(), min)] / i); 65 | if (s < 1e-8) s = 0; 66 | if (s > 0) { 67 | h = acos((0.5 * ((rn - gn) + (rn - bn))) / (std::sqrt((rn - gn) * (rn - gn) + (rn - bn) * (gn - bn)))); 68 | if (b > g) h = 2 * M_PI - h; 69 | } 70 | return {h, s, i}; 71 | } 72 | 73 | #endif //INTERFERENCE_BMP_HPP 74 | -------------------------------------------------------------------------------- /samples/multimodal/images/1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/multimodal/images/1.bmp -------------------------------------------------------------------------------- /samples/multimodal/images/2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/multimodal/images/2.bmp -------------------------------------------------------------------------------- /samples/multimodal/images/3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/multimodal/images/3.bmp -------------------------------------------------------------------------------- /samples/multimodal/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 16.03.23 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | #include "indk/system.h" 13 | #include "indk/neuralnet.h" 14 | #include "bmp.hpp" 15 | 16 | uint64_t getTimestampMS() { 17 | return std::chrono::duration_cast(std::chrono::system_clock::now(). 18 | time_since_epoch()).count(); 19 | } 20 | 21 | std::vector> doBuildInputVector(std::vector &images, std::vector &texts) { 22 | std::vector> input; 23 | for (int d = 0,t = 0; d < images[0].size(); d+=2, t++) { 24 | input.emplace_back(); 25 | for (int i = 0; i < images.size(); i++) { 26 | for (int s = 0; s < 2; s++) { 27 | float r = images[i][d+s][0]; 28 | float g = images[i][d+s][1]; 29 | float b = images[i][d+s][2]; 30 | auto rgbn = std::vector({r/255, g/255, b/255}); 31 | auto HSI = RGB2HSI(rgbn[0], rgbn[1], rgbn[2]); 32 | input.back().emplace_back(HSI[0]/(2*M_PI)); 33 | input.back().emplace_back(HSI[1]); 34 | input.back().emplace_back(HSI[2]); 35 | } 36 | 37 | auto words = texts[i]; 38 | if (t < words.size()) input.back().emplace_back(float(words[t])/255); 39 | else input.back().emplace_back(0); 40 | } 41 | } 42 | return input; 43 | } 44 | 45 | int main() { 46 | constexpr uint8_t COUNT = 3; 47 | constexpr uint16_t IMAGE_SIZE = 128*128; 48 | constexpr char STRUCTURE_PATH[128] = "structures/structure.json"; 49 | constexpr char IMAGES_PATH[128] = "images/"; 50 | std::vector names = {"mug with a melons", "mug with a big apple", "tomato"}; 51 | std::vector variants = {"mug with a melons", "mug wth a meln", "mug with a big apple", "mug w th a bi ap ple", "tomato", "tomto"}; 52 | 53 | // load neural network structure from file 54 | auto NN = new indk::NeuralNet(STRUCTURE_PATH); 55 | 56 | // replicate neurons 57 | for (int i = 2; i <= COUNT; i++) NN -> doReplicateEnsemble("A1", "A"+std::to_string(i), true); 58 | NN -> doStructurePrepare(); 59 | 60 | std::cout << "Threads : " << indk::System::getComputeBackendParameter() << std::endl; 61 | std::cout << "Model name : " << NN->getName() << std::endl; 62 | std::cout << "Model desc : " << NN->getDescription() << std::endl; 63 | std::cout << "Model ver : " << NN->getVersion() << std::endl; 64 | std::cout << "Neuron count: " << NN->getNeuronCount() << std::endl; 65 | std::cout << std::endl; 66 | 67 | std::cout << "List of objects:" << std::endl; 68 | std::vector images; 69 | for (int b = 1; b <= COUNT; b++) { 70 | auto image = doReadBMP(IMAGES_PATH+std::to_string(b)+".bmp"); 71 | images.push_back(image); 72 | std::cout << b << ". Image [" << b << ".bmp] - " << names[b-1] << std::endl; 73 | if (image.size() != IMAGE_SIZE) { 74 | std::cout << "Error loading image " << b << ".bmp" << std::endl; 75 | return 1; 76 | } 77 | } 78 | auto input = doBuildInputVector(images, names); 79 | 80 | auto T = getTimestampMS(); 81 | 82 | // teach the neural network 83 | NN -> doLearn(input); 84 | 85 | // detect 86 | std::cout << std::endl << "Detecting objects:" << std::endl; 87 | for (int b = 1; b <= COUNT; b++) { 88 | std::string name = std::to_string(b)+".bmp"; 89 | auto image = doReadBMP(IMAGES_PATH+name); 90 | std::vector rimages; 91 | for (int i = 1; i <= COUNT; i++) rimages.push_back(image); 92 | 93 | std::cout << "Image [" << b << ".bmp]" << std::endl; 94 | 95 | for (int e = 0; e < variants.size(); e++) { 96 | std::cout << std::setw(35) << std::left << std::to_string(e+1)+". "+variants[e]; 97 | 98 | std::vector rvariants; 99 | for (int i = 1; i <= COUNT; i++) rvariants.push_back(variants[e]); 100 | 101 | auto rinput = doBuildInputVector(rimages, rvariants); 102 | 103 | NN -> doRecognise(rinput); 104 | 105 | auto patterns = NN -> doComparePatterns(indk::PatternCompareFlags::CompareNormalized); 106 | auto r = std::max_element(patterns.begin(), patterns.end()); 107 | if (std::distance(patterns.begin(), r) == b-1) { 108 | std::cout << "[CORRECT ANSWER]" << std::endl; 109 | } else { 110 | std::cout << "[INCORRECT ANSWER]" << std::endl; 111 | // Uncomment to print the response of output neurons to the input data (response - values [0, 1], 0 - minimum response, 1 - maximum response) 112 | // std::cout << "Difference for outputs:" << std::endl; 113 | // for (int i = 0; i < patterns.size(); i++) std::cout << (i+1) << ". " << patterns[i] << std::endl; 114 | } 115 | } 116 | std::cout << std::endl; 117 | } 118 | 119 | T = getTimestampMS() - T; 120 | std::cout << "Done in " << T << " ms" << std::endl; 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /samples/multimodal/structures/structure.json: -------------------------------------------------------------------------------- 1 | { 2 | "entries": ["E1", "E2", "E3", "E4", "E5", "E6", "E7"], 3 | "neurons": [ 4 | { 5 | "name": "N1", 6 | "size": 1000, 7 | "dimensions": 3, 8 | "input_signals": ["E1", "E2", "E3", "E4", "E5", "E6"], 9 | "ensemble": "A1", 10 | 11 | "synapses": [ 12 | { 13 | "type": "cluster", 14 | "position": [500, 500, 0], 15 | "radius": 500, 16 | "neurotransmitter": "activation", 17 | "k1": 2.2 18 | } 19 | ], 20 | 21 | "receptors": [ 22 | { 23 | "type": "cluster", 24 | "position": [500, 500, 0], 25 | "count": 5, 26 | "radius": 5 27 | } 28 | ] 29 | }, 30 | { 31 | "name": "N2", 32 | "size": 500, 33 | "dimensions": 3, 34 | "input_signals": ["E7"], 35 | "ensemble": "A1", 36 | 37 | "synapses": [ 38 | { 39 | "entry": 0, 40 | "position": [250, 250, 0], 41 | "neurotransmitter": "activation", 42 | "k1": 70 43 | } 44 | ], 45 | 46 | "receptors": [ 47 | { 48 | "type": "cluster", 49 | "position": [250, 250, 0], 50 | "count": 10, 51 | "radius": 15 52 | } 53 | ] 54 | }, 55 | { 56 | "name": "N3", 57 | "size": 1000, 58 | "dimensions": 3, 59 | "input_signals": ["N1", "N2"], 60 | "ensemble": "A1", 61 | 62 | "synapses": [ 63 | { 64 | "entry": 0, 65 | "position": [510, 500, 0], 66 | "neurotransmitter": "activation", 67 | "k1": 1 68 | }, 69 | { 70 | "entry": 1, 71 | "position": [500, 510, 0], 72 | "neurotransmitter": "activation", 73 | "k1": 300 74 | } 75 | ], 76 | 77 | "receptors": [ 78 | { 79 | "type": "cluster", 80 | "position": [500, 500, 0], 81 | "count": 5, 82 | "radius": 5 83 | } 84 | ] 85 | } 86 | ], 87 | "output_signals": ["N3"], 88 | "name": "multimodal", 89 | "desc": "neural net structure for interference multimodal sample", 90 | "version": "1.0" 91 | } 92 | -------------------------------------------------------------------------------- /samples/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | project(interference_test) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | add_executable(interference_test main.cpp) 7 | 8 | include_directories(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/include) 9 | target_link_libraries(${PROJECT_NAME} interference_static) 10 | 11 | set(SAMPLE_INSTALL_DIR ${CMAKE_SOURCE_DIR}/dist/bin/test) 12 | install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${SAMPLE_INSTALL_DIR}) 13 | install(DIRECTORY structures DESTINATION ${SAMPLE_INSTALL_DIR}) 14 | -------------------------------------------------------------------------------- /samples/test/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 30.01.23 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | 17 | indk::NeuralNet *NN; 18 | std::vector> X; 19 | 20 | std::vector> backends = { 21 | std::make_tuple(indk::System::ComputeBackends::Default, 0, "singlethread"), 22 | std::make_tuple(indk::System::ComputeBackends::Multithread, 2, "multithread"), 23 | std::make_tuple(indk::System::ComputeBackends::OpenCL, 0, "OpenCL"), 24 | }; 25 | 26 | uint64_t getTimestampMS() { 27 | return std::chrono::duration_cast(std::chrono::system_clock::now(). 28 | time_since_epoch()).count(); 29 | } 30 | 31 | void doLoadModel(const std::string& path, int size) { 32 | std::ifstream structure(path); 33 | NN -> setStructure(structure); 34 | 35 | for (int i = 2; i < size; i++) { 36 | NN -> doReplicateEnsemble("A1", "A"+std::to_string(i)); 37 | } 38 | NN -> doStructurePrepare(); 39 | 40 | std::cout << "Model name : " << NN->getName() << std::endl; 41 | std::cout << "Model desc : " << NN->getDescription() << std::endl; 42 | std::cout << "Model ver : " << NN->getVersion() << std::endl; 43 | std::cout << "Neuron count: " << NN->getNeuronCount() << std::endl; 44 | std::cout << std::endl; 45 | } 46 | 47 | int doTest(float ref) { 48 | auto T = getTimestampMS(); 49 | auto Y = NN -> doLearn(X); 50 | T = getTimestampMS() - T; 51 | std::cout << std::setw(20) << std::left << "done ["+std::to_string(T)+" ms] "; 52 | 53 | bool passed = true; 54 | for (auto &y: Y) { 55 | if (std::fabs(y.first-ref) > 1e-3) { 56 | std::cout << "[FAILED]" << std::endl; 57 | std::cout << "Output value " << y.first << " is not " << ref << std::endl; 58 | std::cout << std::endl; 59 | passed = false; 60 | break; 61 | } 62 | } 63 | if (passed) { 64 | std::cout << "[PASSED]" << std::endl; 65 | } 66 | 67 | return passed; 68 | } 69 | 70 | int doTests(const std::string& name, float ref) { 71 | int count = 0; 72 | 73 | for (auto &b: backends) { 74 | NN -> doReset(); 75 | std::cout << std::setw(50) << std::left << name+" ("+std::get<2>(b)+"): "; 76 | indk::System::setComputeBackend(std::get<0>(b), std::get<1>(b)); 77 | count += doTest(ref); 78 | } 79 | std::cout << std::endl; 80 | 81 | return count; 82 | } 83 | 84 | int main() { 85 | constexpr unsigned STRUCTURE_COUNT = 2; 86 | constexpr float SUPERSTRUCTURE_TEST_REFERENCE_OUTPUT = 0.0291; 87 | constexpr float BENCHMARK_TEST_REFERENCE_OUTPUT = 2.7622; 88 | const unsigned TOTAL_TEST_COUNT = STRUCTURE_COUNT*backends.size(); 89 | 90 | int count = 0; 91 | indk::System::setVerbosityLevel(1); 92 | NN = new indk::NeuralNet(); 93 | 94 | // creating data array 95 | for (int i = 0; i < 170; i++) { 96 | X.push_back({50, 50}); 97 | } 98 | 99 | // running tests 100 | std::cout << "=== SUPERSTRUCTURE TEST ===" << std::endl; 101 | doLoadModel("structures/structure_general.json", 101); 102 | count += doTests("Superstructure test", SUPERSTRUCTURE_TEST_REFERENCE_OUTPUT); 103 | 104 | std::cout << "=== BENCHMARK ===" << std::endl; 105 | doLoadModel("structures/structure_bench.json", 10001); 106 | count += doTests("Benchmark", BENCHMARK_TEST_REFERENCE_OUTPUT); 107 | 108 | std::cout << std::endl; 109 | std::cout << "Tests passed: [" << count << "/" << TOTAL_TEST_COUNT << "]" << std::endl; 110 | delete NN; 111 | 112 | if (count != TOTAL_TEST_COUNT) return 1; 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /samples/test/structures/structure_bench.json: -------------------------------------------------------------------------------- 1 | { 2 | "entries": ["E1", "E2"], 3 | "neurons": [ 4 | { 5 | "name": "N1", 6 | "size": 1200, 7 | "dimensions": 3, 8 | "input_signals": ["E1", "E2"], 9 | "ensemble": "A1", 10 | 11 | "synapses": [ 12 | { 13 | "type": "cluster", 14 | "position": [600, 600, 0], 15 | "radius": 50, 16 | "neurotransmitter": "activation", 17 | "k1": 1050 18 | } 19 | ], 20 | 21 | "receptors": [ 22 | { 23 | "type": "cluster", 24 | "position": [600, 600, 0], 25 | "count": 10, 26 | "radius": 5 27 | } 28 | ] 29 | } 30 | ], 31 | "output_signals": ["N1"], 32 | "name": "benchmark net", 33 | "desc": "neural net structure for benchmark", 34 | "version": "1.1" 35 | } 36 | -------------------------------------------------------------------------------- /samples/test/structures/structure_general.json: -------------------------------------------------------------------------------- 1 | { 2 | "entries": ["E1", "E2"], 3 | "neurons": [ 4 | { 5 | "name": "N1", 6 | "size": 1000, 7 | "dimensions": 3, 8 | "input_signals": ["E1", "E2"], 9 | "ensemble": "A1", 10 | 11 | "synapses": [ 12 | { 13 | "type": "cluster", 14 | "position": [500, 500, 0], 15 | "radius": 25, 16 | "neurotransmitter": "activation", 17 | "k1": 10 18 | } 19 | ], 20 | 21 | "receptors": [ 22 | { 23 | "type": "cluster", 24 | "position": [500, 500, 0], 25 | "count": 15, 26 | "radius": 10 27 | } 28 | ] 29 | }, 30 | { 31 | "name": "N2", 32 | "size": 200, 33 | "dimensions": 3, 34 | "input_signals": ["N1", "N6"], 35 | "ensemble": "A1", 36 | 37 | "synapses": [ 38 | { 39 | "type": "cluster", 40 | "position": [100, 100, 0], 41 | "radius": 30, 42 | "neurotransmitter": "activation", 43 | "k1": 30 44 | } 45 | ], 46 | "receptors": [ 47 | { 48 | "type": "cluster", 49 | "position": [100, 100, 0], 50 | "count": 15, 51 | "radius": 10 52 | } 53 | ] 54 | }, 55 | { 56 | "name": "N3", 57 | "size": 201, 58 | "dimensions": 3, 59 | "input_signals": ["N1", "N6"], 60 | "ensemble": "A1", 61 | 62 | "synapses": [ 63 | { 64 | "type": "cluster", 65 | "position": [100, 100, 0], 66 | "radius": 30, 67 | "neurotransmitter": "activation", 68 | "k1": 50 69 | } 70 | ], 71 | "receptors": [ 72 | { 73 | "type": "cluster", 74 | "position": [100, 100, 0], 75 | "count": 15, 76 | "radius": 10 77 | } 78 | ] 79 | }, 80 | { 81 | "name": "N4", 82 | "size": 300, 83 | "dimensions": 3, 84 | "input_signals": ["N3", "N5", "N6"], 85 | "latency": 1, 86 | "ensemble": "A1", 87 | 88 | "synapses": [ 89 | { 90 | "type": "cluster", 91 | "position": [150, 150, 0], 92 | "radius": 5, 93 | "neurotransmitter": "activation", 94 | "k1": 25 95 | } 96 | ], 97 | "receptors": [ 98 | { 99 | "type": "cluster", 100 | "position": [150, 150, 0], 101 | "count": 15, 102 | "radius": 1 103 | } 104 | ] 105 | }, 106 | { 107 | "name": "N5", 108 | "size": 200, 109 | "dimensions": 3, 110 | "input_signals": ["N2", "N4"], 111 | "ensemble": "A1", 112 | 113 | "synapses": [ 114 | { 115 | "entry": 0, 116 | "position": [100, 100, 0], 117 | "neurotransmitter": "activation", 118 | "k1": 100 119 | } 120 | ], 121 | "receptors": [ 122 | { 123 | "type": "cluster", 124 | "position": [100, 100, 0], 125 | "count": 15, 126 | "radius": 5 127 | }, 128 | { 129 | "type": "cluster", 130 | "position": [100, 100, 0], 131 | "count": 10, 132 | "radius": 10 133 | } 134 | ] 135 | }, 136 | { 137 | "name": "N6", 138 | "size": 100, 139 | "dimensions": 3, 140 | "input_signals": ["N2", "N3", "N4"], 141 | "latency": 2, 142 | "ensemble": "A1", 143 | 144 | "synapses": [ 145 | { 146 | "type": "cluster", 147 | "position": [50, 50, 0], 148 | "radius": 50, 149 | "neurotransmitter": "activation", 150 | "k1": 100 151 | } 152 | ], 153 | "receptors": [ 154 | { 155 | "type": "cluster", 156 | "position": [50, 50, 0], 157 | "count": 15, 158 | "radius": 30 159 | }, 160 | { 161 | "type": "cluster", 162 | "position": [50, 50, 0], 163 | "count": 10, 164 | "radius": 35 165 | } 166 | ] 167 | } 168 | ], 169 | "output_signals": ["N6"], 170 | "name": "superstructure test net", 171 | "desc": "neural net structure for general testing", 172 | "version": "1.3" 173 | } 174 | -------------------------------------------------------------------------------- /samples/vision/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | project(interference_vision) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | add_executable(${PROJECT_NAME} main.cpp bmp.hpp) 7 | 8 | include_directories(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/include) 9 | target_link_libraries(${PROJECT_NAME} interference_static) 10 | 11 | set(SAMPLE_INSTALL_DIR ${CMAKE_SOURCE_DIR}/dist/bin/vision) 12 | install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${SAMPLE_INSTALL_DIR}) 13 | install(DIRECTORY images DESTINATION ${SAMPLE_INSTALL_DIR}) 14 | install(DIRECTORY structures DESTINATION ${SAMPLE_INSTALL_DIR}) 15 | -------------------------------------------------------------------------------- /samples/vision/bmp.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by nickware on 30.01.2023. 3 | // 4 | 5 | #ifndef INTERFERENCE_BMP_HPP 6 | #define INTERFERENCE_BMP_HPP 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | typedef std::vector> BMPImage; 16 | 17 | inline BMPImage doReadBMP(const std::string& filename) { 18 | std::ifstream fbmp(filename, std::ios::binary); 19 | unsigned char info[54]; 20 | std::vector> dbmp; 21 | 22 | if(!fbmp.is_open()) return {}; 23 | fbmp.read(reinterpret_cast(info), 54); 24 | 25 | int width = *(int*)&info[18]; 26 | int height = *(int*)&info[22]; 27 | auto datasize = width * height * 3; 28 | auto* data = new uint8_t[datasize]; 29 | memset(data, 0x0, datasize); 30 | fbmp.read(reinterpret_cast(data), datasize); 31 | for (int i = 0; i < datasize; i+=3) { 32 | dbmp.push_back({data[i+2], data[i+1], data[i]}); 33 | } 34 | 35 | fbmp.close(); 36 | delete [] data; 37 | return dbmp; 38 | } 39 | 40 | inline std::vector RGB2CMYK(float r, float g, float b) { 41 | auto rgb = std::vector({r, g, b}); 42 | auto max = std::max_element(rgb.begin(), rgb.end()); 43 | 44 | auto k = 1 - rgb[std::distance(rgb.begin(), max)]; 45 | auto c = (1-rgb[0]-k) / (1-k); 46 | auto m = (1-rgb[1]-k) / (1-k); 47 | auto y = (1-rgb[2]-k) / (1-k); 48 | 49 | return {c, m, y, k}; 50 | } 51 | 52 | inline std::vector RGB2HSI(float r, float g, float b) { 53 | auto rgb = std::vector({r, g, b}); 54 | auto min = std::min_element(rgb.begin(), rgb.end()); 55 | 56 | float h = 0; 57 | float s = 0; 58 | float i = (r + g + b) / 3; 59 | 60 | float rn = r / (r + g + b); 61 | float gn = g / (r + g + b); 62 | float bn = b / (r + g + b); 63 | 64 | if (i > 0) s = 1 - (rgb[std::distance(rgb.begin(), min)] / i); 65 | if (s < 1e-8) s = 0; 66 | if (s > 0) { 67 | h = acos((0.5 * ((rn - gn) + (rn - bn))) / (std::sqrt((rn - gn) * (rn - gn) + (rn - bn) * (gn - bn)))); 68 | if (b > g) h = 2 * M_PI - h; 69 | } 70 | return {h, s, i}; 71 | } 72 | 73 | #endif //INTERFERENCE_BMP_HPP 74 | -------------------------------------------------------------------------------- /samples/vision/images/learn/1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/learn/1.bmp -------------------------------------------------------------------------------- /samples/vision/images/learn/10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/learn/10.bmp -------------------------------------------------------------------------------- /samples/vision/images/learn/2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/learn/2.bmp -------------------------------------------------------------------------------- /samples/vision/images/learn/3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/learn/3.bmp -------------------------------------------------------------------------------- /samples/vision/images/learn/4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/learn/4.bmp -------------------------------------------------------------------------------- /samples/vision/images/learn/5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/learn/5.bmp -------------------------------------------------------------------------------- /samples/vision/images/learn/6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/learn/6.bmp -------------------------------------------------------------------------------- /samples/vision/images/learn/7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/learn/7.bmp -------------------------------------------------------------------------------- /samples/vision/images/learn/8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/learn/8.bmp -------------------------------------------------------------------------------- /samples/vision/images/learn/9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/learn/9.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/1-1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/1-1.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/1-10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/1-10.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/1-2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/1-2.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/1-3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/1-3.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/1-4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/1-4.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/1-5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/1-5.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/1-6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/1-6.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/1-7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/1-7.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/1-8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/1-8.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/1-9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/1-9.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/10-1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/10-1.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/10-10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/10-10.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/10-2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/10-2.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/10-3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/10-3.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/10-4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/10-4.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/10-5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/10-5.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/10-6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/10-6.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/10-7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/10-7.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/10-8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/10-8.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/10-9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/10-9.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/2-1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/2-1.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/2-10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/2-10.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/2-2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/2-2.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/2-3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/2-3.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/2-4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/2-4.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/2-5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/2-5.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/2-6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/2-6.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/2-7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/2-7.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/2-8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/2-8.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/2-9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/2-9.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/3-1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/3-1.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/3-10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/3-10.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/3-2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/3-2.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/3-3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/3-3.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/3-4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/3-4.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/3-5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/3-5.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/3-6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/3-6.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/3-7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/3-7.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/3-8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/3-8.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/3-9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/3-9.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/4-1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/4-1.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/4-10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/4-10.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/4-2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/4-2.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/4-3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/4-3.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/4-4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/4-4.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/4-5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/4-5.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/4-6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/4-6.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/4-7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/4-7.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/4-8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/4-8.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/4-9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/4-9.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/5-1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/5-1.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/5-10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/5-10.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/5-2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/5-2.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/5-3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/5-3.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/5-4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/5-4.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/5-5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/5-5.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/5-6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/5-6.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/5-7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/5-7.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/5-8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/5-8.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/5-9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/5-9.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/6-1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/6-1.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/6-10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/6-10.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/6-2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/6-2.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/6-3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/6-3.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/6-4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/6-4.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/6-5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/6-5.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/6-6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/6-6.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/6-7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/6-7.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/6-8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/6-8.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/6-9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/6-9.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/7-1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/7-1.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/7-10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/7-10.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/7-2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/7-2.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/7-3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/7-3.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/7-4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/7-4.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/7-5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/7-5.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/7-6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/7-6.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/7-7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/7-7.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/7-8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/7-8.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/7-9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/7-9.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/8-1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/8-1.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/8-10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/8-10.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/8-2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/8-2.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/8-3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/8-3.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/8-4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/8-4.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/8-5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/8-5.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/8-6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/8-6.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/8-7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/8-7.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/8-8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/8-8.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/8-9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/8-9.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/9-1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/9-1.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/9-10.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/9-10.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/9-2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/9-2.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/9-3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/9-3.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/9-4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/9-4.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/9-5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/9-5.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/9-6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/9-6.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/9-7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/9-7.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/9-8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/9-8.bmp -------------------------------------------------------------------------------- /samples/vision/images/test/9-9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickware-group/interference/1f4e9845959252c19901a509c214bb2fe446d717/samples/vision/images/test/9-9.bmp -------------------------------------------------------------------------------- /samples/vision/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 30.01.23 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bmp.hpp" 15 | 16 | uint64_t getTimestampMS() { 17 | return std::chrono::duration_cast(std::chrono::system_clock::now(). 18 | time_since_epoch()).count(); 19 | } 20 | 21 | std::vector> doBuildInputVector(std::vector images) { 22 | std::vector> input; 23 | for (int d = 0; d < images[0].size(); d+=2) { 24 | input.emplace_back(); 25 | for (int i = 0; i < images.size(); i++) { 26 | for (int s = 0; s < 2; s++) { 27 | float r = images[i][d+s][0]; 28 | float g = images[i][d+s][1]; 29 | float b = images[i][d+s][2]; 30 | auto rgbn = std::vector({r/255, g/255, b/255}); 31 | auto HSI = RGB2HSI(rgbn[0], rgbn[1], rgbn[2]); 32 | input.back().emplace_back(HSI[0]/(2*M_PI)); 33 | input.back().emplace_back(HSI[1]); 34 | input.back().emplace_back(HSI[2]); 35 | } 36 | } 37 | } 38 | return input; 39 | } 40 | 41 | void doLog(const std::string& element, uint64_t time, float speed, bool endl = true) { 42 | std::stringstream s; 43 | s << time << "ms"; 44 | if (speed > 0) s << ", " << std::setprecision(2) << std::fixed << speed << " mbit/s"; 45 | std::cout << "[" << std::setw(22) << std::left << std::setfill('.') << s.str() << "] " << std::setw(32) << std::setfill(' ') << element << "done "; 46 | if (endl) std::cout << std::endl; 47 | } 48 | 49 | int main() { 50 | // indk::System::setComputeBackend(indk::System::ComputeBackends::Multithread, 2); 51 | constexpr uint8_t TEACH_COUNT = 10; 52 | constexpr uint8_t TEST_COUNT = 10; 53 | constexpr uint8_t TEST_ELEMENTS = 10; 54 | constexpr uint16_t IMAGE_SIZE = 128*128; 55 | constexpr char STRUCTURE_PATH[128] = "structures/structure.json"; 56 | constexpr char IMAGES_TEACHING_PATH[128] = "images/learn/"; 57 | constexpr char IMAGES_TESTING_PATH[128] = "images/test/"; 58 | 59 | // load neural network structure from file 60 | std::ifstream structure(STRUCTURE_PATH); 61 | auto NN = new indk::NeuralNet(STRUCTURE_PATH); 62 | NN -> setStateSyncEnabled(); 63 | // NN -> doInterlinkInit(4408, 1); 64 | 65 | // replicate neurons for classification 66 | for (int i = 2; i <= TEACH_COUNT; i++) NN -> doReplicateEnsemble("A1", "A"+std::to_string(i), true); 67 | 68 | std::cout << "Threads : " << indk::System::getComputeBackendParameter() << std::endl; 69 | std::cout << "Model name : " << NN->getName() << std::endl; 70 | std::cout << "Model desc : " << NN->getDescription() << std::endl; 71 | std::cout << "Model ver : " << NN->getVersion() << std::endl; 72 | std::cout << "Neuron count: " << NN->getNeuronCount() << std::endl; 73 | std::cout << std::endl; 74 | 75 | // load the images 76 | auto T = getTimestampMS(); 77 | uint64_t Ttotal = 0; 78 | 79 | std::vector images; 80 | for (int b = 1; b <= TEACH_COUNT; b++) { 81 | auto image = doReadBMP(IMAGES_TEACHING_PATH+std::to_string(b)+".bmp"); 82 | images.push_back(image); 83 | if (image.size() != IMAGE_SIZE) { 84 | std::cout << "Error loading image " << b << ".bmp" << std::endl; 85 | return 1; 86 | } 87 | } 88 | T = getTimestampMS() - T; 89 | auto input = doBuildInputVector(images); 90 | doLog("Loading images", T, 0); 91 | 92 | // teach the neural network 93 | T = getTimestampMS(); 94 | NN -> doLearn(input); 95 | T = getTimestampMS() - T; 96 | 97 | // Compute speed 98 | auto S = (IMAGE_SIZE*TEACH_COUNT*24.f/1024/1024)*1000 / T; 99 | float Stotal = 0; 100 | doLog("Teaching neural network", T, S); 101 | 102 | // recognize the images 103 | float rcount = 0; 104 | for (int b = 1; b <= TEST_COUNT; b++) { 105 | for (int e = 1; e <= TEST_ELEMENTS; e++) { 106 | std::string name = std::to_string(b)+"-"+std::to_string(e)+".bmp"; 107 | auto image = doReadBMP(IMAGES_TESTING_PATH+name); 108 | auto rinput = doBuildInputVector({image}); 109 | 110 | T = getTimestampMS(); 111 | NN -> doRecognise(rinput, true, {"E1", "E2", "E3", "E4", "E5", "E6"}); 112 | T = getTimestampMS() - T; 113 | Ttotal += T; 114 | 115 | // Compute speed 116 | S = (IMAGE_SIZE*24./1024/1024)*1000 / T; 117 | Stotal += S; 118 | doLog("Recognizing "+std::to_string(b)+"-"+std::to_string(e)+".bmp", T, S, false); 119 | 120 | auto patterns = NN -> doComparePatterns(indk::PatternCompareFlags::CompareNormalized); 121 | auto r = std::max_element(patterns.begin(), patterns.end()); 122 | if (std::distance(patterns.begin(), r) == b-1) { 123 | std::cout << "[RECOGNIZED]" << std::endl; 124 | rcount++; 125 | } else { 126 | std::cout << "[NOT RECOGNIZED]" << std::endl; 127 | // Uncomment to print the response of output neurons to the input data (response - values [0, 1], 0 - minimum response, 1 - maximum response) 128 | // std::cout << "Difference for outputs:" << std::endl; 129 | // for (int i = 0; i < patterns.size(); i++) std::cout << (i+1) << ". " << patterns[i] << std::endl; 130 | } 131 | } 132 | } 133 | 134 | std::cout << std::endl; 135 | std::cout << "================================== SUMMARY ==================================" << std::endl; 136 | std::cout << "Recognition accuracy: " << rcount/(TEST_COUNT*TEST_ELEMENTS) << " (" << rcount << "/" << TEST_COUNT*TEST_ELEMENTS << ")" << std::endl; 137 | std::cout << "Recognition time: " << Ttotal << " ms" << std::endl; 138 | std::cout << "Recognition speed: " << Stotal/(TEST_COUNT*TEST_ELEMENTS) << " mbit/s (" << 1000/(Ttotal/float(TEST_COUNT*TEST_ELEMENTS)) << " FPS)" << std::endl; 139 | return 0; 140 | } 141 | -------------------------------------------------------------------------------- /samples/vision/structures/structure.json: -------------------------------------------------------------------------------- 1 | { 2 | "entries": ["E1", "E2", "E3", "E4", "E5", "E6"], 3 | "neurons": [ 4 | { 5 | "name": "N1", 6 | "size": 1200, 7 | "dimensions": 3, 8 | "input_signals": ["E1", "E2", "E3", "E4", "E5", "E6"], 9 | "ensemble": "A1", 10 | 11 | "synapses": [ 12 | { 13 | "type": "cluster", 14 | "position": [600, 600, 0], 15 | "radius": 300, 16 | "neurotransmitter": "activation", 17 | "k1": 11 18 | } 19 | ], 20 | 21 | "receptors": [ 22 | { 23 | "type": "cluster", 24 | "position": [600, 600, 0], 25 | "count": 5, 26 | "radius": 50 27 | }, 28 | { 29 | "type": "cluster", 30 | "position": [600, 600, 0], 31 | "count": 3, 32 | "radius": 200 33 | } 34 | ] 35 | } 36 | ], 37 | "output_signals": ["N1"], 38 | "name": "vision", 39 | "desc": "neural net structure for interference vision sample", 40 | "version": "1.3" 41 | } 42 | -------------------------------------------------------------------------------- /src/backends/default.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 18.10.22 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | 13 | indk::ComputeBackendDefault::ComputeBackendDefault() { 14 | zPos = new indk::Position(0, 3); 15 | dRPos = new indk::Position(0, 3); 16 | nRPos = new indk::Position(0, 3); 17 | } 18 | 19 | void indk::ComputeBackendDefault::doRegisterHost(const std::vector&) { 20 | } 21 | 22 | void indk::ComputeBackendDefault::doWaitTarget() { 23 | } 24 | 25 | void indk::ComputeBackendDefault::doProcess(void* Object) { 26 | auto N = (indk::Neuron*)Object; 27 | float FiSum, D, P = 0; 28 | 29 | auto Xm = N -> getXm(); 30 | auto DimensionsCount = N -> getDimensionsCount(); 31 | 32 | zPos -> setXm(Xm); 33 | zPos -> setDimensionsCount(DimensionsCount); 34 | dRPos -> setXm(Xm); 35 | dRPos -> setDimensionsCount(DimensionsCount); 36 | nRPos -> setXm(Xm); 37 | nRPos -> setDimensionsCount(DimensionsCount); 38 | 39 | indk::Position *RPos; 40 | 41 | for (int j = 0; j < N->getEntriesCount(); j++) { 42 | auto E = N -> getEntry(j); 43 | E -> doProcess(); 44 | } 45 | 46 | for (int i = 0; i < N->getReceptorsCount(); i++) { 47 | auto R = N->getReceptor(i); 48 | 49 | if (R->isLocked() && N->getProcessingMode() == indk::Neuron::ProcessingModeAutoReset) { 50 | R -> doPrepare(); 51 | } 52 | 53 | if (!R->isLocked()) RPos = R -> getPos(); 54 | else RPos = R -> getPosf(); 55 | indk::Position *SPos; 56 | std::pair FiValues; 57 | FiSum = 0; 58 | dRPos -> doZeroPosition(); 59 | 60 | for (int j = 0; j < N->getEntriesCount(); j++) { 61 | auto E = N -> getEntry(j); 62 | 63 | for (unsigned int k = 0; k < E->getSynapsesCount(); k++) { 64 | auto *S = E -> getSynapse(k); 65 | SPos = S -> getPos(); 66 | D = SPos -> getDistanceFrom(RPos); 67 | FiValues = indk::Computer::getFiFunctionValue(S->getLambda(), S->getGamma(), S->getdGamma(), D); 68 | if (FiValues.second > 0) { 69 | indk::Computer::getNewPosition(nRPos, RPos, SPos, indk::Computer::getFiVectorLength(FiValues.second), D); 70 | dRPos -> doAdd(nRPos); 71 | } 72 | FiSum += FiValues.first; 73 | } 74 | } 75 | 76 | if (R->isLocked() && N->getProcessingMode() == indk::Neuron::ProcessingModeAutoRollback) { 77 | auto NPos = indk::Position(*RPos); 78 | NPos.doAdd(dRPos); 79 | auto scopes = R -> getReferencePosScopes(); 80 | float dmin = -1; 81 | 82 | for (auto s: scopes) { 83 | auto d = indk::Position::getDistance(&NPos, s); 84 | if (dmin == -1 || d <= dmin) dmin = d; 85 | } 86 | if (dmin > 10e-6) 87 | continue; 88 | } 89 | 90 | R -> setFi(FiSum); 91 | R -> doUpdatePos(dRPos); 92 | P += indk::Computer::getReceptorInfluenceValue(R->doCheckActive(), R->getdFi(), dRPos, zPos); 93 | R -> doUpdateSensitivityValue(); 94 | } 95 | P /= (float)N->getReceptorsCount(); 96 | 97 | N -> doFinalizeInput(P); 98 | 99 | if (N->isLearned() && N->getProcessingMode() == indk::Neuron::ProcessingModeAutoRollback) { 100 | if (P == 0) { 101 | for (int j = 0; j < N->getEntriesCount(); j++) { 102 | N -> getEntry(j) -> doRollback(); 103 | } 104 | } 105 | } else if (N->isLearned() && N->getProcessingMode() == indk::Neuron::ProcessingModeAutoReset) { 106 | for (int j = 0; j < N->getEntriesCount(); j++) { 107 | N -> getEntry(j) -> doFinalize(); 108 | } 109 | } 110 | } 111 | 112 | void indk::ComputeBackendDefault::doUnregisterHost() { 113 | } 114 | -------------------------------------------------------------------------------- /src/backends/multithread.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 23.02.23 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | indk::ComputeBackendMultithread::ComputeBackendMultithread(int WC) { 15 | WorkerCount = WC; 16 | while (Workers.size() < WorkerCount) { 17 | auto w = new indk::Worker; 18 | w -> thread = std::thread(tWorker, (void*)w); 19 | Workers.emplace_back(w); 20 | } 21 | } 22 | 23 | // TODO: handling of cases when computing of multiple neural networks goes in parallel 24 | void indk::ComputeBackendMultithread::doRegisterHost(const std::vector& objects) { 25 | int last = 0; 26 | for (const auto &o: objects) { 27 | if (last >= Workers.size()) last = 0; 28 | Workers[last] -> objects.push_back(o); 29 | Workers[last] -> event = new indk::Event; 30 | Workers[last] -> done = false; 31 | last++; 32 | } 33 | for (const auto &w: Workers) { 34 | w -> cv.notify_one(); 35 | } 36 | } 37 | 38 | void indk::ComputeBackendMultithread::doWaitTarget() { 39 | for (const auto &w: Workers) { 40 | while (!w->done.load()) { 41 | ((indk::Event*)w->event) -> doWaitTimed(100); 42 | } 43 | } 44 | } 45 | 46 | void indk::ComputeBackendMultithread::doProcess(void* object) { 47 | } 48 | 49 | [[noreturn]] void indk::ComputeBackendMultithread::tWorker(void* object) { 50 | auto worker = (indk::Worker*)object; 51 | 52 | while (true) { 53 | std::unique_lock lk(worker->m); 54 | worker -> cv.wait(lk); 55 | 56 | auto zPos = new indk::Position(0, 3); 57 | auto dRPos = new indk::Position(0, 3); 58 | auto nRPos = new indk::Position(0, 3); 59 | 60 | std::vector times; 61 | int tdone = 0; 62 | uint64_t size = worker -> objects.size(); 63 | while (tdone < size) { 64 | for (uint64_t n = 0; n < size; n++) { 65 | if (n >= times.size()) times.push_back(0); 66 | auto N = (indk::Neuron*)worker -> objects[n]; 67 | auto t = times[n]; 68 | auto ComputeSize = N -> getSignalBufferSize(); 69 | 70 | if (t >= ComputeSize) continue; 71 | 72 | if (N->getState(t) != indk::Neuron::States::Pending) { 73 | continue; 74 | } 75 | 76 | float FiSum, D, P = 0; 77 | auto Xm = N -> getXm(); 78 | auto DimensionsCount = N -> getDimensionsCount(); 79 | 80 | zPos -> setXm(Xm); 81 | zPos -> setDimensionsCount(DimensionsCount); 82 | dRPos -> setXm(Xm); 83 | dRPos -> setDimensionsCount(DimensionsCount); 84 | nRPos -> setXm(Xm); 85 | nRPos -> setDimensionsCount(DimensionsCount); 86 | 87 | indk::Position *RPos; 88 | 89 | for (int j = 0; j < N->getEntriesCount(); j++) { 90 | auto E = N -> getEntry(j); 91 | E -> doProcess(); 92 | } 93 | 94 | for (int i = 0; i < N->getReceptorsCount(); i++) { 95 | auto R = N->getReceptor(i); 96 | if (!R->isLocked()) RPos = R -> getPos(); 97 | else RPos = R -> getPosf(); 98 | indk::Position *SPos; 99 | std::pair FiValues; 100 | FiSum = 0; 101 | dRPos -> doZeroPosition(); 102 | 103 | for (int j = 0; j < N->getEntriesCount(); j++) { 104 | auto E = N -> getEntry(j); 105 | 106 | for (unsigned int k = 0; k < E->getSynapsesCount(); k++) { 107 | auto *S = E -> getSynapse(k); 108 | SPos = S -> getPos(); 109 | D = SPos -> getDistanceFrom(RPos); 110 | FiValues = indk::Computer::getFiFunctionValue(S->getLambda(), S->getGamma(), S->getdGamma(), D); 111 | if (FiValues.second > 0) { 112 | indk::Computer::getNewPosition(nRPos, RPos, SPos, indk::Computer::getFiVectorLength(FiValues.second), D); 113 | dRPos -> doAdd(nRPos); 114 | } 115 | FiSum += FiValues.first; 116 | } 117 | } 118 | 119 | R -> setFi(FiSum); 120 | R -> doUpdatePos(dRPos); 121 | P += indk::Computer::getReceptorInfluenceValue(R->doCheckActive(), R->getdFi(), dRPos, zPos); 122 | R -> doUpdateSensitivityValue(); 123 | } 124 | P /= (float)N->getReceptorsCount(); 125 | 126 | N -> doFinalizeInput(P); 127 | t++; 128 | times[n] = t; 129 | if (t >= ComputeSize) { 130 | tdone++; 131 | } 132 | } 133 | size = worker -> objects.size(); 134 | } 135 | 136 | delete zPos; 137 | delete nRPos; 138 | delete dRPos; 139 | 140 | worker -> done.store(true); 141 | ((indk::Event*)worker->event) -> doNotifyOne(); 142 | } 143 | } 144 | 145 | void indk::ComputeBackendMultithread::doUnregisterHost() { 146 | for (const auto &w: Workers) { 147 | w -> objects.clear(); 148 | delete (indk::Event*)w->event; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/computer.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 17.10.22 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | 13 | indk::Computer::Computer() { 14 | 15 | } 16 | 17 | std::vector indk::Computer::doCompareCPFunction(std::vector CP, std::vector CPf) { 18 | std::vector R; 19 | int64_t L = CP.size(); 20 | if (CPf.size() < L) L = CPf.size(); 21 | R.reserve(L); 22 | for (long long i = 0; i < L; i++) R.push_back(indk::Position::getDistance(CP[i], CPf[i])); 23 | return R; 24 | } 25 | 26 | float indk::Computer::doCompareCPFunctionD(std::vector CP, std::vector CPf) { 27 | float R = 0; 28 | int64_t L = CP.size(); 29 | if (CPf.size() < L) L = CPf.size(); 30 | for (int i = 0; i < L; i++) R += indk::Position::getDistance(CP[i], CPf[i]); 31 | return R; 32 | } 33 | 34 | float indk::Computer::doCompareFunction(indk::Position *R, indk::Position *Rf) { 35 | return indk::Position::getDistance(R, Rf); 36 | } 37 | 38 | float indk::Computer::getGammaFunctionValue(float oG, float k1, float k2, float Xt) { 39 | float nGamma; 40 | nGamma = oG + (k1*Xt-oG/k2); 41 | return nGamma; 42 | } 43 | 44 | std::pair indk::Computer::getFiFunctionValue(float Lambda, float Gamma, float dGamma, float D) { 45 | float E = Lambda * std::exp(-Lambda*D); 46 | return std::make_pair(Gamma*E, dGamma*E); 47 | } 48 | 49 | float indk::Computer::getReceptorInfluenceValue(bool Active, float dFi, indk::Position *dPos, indk::Position *RPr) { 50 | float Yn = 0; 51 | auto d = dPos -> getDistanceFrom(RPr); 52 | if (d > 0 && Active) Yn = d; // TODO: research output variants 53 | return Yn; 54 | } 55 | 56 | float indk::Computer::getRcValue(float k3, float Rs, float Fi, float dFi) { 57 | if (Fi >= Rs && dFi > 0) Rs += dFi / k3; 58 | else Rs = Rs / (k3*Rs+1); 59 | return Rs; 60 | } 61 | 62 | void indk::Computer::getNewPosition(indk::Position *nRPos, indk::Position *R, indk::Position *S, float FiL, float D) { 63 | nRPos -> setPosition(R); 64 | nRPos -> doSubtract(S); 65 | nRPos -> doDivide(D); 66 | nRPos -> doMultiply(FiL); 67 | } 68 | 69 | float indk::Computer::getLambdaValue(unsigned int Xm) { 70 | return pow(10, -(log(Xm)/log(2)-6)); 71 | } 72 | 73 | float indk::Computer::getFiVectorLength(float dFi) { 74 | return sqrt(dFi); 75 | } 76 | 77 | float indk::Computer::getSynapticSensitivityValue(unsigned int W, unsigned int OW) { 78 | float S = float(W) / OW; 79 | return 39.682*S*S*S*S - 170.22*S*S*S + 267.81*S*S - 178.8*S + 43.072; 80 | } 81 | -------------------------------------------------------------------------------- /src/error.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: error.cpp 3 | // Purpose: Exception system class 4 | // Author: Nickolay Babbysh 5 | // Created: 07.05.2019 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | 12 | indk::Error::Error() { 13 | ET = 0; 14 | } 15 | 16 | indk::Error::Error(ExceptionType _ET) { 17 | ET = _ET; 18 | } 19 | 20 | indk::Error::Error(ExceptionType _ET, std::vector _ED) { 21 | ET = _ET; 22 | ED = std::move(_ED); 23 | } 24 | 25 | const char* indk::Error::what() const noexcept { 26 | std::string Msg; 27 | char *B; 28 | switch (ET) { 29 | case EX_NEURALNET_NEURONS: 30 | Msg = std::string("EX_NEURALNET_NEURONS ~ Out of neuron list"); 31 | break; 32 | case EX_NEURALNET_INPUT: 33 | Msg = std::string("EX_NEURALNET_INPUT ~ The number of input signals does not match the neural net entries count"); 34 | break; 35 | case EX_NEURALNET_ENTRIES: 36 | Msg = std::string("EX_NEURALNET_ENTRIES ~ Out of neural net entries list"); 37 | break; 38 | case EX_NEURALNET_NEURON_ENTRIES: 39 | Msg = std::string("EX_NEURALNET_NEURON_ENTRIES ~ The number of links more than the neuron entries count"); 40 | break; 41 | case EX_NEURALNET_LINKTYPE: 42 | Msg = std::string("EX_NEURALNET_LINKTYPE ~ Unknown link type"); 43 | break; 44 | case EX_NEURON_INPUT: 45 | Msg = std::string("EX_NEURON_INPUT ~ The number of input signals does not match the neuron entries count"); 46 | break; 47 | case EX_NEURON_ENTRIES: 48 | Msg = std::string("EX_NEURON_ENTRIES ~ Out of entry list"); 49 | break; 50 | case EX_NEURON_RECEPTORS: 51 | Msg = std::string("EX_NEURON_RECEPTORS ~ Out of receptor list"); 52 | break; 53 | case EX_POSITION_OUT_RANGES: 54 | Msg = std::string("EX_POSITION_OUT_RANGES ~ Coordinates out of range ("+std::to_string(ED[0])+" < 0 || "+std::to_string(ED[0])+" > "+std::to_string(ED[1])+")"); 55 | break; 56 | case EX_POSITION_RANGES: 57 | Msg = std::string("EX_POSITION_RANGES ~ Not equal coordinates ranges"); 58 | break; 59 | case EX_POSITION_DIMENSIONS: 60 | Msg = std::string("EX_POSITION_DIMENSIONS ~ Not equal space dimensions of positions"); 61 | break; 62 | default: 63 | Msg = std::string("No exception"); 64 | } 65 | auto *S = new char[Msg.size()]; 66 | sprintf(S, "%s", Msg.c_str()); 67 | return S; 68 | } 69 | -------------------------------------------------------------------------------- /src/interlink.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: interlink.cpp 3 | // Purpose: Interlink service class 4 | // Author: Nickolay Babbysh 5 | // Created: 19.07.2023 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | typedef nlohmann::json json; 17 | 18 | indk::Interlink::Interlink() { 19 | Input = nullptr; 20 | Output = nullptr; 21 | Interlinked.store(false); 22 | doInitInput(4408, 5); 23 | } 24 | 25 | indk::Interlink::Interlink(int port, int timeout) { 26 | Input = nullptr; 27 | Output = nullptr; 28 | Interlinked.store(false); 29 | doInitInput(port, timeout); 30 | } 31 | 32 | void indk::Interlink::doInitInput(int port, int _timeout) { 33 | Input = new httplib::Server(); 34 | auto input = (httplib::Server*)Input; 35 | 36 | input -> set_idle_interval(60, 0); 37 | input -> set_read_timeout(60, 0); 38 | input -> set_write_timeout(60, 0); 39 | input -> set_keep_alive_timeout(60); 40 | 41 | input -> Post("/io_init", [&](const httplib::Request &req, httplib::Response &res, const httplib::ContentReader &content_reader) { 42 | Host = req.remote_addr; 43 | std::string body; 44 | 45 | content_reader([&](const char *data, size_t data_length) { 46 | body.append(data, data_length); 47 | return true; 48 | }); 49 | 50 | auto j = json::parse(body); 51 | OutputPort = j["port"].get(); 52 | Structure = j["structure"].get(); 53 | 54 | doInitOutput(); 55 | }); 56 | 57 | input -> Post("/io_ping", [&](const httplib::Request &req, httplib::Response &res) { 58 | }); 59 | 60 | input -> Post("/io_model_write_structure", [&](const httplib::Request &req, httplib::Response &res) { 61 | Structure = ""; 62 | res.set_content_provider( 63 | "text/plain", 64 | [&](size_t offset, httplib::DataSink &sink) { 65 | sink.write(Structure.c_str(), Structure.size()); 66 | sink.done(); 67 | return true; 68 | }); 69 | }); 70 | 71 | Thread = std::thread([this, port, input]() { 72 | input -> listen("0.0.0.0", port); 73 | }); 74 | 75 | int timeout = 0; 76 | while (!Interlinked.load() && timeout < _timeout) { 77 | sleep(1); 78 | timeout++; 79 | if (indk::System::getVerbosityLevel() > 1) 80 | std::cout << "Interlink connection timeout " << timeout << std::endl; 81 | } 82 | } 83 | 84 | void indk::Interlink::doInitOutput() { 85 | if (indk::System::getVerbosityLevel() > 0) 86 | std::cout << "Incoming Interlink connection from " << Host+":"+OutputPort << std::endl; 87 | Output = new httplib::Client(Host+":"+OutputPort); 88 | auto output = (httplib::Client*)Output; 89 | output -> set_read_timeout(5, 0); 90 | output -> set_write_timeout(5, 0); 91 | output -> set_connection_timeout(0, 500000); 92 | Interlinked.store(true); 93 | } 94 | 95 | void indk::Interlink::doSend(const std::string& command, const std::string& data) { 96 | if (!Interlinked.load() || !Output) return; 97 | auto output = (httplib::Client*)Output; 98 | auto res = output -> Post("/"+command, data.size(), 99 | [data](size_t offset, size_t length, httplib::DataSink &sink) { 100 | sink.write(data.c_str()+offset, length); 101 | return true; 102 | }, 103 | "text/plain"); 104 | 105 | // std::cout << "Sent command " << command << std::endl; 106 | //std::this_thread::sleep_for(std::chrono::milliseconds(10)); 107 | if (!res || res && res->status != 200) { 108 | std::cout << "Send failed " << command << std::endl; 109 | Interlinked.store(false); 110 | } 111 | } 112 | 113 | void indk::Interlink::doUpdateStructure(const std::string &data) { 114 | doSend("io_app_update_structure", data); 115 | } 116 | 117 | void indk::Interlink::doUpdateModelData(const std::string &data) { 118 | doSend("io_app_update_model_data", data); 119 | } 120 | 121 | void indk::Interlink::doUpdateMetrics(const std::string &data) { 122 | doSend("io_app_update_metrics", data); 123 | } 124 | 125 | void indk::Interlink::setStructure(const std::string &data) { 126 | doSend("io_app_write_structure", data); 127 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 128 | } 129 | 130 | std::string indk::Interlink::getStructure() { 131 | return Structure; 132 | } 133 | 134 | bool indk::Interlink::isInterlinked() { 135 | return Interlinked.load(); 136 | } 137 | 138 | indk::Interlink::~Interlink() { 139 | Thread.detach(); 140 | delete (httplib::Server*)Input; 141 | delete (httplib::Client*)Output; 142 | } 143 | -------------------------------------------------------------------------------- /src/neuron/entry.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: neuron/entry.cpp 3 | // Purpose: Neuron entry class 4 | // Author: Nickolay Babbysh 5 | // Created: 29.04.2019 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | 13 | indk::Neuron::Entry::Entry() { 14 | t = 0; 15 | tm = -1; 16 | SignalPointer = 0; 17 | Signal = new float; 18 | SignalSize = 1; 19 | } 20 | 21 | indk::Neuron::Entry::Entry(const Entry &E) { 22 | for (int64_t i = 0; i < E.getSynapsesCount(); i++) { 23 | auto *S = new Synapse(*E.getSynapse(i)); 24 | Synapses.push_back(S); 25 | } 26 | t = 0; 27 | tm = -1; 28 | Signal = new float; 29 | SignalSize = 1; 30 | SignalPointer = 0; 31 | } 32 | 33 | bool indk::Neuron::Entry::doCheckState(int64_t tn) const { 34 | // std::cout << "check state " << tm << " " << tn << std::endl; 35 | return tm >= tn; 36 | } 37 | 38 | void indk::Neuron::Entry::doAddSynapse(indk::Position *SPos, unsigned int Xm, float k1, int64_t Tl, int NT) { 39 | auto *S = new Synapse(SPos, k1, indk::Computer::getLambdaValue(Xm), Tl, NT); 40 | Synapses.push_back(S); 41 | } 42 | 43 | void indk::Neuron::Entry::doIn(float Xt, int64_t tn) { 44 | if (SignalPointer >= SignalSize) SignalPointer = 0; 45 | Signal[SignalPointer] = Xt; 46 | SignalPointer++; 47 | tm = tn; 48 | // std::cout << "In entry " << tm << " " << t << " " << SignalPointer << " " << Signal[SignalPointer-1] << " " << Xt << std::endl; 49 | } 50 | 51 | void indk::Neuron::Entry::doProcess() { 52 | auto d = tm - t + 1; 53 | // std::cout << "Entry " << tm << " " << t << " " << d << " " << SignalPointer-d << " " << Signal[SignalPointer-d] << std::endl; 54 | if (d > 0 && SignalPointer-d >= 0) { 55 | // std::cout << SignalPointer-d << std::endl; 56 | for (auto S: Synapses) { 57 | if (t >= S->getTl()) S -> doIn(Signal[SignalPointer-d]); 58 | else S -> doIn(0); 59 | } 60 | t++; 61 | } 62 | } 63 | 64 | void indk::Neuron::Entry::doPrepare() { 65 | t = 0; 66 | tm = -1; 67 | for (auto S: Synapses) S -> doReset(); 68 | //for (auto S: Synapses) S -> doPrepare(); 69 | } 70 | 71 | void indk::Neuron::Entry::doFinalize() { 72 | //for (auto Sig: Signal) 73 | //for (auto S: Synapses) S -> doIn(Sig); 74 | for (auto S: Synapses) S -> doReset(); 75 | //Signal.clear(); 76 | } 77 | 78 | void indk::Neuron::Entry::doReserveSignalBuffer(uint64_t L) { 79 | delete [] Signal; 80 | Signal = new float[L]; 81 | SignalSize = L; 82 | SignalPointer = 0; 83 | } 84 | 85 | void indk::Neuron::Entry::doRollback() { 86 | for (auto S: Synapses) S -> doRollback(); 87 | } 88 | 89 | void indk::Neuron::Entry::setLambda(float _l) { 90 | for (auto S: Synapses) S ->setLambda(_l); 91 | } 92 | 93 | void indk::Neuron::Entry::setk1(float _k1) { 94 | for (auto S: Synapses) S -> setk1(_k1); 95 | } 96 | 97 | void indk::Neuron::Entry::setk2(float _k2) { 98 | for (auto S: Synapses) S -> setk2(_k2); 99 | } 100 | 101 | indk::Neuron::Synapse* indk::Neuron::Entry::getSynapse(int64_t SID) const { 102 | return Synapses[SID]; 103 | } 104 | 105 | int64_t indk::Neuron::Entry::getSynapsesCount() const { 106 | return Synapses.size(); 107 | } 108 | 109 | float indk::Neuron::Entry::getIn() { 110 | auto d = tm - t + 1; 111 | if (d > 0 && SignalPointer-d >= 0) { 112 | t++; 113 | return Signal[SignalPointer-d]; 114 | } 115 | return 0; 116 | } 117 | 118 | indk::Neuron::Entry::~Entry() { 119 | for (auto S: Synapses) delete S; 120 | } 121 | -------------------------------------------------------------------------------- /src/neuron/receptor.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: neuron/receptor.cpp 3 | // Purpose: Neuron receptor class 4 | // Author: Nickolay Babbysh 5 | // Created: 29.04.2019 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | 13 | indk::Neuron::Receptor::Receptor() { 14 | DefaultPos = new indk::Position(); 15 | PhantomPos = new indk::Position(); 16 | k3 = 0; 17 | Rs = 0.01; 18 | Locked = false; 19 | L = 0; 20 | Lf = 0; 21 | Fi = 0; 22 | dFi = 0; 23 | doCreateNewScope(); 24 | } 25 | 26 | indk::Neuron::Receptor::Receptor(const Receptor &R) { 27 | CP = R.getCP(); 28 | CPf = R.getCPf(); 29 | DefaultPos = new indk::Position(*R.getPos0()); 30 | PhantomPos = new indk::Position(*R.getPosf()); 31 | k3 = R.getk3(); 32 | Rs = R.getSensitivityValue(); 33 | Locked = R.isLocked(); 34 | L = R.getL(); 35 | Lf = R.getLf(); 36 | Fi = 0; 37 | dFi = 0; 38 | doCreateNewScope(); 39 | } 40 | 41 | indk::Neuron::Receptor::Receptor(indk::Position *_RPos, float _k3) { 42 | DefaultPos = new indk::Position(*_RPos); 43 | PhantomPos = new indk::Position(*_RPos); 44 | k3 = _k3; 45 | Rs = 0.01; 46 | Locked = false; 47 | L = 0; 48 | Lf = 0; 49 | Fi = 0; 50 | dFi = 0; 51 | doCreateNewScope(); 52 | } 53 | 54 | bool indk::Neuron::Receptor::doCheckActive() const { 55 | return Fi >= Rs; 56 | } 57 | 58 | void indk::Neuron::Receptor::doLock() { 59 | Locked = true; 60 | } 61 | 62 | void indk::Neuron::Receptor::doUnlock() { 63 | Locked = false; 64 | } 65 | 66 | void indk::Neuron::Receptor::doCreateNewScope() { 67 | auto pos = new indk::Position(*DefaultPos); 68 | Scope = ReferencePos.size(); 69 | ReferencePos.push_back(pos); 70 | } 71 | 72 | void indk::Neuron::Receptor::doChangeScope(uint64_t scope) { 73 | if (scope > ReferencePos.size()) { 74 | Scope = ReferencePos.size() - 1; 75 | return; 76 | } 77 | Scope = scope; 78 | } 79 | 80 | void indk::Neuron::Receptor::doReset() { 81 | CPf.clear(); 82 | Rs = 0.01; 83 | Lf = 0; 84 | Fi = 0; 85 | dFi = 0; 86 | ReferencePos.clear(); 87 | PhantomPos -> setPosition(DefaultPos); 88 | Locked = false; 89 | } 90 | 91 | void indk::Neuron::Receptor::doPrepare() { 92 | CPf.clear(); 93 | Rs = 0.01; 94 | Lf = 0; 95 | Fi = 0; 96 | dFi = 0; 97 | if (Locked) PhantomPos -> setPosition(DefaultPos); 98 | else ReferencePos[Scope] -> setPosition(DefaultPos); 99 | } 100 | 101 | void indk::Neuron::Receptor::doSavePos() { 102 | if (Locked) CPf.push_back(new indk::Position(*PhantomPos)); 103 | else CP.push_back(new indk::Position(*ReferencePos[Scope])); 104 | } 105 | 106 | void indk::Neuron::Receptor::doUpdateSensitivityValue() { 107 | Rs = indk::Computer::getRcValue(k3, Rs, Fi, dFi); 108 | } 109 | 110 | void indk::Neuron::Receptor::doUpdatePos(indk::Position *_RPos) { 111 | if (Locked) { 112 | Lf += indk::Position::getDistance(PhantomPos, _RPos); 113 | PhantomPos -> doAdd(_RPos); 114 | } else { 115 | L += indk::Position::getDistance(ReferencePos[Scope], _RPos); 116 | ReferencePos[Scope] -> doAdd(_RPos); 117 | } 118 | } 119 | 120 | void indk::Neuron::Receptor::setPos(indk::Position *_RPos) { 121 | if (Locked) { 122 | PhantomPos -> setPosition(_RPos); 123 | } else { 124 | ReferencePos[Scope] -> setPosition(_RPos); 125 | } 126 | } 127 | 128 | void indk::Neuron::Receptor::setRs(float _Rs) { 129 | Rs = _Rs; 130 | } 131 | 132 | void indk::Neuron::Receptor::setk3(float _k3) { 133 | k3 = _k3; 134 | } 135 | 136 | void indk::Neuron::Receptor::setFi(float _Fi) { 137 | dFi = _Fi - Fi; 138 | Fi = _Fi; 139 | } 140 | 141 | std::vector indk::Neuron::Receptor::getCP() const { 142 | return CP; 143 | } 144 | 145 | std::vector indk::Neuron::Receptor::getCPf() const { 146 | return CPf; 147 | } 148 | 149 | indk::Position* indk::Neuron::Receptor::getPos() const { 150 | return ReferencePos[Scope]; 151 | } 152 | 153 | indk::Position* indk::Neuron::Receptor::getPos0() const { 154 | return DefaultPos; 155 | } 156 | 157 | indk::Position* indk::Neuron::Receptor::getPosf() const { 158 | return PhantomPos; 159 | } 160 | 161 | std::vector indk::Neuron::Receptor::getReferencePosScopes() { 162 | return ReferencePos; 163 | } 164 | 165 | float indk::Neuron::Receptor::getRs() const { 166 | return Rs; 167 | } 168 | 169 | float indk::Neuron::Receptor::getk3() const { 170 | return k3; 171 | } 172 | 173 | float indk::Neuron::Receptor::getFi() { 174 | return Fi; 175 | } 176 | 177 | float indk::Neuron::Receptor::getdFi() { 178 | return dFi; 179 | } 180 | 181 | float indk::Neuron::Receptor::getSensitivityValue() const { 182 | return Rs; 183 | } 184 | 185 | bool indk::Neuron::Receptor::isLocked() const { 186 | return Locked; 187 | } 188 | 189 | float indk::Neuron::Receptor::getL() const { 190 | return L; 191 | } 192 | 193 | float indk::Neuron::Receptor::getLf() const { 194 | return Lf; 195 | } 196 | -------------------------------------------------------------------------------- /src/neuron/synapse.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: neuron/synaps.cpp 3 | // Purpose: Neuron synaps class 4 | // Author: Nickolay Babbysh 5 | // Created: 29.04.2019 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | 13 | indk::Neuron::Synapse::Synapse() { 14 | SPos = new indk::Position(); 15 | ok1 = 0; 16 | ok2 = 0; 17 | k1 = 0; 18 | k2 = 0; 19 | Lambda = 0; 20 | Tl = 0; 21 | Gamma = 0; 22 | dGamma = 0; 23 | QCounter = -1; 24 | QSize = 0; 25 | } 26 | 27 | indk::Neuron::Synapse::Synapse(const Synapse &S) { 28 | SPos = S.getPos(); 29 | ok1 = S.getk1(); 30 | ok2 = S.getk2(); 31 | k1 = ok1; 32 | k2 = ok2; 33 | Lambda = S.getLambda(); 34 | Tl = S.getTl(); 35 | Gamma = S.getGamma(); 36 | dGamma = S.getdGamma(); 37 | QCounter = -1; 38 | QSize = 0; 39 | } 40 | 41 | indk::Neuron::Synapse::Synapse(indk::Position *_SPos, float _k1, float _Lambda, int64_t _Tl, int NT) { 42 | SPos = _SPos; 43 | ok1 = _k1; 44 | ok2 = ok1 * 1000; 45 | k1 = ok1; 46 | k2 = ok2; 47 | Lambda = _Lambda; 48 | Tl = _Tl; 49 | Gamma = 0; 50 | dGamma = 0; 51 | QCounter = -1; 52 | QSize = 0; 53 | NeurotransmitterType = NT; 54 | } 55 | 56 | void indk::Neuron::Synapse::doIn(float X) { 57 | float nGamma = indk::Computer::getGammaFunctionValue(Gamma, k1, k2, X); 58 | lGamma = Gamma; 59 | ldGamma = dGamma; 60 | dGamma = nGamma - Gamma; 61 | Gamma = nGamma; 62 | } 63 | 64 | void indk::Neuron::Synapse::doSendToQueue(float X, float WVSum) { 65 | // float GammaLast = !QSize ? 0 : GammaQ[QSize-1]; 66 | // float nGamma = ComputeBackend->getGammaFunctionValue(GammaLast, k1, k2, X); 67 | // GammaQ[QSize] = nGamma; 68 | // QSize++; 69 | } 70 | 71 | bool indk::Neuron::Synapse::doInFromQueue(int64_t tT) { 72 | if (tT >= QSize) return false; 73 | if (tT == QCounter) return true; 74 | float nGamma = GammaQ[tT]; 75 | dGamma = nGamma - Gamma; 76 | Gamma = nGamma; 77 | QCounter = tT; 78 | return true; 79 | } 80 | 81 | void indk::Neuron::Synapse::doPrepare() { 82 | ok1 = k1; 83 | ok2 = k2; 84 | } 85 | 86 | void indk::Neuron::Synapse::doReset() { 87 | Gamma = 0; 88 | dGamma = 0; 89 | QCounter = 0; 90 | QSize = 0; 91 | GammaQ.clear(); 92 | // k1 = ok1; 93 | // k2 = ok2; 94 | } 95 | 96 | void indk::Neuron::Synapse::doRollback() { 97 | dGamma = ldGamma; 98 | Gamma = lGamma; 99 | } 100 | 101 | void indk::Neuron::Synapse::setGamma(float gamma) { 102 | dGamma = gamma - Gamma; 103 | Gamma = gamma; 104 | } 105 | 106 | void indk::Neuron::Synapse::setk1(float _k1) { 107 | k1 = _k1; 108 | } 109 | 110 | void indk::Neuron::Synapse::setk2(float _k2) { 111 | k2 = _k2; 112 | } 113 | 114 | void indk::Neuron::Synapse::setLambda(float L) { 115 | Lambda = L; 116 | } 117 | 118 | indk::Position* indk::Neuron::Synapse::getPos() const { 119 | return SPos; 120 | } 121 | 122 | float indk::Neuron::Synapse::getk1() const { 123 | return k1; 124 | } 125 | 126 | float indk::Neuron::Synapse::getk2() const { 127 | return k2; 128 | } 129 | 130 | float indk::Neuron::Synapse::getLambda() const { 131 | return Lambda; 132 | } 133 | 134 | int64_t indk::Neuron::Synapse::getTl() const { 135 | return Tl; 136 | } 137 | 138 | float indk::Neuron::Synapse::getGamma() const { 139 | return Gamma; 140 | } 141 | 142 | float indk::Neuron::Synapse::getdGamma() const { 143 | return dGamma; 144 | } 145 | 146 | int64_t indk::Neuron::Synapse::getQSize() { 147 | return QSize; 148 | } 149 | 150 | int indk::Neuron::Synapse::getNeurotransmitterType() const { 151 | return NeurotransmitterType; 152 | } 153 | -------------------------------------------------------------------------------- /src/position.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: position.cpp 3 | // Purpose: Multidimensional position class 4 | // Author: Nickolay Babbysh 5 | // Created: 07.05.2019 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | indk::Position::Position() { 16 | Xm = 0; 17 | DimensionsCount = 0; 18 | X = nullptr; 19 | } 20 | 21 | indk::Position::Position(const indk::Position &P) { 22 | Xm = P.getXm(); 23 | DimensionsCount = P.getDimensionsCount(); 24 | X = new float[DimensionsCount]; 25 | for (unsigned int i = 0; i < DimensionsCount; i++) X[i] = P.getPositionValue(i); 26 | } 27 | 28 | indk::Position::Position(unsigned int _Xm, unsigned int _DimensionsCount) { 29 | Xm = _Xm; 30 | DimensionsCount = _DimensionsCount; 31 | X = new float[DimensionsCount]; 32 | for (unsigned int i = 0; i < DimensionsCount; i++) X[i] = 0; 33 | } 34 | 35 | indk::Position::Position(unsigned int _Xm, std::vector _X) { 36 | Xm = _Xm; 37 | DimensionsCount = (unsigned)_X.size(); 38 | X = new float[DimensionsCount]; 39 | for (int i = 0; i < DimensionsCount; i++) { 40 | if (_X[i] < 0 || _X[i] > Xm) { 41 | throw indk::Error(indk::Error::EX_POSITION_OUT_RANGES, {_X[i], (float)Xm}); 42 | } 43 | X[i] = _X[i]; 44 | } 45 | } 46 | 47 | void indk::Position::doAdd(const indk::Position *P) { 48 | if (DimensionsCount != P->getDimensionsCount()) { 49 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 50 | } 51 | if (Xm != P->getXm()) { 52 | throw indk::Error(indk::Error::EX_POSITION_RANGES); 53 | } 54 | for (unsigned int i = 0; i < DimensionsCount; i++) { 55 | X[i] += P->getPositionValue(i); 56 | if (X[i] > Xm) { 57 | throw indk::Error(indk::Error::EX_POSITION_OUT_RANGES, {X[i], (float)Xm}); 58 | } 59 | } 60 | } 61 | 62 | void indk::Position::doSubtract(const indk::Position *P) { 63 | if (DimensionsCount != P->getDimensionsCount()) { 64 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 65 | } 66 | if (Xm != P->getXm()) { 67 | throw indk::Error(indk::Error::EX_POSITION_RANGES); 68 | } 69 | for (unsigned int i = 0; i < DimensionsCount; i++) { 70 | X[i] = (X[i]-P->getPositionValue(i)); 71 | if (X[i] > Xm) { 72 | throw indk::Error(indk::Error::EX_POSITION_OUT_RANGES, {X[i], (float)Xm}); 73 | } 74 | } 75 | } 76 | 77 | void indk::Position::doDivide(float D) { 78 | for (unsigned int i = 0; i < DimensionsCount; i++) { 79 | X[i] /= D; 80 | } 81 | } 82 | 83 | void indk::Position::doMultiply(float M) { 84 | for (unsigned int i = 0; i < DimensionsCount; i++) { 85 | X[i] *= M; 86 | if (X[i] > Xm) { 87 | throw indk::Error(indk::Error::EX_POSITION_OUT_RANGES, {X[i], (float)Xm}); 88 | } 89 | } 90 | } 91 | 92 | void indk::Position::doZeroPosition() { 93 | for (unsigned int i = 0; i < DimensionsCount; i++) X[i] = 0; 94 | } 95 | 96 | void indk::Position::setPosition(const indk::Position &P) { 97 | if (P.getDimensionsCount() < DimensionsCount) { 98 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 99 | } 100 | for (unsigned int i = 0; i < DimensionsCount; i++) X[i] = P.getPositionValue(i); 101 | } 102 | 103 | void indk::Position::setPosition(const indk::Position *P) { 104 | if (P->getDimensionsCount() < DimensionsCount) { 105 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 106 | } 107 | for (unsigned int i = 0; i < DimensionsCount; i++) X[i] = P -> getPositionValue(i); 108 | } 109 | 110 | void indk::Position::setPosition(std::vector _X) { 111 | if (_X.size() != DimensionsCount) { 112 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 113 | } 114 | for (int i = 0; i < DimensionsCount; i++) { 115 | if (_X[i] < 0 || _X[i] > Xm) { 116 | throw indk::Error(indk::Error::EX_POSITION_OUT_RANGES, {_X[i], (float)Xm}); 117 | } 118 | X[i] = _X[i]; 119 | } 120 | } 121 | 122 | void indk::Position::setXm(unsigned int _Xm) { 123 | Xm = _Xm; 124 | } 125 | 126 | void indk::Position::setDimensionsCount(unsigned int _DimensionsCount) { 127 | DimensionsCount = _DimensionsCount; 128 | } 129 | 130 | unsigned int indk::Position::getDimensionsCount() const { 131 | return DimensionsCount; 132 | } 133 | 134 | unsigned int indk::Position::getXm() const { 135 | return Xm; 136 | } 137 | 138 | float indk::Position::getPositionValue(unsigned int DNum) const { 139 | if (DNum >= DimensionsCount) return -1; 140 | return X[DNum]; 141 | } 142 | 143 | float indk::Position::getDistanceFrom(const indk::Position *P) { 144 | if (DimensionsCount != P->getDimensionsCount()) { 145 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 146 | } 147 | float D = 0, CValue; 148 | for (unsigned int i = 0; i < DimensionsCount; i++) { 149 | CValue = X[i] - P -> getPositionValue(i); 150 | D += CValue * CValue; 151 | } 152 | return sqrt(D); 153 | } 154 | 155 | indk::Position::~Position() { 156 | delete [] X; 157 | } 158 | 159 | indk::Position& indk::Position::operator=(const indk::Position &P) { 160 | if (this == &P) return *this; 161 | delete X; 162 | Xm = P.getXm(); 163 | DimensionsCount = P.getDimensionsCount(); 164 | X = new float[DimensionsCount]; 165 | for (unsigned int i = 0; i < DimensionsCount; i++) X[i] = P.getPositionValue(i); 166 | return *this; 167 | } 168 | 169 | indk::Position indk::operator+(const indk::Position &L, const indk::Position &R) { 170 | if (L.getDimensionsCount() != R.getDimensionsCount()) { 171 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 172 | } 173 | if (L.getXm() != R.getXm()) { 174 | throw indk::Error(indk::Error::EX_POSITION_RANGES); 175 | } 176 | std::vector PV; 177 | for (unsigned int i = 0; i < L.getDimensionsCount(); i++) { 178 | PV.push_back(L.getPositionValue(i)+R.getPositionValue(i)); 179 | } 180 | return indk::Position(L.getXm(), PV); 181 | } 182 | 183 | indk::Position indk::operator-(const indk::Position &L, const indk::Position &R) { 184 | if (L.getDimensionsCount() != R.getDimensionsCount()) { 185 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 186 | } 187 | if (L.getXm() != R.getXm()) { 188 | throw indk::Error(indk::Error::EX_POSITION_RANGES); 189 | } 190 | std::vector PV; 191 | for (unsigned int i = 0; i < L.getDimensionsCount(); i++) { 192 | PV.push_back(fabs(L.getPositionValue(i)-R.getPositionValue(i))); 193 | } 194 | return indk::Position(L.getXm(), PV); 195 | } 196 | 197 | indk::Position indk::operator/(const indk::Position &P, float D) { 198 | std::vector PV; 199 | for (unsigned int i = 0; i < P.getDimensionsCount(); i++) { 200 | PV.push_back(P.getPositionValue(i)/D); 201 | } 202 | return indk::Position(P.getXm(), PV); 203 | } 204 | 205 | indk::Position indk::operator*(const indk::Position &P, float M) { 206 | std::vector PV; 207 | for (unsigned int i = 0; i < P.getDimensionsCount(); i++) { 208 | PV.push_back(P.getPositionValue(i)*M); 209 | } 210 | return indk::Position(P.getXm(), PV); 211 | } 212 | 213 | float indk::Position::getDistance(const indk::Position &L, const indk::Position &R) { 214 | if (L.getDimensionsCount() != R.getDimensionsCount()) { 215 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 216 | } 217 | float D = 0; 218 | for (unsigned int i = 0; i < L.getDimensionsCount(); i++) { 219 | D += (L.getPositionValue(i)-R.getPositionValue(i))*(L.getPositionValue(i)-R.getPositionValue(i)); 220 | } 221 | return std::sqrt(D); 222 | } 223 | 224 | indk::Position* indk::Position::getSum(const indk::Position *L, const indk::Position *R) { 225 | if (L->getDimensionsCount() != R->getDimensionsCount()) { 226 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 227 | } 228 | if (L->getXm() != R->getXm()) { 229 | throw indk::Error(indk::Error::EX_POSITION_RANGES); 230 | } 231 | std::vector PV; 232 | for (unsigned int i = 0; i < L->getDimensionsCount(); i++) { 233 | PV.push_back(L->getPositionValue(i)+R->getPositionValue(i)); 234 | } 235 | return new indk::Position(L->getXm(), PV); 236 | } 237 | 238 | indk::Position* indk::Position::getDiff(const indk::Position *L, const indk::Position *R) { 239 | if (L->getDimensionsCount() != R->getDimensionsCount()) { 240 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 241 | } 242 | if (L->getXm() != R->getXm()) { 243 | throw indk::Error(indk::Error::EX_POSITION_RANGES); 244 | } 245 | std::vector PV; 246 | for (unsigned int i = 0; i < L->getDimensionsCount(); i++) { 247 | PV.push_back(fabs(L->getPositionValue(i)-R->getPositionValue(i))); 248 | } 249 | return new indk::Position(L->getXm(), PV); 250 | } 251 | 252 | indk::Position* indk::Position::getQuotient(const indk::Position *P, float D) { 253 | std::vector PV; 254 | for (unsigned int i = 0; i < P->getDimensionsCount(); i++) { 255 | PV.push_back(P->getPositionValue(i)/D); 256 | } 257 | return new indk::Position(P->getXm(), PV); 258 | } 259 | 260 | indk::Position* indk::Position::getProduct(const indk::Position *P, float M) { 261 | std::vector PV; 262 | for (unsigned int i = 0; i < P->getDimensionsCount(); i++) { 263 | PV.push_back(P->getPositionValue(i)*M); 264 | } 265 | return new indk::Position(P->getXm(), PV); 266 | } 267 | 268 | float indk::Position::getDistance(const indk::Position *L, const indk::Position *R) { 269 | if (L->getDimensionsCount() != R->getDimensionsCount()) { 270 | throw indk::Error(indk::Error::EX_POSITION_DIMENSIONS); 271 | } 272 | float D = 0; 273 | for (unsigned int i = 0; i < L->getDimensionsCount(); i++) { 274 | D += (L->getPositionValue(i)-R->getPositionValue(i))*(L->getPositionValue(i)-R->getPositionValue(i)); 275 | } 276 | return std::sqrt(D); 277 | } 278 | -------------------------------------------------------------------------------- /src/profiler.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 23.08.23 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | 13 | std::multimap> Callbacks; 14 | 15 | void indk::Profiler::doAttachCallback(indk::NeuralNet *object, int flag, indk::ProfilerCallback callback) { 16 | Callbacks.insert(std::make_pair(object, std::make_pair(callback, flag))); 17 | } 18 | 19 | void indk::Profiler::doEmit(indk::NeuralNet *object, int flag) { 20 | auto callback = Callbacks.equal_range(object); 21 | for (auto it = callback.first; it != callback.second; it++) { 22 | if (it->second.second == flag) it->second.first(object); 23 | } 24 | } 25 | 26 | indk::Profiler::~Profiler() { 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/system.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // Name: 3 | // Purpose: 4 | // Author: Nickolay Babbysh 5 | // Created: 18.10.22 6 | // Copyright: (c) NickWare Group 7 | // Licence: MIT licence 8 | ///////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | int CurrentComputeBackend = -1, VerbosityLevel = 1; 16 | int ComputeBackendParameter = 0; 17 | bool SynchronizationNeeded; 18 | indk::Computer *ComputeBackend; 19 | 20 | void indk::System::setComputeBackend(int Backend, int Parameter) { 21 | if (CurrentComputeBackend != -1) delete ComputeBackend; 22 | CurrentComputeBackend = Backend; 23 | 24 | switch (CurrentComputeBackend) { 25 | case indk::System::ComputeBackends::Default: 26 | SynchronizationNeeded = false; 27 | ComputeBackend = new indk::ComputeBackendDefault(); 28 | Parameter = 1; 29 | break; 30 | case indk::System::ComputeBackends::Multithread: 31 | SynchronizationNeeded = true; 32 | ComputeBackend = new indk::ComputeBackendMultithread(Parameter>1?Parameter:indk_MULTITHREAD_DEFAULT_NUM); 33 | break; 34 | case indk::System::ComputeBackends::OpenCL: 35 | #ifdef INDK_OPENCL_SUPPORT 36 | SynchronizationNeeded = true; 37 | ComputeBackend = new indk::ComputeBackendOpenCL(); 38 | Parameter = 1; 39 | break; 40 | #else 41 | std::cerr << std::endl; 42 | std::cerr << "The OpenCL compute backend is not supported by the current build. Rebuild interfernce library with the INDK_OPENCL_SUPPORT=ON flag." << std::endl; 43 | CurrentComputeBackend = -1; 44 | return; 45 | #endif 46 | } 47 | ComputeBackendParameter = Parameter; 48 | } 49 | 50 | indk::Computer* indk::System::getComputeBackend() { 51 | return ComputeBackend; 52 | } 53 | 54 | int indk::System::getComputeBackendKind() { 55 | return CurrentComputeBackend; 56 | } 57 | 58 | bool indk::System::isSynchronizationNeeded() { 59 | return SynchronizationNeeded; 60 | } 61 | 62 | void indk::System::setVerbosityLevel(int VL) { 63 | VerbosityLevel = VL; 64 | } 65 | 66 | int indk::System::getVerbosityLevel() { 67 | return VerbosityLevel; 68 | } 69 | 70 | int indk::System::getComputeBackendParameter() { 71 | return ComputeBackendParameter; 72 | } 73 | 74 | bool indk::Event::doWaitTimed(int T) { 75 | auto rTimeout = std::chrono::milliseconds(T); 76 | bool bTimeout = false; 77 | bool bRet; 78 | std::unique_lock oNotifierLock(m_oMutex); 79 | while (!m_bEvent && !bTimeout) { 80 | bTimeout = std::cv_status::timeout == m_oConditionVariable.wait_for(oNotifierLock, rTimeout); 81 | } 82 | bRet = m_bEvent; 83 | m_bEvent = false; 84 | return bRet; 85 | } 86 | 87 | bool indk::Event::doWait() { 88 | bool bRet; 89 | std::unique_lock oNotifierLock(m_oMutex); 90 | m_oConditionVariable.wait(oNotifierLock); 91 | bRet = m_bEvent; 92 | m_bEvent = false; 93 | return bRet; 94 | } 95 | 96 | void indk::Event::doNotifyOne() { 97 | std::unique_lock oNotifierLock(m_oMutex); 98 | m_bEvent = true; 99 | m_oConditionVariable.notify_one(); 100 | } 101 | --------------------------------------------------------------------------------