/** * @file resource_from_weight.h * @brief Function for calculating resource from weight. * @kaspersky_support D. Postnikov * @date 03.02.2026 * @license Apache 2.0 * @copyright © 2026 AO Kaspersky Lab * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include /** * @brief Calculate synaptic resource value from weight parameters for resource-based plasticity. * * @details The transformation maps weight ranges to resource ranges while maintaining the inverse * relationship between weight and resource in resource-based models. * * The mathematical formula used is: * resource = (weight - min_weight) * (max_weight - min_weight) / ((max_weight - min_weight) - (weight - min_weight)) * * This ensures that: * - Minimum weights map to low resource values * - Maximum weights map to high resource values * * @param weight current synaptic weight value to convert. * @param min_weight minimum allowable weight value for the range. * @param max_weight maximum allowable weight value for the range. * * @return calculated resource value for the given weight. * * @throws `std::logic_error` if weight is outside valid range or parameters are invalid. */ inline float resource_from_weight(float weight, float min_weight, float max_weight) { // Max weight is only possible with infinite resource, so we should select a value less than that. float eps = 1e-6; if (min_weight > max_weight) std::swap(min_weight, max_weight); if (weight < min_weight || weight >= max_weight - eps) throw std::logic_error( std::string("Weight should not be less than") + std::to_string(min_weight) + ", more than" + std::to_string(max_weight) + "or too close to it. Weight = " + std::to_string(weight)); double diff = max_weight - min_weight; double over = weight - min_weight; return static_cast(over * diff / (diff - over)); }