\doxysection{evolve\+\_\+optimizer.\+h} \hypertarget{evolve__optimizer_8h_source}{}\label{evolve__optimizer_8h_source}\index{E:/.WORK/STM32/STM32\_ExtendedLibs/MyLibsGeneral/Inc/evolve\_optimizer.h@{E:/.WORK/STM32/STM32\_ExtendedLibs/MyLibsGeneral/Inc/evolve\_optimizer.h}} \mbox{\hyperlink{evolve__optimizer_8h}{Go to the documentation of this file.}} \begin{DoxyCode}{0} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00001}00001\ \textcolor{comment}{/**}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00002}00002\ \textcolor{comment}{******************************************************************************}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00003}00003\ \textcolor{comment}{*\ @file\ evolve\_optimizer.h}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00004}00004\ \textcolor{comment}{*\ @brief\ Заголовочный\ файл\ для\ адаптивного\ подбора\ параметров}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00005}00005\ \textcolor{comment}{******************************************************************************}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00006}00006\ \textcolor{comment}{*\ @addtogroup\ EVOLVE\_OPTIMIZER\ \ \ \ Evolve\ optimizer}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00007}00007\ \textcolor{comment}{*\ @ingroup\ \ \ \ MYLIBS\_DEFINES}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00008}00008\ \textcolor{comment}{*\ @brief\ \ \ \ \ \ Библиотека\ для\ эволюционного\ подбора\ параметров}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00009}00009\ \textcolor{comment}{*\ @details}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00010}00010\ \textcolor{comment}{Поддерживает:}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00011}00011\ \textcolor{comment}{-\/\ Любое\ количество\ параметров}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00012}00012\ \textcolor{comment}{-\/\ Генерацию\ новых\ параметров\ на\ основе\ лучших\ кандидатов}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00013}00013\ \textcolor{comment}{-\/\ Мутацию\ для\ поиска\ оптимальных\ параметров}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00014}00014\ \textcolor{comment}{-\/\ Несколько\ независимых\ оптимизаторов\ в\ одной\ программе}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00015}00015\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00016}00016\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00017}00017\ \textcolor{comment}{Параметры\ для\ конфигурации:}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00018}00018\ \textcolor{comment}{-\/\ @ref\ ENABLE\_EVOLVE\_OPTIMIZATION\ -\/\ Включить\ оптимизацию\ параметров}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00019}00019\ \textcolor{comment}{\ \ Если\ библиотека\ отключена\ @ref\ ENABLE\_EVOLVE\_OPTIMIZATION,\ то\ вставляются\ }} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00020}00020\ \textcolor{comment}{\ \ заглушки,\ никак\ не\ влияющие\ на\ параметры\ и\ остальную\ программу}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00021}00021\ \textcolor{comment}{-\/\ @ref\ EVOLVE\_MAX\_PARAMS\ \ \ \ \ \ \ \ \ \ -\/\ Максимальное\ количество\ параметров}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00022}00022\ \textcolor{comment}{-\/\ @ref\ EVOLVE\_MAX\_CANDIDATES\ \ \ \ \ \ -\/\ Максимальное\ количество\ кандидатов\ для\ обучения}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00023}00023\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00024}00024\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00025}00025\ \textcolor{comment}{@par\ Пример\ использования:}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00026}00026\ \textcolor{comment}{@code}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00027}00027\ \textcolor{comment}{\#include\ "{}evolve\_optimizer.h"{}}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00028}00028\ \textcolor{comment}{\#define\ N\_PARAMS\ \ \ \ \ \ \ \ 4}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00029}00029\ \textcolor{comment}{\#define\ N\_CANDIDATES\ \ \ \ 100}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00030}00030\ \textcolor{comment}{\#define\ N\_BEST\ \ \ \ \ \ \ \ \ \ 10}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00031}00031\ \textcolor{comment}{\#define\ IQ\_MUTATION\ \ \ \ \ (PARAM\_SCALE\_Q16(0.1,\ 0,\ 1))}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00032}00032\ \textcolor{comment}{int32\_t\ params[N\_PARAMS];}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00033}00033\ \textcolor{comment}{EvolveOptimizer\_t\ optimizer;}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00034}00034\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00035}00035\ \textcolor{comment}{//\ Формирование\ параметров}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00036}00036\ \textcolor{comment}{uint16\_t\ \ param\_u16\ =\ 800;}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00037}00037\ \textcolor{comment}{float\ \ \ \ \ param\_f\ \ \ =\ 0.01f;}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00038}00038\ \textcolor{comment}{uint8\_t\ \ \ param\_u8\ \ =\ 40;}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00039}00039\ \textcolor{comment}{int16\_t\ \ \ param\_i16\ =\ 1600;}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00040}00040\ \textcolor{comment}{params[0]\ =\ PARAM\_SCALE\_Q16(param\_u16,\ \ 0,\ \ \ \ \ \ 1000);}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00041}00041\ \textcolor{comment}{params[1]\ =\ PARAM\_SCALE\_Q16(param\_f,\ \ \ \ 0.001f,\ 0.1f);}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00042}00042\ \textcolor{comment}{params[2]\ =\ PARAM\_SCALE\_Q16(param\_u8,\ \ \ 10,\ \ \ \ \ 100);}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00043}00043\ \textcolor{comment}{params[3]\ =\ PARAM\_SCALE\_Q16(param\_i16,\ \ 500,\ \ \ \ 5000);}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00044}00044\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00045}00045\ \textcolor{comment}{//\ Инициалиазция}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00046}00046\ \textcolor{comment}{EvolveOptimizer\_Init(\&optimizer,\ N\_PARAMS,\ N\_CANDIDATES,\ N\_BEST,\ IQ\_MUTATION,\ params);}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00047}00047\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00048}00048\ \textcolor{comment}{//\ Шаг\ эволюции}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00049}00049\ \textcolor{comment}{int32\_t\ loss\ =\ calc\_iq\_loss();\ //\ расчет\ эффективности\ параметров}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00050}00050\ \textcolor{comment}{EvolveOptimizer\_Step(\&optimizer,\ params,\ loss);}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00051}00051\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00052}00052\ \textcolor{comment}{//\ Взятие\ следующих\ для\ эволюции\ параметров}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00053}00053\ \textcolor{comment}{param\_u16\ =\ PARAM\_UNSCALE\_Q16(params[0],\ \ 0,\ \ \ \ \ \ 1000);}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00054}00054\ \textcolor{comment}{param\_f\ \ \ =\ PARAM\_UNSCALE\_Q16(params[1],\ \ 0.001f,\ 0.1f);}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00055}00055\ \textcolor{comment}{param\_u8\ \ =\ PARAM\_UNSCALE\_Q16(params[2],\ \ 10,\ \ \ \ \ 100);}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00056}00056\ \textcolor{comment}{param\_i16\ =\ PARAM\_UNSCALE\_Q16(params[3],\ \ 500,\ \ \ \ 5000);}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00057}00057\ \textcolor{comment}{@endcode}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00058}00058\ \textcolor{comment}{*\ @\{\ \ }} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00059}00059\ \textcolor{comment}{*****************************************************************************/}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00060}00060\ \textcolor{preprocessor}{\#ifndef\ \_\_EVOLVE\_OPTIMIZER\_H\_}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00061}00061\ \textcolor{preprocessor}{\#define\ \_\_EVOLVE\_OPTIMIZER\_H\_}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00062}00062\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00063}00063\ \textcolor{preprocessor}{\#include\ "{}\mbox{\hyperlink{mylibs__defs_8h}{mylibs\_defs.h}}"{}}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00064}00064\ \textcolor{preprocessor}{\#include\ }} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00065}00065\ \textcolor{preprocessor}{\#include\ }} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00066}00066\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00067}00067\ \textcolor{preprocessor}{\#ifdef\ ENABLE\_EVOLVE\_OPTIMIZATION}\textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00068}00068\ \textcolor{comment}{/**}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00069}00069\ \textcolor{comment}{\ \ *\ @brief\ Линейное\ масштабирование\ x\ из\ диапазона\ [min\_val,\ max\_val]\ в\ Q16.16\ [0,\ 65536)}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00070}00070\ \textcolor{comment}{\ \ */}\textcolor{preprocessor}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00071}\mbox{\hyperlink{group___e_v_o_l_v_e___o_p_t_i_m_i_z_e_r_gaff223481fcffea77394a331afde29fdc}{00071}}\ \textcolor{preprocessor}{\#define\ PARAM\_SCALE\_Q16(x,\ min\_val,\ max\_val)\ \(\backslash\)}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00072}00072\ \textcolor{preprocessor}{((int32\_t)((((float)(x)\ -\/\ (float)(min\_val))\ /\ ((float)(max\_val)\ -\/\ (float)(min\_val)))\ *\ 65536.0f))}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00073}00073\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00074}00074\ \textcolor{comment}{/**}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00075}00075\ \textcolor{comment}{\ \ *\ @brief\ Обратное\ линейное\ масштабирование\ Q16.16\ значения\ в\ диапазон\ [min\_val,\ max\_val]}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00076}00076\ \textcolor{comment}{\ */}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00077}\mbox{\hyperlink{group___e_v_o_l_v_e___o_p_t_i_m_i_z_e_r_ga71a0c240c667e976d9caef391ab767ba}{00077}}\ \textcolor{preprocessor}{\#define\ PARAM\_UNSCALE\_Q16(q16\_val,\ min\_val,\ max\_val)\ \(\backslash\)}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00078}00078\ \textcolor{preprocessor}{(((float)(q16\_val)\ /\ 65536.0f)\ *\ ((float)(max\_val)\ -\/\ (float)(min\_val))\ +\ (float)(min\_val))}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00079}00079\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00080}00080\ \textcolor{preprocessor}{\#ifndef\ local\_time}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00081}\mbox{\hyperlink{group___e_v_o_l_v_e___o_p_t_i_m_i_z_e_r_ga9c853b02c22f26023c34d1d404b6d653}{00081}}\ \textcolor{preprocessor}{\#define\ local\_time()\ \ HAL\_GetTick()\ }\textcolor{comment}{///<\ Локальное\ время}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00082}00082\ \textcolor{preprocessor}{\#endif}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00083}00083\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00084}00084\ \textcolor{comment}{/**}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00085}00085\ \textcolor{comment}{\ \ *\ @brief\ Структура\ эволюционного\ оптимизатора}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00086}00086\ \textcolor{comment}{\ \ */}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00087}\mbox{\hyperlink{struct_evolve_optimizer__t}{00087}}\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }\{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00088}\mbox{\hyperlink{struct_evolve_optimizer__t_a1c270fce82e0c97e6c88fd0971c1eed8}{00088}}\ \ \ uint16\_t\ \mbox{\hyperlink{struct_evolve_optimizer__t_a1c270fce82e0c97e6c88fd0971c1eed8}{n\_params}};\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Количество\ параметров}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00089}\mbox{\hyperlink{struct_evolve_optimizer__t_ac35c7ca8cede4ab9c53a2845a91cd1b1}{00089}}\ \ \ uint16\_t\ \mbox{\hyperlink{struct_evolve_optimizer__t_ac35c7ca8cede4ab9c53a2845a91cd1b1}{n\_cand}};\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Количество\ кандидатов}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00090}\mbox{\hyperlink{struct_evolve_optimizer__t_a950cd70cb6bff33b78ad35ee58c729a6}{00090}}\ \ \ uint16\_t\ \mbox{\hyperlink{struct_evolve_optimizer__t_a950cd70cb6bff33b78ad35ee58c729a6}{n\_best}};\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Количество\ лучших,\ усредняемых}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00091}\mbox{\hyperlink{struct_evolve_optimizer__t_a5391277564b54e2271856a2466033df5}{00091}}\ \ \ uint16\_t\ \mbox{\hyperlink{struct_evolve_optimizer__t_a5391277564b54e2271856a2466033df5}{iq\_mutation}};\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Амплитуда\ мутации\ в\ Q16.16}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00092}\mbox{\hyperlink{struct_evolve_optimizer__t_a0ae0e63ba351335d4445d927991c64ae}{00092}}\ \ \ uint16\_t\ \mbox{\hyperlink{struct_evolve_optimizer__t_a0ae0e63ba351335d4445d927991c64ae}{cand\_index}};\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{///<\ Индекс\ кандидата\ для\ обработки}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00093}\mbox{\hyperlink{struct_evolve_optimizer__t_ac723b265a3d43b754489f14310314f0d}{00093}}\ \ \ int32\_t\ \mbox{\hyperlink{struct_evolve_optimizer__t_ac723b265a3d43b754489f14310314f0d}{loss}}[\mbox{\hyperlink{group___e_v_o_l_v_e___c_o_n_f_i_g_ga7c1396e5c95c766afccdb45cbfcf5971}{EVOLVE\_MAX\_CANDIDATES}}];\ \ \ \ \textcolor{comment}{///<\ Loss\ для\ каждого\ кандидата}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00094}\mbox{\hyperlink{struct_evolve_optimizer__t_a61bd0c8ae322a79936ba1f2ae22e0e0d}{00094}}\ \ \ int32\_t\ \mbox{\hyperlink{struct_evolve_optimizer__t_a61bd0c8ae322a79936ba1f2ae22e0e0d}{candidates}}[\mbox{\hyperlink{group___e_v_o_l_v_e___c_o_n_f_i_g_ga7c1396e5c95c766afccdb45cbfcf5971}{EVOLVE\_MAX\_CANDIDATES}}][\mbox{\hyperlink{group___e_v_o_l_v_e___c_o_n_f_i_g_ga80d6162495fc3776d8902610657d4fee}{EVOLVE\_MAX\_PARAMS}}];\ \ \ \ \textcolor{comment}{///<\ Параметры\ кандидатов}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00095}\mbox{\hyperlink{struct_evolve_optimizer__t_a74b81df1c298c99162310f3f7d91735a}{00095}}\ \ \ uint16\_t\ \mbox{\hyperlink{struct_evolve_optimizer__t_a74b81df1c298c99162310f3f7d91735a}{sorted\_idx}}[\mbox{\hyperlink{group___e_v_o_l_v_e___c_o_n_f_i_g_ga7c1396e5c95c766afccdb45cbfcf5971}{EVOLVE\_MAX\_CANDIDATES}}];\ \textcolor{comment}{///<\ Индексы\ отсортированных\ кандидатов}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00096}00096\ \}\ \mbox{\hyperlink{struct_evolve_optimizer__t}{EvolveOptimizer\_t}};} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00097}00097\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00098}00098\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00099}00099\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00100}00100\ \textcolor{comment}{/**\ }} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00101}00101\ \textcolor{comment}{\ *\ @cond\ EVOLVE\_INTERNAL}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00102}00102\ \textcolor{comment}{\ */}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00103}00103\ \textcolor{preprocessor}{\#define\ Q16\_MUL(a,b)\ ((int32\_t)(((int64\_t)(a)\ *\ (int64\_t)(b))\ >>\ 16))}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00104}00104\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00105}00105\ \textcolor{comment}{//\ Вспомогательный\ указатель\ для\ сортировки}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00106}00106\ \textcolor{keyword}{static}\ \mbox{\hyperlink{struct_evolve_optimizer__t}{EvolveOptimizer\_t}}\ *g\_sort\_opt;\ \textcolor{comment}{//\ глобальный\ указатель\ на\ текущий\ оптимизатор}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00107}00107\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00108}00108\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}\ cmp\_idx(\textcolor{keyword}{const}\ \textcolor{keywordtype}{void}\ *a,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{void}\ *b)\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00109}00109\ \ \ \textcolor{keywordflow}{if}\ (g\_sort\_opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac723b265a3d43b754489f14310314f0d}{loss}}[*(\textcolor{keyword}{const}\ uint16\_t*)a]\ <\ g\_sort\_opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac723b265a3d43b754489f14310314f0d}{loss}}[*(\textcolor{keyword}{const}\ uint16\_t*)b])\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00110}00110\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00111}00111\ \ \ \textcolor{keywordflow}{if}\ (g\_sort\_opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac723b265a3d43b754489f14310314f0d}{loss}}[*(\textcolor{keyword}{const}\ uint16\_t*)a]\ >\ g\_sort\_opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac723b265a3d43b754489f14310314f0d}{loss}}[*(\textcolor{keyword}{const}\ uint16\_t*)b])\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00112}00112\ \ \ \ \ \textcolor{keywordflow}{return}\ 1;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00113}00113\ \ \ \textcolor{keywordflow}{return}\ 0;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00114}00114\ \}\textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00115}00115\ \textcolor{comment}{/**\ @endcond\ */}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00116}00116\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00117}00117\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00118}00118\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00119}00119\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00120}00120\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00121}00121\ \textcolor{comment}{/**}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00122}00122\ \textcolor{comment}{\ \ *\ @brief\ Инициализация\ эволюционного\ оптимизатора.}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00123}00123\ \textcolor{comment}{\ \ *\ @param\ opt\ \ \ \ \ \ \ \ \ \ Указатель\ на\ структуру\ оптимизатора}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00124}00124\ \textcolor{comment}{\ \ *\ @param\ n\_params\ \ \ \ \ Количество\ параметров\ в\ одном\ кандидате}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00125}00125\ \textcolor{comment}{\ \ *\ @param\ n\_cand\ \ \ \ \ \ \ Количество\ кандидатов}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00126}00126\ \textcolor{comment}{\ \ *\ @param\ n\_best\ \ \ \ \ \ \ Количество\ лучших,\ усредняемых}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00127}00127\ \textcolor{comment}{\ \ *\ @param\ iq\_mutation\ \ Амплитуда\ мутации\ в\ Q16.16}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00128}00128\ \textcolor{comment}{\ \ *\ @param\ start\_params\ Начальные\ параметры\ (Q16.16)}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00129}00129\ \textcolor{comment}{\ \ *\ @return\ 0\ \ \ —\ если\ окей,\ }} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00130}00130\ \textcolor{comment}{\ \ *\ \ \ \ \ \ \ \ \ -\/1\ \ —\ если\ ошибка}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00131}00131\ \textcolor{comment}{\ \ */}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00132}\mbox{\hyperlink{group___e_v_o_l_v_e___o_p_t_i_m_i_z_e_r_gadaf3a90724197e1737d5c70c7bcc75e1}{00132}}\ \_\_STATIC\_INLINE\ \textcolor{keywordtype}{int}\ \mbox{\hyperlink{group___e_v_o_l_v_e___o_p_t_i_m_i_z_e_r_gadaf3a90724197e1737d5c70c7bcc75e1}{EvolveOptimizer\_Init}}(\mbox{\hyperlink{struct_evolve_optimizer__t}{EvolveOptimizer\_t}}*\ opt,} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00133}00133\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint16\_t\ n\_params,} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00134}00134\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint16\_t\ n\_cand,} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00135}00135\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint16\_t\ n\_best,} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00136}00136\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint16\_t\ iq\_mutation,} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00137}00137\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\_t*\ start\_params)} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00138}00138\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00139}00139\ \ \ \textcolor{keywordflow}{if}((opt\ =\ NULL)\ ||\ (start\_params\ ==\ NULL))} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00140}00140\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00141}00141\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00142}00142\ \ \ \textcolor{keywordflow}{if}(n\_params\ >\ \mbox{\hyperlink{group___e_v_o_l_v_e___c_o_n_f_i_g_ga80d6162495fc3776d8902610657d4fee}{EVOLVE\_MAX\_PARAMS}})} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00143}00143\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00144}00144\ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a1c270fce82e0c97e6c88fd0971c1eed8}{n\_params}}\ \ \ \ \ =\ n\_params;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00145}00145\ \ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00146}00146\ \ \ \textcolor{keywordflow}{if}(n\_cand\ >\ \mbox{\hyperlink{group___e_v_o_l_v_e___c_o_n_f_i_g_ga7c1396e5c95c766afccdb45cbfcf5971}{EVOLVE\_MAX\_CANDIDATES}})} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00147}00147\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00148}00148\ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac35c7ca8cede4ab9c53a2845a91cd1b1}{n\_cand}}\ \ \ \ \ \ \ =\ n\_cand;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00149}00149\ \ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00150}00150\ \ \ \textcolor{keywordflow}{if}(n\_best\ >\ \mbox{\hyperlink{group___e_v_o_l_v_e___c_o_n_f_i_g_ga7c1396e5c95c766afccdb45cbfcf5971}{EVOLVE\_MAX\_CANDIDATES}}/2)} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00151}00151\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00152}00152\ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a950cd70cb6bff33b78ad35ee58c729a6}{n\_best}}\ \ \ \ \ \ \ =\ n\_best;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00153}00153\ \ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00154}00154\ \ \ \textcolor{keywordflow}{if}(iq\_mutation\ >\ 32768)} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00155}00155\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00156}00156\ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a5391277564b54e2271856a2466033df5}{iq\_mutation}}\ \ =\ iq\_mutation;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00157}00157\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00158}00158\ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ i\ =\ 0;\ i\ <\ n\_cand;\ i++)\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00159}00159\ \ \ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ j\ =\ 0;\ j\ <\ n\_params;\ j++)\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00160}00160\ \ \ \ \ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a61bd0c8ae322a79936ba1f2ae22e0e0d}{candidates}}[i][j]\ =\ start\_params[j];} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00161}00161\ \ \ \ \ \}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00162}00162\ \ \ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac723b265a3d43b754489f14310314f0d}{loss}}[i]\ =\ 0;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00163}00163\ \ \ \}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00164}00164\ \ \ uint32\_t\ seed\ =\ \mbox{\hyperlink{group___e_v_o_l_v_e___o_p_t_i_m_i_z_e_r_ga9c853b02c22f26023c34d1d404b6d653}{local\_time}}()\ +\ (ADC1-\/>DR\ \&\ 0xFF);} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00165}00165\ \ \ srand(seed);} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00166}00166\ \ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00167}00167\ \ \ \textcolor{keywordflow}{return}\ 0;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00168}00168\ \}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00169}00169\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00170}00170\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00171}00171\ \textcolor{comment}{/**}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00172}00172\ \textcolor{comment}{\ \ *\ @brief\ Один\ шаг\ эволюционного\ оптимизатора.}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00173}00173\ \textcolor{comment}{\ \ *\ @param\ opt\ \ \ \ Указатель\ на\ структуру\ оптимизатора}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00174}00174\ \textcolor{comment}{\ \ *\ @param\ params\ Массив\ параметров,\ которые\ будут\ обновлены\ (на\ выходе\ —\ новые\ параметры)}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00175}00175\ \textcolor{comment}{\ \ *\ @param\ loss\ \ \ Loss\ текущего\ кандидата\ (Q16.16)}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00176}00176\ \textcolor{comment}{\ \ *\ @return\ 0\ \ \ —\ если\ окей,\ }} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00177}00177\ \textcolor{comment}{\ \ *\ \ \ \ \ \ \ \ \ -\/1\ \ —\ если\ ошибка}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00178}00178\ \textcolor{comment}{\ \ *\ @details}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00179}00179\ \textcolor{comment}{\ \ *\ Сохраняет\ loss\ текущего\ кандидата\ и\ формирует\ параметры\ следующего\ кандидата.}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00180}00180\ \textcolor{comment}{\ \ *\ Если\ накоплено\ n\_cand\ кандидатов,\ генерируется\ новое\ поколение.}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00181}00181\ \textcolor{comment}{\ \ *\ Новое\ поколение\ формируется\ случайным\ выбором\ из\ n\_best\ лучших\ с\ добавлением\ мутации.}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00182}00182\ \textcolor{comment}{\ \ *}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00183}00183\ \textcolor{comment}{\ \ *\ На\ выходе\ params\ содержит\ параметры\ следующего\ кандидата\ для\ измерений.}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00184}00184\ \textcolor{comment}{\ \ *\ @note\ Функция\ использует\ глобальную\ внутреннюю\ переменную\ для\ сортировки.}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00185}00185\ \textcolor{comment}{\ \ *\ Надо\ убедится\ что\ только\ один\ экземпляр\ функции\ запущен\ в\ момент\ времени}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00186}00186\ \textcolor{comment}{\ \ */}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00187}\mbox{\hyperlink{group___e_v_o_l_v_e___o_p_t_i_m_i_z_e_r_ga75acd71c85d4e60dc0eb31bc5780086a}{00187}}\ \_\_STATIC\_INLINE\ \textcolor{keywordtype}{int}\ \mbox{\hyperlink{group___e_v_o_l_v_e___o_p_t_i_m_i_z_e_r_ga75acd71c85d4e60dc0eb31bc5780086a}{EvolveOptimizer\_Step}}(\mbox{\hyperlink{struct_evolve_optimizer__t}{EvolveOptimizer\_t}}*\ opt,} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00188}00188\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\_t*\ params,} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00189}00189\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\_t\ \ \ loss)} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00190}00190\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00191}00191\ \ \ \textcolor{keywordflow}{if}((opt\ =\ NULL)\ ||\ (params\ ==\ NULL))} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00192}00192\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00193}00193\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00194}00194\ \ \ uint16\_t\ n\_params\ =\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a1c270fce82e0c97e6c88fd0971c1eed8}{n\_params}};\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00195}00195\ \ \ \textcolor{keywordflow}{if}(n\_params\ >\ \mbox{\hyperlink{group___e_v_o_l_v_e___c_o_n_f_i_g_ga80d6162495fc3776d8902610657d4fee}{EVOLVE\_MAX\_PARAMS}})} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00196}00196\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00197}00197\ \ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00198}00198\ \ \ uint16\_t\ n\_cand\ =\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac35c7ca8cede4ab9c53a2845a91cd1b1}{n\_cand}};\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00199}00199\ \ \ \textcolor{keywordflow}{if}(n\_cand\ >\ \mbox{\hyperlink{group___e_v_o_l_v_e___c_o_n_f_i_g_ga7c1396e5c95c766afccdb45cbfcf5971}{EVOLVE\_MAX\_CANDIDATES}})} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00200}00200\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00201}00201\ \ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00202}00202\ \ \ uint16\_t\ n\_best\ =\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a950cd70cb6bff33b78ad35ee58c729a6}{n\_best}};\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00203}00203\ \ \ \textcolor{keywordflow}{if}(n\_best\ >\ \mbox{\hyperlink{group___e_v_o_l_v_e___c_o_n_f_i_g_ga7c1396e5c95c766afccdb45cbfcf5971}{EVOLVE\_MAX\_CANDIDATES}}/2)} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00204}00204\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00205}00205\ \ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00206}00206\ \ \ uint16\_t\ mut\ =\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a5391277564b54e2271856a2466033df5}{iq\_mutation}};} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00207}00207\ \ \ \textcolor{keywordflow}{if}(mut\ >\ 32768)} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00208}00208\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;\ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00209}00209\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00210}00210\ \ \ \textcolor{comment}{//\ 1.\ Сохраняем\ loss\ текущего\ кандидата}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00211}00211\ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac723b265a3d43b754489f14310314f0d}{loss}}[opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a0ae0e63ba351335d4445d927991c64ae}{cand\_index}}]\ =\ loss;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00212}00212\ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a0ae0e63ba351335d4445d927991c64ae}{cand\_index}}++;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00213}00213\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00214}00214\ \ \ \textcolor{keywordflow}{if}\ (opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a0ae0e63ba351335d4445d927991c64ae}{cand\_index}}\ >=\ n\_cand)\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00215}00215\ \ \ \ \ \textcolor{comment}{//\ 2.\ Сортируем\ текущее\ поколение\ по\ loss}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00216}00216\ \ \ \ \ \textcolor{keywordflow}{for}(uint16\_t\ i\ =\ 0;\ i\ <\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac35c7ca8cede4ab9c53a2845a91cd1b1}{n\_cand}};\ i++)} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00217}00217\ \ \ \ \ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a74b81df1c298c99162310f3f7d91735a}{sorted\_idx}}[i]\ =\ i;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00218}00218\ \ \ \ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00219}00219\ \ \ \ \ g\_sort\_opt\ =\ opt;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00220}00220\ \ \ \ \ qsort(opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a74b81df1c298c99162310f3f7d91735a}{sorted\_idx}},\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac35c7ca8cede4ab9c53a2845a91cd1b1}{n\_cand}},\ \textcolor{keyword}{sizeof}(uint16\_t),\ cmp\_idx);} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00221}00221\ \ \ \ \ g\_sort\_opt\ =\ NULL;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00222}00222\ \textcolor{comment}{//\ \ \ \ for\ (uint16\_t\ i\ =\ 0;\ i\ <\ n\_cand\ -\/\ 1;\ i++)\ \{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00223}00223\ \textcolor{comment}{//\ \ \ \ \ \ for\ (uint16\_t\ j\ =\ i\ +\ 1;\ j\ <\ n\_cand;\ j++)\ \{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00224}00224\ \textcolor{comment}{//\ \ \ \ \ \ \ \ if\ (opt-\/>loss[j]\ <\ opt-\/>loss[i])\ \{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00225}00225\ \textcolor{comment}{//\ \ \ \ \ \ \ \ \ \ int32\_t\ tmp\_loss\ =\ opt-\/>loss[i];}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00226}00226\ \textcolor{comment}{//\ \ \ \ \ \ \ \ \ \ opt-\/>loss[i]\ =\ opt-\/>loss[j];}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00227}00227\ \textcolor{comment}{//\ \ \ \ \ \ \ \ \ \ opt-\/>loss[j]\ =\ tmp\_loss;}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00228}00228\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00229}00229\ \textcolor{comment}{//\ \ \ \ \ \ \ \ \ \ for\ (uint16\_t\ k\ =\ 0;\ k\ <\ n\_params;\ k++)\ \{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00230}00230\ \textcolor{comment}{//\ \ \ \ \ \ \ \ \ \ \ \ int32\_t\ tmp\ =\ opt-\/>candidates[i][k];}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00231}00231\ \textcolor{comment}{//\ \ \ \ \ \ \ \ \ \ \ \ opt-\/>candidates[i][k]\ =\ opt-\/>candidates[j][k];}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00232}00232\ \textcolor{comment}{//\ \ \ \ \ \ \ \ \ \ \ \ opt-\/>candidates[j][k]\ =\ tmp;}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00233}00233\ \textcolor{comment}{//\ \ \ \ \ \ \ \ \ \ \}}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00234}00234\ \textcolor{comment}{//\ \ \ \ \ \ \ \ \}}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00235}00235\ \textcolor{comment}{//\ \ \ \ \ \ \}}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00236}00236\ \textcolor{comment}{//\ \ \ \ \}}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00237}00237\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00238}00238\ \ \ \ \ \textcolor{comment}{//\ 3.\ Генерируем\ новое\ поколение:\ каждый\ кандидат\ берется\ случайно\ из\ лучших\ с\ мутацией}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00239}00239\ \ \ \ \ uint16\_t\ n\_elite\ =\ 2;\ \textcolor{comment}{//\ количество\ элитных\ кандидатов,\ которые\ сохраняем\ без\ изменений}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00240}00240\ \ \ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ c\ =\ 0;\ c\ <\ n\_cand;\ c++)\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00241}00241\ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (c\ <\ n\_elite)\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00242}00242\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ Копируем\ лучших\ кандидатов\ напрямую\ без\ мутации}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00243}00243\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ i\ =\ 0;\ i\ <\ n\_params;\ i++)\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00244}00244\ \ \ \ \ \ \ \ \ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a61bd0c8ae322a79936ba1f2ae22e0e0d}{candidates}}[c][i]\ =\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a61bd0c8ae322a79936ba1f2ae22e0e0d}{candidates}}[opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a74b81df1c298c99162310f3f7d91735a}{sorted\_idx}}[c]][i];\ \textcolor{comment}{//\ просто\ сохраняем\ параметры}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00245}00245\ \ \ \ \ \ \ \ \ \}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00246}00246\ \ \ \ \ \ \ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac723b265a3d43b754489f14310314f0d}{loss}}[c]\ =\ 0;\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00247}00247\ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00248}00248\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ Остальные\ кандидаты\ формируются\ с\ кроссовером\ и\ мутацией}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00249}00249\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ i\ =\ 0;\ i\ <\ n\_params;\ i++)\ \{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00250}00250\ \ \ \ \ \ \ \ \ \ \ int32\_t\ noise\ =\ (rand()\ \%\ (2\ *\ mut))\ -\/\ mut;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00251}00251\ \ \ \ \ \ \ \ \ \ \ uint16\_t\ parent\ =\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a74b81df1c298c99162310f3f7d91735a}{sorted\_idx}}[rand()\ \%\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a950cd70cb6bff33b78ad35ee58c729a6}{n\_best}}];\ \textcolor{comment}{//\ каждый\ параметр\ из\ случайного\ лучшего}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00252}00252\ \ \ \ \ \ \ \ \ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a61bd0c8ae322a79936ba1f2ae22e0e0d}{candidates}}[c][i]\ =\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a61bd0c8ae322a79936ba1f2ae22e0e0d}{candidates}}[parent][i]\ +\ noise;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00253}00253\ \ \ \ \ \ \ \ \ \}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00254}00254\ \ \ \ \ \ \ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_ac723b265a3d43b754489f14310314f0d}{loss}}[c]\ =\ 0;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00255}00255\ \ \ \ \ \ \ \}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00256}00256\ \ \ \ \ \}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00257}00257\ \ \ \ \ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a0ae0e63ba351335d4445d927991c64ae}{cand\_index}}\ =\ 0;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00258}00258\ \ \ \}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00259}00259\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00260}00260\ \ \ \textcolor{comment}{//\ 4.\ Возвращаем\ параметры\ следующего\ кандидата}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00261}00261\ \ \ \textcolor{keywordflow}{for}\ (uint16\_t\ i\ =\ 0;\ i\ <\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a1c270fce82e0c97e6c88fd0971c1eed8}{n\_params}};\ i++)} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00262}00262\ \ \ \ \ params[i]\ =\ opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a61bd0c8ae322a79936ba1f2ae22e0e0d}{candidates}}[opt-\/>\mbox{\hyperlink{struct_evolve_optimizer__t_a0ae0e63ba351335d4445d927991c64ae}{cand\_index}}][i];} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00263}00263\ \ \ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00264}00264\ \ \ \textcolor{keywordflow}{return}\ 0;} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00265}00265\ \}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00266}00266\ \textcolor{preprocessor}{\#else\ }\textcolor{comment}{//\ ENABLE\_EVOLVE\_OPTIMIZATION}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00267}00267\ \textcolor{comment}{//заглушки}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00268}00268\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }\{} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00269}00269\ \ \ uint16\_t\ n\_params;\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00270}00270\ \ \ uint16\_t\ n\_cand;\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00271}00271\ \ \ uint16\_t\ n\_best;\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00272}00272\ \ \ uint16\_t\ iq\_mutation;\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00273}00273\ \ \ int32\_t\ loss[0];\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00274}00274\ \ \ int32\_t\ candidates[0][0];\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00275}00275\ \}\ \mbox{\hyperlink{struct_evolve_optimizer__t}{EvolveOptimizer\_t}};} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00276}00276\ \textcolor{preprocessor}{\#define\ EvolveOptimizer\_Init(opt,\ n\_params,\ n\_cand,\ n\_best,\ iq\_mutation,\ start\_params)}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00277}00277\ \textcolor{preprocessor}{\#define\ EvolveOptimizer\_Step(opt,\ params,\ LossFunc)}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00278}00278\ \textcolor{preprocessor}{\#define\ PARAM\_SCALE\_Q16(x,\ min\_val,\ max\_val)\ \ \ \ \ \ \ \ \ \ \ \ (x)}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00279}00279\ \textcolor{preprocessor}{\#define\ PARAM\_UNSCALE\_Q16(q16\_val,\ min\_val,\ max\_val)\ \ \ \ (q16\_val)}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00280}00280\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{//\ ENABLE\_EVOLVE\_OPTIMIZATION}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00281}00281\ } \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00282}00282\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{//\ \_\_EVOLVE\_OPTIMIZER\_H\_}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00283}00283\ \textcolor{comment}{}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00284}00284\ \textcolor{comment}{/**\ EVOLVE\_OPTIMIZER}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00285}00285\ \textcolor{comment}{\ \ *\ @\}}} \DoxyCodeLine{\Hypertarget{evolve__optimizer_8h_source_l00286}00286\ \textcolor{comment}{\ \ */}} \end{DoxyCode}