\doxysection{gen\+\_\+optimizer.\+h}
\hypertarget{gen__optimizer_8h_source}{}\label{gen__optimizer_8h_source}\index{E:/.WORK/STM32/STM\_Libs/STM32\_ExtendedLibs/MyLibs/Inc/gen\_optimizer.h@{E:/.WORK/STM32/STM\_Libs/STM32\_ExtendedLibs/MyLibs/Inc/gen\_optimizer.h}}
\mbox{\hyperlink{gen__optimizer_8h}{Go to the documentation of this file.}}
\begin{DoxyCode}{0}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00001}00001\ \textcolor{comment}{/**}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00002}00002\ \textcolor{comment}{******************************************************************************}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00003}00003\ \textcolor{comment}{*\ @file\ gen\_optimizer.h}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00004}00004\ \textcolor{comment}{*\ @brief\ Заголовочный\ файл\ для\ адаптивного\ подбора\ параметров}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00005}00005\ \textcolor{comment}{******************************************************************************}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00006}00006\ \textcolor{comment}{*\ @addtogroup\ GEN\_OPTIMIZER\ \ \ \ Genetic\ optimizer}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00007}00007\ \textcolor{comment}{*\ @brief\ \ \ \ \ \ Библиотека\ для\ эволюционного\ подбора\ параметров}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00008}00008\ \textcolor{comment}{*\ @details}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00009}00009\ \textcolor{comment}{Поддерживает:}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00010}00010\ \textcolor{comment}{-\/\ Любое\ количество\ параметров}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00011}00011\ \textcolor{comment}{-\/\ Генерацию\ новых\ параметров\ на\ основе\ лучших\ кандидатов}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00012}00012\ \textcolor{comment}{-\/\ Мутацию\ для\ поиска\ оптимальных\ параметров}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00013}00013\ \textcolor{comment}{-\/\ Несколько\ независимых\ оптимизаторов\ в\ одной\ программе}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00014}00014\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00015}00015\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00016}00016\ \textcolor{comment}{Параметры\ для\ конфигурации:}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00017}00017\ \textcolor{comment}{-\/\ @ref\ GEN\_OPTIMIZATION\_ENABLE\ -\/\ Включить\ оптимизацию\ параметров}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00018}00018\ \textcolor{comment}{\ \ Если\ библиотека\ отключена\ @ref\ GEN\_OPTIMIZATION\_ENABLE,\ то\ вставляются\ }}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00019}00019\ \textcolor{comment}{\ \ заглушки,\ никак\ не\ влияющие\ на\ параметры\ и\ остальную\ программу}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00020}00020\ \textcolor{comment}{-\/\ @ref\ GEN\_MAX\_PARAMS\ \ \ \ \ \ \ \ \ \ -\/\ Максимальное\ количество\ параметров}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00021}00021\ \textcolor{comment}{-\/\ @ref\ GEN\_MAX\_CANDIDATES\ \ \ \ \ \ -\/\ Максимальное\ количество\ кандидатов\ для\ обучения}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00022}00022\ \textcolor{comment}{-\/\ (опционально)\ @ref\ GEN\_MUTATION\_MIN\_PCT\ \ \ \ \ \ \ -\/\ Минимальная\ мутация\ в\ процентах\ от\ Loss\ (по\ умолчанию\ 10\%)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00023}00023\ \textcolor{comment}{-\/\ (опционально)\ @ref\ GEN\_MUTATION\_MAX\_PCT\ \ \ \ \ \ \ -\/\ Максимальная\ мутация\ в\ процентах\ от\ Loss\ (по\ умолчанию\ 100\%)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00024}00024\ \textcolor{comment}{-\/\ (опционально)\ @ref\ ELOVLE\_N\_ELITE\_CANDIDATE\ \ \ -\/\ Количество\ кандидатов,\ которые\ проходят\ в\ поколение\ без\ изменений}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00025}00025\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00026}00026\ \textcolor{comment}{@par\ Пример\ использования:}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00027}00027\ \textcolor{comment}{@code}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00028}00028\ \textcolor{comment}{\#include\ "{}gen\_optimizer.h"{}}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00029}00029\ \textcolor{comment}{\#define\ N\_PARAMS\ \ \ \ \ \ \ \ 4}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00030}00030\ \textcolor{comment}{\#define\ N\_CANDIDATES\ \ \ \ 100}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00031}00031\ \textcolor{comment}{\#define\ N\_BEST\ \ \ \ \ \ \ \ \ \ 10}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00032}00032\ \textcolor{comment}{\#define\ MUTATION\ \ \ \ \ \ \ \ 0.1f}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00033}00033\ \textcolor{comment}{float\ params[N\_PARAMS];}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00034}00034\ \textcolor{comment}{GenOptimizer\_t\ optimizer;}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00035}00035\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00036}00036\ \textcolor{comment}{//\ Формирование\ параметров}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00037}00037\ \textcolor{comment}{uint16\_t\ \ param\_u16\ =\ 800;}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00038}00038\ \textcolor{comment}{float\ \ \ \ \ param\_f\ \ \ =\ 0.01f;}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00039}00039\ \textcolor{comment}{uint8\_t\ \ \ param\_u8\ \ =\ 40;}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00040}00040\ \textcolor{comment}{int16\_t\ \ \ param\_i16\ =\ 1600;}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00041}00041\ \textcolor{comment}{params[0]\ =\ PARAM\_SCALE(param\_u16,\ \ 0.0f,\ \ \ 1000.0f);}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00042}00042\ \textcolor{comment}{params[1]\ =\ PARAM\_SCALE(param\_f,\ \ \ \ 0.001f,\ 0.1f);}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00043}00043\ \textcolor{comment}{params[2]\ =\ PARAM\_SCALE(param\_u8,\ \ \ 10.0f,\ \ 100.0f);}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00044}00044\ \textcolor{comment}{params[3]\ =\ PARAM\_SCALE(param\_i16,\ \ 500.0f,\ 5000.0f);}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00045}00045\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00046}00046\ \textcolor{comment}{//\ Инициалиазция}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00047}00047\ \textcolor{comment}{GenOptimizer\_Init(\&optimizer,\ N\_PARAMS,\ N\_CANDIDATES,\ N\_BEST,\ MUTATION,\ params);}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00048}00048\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00049}00049\ \textcolor{comment}{//\ Шаг\ эволюции}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00050}00050\ \textcolor{comment}{float\ loss\ =\ calc\_loss();\ //\ расчет\ эффективности\ параметров\ (от\ 0\ до\ 1)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00051}00051\ \textcolor{comment}{GenOptimizer\_Step(\&optimizer,\ params,\ loss);}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00052}00052\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00053}00053\ \textcolor{comment}{//\ Взятие\ следующих\ для\ эволюции\ параметров}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00054}00054\ \textcolor{comment}{param\_u16\ =\ PARAM\_UNSCALE(params[0],\ \ 0.0f,\ \ \ 1000.0f);}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00055}00055\ \textcolor{comment}{param\_f\ \ \ =\ PARAM\_UNSCALE(params[1],\ \ 0.001f,\ 0.1f);}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00056}00056\ \textcolor{comment}{param\_u8\ \ =\ PARAM\_UNSCALE(params[2],\ \ 10.0f,\ \ 100.0f);}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00057}00057\ \textcolor{comment}{param\_i16\ =\ PARAM\_UNSCALE(params[3],\ \ 500.0f,\ 5000.0f);}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00058}00058\ \textcolor{comment}{@endcode}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00059}00059\ \textcolor{comment}{*\ @\{\ \ }}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00060}00060\ \textcolor{comment}{*****************************************************************************/}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00061}00061\ \textcolor{preprocessor}{\#ifndef\ \_\_GEN\_OPTIMIZER\_H\_}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00062}00062\ \textcolor{preprocessor}{\#define\ \_\_GEN\_OPTIMIZER\_H\_}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00063}00063\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00064}00064\ \textcolor{preprocessor}{\#include\ "{}\mbox{\hyperlink{mylibs__defs_8h}{mylibs\_defs.h}}"{}}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00065}00065\ \textcolor{preprocessor}{\#include\ <stdint.h>}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00066}00066\ \textcolor{preprocessor}{\#include\ <stdlib.h>}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00067}00067\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00068}00068\ \textcolor{preprocessor}{\#ifdef\ GEN\_OPTIMIZATION\_ENABLE}\textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00069}00069\ \textcolor{comment}{/**}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00070}00070\ \textcolor{comment}{\ \ *\ @brief\ Линейное\ масштабирование\ x\ из\ диапазона\ [min\_val,\ max\_val]\ в\ диапазон\ [0,\ 1)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00071}00071\ \textcolor{comment}{\ \ */}\textcolor{preprocessor}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00072}00072\ \textcolor{preprocessor}{\#define\ PARAM\_SCALE(x,\ min\_val,\ max\_val)\ \(\backslash\)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00073}00073\ \textcolor{preprocessor}{(((float)(x)\ -\/\ (float)(min\_val))\ /\ ((float)(max\_val)\ -\/\ (float)(min\_val)))}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00074}00074\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00075}00075\ \textcolor{comment}{/**}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00076}00076\ \textcolor{comment}{\ \ *\ @brief\ Обратное\ линейное\ масштабирование\ значения\ из\ [0,\ 1)\ в\ диапазон\ [min\_val,\ max\_val]}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00077}00077\ \textcolor{comment}{\ \ */}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00078}00078\ \textcolor{preprocessor}{\#define\ PARAM\_UNSCALE(val,\ min\_val,\ max\_val)\ \(\backslash\)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00079}00079\ \textcolor{preprocessor}{(((float)(val))\ *\ ((float)(max\_val)\ -\/\ (float)(min\_val))\ +\ (float)(min\_val))}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00080}00080\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00081}00081\ \textcolor{preprocessor}{\#ifndef\ local\_time}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00082}00082\ \textcolor{preprocessor}{\#define\ local\_time()\ \ HAL\_GetTick()\ }\textcolor{comment}{///<\ Локальное\ время}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00083}00083\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00084}00084\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00085}00085\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00086}00086\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00087}00087\ \textcolor{preprocessor}{\#ifndef\ GEN\_MUTATION\_MIN\_PCT}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00088}00088\ \textcolor{preprocessor}{\#define\ GEN\_MUTATION\_MIN\_PCT\ \ \ 10\ \ \ \ }\textcolor{comment}{///<\ Минимальная\ мутация\ (в\ процентах\ от\ Loss)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00089}00089\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00090}00090\ \textcolor{preprocessor}{\#ifndef\ GEN\_MUTATION\_MAX\_PCT}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00091}00091\ \textcolor{preprocessor}{\#define\ GEN\_MUTATION\_MAX\_PCT\ \ \ 100\ \ \ }\textcolor{comment}{///<\ Максимальная\ мутация\ (в\ процентах\ от\ Loss)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00092}00092\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00093}00093\ \textcolor{preprocessor}{\#ifndef\ ELOVLE\_N\_ELITE\_CANDIDATE}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00094}00094\ \textcolor{preprocessor}{\#define\ ELOVLE\_N\_ELITE\_CANDIDATE\ \ 2\ \ \ \ \ }\textcolor{comment}{///<\ Количество\ кандидатов,\ которые\ проходят\ в\ поколение\ без\ изменений\ (по\ умолчанию\ 2)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00095}00095\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00096}00096\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00097}00097\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00098}00098\ \textcolor{comment}{/**}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00099}00099\ \textcolor{comment}{\ \ *\ @brief\ Структура\ эволюционного\ оптимизатора}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00100}00100\ \textcolor{comment}{\ \ */}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00101}00101\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }\{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00102}00102\ \ \ \textcolor{keywordtype}{float}\ \ \ \ \ stability;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Коэффициент\ насколько\ стабильная\ популяция\ (0..1)(@ref\ n\_cand)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00103}00103\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00104}00104\ \ \ uint16\_t\ \ n\_params;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Количество\ параметров}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00105}00105\ \ \ uint16\_t\ \ n\_cand;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Количество\ кандидатов\ в\ популяции}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00106}00106\ \ \ uint16\_t\ \ n\_best;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Количество\ лучших,\ усредняемых}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00107}00107\ \ \ \textcolor{keywordtype}{float}\ \ \ \ \ mutation\_amp;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Амплитуда\ мутации\ (0..1)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00108}00108\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00109}00109\ \ \ uint16\_t\ \ cand\_index;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Индекс\ текущего\ кандидата}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00110}00110\ \ \ uint16\_t\ \ gen\_index;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Индекс\ популяции\ \ }}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00111}00111\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00112}00112\ \ \ \textcolor{comment}{//INTERNAL}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00113}00113\ \ \ \textcolor{keywordtype}{float}\ \ \ \ \ gen\_mut;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Амплитуда\ мутации\ у\ текущей\ популяции\ \ }}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00114}00114\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00115}00115\ \ \ \textcolor{keywordtype}{float}\ \ \ \ \ loss[\mbox{\hyperlink{group___g_e_n___c_o_n_f_i_g_ga201cffde04ebc6540869c74bd1dae869}{GEN\_MAX\_CANDIDATES}}];\ \ \ \ \textcolor{comment}{///<\ Loss\ для\ каждого\ кандидата}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00116}00116\ \ \ \textcolor{keywordtype}{float}\ \ \ \ \ candidates[\mbox{\hyperlink{group___g_e_n___c_o_n_f_i_g_ga201cffde04ebc6540869c74bd1dae869}{GEN\_MAX\_CANDIDATES}}][\mbox{\hyperlink{group___g_e_n___c_o_n_f_i_g_ga8daf4b266a95353d580bd7946f47c0ab}{GEN\_MAX\_PARAMS}}];\ \textcolor{comment}{///<\ Параметры\ кандидатов}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00117}00117\ \ \ uint16\_t\ \ sorted\_idx[\mbox{\hyperlink{group___g_e_n___c_o_n_f_i_g_ga201cffde04ebc6540869c74bd1dae869}{GEN\_MAX\_CANDIDATES}}];\ \textcolor{comment}{///<\ Индексы\ отсортированных\ кандидатов}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00118}00118\ \}\ \mbox{\hyperlink{struct_gen_optimizer__t}{GenOptimizer\_t}};}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00119}00119\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00120}00120\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00121}00121\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00122}00122\ \textcolor{comment}{/**\ }}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00123}00123\ \textcolor{comment}{\ *\ @cond\ GEN\_INTERNAL}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00124}00124\ \textcolor{comment}{\ */}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00125}00125\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00126}00126\ \textcolor{comment}{//\ Вспомогательный\ указатель\ для\ сортировки}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00127}00127\ \textcolor{keyword}{static}\ \mbox{\hyperlink{struct_gen_optimizer__t}{GenOptimizer\_t}}\ *g\_sort\_opt;\ \textcolor{comment}{//\ глобальный\ указатель\ на\ текущий\ оптимизатор}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00128}00128\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00129}00129\ \textcolor{comment}{//\ функция\ условия\ сортировки}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00130}00130\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}\ cmp\_idx(\textcolor{keyword}{const}\ \textcolor{keywordtype}{void}\ *a,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{void}\ *b)\ \{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00131}00131\ \ \ \textcolor{keywordflow}{if}\ (g\_sort\_opt-\/>loss[*(\textcolor{keyword}{const}\ uint16\_t*)a]\ <\ g\_sort\_opt-\/>loss[*(\textcolor{keyword}{const}\ uint16\_t*)b])\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00132}00132\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00133}00133\ \ \ \textcolor{keywordflow}{if}\ (g\_sort\_opt-\/>loss[*(\textcolor{keyword}{const}\ uint16\_t*)a]\ >\ g\_sort\_opt-\/>loss[*(\textcolor{keyword}{const}\ uint16\_t*)b])\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00134}00134\ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00135}00135\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00136}00136\ \}\textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00137}00137\ \textcolor{comment}{/**\ @endcond\ */}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00138}00138\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00139}00139\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00140}00140\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00141}00141\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00142}00142\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00143}00143\ \textcolor{comment}{/**}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00144}00144\ \textcolor{comment}{\ \ *\ @brief\ Инициализация\ эволюционного\ оптимизатора.}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00145}00145\ \textcolor{comment}{\ \ *\ @param\ opt\ \ \ \ \ \ \ \ \ \ Указатель\ на\ структуру\ оптимизатора}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00146}00146\ \textcolor{comment}{\ \ *\ @param\ n\_params\ \ \ \ \ Количество\ параметров\ в\ одном\ кандидате}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00147}00147\ \textcolor{comment}{\ \ *\ @param\ n\_cand\ \ \ \ \ \ \ Количество\ кандидатов}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00148}00148\ \textcolor{comment}{\ \ *\ @param\ n\_best\ \ \ \ \ \ \ Количество\ лучших,\ усредняемых}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00149}00149\ \textcolor{comment}{\ \ *\ @param\ mutation\_amp\ Амплитуда\ мутации\ (в\ диапазоне\ 0.0–1.0)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00150}00150\ \textcolor{comment}{\ \ *\ @param\ start\_params\ Начальные\ параметры\ (в\ диапазоне\ 0.0–1.0)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00151}00151\ \textcolor{comment}{\ \ *\ @return\ 0\ \ \ —\ если\ окей,\ }}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00152}00152\ \textcolor{comment}{\ \ *\ \ \ \ \ \ \ \ \ -\/1\ \ —\ если\ ошибка}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00153}00153\ \textcolor{comment}{\ \ */}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00154}00154\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}\ GenOptimizer\_Init(\mbox{\hyperlink{struct_gen_optimizer__t}{GenOptimizer\_t}}*\ opt,}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00155}00155\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint16\_t\ n\_params,}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00156}00156\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint16\_t\ n\_cand,}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00157}00157\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint16\_t\ n\_best,}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00158}00158\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{float}\ mutation\_amp,}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00159}00159\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{float}*\ start\_params)}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00160}00160\ \{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00161}00161\ \ \ \textcolor{keywordflow}{if}((opt\ ==\ NULL)\ ||\ (start\_params\ ==\ NULL))}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00162}00162\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00163}00163\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00164}00164\ \ \ \textcolor{keywordflow}{if}(n\_params\ >\ \mbox{\hyperlink{group___g_e_n___c_o_n_f_i_g_ga8daf4b266a95353d580bd7946f47c0ab}{GEN\_MAX\_PARAMS}})}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00165}00165\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00166}00166\ \ \ opt-\/>n\_params\ \ \ \ \ =\ n\_params;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00167}00167\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00168}00168\ \ \ \textcolor{keywordflow}{if}(n\_cand\ >\ \mbox{\hyperlink{group___g_e_n___c_o_n_f_i_g_ga201cffde04ebc6540869c74bd1dae869}{GEN\_MAX\_CANDIDATES}})}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00169}00169\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00170}00170\ \ \ opt-\/>n\_cand\ \ \ \ \ \ \ =\ n\_cand;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00171}00171\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00172}00172\ \ \ \textcolor{keywordflow}{if}(n\_best\ >\ \mbox{\hyperlink{group___g_e_n___c_o_n_f_i_g_ga201cffde04ebc6540869c74bd1dae869}{GEN\_MAX\_CANDIDATES}}/2)}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00173}00173\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00174}00174\ \ \ opt-\/>n\_best\ \ \ \ \ \ \ =\ n\_best;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00175}00175\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00176}00176\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00177}00177\ \ \ \textcolor{keywordflow}{if}((mutation\_amp\ >\ 1)\ ||\ (mutation\_amp\ <\ 0))}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00178}00178\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00179}00179\ \ \ \textcolor{keywordflow}{if}(mutation\_amp\ <=\ 0.001f)}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00180}00180\ \ \ \ \ mutation\_amp\ =\ 0.001f;\ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00181}00181\ \ \ opt-\/>mutation\_amp\ \ =\ mutation\_amp;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00182}00182\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00183}00183\ \ \ uint32\_t\ seed\ =\ \mbox{\hyperlink{group___t_r_a_c_e___s_e_r_i_a_l_ga9c853b02c22f26023c34d1d404b6d653}{local\_time}}();}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00184}00184\ \textcolor{preprocessor}{\#ifdef\ ADC1}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00185}00185\ \ \ seed\ +=\ (ADC1-\/>DR\ \&\ 0xFF);}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00186}00186\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00187}00187\ \ \ srand(seed);}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00188}00188\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00189}00189\ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ i\ =\ 0;\ i\ <\ n\_cand;\ i++)\ \{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00190}00190\ \ \ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ j\ =\ 0;\ j\ <\ n\_params;\ j++)\ \{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00191}00191\ \ \ \ \ \ \ \textcolor{comment}{//\ Добавляем\ случайную\ мутацию\ вокруг\ стартового\ параметра}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00192}00192\ \ \ \ \ \ \ \textcolor{keywordtype}{float}\ base\ =\ start\_params[j];}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00193}00193\ \ \ \ \ \ \ \textcolor{keywordtype}{float}\ inv\_randmax\ =\ 1.0f\ /\ (float)RAND\_MAX;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00194}00194\ \ \ \ \ \ \ \textcolor{keywordtype}{float}\ noise\ =\ ((float)rand()\ *\ inv\_randmax\ *\ 2.0f\ -\/\ 1.0f)\ *\ mutation\_amp;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00195}00195\ \ \ \ \ \ \ opt-\/>candidates[i][j]\ =\ base\ +\ noise;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00196}00196\ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (opt-\/>candidates[i][j]\ <\ 0.0f)\ opt-\/>candidates[i][j]\ =\ 0.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00197}00197\ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (opt-\/>candidates[i][j]\ >\ 1.0f)\ opt-\/>candidates[i][j]\ =\ 1.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00198}00198\ \ \ \ \ \}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00199}00199\ \ \ \ \ opt-\/>loss[i]\ =\ 0.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00200}00200\ \ \ \}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00201}00201\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00202}00202\ \ \ opt-\/>cand\_index\ =\ 0;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00203}00203\ \ \ opt-\/>gen\_index\ =\ 0;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00204}00204\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00205}00205\ \}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00206}00206\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00207}00207\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00208}00208\ \textcolor{comment}{/**}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00209}00209\ \textcolor{comment}{\ \ *\ @brief\ Один\ шаг\ эволюционного\ оптимизатора.}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00210}00210\ \textcolor{comment}{\ \ *\ @param\ opt\ \ \ \ Указатель\ на\ структуру\ оптимизатора}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00211}00211\ \textcolor{comment}{\ \ *\ @param\ params\ Массив\ параметров,\ которые\ будут\ обновлены\ (на\ выходе\ —\ новые\ параметры)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00212}00212\ \textcolor{comment}{\ \ *\ @param\ loss\ \ \ Loss\ текущего\ кандидата}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00213}00213\ \textcolor{comment}{\ \ *\ @return\ 0\ \ \ —\ если\ окей,\ }}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00214}00214\ \textcolor{comment}{\ \ *\ \ \ \ \ \ \ \ \ -\/1\ \ —\ если\ ошибка}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00215}00215\ \textcolor{comment}{\ \ *\ @details}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00216}00216\ \textcolor{comment}{\ \ *\ Сохраняет\ loss\ текущего\ кандидата\ и\ формирует\ параметры\ следующего\ кандидата.}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00217}00217\ \textcolor{comment}{\ \ *\ Если\ накоплено\ n\_cand\ кандидатов,\ генерируется\ новое\ поколение.}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00218}00218\ \textcolor{comment}{\ \ *\ Новое\ поколение\ формируется\ случайным\ выбором\ из\ n\_best\ лучших\ с\ добавлением\ мутации.}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00219}00219\ \textcolor{comment}{\ \ *}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00220}00220\ \textcolor{comment}{\ \ *\ На\ выходе\ params\ содержит\ параметры\ следующего\ кандидата\ для\ измерений.}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00221}00221\ \textcolor{comment}{\ \ *\ @note\ Функция\ использует\ глобальную\ внутреннюю\ переменную\ для\ сортировки.}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00222}00222\ \textcolor{comment}{\ \ *\ Надо\ убедится\ что\ только\ один\ экземпляр\ функции\ запущен\ в\ момент\ времени}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00223}00223\ \textcolor{comment}{\ \ */}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00224}00224\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}\ GenOptimizer\_Step(\mbox{\hyperlink{struct_gen_optimizer__t}{GenOptimizer\_t}}*\ opt,}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00225}00225\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{float}*\ params,}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00226}00226\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{float}\ loss)}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00227}00227\ \{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00228}00228\ \ \ \textcolor{keywordflow}{if}((opt\ ==\ NULL)\ ||\ (params\ ==\ NULL))}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00229}00229\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00230}00230\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00231}00231\ \ \ uint16\_t\ n\_params\ =\ opt-\/>n\_params;\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00232}00232\ \ \ \textcolor{keywordflow}{if}(n\_params\ >\ \mbox{\hyperlink{group___g_e_n___c_o_n_f_i_g_ga8daf4b266a95353d580bd7946f47c0ab}{GEN\_MAX\_PARAMS}})}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00233}00233\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00234}00234\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00235}00235\ \ \ uint16\_t\ n\_cand\ =\ opt-\/>n\_cand;\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00236}00236\ \ \ \textcolor{keywordflow}{if}(n\_cand\ >\ \mbox{\hyperlink{group___g_e_n___c_o_n_f_i_g_ga201cffde04ebc6540869c74bd1dae869}{GEN\_MAX\_CANDIDATES}})}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00237}00237\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00238}00238\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00239}00239\ \ \ uint16\_t\ n\_best\ =\ opt-\/>n\_best;\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00240}00240\ \ \ \textcolor{keywordflow}{if}(n\_best\ >\ \mbox{\hyperlink{group___g_e_n___c_o_n_f_i_g_ga201cffde04ebc6540869c74bd1dae869}{GEN\_MAX\_CANDIDATES}}/2)}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00241}00241\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00242}00242\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00243}00243\ \ \ \textcolor{keywordtype}{float}\ \ \ \ mut\ \ \ \ \ \ =\ opt-\/>mutation\_amp;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00244}00244\ \ \ \textcolor{keywordflow}{if}((mut\ >\ 1)\ ||(mut\ <\ 0))}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00245}00245\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00246}00246\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00247}00247\ \ \ \textcolor{comment}{//\ 1.\ Сохраняем\ loss\ текущего\ кандидата}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00248}00248\ \ \ opt-\/>loss[opt-\/>cand\_index]\ =\ loss;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00249}00249\ \ \ opt-\/>cand\_index++;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00250}00250\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00251}00251\ \ \ \textcolor{keywordflow}{if}\ (opt-\/>cand\_index\ >=\ n\_cand)\ \{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00252}00252\ \ \ \ \ \textcolor{comment}{//\ 2.\ Сортируем\ текущее\ поколение\ по\ loss}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00253}00253\ \ \ \ \ \textcolor{keywordflow}{for}(uint16\_t\ i\ =\ 0;\ i\ <\ opt-\/>n\_cand;\ i++)}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00254}00254\ \ \ \ \ \ \ opt-\/>sorted\_idx[i]\ =\ i;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00255}00255\ \ \ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00256}00256\ \ \ \ \ g\_sort\_opt\ =\ opt;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00257}00257\ \ \ \ \ qsort(opt-\/>sorted\_idx,\ opt-\/>n\_cand,\ \textcolor{keyword}{sizeof}(uint16\_t),\ cmp\_idx);}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00258}00258\ \ \ \ \ g\_sort\_opt\ =\ NULL;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00259}00259\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00260}00260\ \ \ \ \ \textcolor{comment}{//\ -\/-\/-\/\ Адаптивная\ мутация\ в\ зависимости\ от\ Loss\ -\/-\/-\/}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00261}00261\ \ \ \ \ \textcolor{keywordtype}{float}\ best\_loss\ =\ opt-\/>loss[opt-\/>sorted\_idx[0]];}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00262}00262\ \ \ \ \ \textcolor{keywordtype}{float}\ worst\_loss\ =\ opt-\/>loss[opt-\/>sorted\_idx[opt-\/>n\_cand\ -\/\ 1]];}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00263}00263\ \ \ \ \ \textcolor{keywordtype}{float}\ diff\ =\ worst\_loss\ -\/\ best\_loss;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00264}00264\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00265}00265\ \ \ \ \ \textcolor{keywordtype}{float}\ sum\_loss\ =\ 0.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00266}00266\ \ \ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ i\ =\ 0;\ i\ <\ n\_cand;\ i++)}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00267}00267\ \ \ \ \ \ \ sum\_loss\ +=\ opt-\/>loss[i];}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00268}00268\ \ \ \ \ \textcolor{keywordtype}{float}\ avg\_loss\ =\ sum\_loss\ /\ (float)n\_cand;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00269}00269\ \ \ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00270}00270\ \ \ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00271}00271\ \ \ \ \ \textcolor{keywordtype}{float}\ loss\_ratio\ =\ (diff\ >\ 0.0f)\ ?\ ((avg\_loss\ -\/\ best\_loss)\ /\ diff)\ :\ 0.5f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00272}00272\ \ \ \ \ \textcolor{keywordflow}{if}\ (loss\_ratio\ <\ 0.0f)\ loss\_ratio\ =\ 0.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00273}00273\ \ \ \ \ \textcolor{keywordflow}{if}\ (loss\_ratio\ >\ 1.0f)\ loss\_ratio\ =\ 1.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00274}00274\ \ \ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00275}00275\ \ \ \ \ \textcolor{comment}{//\ Записываем\ стабильность\ популяции\ в\ структуру}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00276}00276\ \ \ \ \ \textcolor{keywordflow}{if}(diff\ <\ 0.0f)\ diff\ =\ 0.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00277}00277\ \ \ \ \ \textcolor{keywordflow}{if}(diff\ >\ 1.0f)\ diff\ =\ 1.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00278}00278\ \ \ \ \ opt-\/>stability\ =\ (1.0f\ -\/\ worst\_loss)\ *\ (1.0f\ -\/\ (worst\_loss\ -\/\ best\_loss));}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00279}00279\ \ \ \ \ \textcolor{keywordflow}{if}(opt-\/>stability\ <\ 0.0f)\ opt-\/>stability\ =\ 0.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00280}00280\ \ \ \ \ \textcolor{keywordflow}{if}(opt-\/>stability\ >\ 1.0f)\ opt-\/>stability\ =\ 1.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00281}00281\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00282}00282\ \ \ \ \ \textcolor{keywordtype}{float}\ mut\_pct\ =\ GEN\_MUTATION\_MIN\_PCT\ +}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00283}00283\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (GEN\_MUTATION\_MAX\_PCT\ -\/\ GEN\_MUTATION\_MIN\_PCT)\ *\ loss\_ratio;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00284}00284\ \ \ \ \ \textcolor{keywordtype}{float}\ adaptive\_mut\ =\ mut\ *\ (mut\_pct\ /\ 100.0f);}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00285}00285\ \ \ \ \ \textcolor{keywordflow}{if}\ (adaptive\_mut\ <\ 0.0001f)\ adaptive\_mut\ =\ 0.0001f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00286}00286\ \ \ \ \ opt-\/>gen\_mut\ =\ adaptive\_mut;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00287}00287\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00288}00288\ \ \ \ \ \textcolor{comment}{//\ 3.\ Генерируем\ новое\ поколение}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00289}00289\ \ \ \ \ uint16\_t\ n\_elite\ =\ ELOVLE\_N\_ELITE\_CANDIDATE;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00290}00290\ \ \ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ c\ =\ 0;\ c\ <\ n\_cand;\ c++)\ \{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00291}00291\ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (c\ <\ n\_elite)\ \{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00292}00292\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ i\ =\ 0;\ i\ <\ n\_params;\ i++)}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00293}00293\ \ \ \ \ \ \ \ \ \ \ opt-\/>candidates[c][i]\ =\ opt-\/>candidates[opt-\/>sorted\_idx[c]][i];}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00294}00294\ \ \ \ \ \ \ \ \ opt-\/>loss[c]\ =\ 0.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00295}00295\ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00296}00296\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ i\ =\ 0;\ i\ <\ n\_params;\ i++)\ \{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00297}00297\ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{float}\ inv\_randmax\ =\ 1.0f\ /\ (float)RAND\_MAX;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00298}00298\ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{float}\ noise\ =\ ((float)rand()\ *\ inv\_randmax\ *\ 2.0f\ -\/\ 1.0f)\ *\ adaptive\_mut;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00299}00299\ \ \ \ \ \ \ \ \ \ \ uint16\_t\ parent\ =\ opt-\/>sorted\_idx[rand()\ \%\ opt-\/>n\_best];}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00300}00300\ \ \ \ \ \ \ \ \ \ \ opt-\/>candidates[c][i]\ =\ opt-\/>candidates[parent][i]\ +\ noise;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00301}00301\ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (opt-\/>candidates[c][i]\ <\ 0.0f)\ opt-\/>candidates[c][i]\ =\ 0.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00302}00302\ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (opt-\/>candidates[c][i]\ >\ 1.0f)\ opt-\/>candidates[c][i]\ =\ 1.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00303}00303\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00304}00304\ \ \ \ \ \ \ \ \ opt-\/>loss[c]\ =\ 0.0f;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00305}00305\ \ \ \ \ \ \ \}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00306}00306\ \ \ \ \ \}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00307}00307\ \ \ \ \ opt-\/>cand\_index\ =\ 0;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00308}00308\ \ \ \ \ opt-\/>gen\_index++;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00309}00309\ \ \ \}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00310}00310\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00311}00311\ \ \ \textcolor{comment}{//\ 4.\ Возвращаем\ параметры\ следующего\ кандидата}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00312}00312\ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ i\ =\ 0;\ i\ <\ opt-\/>n\_params;\ i++)}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00313}00313\ \ \ \ \ params[i]\ =\ opt-\/>candidates[opt-\/>cand\_index][i];}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00314}00314\ \ \ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00315}00315\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00316}00316\ \}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00317}00317\ \textcolor{preprocessor}{\#else\ }\textcolor{comment}{//\ GEN\_OPTIMIZATION\_ENABLE}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00318}00318\ \textcolor{comment}{//заглушки}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00319}\mbox{\hyperlink{struct_gen_optimizer__t}{00319}}\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }\{}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00320}00320\ \ \ uint16\_t\ n\_params;\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00321}00321\ \ \ uint16\_t\ n\_cand;\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00322}00322\ \ \ uint16\_t\ n\_best;\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00323}00323\ \ \ \textcolor{keywordtype}{float}\ mutation\_amp;\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00324}00324\ \ \ \textcolor{keywordtype}{float}\ loss[0];\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00325}00325\ \ \ \textcolor{keywordtype}{float}\ candidates[0][0];\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00326}00326\ \}\ \mbox{\hyperlink{struct_gen_optimizer__t}{GenOptimizer\_t}};}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00327}00327\ \textcolor{preprocessor}{\#define\ GenOptimizer\_Init(opt,\ n\_params,\ n\_cand,\ n\_best,\ mutation\_amp,\ start\_params)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00328}00328\ \textcolor{preprocessor}{\#define\ GenOptimizer\_Step(opt,\ params,\ LossFunc)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00329}00329\ \textcolor{preprocessor}{\#define\ PARAM\_SCALE(x,\ min\_val,\ max\_val)\ \ \ \ \ \ \ \ \ (x)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00330}00330\ \textcolor{preprocessor}{\#define\ PARAM\_UNSCALE(val,\ min\_val,\ max\_val)\ \ \ \ \ (val)}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00331}00331\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{//\ GEN\_OPTIMIZATION\_ENABLE}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00332}00332\ }
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00333}00333\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{//\ \_\_GEN\_OPTIMIZER\_H\_}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00334}00334\ \textcolor{comment}{}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00335}00335\ \textcolor{comment}{/**\ GEN\_OPTIMIZER}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00336}00336\ \textcolor{comment}{\ \ *\ @\}}}
\DoxyCodeLine{\Hypertarget{gen__optimizer_8h_source_l00337}00337\ \textcolor{comment}{\ \ */}}

\end{DoxyCode}
