555 lines
92 KiB
XML
555 lines
92 KiB
XML
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
|
|
<section xmlns="http://docbook.org/ns/docbook" version="5.0" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="__exponential_8h_source" xml:lang="en-US">
|
|
<title>Exponential.h</title>
|
|
<indexterm><primary>Exponential/Exponential.h</primary></indexterm>
|
|
<programlisting linenumbering="unnumbered">1 <emphasis role="preprocessor">#pragma once</emphasis>
|
|
2 <emphasis role="preprocessor">#ifndef JONATHAN_RAMPERSAD_EXPONENTIAL_H_</emphasis>
|
|
3 <emphasis role="preprocessor">#define JONATHAN_RAMPERSAD_EXPONENTIAL_H_</emphasis>
|
|
4
|
|
5 <emphasis role="preprocessor">#include <ostream></emphasis>
|
|
6 <emphasis role="preprocessor">#include <vector></emphasis>
|
|
7 <emphasis role="preprocessor">#include <float.h></emphasis>
|
|
8 <emphasis role="preprocessor">#include <random></emphasis>
|
|
9 <emphasis role="preprocessor">#include <algorithm></emphasis>
|
|
10 <emphasis role="preprocessor">#include <execution></emphasis>
|
|
11 <emphasis role="preprocessor">#include <exception></emphasis>
|
|
12
|
|
13 <emphasis role="keyword">namespace </emphasis>JRAMPERSAD
|
|
14 {
|
|
15     <emphasis role="keyword">namespace </emphasis>EXPONENTIAL
|
|
16     {
|
|
21         <emphasis role="keyword">struct </emphasis><link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options">GA_Options</link>
|
|
22         {
|
|
24             <emphasis role="keywordtype">double</emphasis> <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options_1a316979973a2a6b70b00520c2f753a43c">min_range</link> = -100;
|
|
26             <emphasis role="keywordtype">double</emphasis> <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options_1a9b8f1e5367f6b0d8b16eecaea53b40e2">max_range</link> = 100;
|
|
28             <emphasis role="keywordtype">unsigned</emphasis> <emphasis role="keywordtype">int</emphasis> <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options_1a4a67bad303f8a4fca40020a0802524c5">num_of_generations</link> = 10;
|
|
30             <emphasis role="keywordtype">unsigned</emphasis> <emphasis role="keywordtype">int</emphasis> <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options_1ad133af29dbbc26b8c3d507d359c03326">sample_size</link> = 1000;
|
|
32             <emphasis role="keywordtype">unsigned</emphasis> <emphasis role="keywordtype">int</emphasis> <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options_1a6ec95fe6cc95dc32727659cf5bb1be12">data_size</link> = 100000;
|
|
34             <emphasis role="keywordtype">double</emphasis> <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options_1a736488b3cfeebda7b93b3e8c6f576bf8">mutation_percentage</link> = 0.01;
|
|
35         };
|
|
36
|
|
37         <emphasis role="keyword">namespace </emphasis>detail
|
|
38         {
|
|
39             <emphasis role="keyword">template</emphasis><<emphasis role="keyword">typename</emphasis> T>
|
|
40             [[nodiscard(<emphasis role="stringliteral">"MATH::ABS(T) returns a value of type T"</emphasis>)]] T ABS(<emphasis role="keyword">const</emphasis> T& n) <emphasis role="keyword">noexcept</emphasis>
|
|
41             {
|
|
42                 <emphasis role="keywordflow">return</emphasis> n < 0 ? n * -1 : n;
|
|
43             }
|
|
44
|
|
45             <emphasis role="keyword">template</emphasis><<emphasis role="keyword">typename</emphasis> T>
|
|
46             [[nodiscard(<emphasis role="stringliteral">"MATH::NEGATE(T) returns a value of type T"</emphasis>)]] T NEGATE(<emphasis role="keyword">const</emphasis> T& n) <emphasis role="keyword">noexcept</emphasis>
|
|
47             {
|
|
48                 <emphasis role="keywordflow">return</emphasis> n * -1;
|
|
49             }
|
|
50
|
|
51             <emphasis role="keyword">template</emphasis><<emphasis role="keyword">typename</emphasis> T>
|
|
52             [[nodiscard(<emphasis role="stringliteral">"MATH::POW(T, int) returns a value of type T"</emphasis>)]] T POW(<emphasis role="keyword">const</emphasis> T& n, <emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">int</emphasis>& exp) <emphasis role="keyword">noexcept</emphasis>
|
|
53             {
|
|
54                 <emphasis role="keywordflow">if</emphasis> (exp == 0)
|
|
55                     <emphasis role="keywordflow">return</emphasis> 1;
|
|
56
|
|
57                 T res = n;
|
|
58                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">int</emphasis> i = 1; i < exp; i++)
|
|
59                 {
|
|
60                     res *= n;
|
|
61                 }
|
|
62
|
|
63                 <emphasis role="keywordflow">return</emphasis> res;
|
|
64             }
|
|
65
|
|
66             <emphasis role="keyword">template</emphasis><<emphasis role="keyword">typename</emphasis> T>
|
|
67             [[nodiscard(<emphasis role="stringliteral">"MATH::SUM(std::vector<T>) returns a value of type T"</emphasis>)]] T SUM(<emphasis role="keyword">const</emphasis> std::vector<T>& vec) <emphasis role="keyword">noexcept</emphasis>
|
|
68             {
|
|
69                 T res{};
|
|
70                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& val : vec)
|
|
71                     res += val;
|
|
72                 <emphasis role="keywordflow">return</emphasis> res;
|
|
73             }
|
|
74
|
|
75             <emphasis role="keyword">template</emphasis><<emphasis role="keyword">typename</emphasis> T>
|
|
76             [[nodiscard]] T MEDIAN(std::vector<T> vec) <emphasis role="keyword">noexcept</emphasis>
|
|
77             {
|
|
78                 std::sort(
|
|
79                     vec.begin(),
|
|
80                     vec.end(),
|
|
81                     [](<emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& lhs, <emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& rhs) {
|
|
82                         return lhs < rhs;
|
|
83                     });
|
|
84
|
|
85                 <emphasis role="keywordflow">return</emphasis> vec[vec.size() / 2];
|
|
86             }
|
|
87
|
|
88             <emphasis role="keyword">template</emphasis><<emphasis role="keyword">typename</emphasis> T>
|
|
89             [[nodiscard]] <emphasis role="keywordtype">double</emphasis> MEAN(<emphasis role="keyword">const</emphasis> std::vector<T>& vec) <emphasis role="keyword">noexcept</emphasis>
|
|
90             {
|
|
91                 <emphasis role="keywordflow">return</emphasis> SUM(vec) / vec.size();
|
|
92             }
|
|
93
|
|
94             <emphasis role="keyword">template</emphasis><<emphasis role="keyword">typename</emphasis> T>
|
|
95             [[noreturn]] <emphasis role="keywordtype">void</emphasis> SortASC(std::vector<T>& vec)
|
|
96             {
|
|
97                 std::sort(
|
|
98                     std::execution::par,
|
|
99                     vec.begin(), vec.end(),
|
|
100                     [](<emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& lhs, <emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& rhs) {
|
|
101                         return lhs < rhs;
|
|
102                     });
|
|
103             }
|
|
104
|
|
105             <emphasis role="keyword">template</emphasis><<emphasis role="keyword">typename</emphasis> T>
|
|
106             [[noreturn]] <emphasis role="keywordtype">void</emphasis> SortDESC(std::vector<T>& vec)
|
|
107             {
|
|
108                 std::sort(
|
|
109                     std::execution::par,
|
|
110                     vec.begin(), vec.end(),
|
|
111                     [](<emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& lhs, <emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& rhs) {
|
|
112                         return lhs > rhs;
|
|
113                     });
|
|
114             }
|
|
115
|
|
116             <emphasis role="keyword">template</emphasis> <<emphasis role="keywordtype">int</emphasis> lrgst_expo> <emphasis role="comment">// Genetic Algorithm helper struct</emphasis>
|
|
117             <emphasis role="keyword">struct </emphasis>GA_Solution
|
|
118             {
|
|
119                 <emphasis role="keywordtype">double</emphasis> rank, x, y_val;
|
|
120                 <emphasis role="keywordtype">bool</emphasis> ranked;
|
|
121
|
|
122                 GA_Solution() : rank(0), x(0), y_val(0), ranked(false) {}
|
|
123                 GA_Solution(<emphasis role="keywordtype">double</emphasis> Rank, <emphasis role="keywordtype">double</emphasis> x_val, <emphasis role="keywordtype">double</emphasis> y = 0) : rank(Rank), x(x_val), y_val(y), ranked(false) {}
|
|
124                 <emphasis role="keyword">virtual</emphasis> ~GA_Solution() = <emphasis role="keywordflow">default</emphasis>;
|
|
125
|
|
126                 <emphasis role="keywordtype">void</emphasis> fitness(<emphasis role="keyword">const</emphasis> std::vector<int>& constants)
|
|
127                 {
|
|
128                     <emphasis role="keywordtype">double</emphasis> ans = 0;
|
|
129                     <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">int</emphasis> i = lrgst_expo; i >= 0; i--)
|
|
130                         ans += constants[i] * POW(x, (lrgst_expo - i));
|
|
131
|
|
132                     ans -= y_val;
|
|
133                     rank = (ans == 0) ? DBL_MAX : ABS(1 / ans);
|
|
134                 }
|
|
135             };
|
|
136         }
|
|
137
|
|
138         <emphasis role="keyword">using namespace </emphasis>detail;
|
|
143         <emphasis role="keyword">template</emphasis> <<emphasis role="keywordtype">int</emphasis> lrgst_expo>
|
|
144         <emphasis role="keyword">class </emphasis><link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>
|
|
145         {
|
|
146         <emphasis role="keyword">private</emphasis>:
|
|
147             std::vector<int> constants;
|
|
148
|
|
149         <emphasis role="keyword">public</emphasis>:
|
|
150             <emphasis role="comment">// Speicialty function to get the real roots of a Quadratic Function without relying on a Genetic Algorithm to approximate</emphasis>
|
|
151             <emphasis role="keyword">friend</emphasis> std::vector<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function_1a8f5b8975b6e7318c093a963cd0b43db6">QuadraticSolve</link>(<emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<2></link>& f);
|
|
152
|
|
153         <emphasis role="keyword">public</emphasis>:
|
|
158             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>(<emphasis role="keyword">const</emphasis> std::vector<int>& constnts);
|
|
163             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>(std::vector<int>&& constnts);
|
|
164             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>(<emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>& other) = <emphasis role="keywordflow">default</emphasis>;
|
|
165             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>&& other) <emphasis role="keyword">noexcept</emphasis> = <emphasis role="keywordflow">default</emphasis>;
|
|
166             <emphasis role="keyword">virtual</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">~Function</link>();
|
|
167
|
|
168             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>& operator=(<emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>& other) = <emphasis role="keywordflow">default</emphasis>;
|
|
169             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>& operator=(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link>&& other) <emphasis role="keyword">noexcept</emphasis> = <emphasis role="keywordflow">default</emphasis>;
|
|
170
|
|
171             <emphasis role="comment">// Operator function to display function object in a human readable format</emphasis>
|
|
172             <emphasis role="keyword">friend</emphasis> std::ostream& operator<<(std::ostream& os, <emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo></link> func)
|
|
173             {
|
|
174                 <emphasis role="keywordflow">if</emphasis> (lrgst_expo == 0)
|
|
175                 {
|
|
176                     os << func.constants[0];
|
|
177                     <emphasis role="keywordflow">return</emphasis> os;
|
|
178                 }
|
|
179
|
|
180                 <emphasis role="keywordflow">if</emphasis> (func.constants[0] == 1)
|
|
181                     os << <emphasis role="stringliteral">"x"</emphasis>;
|
|
182                 <emphasis role="keywordflow">else</emphasis> <emphasis role="keywordflow">if</emphasis> (func.constants[0] == -1)
|
|
183                     os << <emphasis role="stringliteral">"-x"</emphasis>;
|
|
184                 <emphasis role="keywordflow">else</emphasis>
|
|
185                     os << func.constants[0] << <emphasis role="stringliteral">"x"</emphasis>;
|
|
186
|
|
187                 <emphasis role="keywordflow">if</emphasis> (lrgst_expo != 1)
|
|
188                     os << <emphasis role="stringliteral">"^"</emphasis> << lrgst_expo;
|
|
189
|
|
190                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">int</emphasis> i = lrgst_expo - 1; i > 0; i--)
|
|
191                 {
|
|
192                     <emphasis role="keywordtype">int</emphasis> n = func.constants[lrgst_expo - i];
|
|
193                     <emphasis role="keywordflow">if</emphasis> (n == 0) <emphasis role="keywordflow">continue</emphasis>;
|
|
194
|
|
195                     <emphasis role="keyword">auto</emphasis> s = n > 0 ? <emphasis role="stringliteral">" + "</emphasis> : <emphasis role="stringliteral">" - "</emphasis>;
|
|
196
|
|
197                     <emphasis role="keywordflow">if</emphasis> (n != 1)
|
|
198                         os << s << ABS(n) << <emphasis role="stringliteral">"x"</emphasis>;
|
|
199                     <emphasis role="keywordflow">else</emphasis>
|
|
200                         os << s << <emphasis role="stringliteral">"x"</emphasis>;
|
|
201
|
|
202                     <emphasis role="keywordflow">if</emphasis> (i != 1)
|
|
203                         os << <emphasis role="stringliteral">"^"</emphasis> << i;
|
|
204                 }
|
|
205
|
|
206                 <emphasis role="keywordtype">int</emphasis> n = func.constants[lrgst_expo];
|
|
207                 <emphasis role="keywordflow">if</emphasis> (n == 0) <emphasis role="keywordflow">return</emphasis> os;
|
|
208
|
|
209                 <emphasis role="keyword">auto</emphasis> s = n > 0 ? <emphasis role="stringliteral">" + "</emphasis> : <emphasis role="stringliteral">" - "</emphasis>;
|
|
210                 os << s;
|
|
211
|
|
212                 os << ABS(n);
|
|
213
|
|
214                 <emphasis role="keywordflow">return</emphasis> os;
|
|
215             }
|
|
216
|
|
217             <emphasis role="keyword">template</emphasis><<emphasis role="keywordtype">int</emphasis> e1, <emphasis role="keywordtype">int</emphasis> e2, <emphasis role="keywordtype">int</emphasis> r>
|
|
218             <emphasis role="keyword">friend</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<r></link> operator+(<emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<e1></link>& f1, <emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<e2></link>& f2); <emphasis role="comment">// Operator to add two functions</emphasis>
|
|
219             <emphasis role="keyword">template</emphasis><<emphasis role="keywordtype">int</emphasis> e1, <emphasis role="keywordtype">int</emphasis> e2, <emphasis role="keywordtype">int</emphasis> r>
|
|
220             <emphasis role="keyword">friend</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<r></link> operator-(<emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<e1></link>& f1, <emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<e2></link>& f2); <emphasis role="comment">// Operator to subtract two functions</emphasis>
|
|
221
|
|
222             <emphasis role="comment">// Operators to multiply a function by a constant (Scaling it)</emphasis>
|
|
223             <emphasis role="keyword">friend</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo></link> operator*(<emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo></link>& f, <emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">int</emphasis>& c)
|
|
224             {
|
|
225                 <emphasis role="keywordflow">if</emphasis> (c == 1) <emphasis role="keywordflow">return</emphasis> f;
|
|
226                 <emphasis role="keywordflow">if</emphasis> (c == 0) <emphasis role="keywordflow">throw</emphasis> std::logic_error(<emphasis role="stringliteral">"Cannot multiply a function by 0"</emphasis>);
|
|
227
|
|
228                 std::vector<int> res;
|
|
229                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& val : f.constants)
|
|
230                     res.push_back(c * val);
|
|
231
|
|
232                 <emphasis role="keywordflow">return</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo></link>(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">res</link>);
|
|
233             }
|
|
234             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo></link>& operator*=(<emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">int</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">c</link>)
|
|
235             {
|
|
236                 <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">c</link> == 1) <emphasis role="keywordflow">return</emphasis> *<emphasis role="keyword">this</emphasis>;
|
|
237                 <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">c</link> == 0) <emphasis role="keywordflow">throw</emphasis> std::logic_error(<emphasis role="stringliteral">"Cannot multiply a function by 0"</emphasis>);
|
|
238
|
|
239                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">val</link> : this->constants)
|
|
240                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">val</link> *= <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">c</link>;
|
|
241
|
|
242                 <emphasis role="keywordflow">return</emphasis> *<emphasis role="keyword">this</emphasis>;
|
|
243             }
|
|
244
|
|
249             [[<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">nodiscard</link>(<emphasis role="stringliteral">"MATH::EXP::Function::differential() returns the differential, the calling object is not changed"</emphasis>)]]
|
|
250             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link><<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link> - 1> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function_1ae43c705b427ac1ef27aed061a63e500e">differential</link>() <emphasis role="keyword">const</emphasis>;
|
|
251
|
|
257             [[<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">nodiscard</link>]] std::vector<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function_1ad090de9f6636094f14f1279615fccbc0">get_real_roots</link>(<emphasis role="keyword">const</emphasis> <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options">GA_Options</link>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link> = <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options">GA_Options</link>()) <emphasis role="keyword">const</emphasis>;
|
|
258
|
|
264             [[<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">nodiscard</link>]] <emphasis role="keywordtype">double</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function_1a5464547daff0c43faccdc40ea480bab4">solve_y</link>(<emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">double</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">x_val</link>) <emphasis role="keyword">const</emphasis> <emphasis role="keyword">noexcept</emphasis>;
|
|
265
|
|
272             [[<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">nodiscard</link>]] std::vector<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function_1a46b9671c4a29b2b2b34586048a3b795a">solve_x</link>(<emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">double</emphasis>& y_val, <emphasis role="keyword">const</emphasis> <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options">GA_Options</link>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link> = <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options">GA_Options</link>()) <emphasis role="keyword">const</emphasis>;
|
|
273         };
|
|
274
|
|
280         std::vector<double> QuadraticSolve(<emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<2></link>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">f</link>)
|
|
281         {
|
|
282             std::vector<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">res</link>;
|
|
283
|
|
284             <emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">int</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">a</link> = <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">f</link>.constants[0];
|
|
285             <emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">int</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">b</link> = <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">f</link>.constants[1];
|
|
286             <emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">int</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">c</link> = <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">f</link>.constants[2];
|
|
287
|
|
288             <emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">double</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sqr_val</link> = <emphasis role="keyword">static_cast<</emphasis><emphasis role="keywordtype">double</emphasis><emphasis role="keyword">></emphasis>(POW(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">b</link>, 2) - (4 * <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">a</link> * <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">c</link>));
|
|
289
|
|
290             <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sqr_val</link> < 0)
|
|
291             {
|
|
292                 <emphasis role="keywordflow">return</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">res</link>;
|
|
293             }
|
|
294
|
|
295             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">res</link>.push_back(((NEGATE(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">b</link>) + <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sqrt</link>(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sqr_val</link>)) / 2 * <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">a</link>));
|
|
296             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">res</link>.push_back(((NEGATE(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">b</link>) - <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sqrt</link>(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sqr_val</link>)) / 2 * <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">a</link>));
|
|
297             <emphasis role="keywordflow">return</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">res</link>;
|
|
298         }
|
|
299
|
|
300         <emphasis role="keyword">template</emphasis><<emphasis role="keywordtype">int</emphasis> e1, <emphasis role="keywordtype">int</emphasis> e2, <emphasis role="keywordtype">int</emphasis> r = (e1 > <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">e2</link> ? <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">e1</link> : <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">e2</link>)>
|
|
301             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<r></link> <emphasis role="keyword">operator</emphasis>+(<emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<e1></link>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">f1</link>, <emphasis role="keyword">const</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<e2></link>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">f2</link>)
|
|
302         {
|
|
303             std::vector<int> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">res</link>;
|
|
304             <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">e1</link> > <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">e2</link>)
|
|
305             {
|
|
306                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">val</link> : <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">f1</link>.constants)
|
|
307                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">res</link>.<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">push_back</link>(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">val</link>);
|
|
308
|
|
309                 <emphasis role="keywordtype">int</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> = <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">e1</link> - <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">e2</link>;
|
|
310                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">val</link> : <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">f2</link>.constants)
|
|
311                 {
|
|
312                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">res</link>[<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>] += <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">val</link>;
|
|
313                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>++;
|
|
314                 }
|
|
315             }
|
|
316             <emphasis role="keywordflow">else</emphasis>
|
|
317             {
|
|
318                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& val : f2.constants)
|
|
319                     res.push_back(val);
|
|
320
|
|
321                 <emphasis role="keywordtype">int</emphasis> i = e2 - e1;
|
|
322                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& val : f1.constants)
|
|
323                 {
|
|
324                     res[i] += val;
|
|
325                     i++;
|
|
326                 }
|
|
327             }
|
|
328
|
|
329             <emphasis role="keywordflow">return</emphasis> Function<r>{res};
|
|
330         }
|
|
331
|
|
332         <emphasis role="keyword">template</emphasis><<emphasis role="keywordtype">int</emphasis> e1, <emphasis role="keywordtype">int</emphasis> e2, <emphasis role="keywordtype">int</emphasis> r = (e1 > e2 ? e1 : e2)>
|
|
333             Function<r> <emphasis role="keyword">operator</emphasis>-(<emphasis role="keyword">const</emphasis> Function<e1>& f1, <emphasis role="keyword">const</emphasis> Function<e2>& f2)
|
|
334         {
|
|
335             std::vector<int> res;
|
|
336             <emphasis role="keywordflow">if</emphasis> (e1 > e2)
|
|
337             {
|
|
338                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& val : f1.constants)
|
|
339                     res.push_back(val);
|
|
340
|
|
341                 <emphasis role="keywordtype">int</emphasis> i = e1 - e2;
|
|
342                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& val : f2.constants)
|
|
343                 {
|
|
344                     res[i] -= val;
|
|
345                     i++;
|
|
346                 }
|
|
347             }
|
|
348             <emphasis role="keywordflow">else</emphasis>
|
|
349             {
|
|
350                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& val : f2.constants)
|
|
351                     res.push_back(val);
|
|
352
|
|
353                 <emphasis role="keywordtype">int</emphasis> i = e2 - e1;
|
|
354
|
|
355                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">int</emphasis> j = 0; j < i; j++)
|
|
356                     res[j] *= -1;
|
|
357
|
|
358                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& val : f1.constants)
|
|
359                 {
|
|
360                     res[i] = val - res[i];
|
|
361                     i++;
|
|
362                 }
|
|
363             }
|
|
364
|
|
365             <emphasis role="keywordflow">return</emphasis> Function<r>{res};
|
|
366         }
|
|
367
|
|
368         <emphasis role="keyword">template</emphasis> <<emphasis role="keywordtype">int</emphasis> lrgst_expo>
|
|
369         <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo>::Function</link>(<emphasis role="keyword">const</emphasis> std::vector<int>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">constnts</link>)
|
|
370         {
|
|
371             <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link> < 0)
|
|
372                 <emphasis role="keywordflow">throw</emphasis> std::logic_error(<emphasis role="stringliteral">"Function template argument must not be less than 0"</emphasis>);
|
|
373
|
|
374             <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">constnts</link>.size() != <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link> + 1)
|
|
375                 <emphasis role="keywordflow">throw</emphasis> std::logic_error(<emphasis role="stringliteral">"Function<n> must be created with (n+1) integers in vector object"</emphasis>);
|
|
376
|
|
377             <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">constnts</link>[0] == 0)
|
|
378                 <emphasis role="keywordflow">throw</emphasis> std::logic_error(<emphasis role="stringliteral">"First value should not be 0"</emphasis>);
|
|
379
|
|
380             constants = <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">constnts</link>;
|
|
381         }
|
|
382
|
|
383         <emphasis role="keyword">template</emphasis><<emphasis role="keywordtype">int</emphasis> lrgst_expo>
|
|
384         <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo>::Function</link>(std::vector<int>&& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">constnts</link>)
|
|
385         {
|
|
386             <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link> < 0)
|
|
387                 <emphasis role="keywordflow">throw</emphasis> std::logic_error(<emphasis role="stringliteral">"Function template argument must not be less than 0"</emphasis>);
|
|
388
|
|
389             <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">constnts</link>.size() != <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link> + 1)
|
|
390                 <emphasis role="keywordflow">throw</emphasis> std::logic_error(<emphasis role="stringliteral">"Function<n> must be created with (n+1) integers in vector object"</emphasis>);
|
|
391
|
|
392             <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">constnts</link>[0] == 0)
|
|
393                 <emphasis role="keywordflow">throw</emphasis> std::logic_error(<emphasis role="stringliteral">"First value should not be 0"</emphasis>);
|
|
394
|
|
395             constants = std::move(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">constnts</link>);
|
|
396         }
|
|
397
|
|
398         <emphasis role="keyword">template</emphasis> <<emphasis role="keywordtype">int</emphasis> lrgst_expo>
|
|
399         <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo>::~Function</link>()
|
|
400         {
|
|
401             constants.clear();
|
|
402         }
|
|
403
|
|
404         <emphasis role="keyword">template</emphasis> <<emphasis role="keywordtype">int</emphasis> lrgst_expo>
|
|
405         <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link><<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link> - 1> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo>::differential</link>()<emphasis role="keyword"> const</emphasis>
|
|
406 <emphasis role="keyword">        </emphasis>{
|
|
407             <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link> == 0)
|
|
408                 <emphasis role="keywordflow">throw</emphasis> std::logic_error(<emphasis role="stringliteral">"Cannot differentiate a number (Function<0>)"</emphasis>);
|
|
409
|
|
410             std::vector<int> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">result</link>;
|
|
411             <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">int</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> = 0; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> < <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link>; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>++)
|
|
412             {
|
|
413                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">result</link>.push_back(constants[<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>] * (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link> - <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>));
|
|
414             }
|
|
415
|
|
416             <emphasis role="keywordflow">return</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function</link><<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link> - 1>{<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">result</link>};
|
|
417         }
|
|
418
|
|
419         <emphasis role="keyword">template</emphasis><<emphasis role="keywordtype">int</emphasis> lrgst_expo>
|
|
420         std::vector<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo>::get_real_roots</link>(<emphasis role="keyword">const</emphasis> <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options">GA_Options</link>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>)<emphasis role="keyword"> const</emphasis>
|
|
421 <emphasis role="keyword">        </emphasis>{
|
|
422             <emphasis role="comment">// Create initial random solutions</emphasis>
|
|
423             std::random_device <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">device</link>;
|
|
424             std::uniform_real_distribution<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">unif</link>(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.min_range, <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.max_range);
|
|
425             std::vector<GA_Solution<lrgst_expo>> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>;
|
|
426
|
|
427             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.resize(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.data_size);
|
|
428             <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">unsigned</emphasis> <emphasis role="keywordtype">int</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> = 0; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> < <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.sample_size; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>++)
|
|
429                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>[<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>] = (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">GA_Solution<lrgst_expo></link>{0, <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">unif</link>(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">device</link>)});
|
|
430
|
|
431             <emphasis role="keywordtype">float</emphasis> timer{ 0 };
|
|
432
|
|
433             <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">unsigned</emphasis> <emphasis role="keywordtype">int</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">count</link> = 0; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">count</link> < <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.num_of_generations; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">count</link>++)
|
|
434             {
|
|
435                 std::generate(std::execution::par, <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.begin() + <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.sample_size, <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.end(), [&<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">unif</link>, &<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">device</link>]() {
|
|
436                     return GA_Solution<lrgst_expo>{0, unif(device)};
|
|
437                     });
|
|
438
|
|
439                 <emphasis role="comment">// Run our fitness function</emphasis>
|
|
440                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">s</link> : <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>) { <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">s</link>.fitness(constants); }
|
|
441
|
|
442                 <emphasis role="comment">// Sort our solutions by rank</emphasis>
|
|
443                 std::sort(std::execution::par, <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.begin(), <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.end(),
|
|
444                     [](<emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lhs</link>, <emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">rhs</link>) {
|
|
445                         return lhs.rank > rhs.rank;
|
|
446                     });
|
|
447
|
|
448                 <emphasis role="comment">// Take top solutions</emphasis>
|
|
449                 std::vector<GA_Solution<lrgst_expo>> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>;
|
|
450                 std::copy(
|
|
451                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.begin(),
|
|
452                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.begin() + <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.sample_size,
|
|
453                     std::back_inserter(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>)
|
|
454                 );
|
|
455                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.clear();
|
|
456
|
|
457                 <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">count</link> + 1 == <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.num_of_generations)
|
|
458                 {
|
|
459                     std::copy(
|
|
460                         <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.begin(),
|
|
461                         <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.end(),
|
|
462                         std::back_inserter(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>)
|
|
463                     );
|
|
464                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.clear();
|
|
465                     <emphasis role="keywordflow">break</emphasis>;
|
|
466                 }
|
|
467
|
|
468                 <emphasis role="comment">// Mutate the top solutions by %</emphasis>
|
|
469                 std::uniform_real_distribution<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">m</link>((1 - <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.mutation_percentage), (1 + <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.mutation_percentage));
|
|
470                 std::for_each(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.begin(), <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.end(), [&<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">m</link>, &<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">device</link>](<emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">s</link>) {
|
|
471                     s.x *= m(device);
|
|
472                     });
|
|
473
|
|
474                 <emphasis role="comment">// Cross over not needed as it's only one value</emphasis>
|
|
475
|
|
476                 std::copy(
|
|
477                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.begin(),
|
|
478                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.end(),
|
|
479                     std::back_inserter(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>)
|
|
480                 );
|
|
481                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.clear();
|
|
482                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.resize(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.data_size);
|
|
483             }
|
|
484
|
|
485             std::sort(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.begin(), <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.end(),
|
|
486                 [](<emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lhs</link>, <emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">rhs</link>) {
|
|
487                     return lhs.x < rhs.x;
|
|
488                 });
|
|
489
|
|
490             std::vector<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">ans</link>;
|
|
491             <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">s</link> : <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>)
|
|
492             {
|
|
493                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">ans</link>.push_back(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">s</link>.x);
|
|
494             }
|
|
495             <emphasis role="keywordflow">return</emphasis> ans;
|
|
496         }
|
|
497
|
|
498         <emphasis role="keyword">template</emphasis><<emphasis role="keywordtype">int</emphasis> lrgst_expo>
|
|
499         <emphasis role="keywordtype">double</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo>::solve_y</link>(<emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">double</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">x_val</link>) <emphasis role="keyword">const</emphasis> <emphasis role="keyword">noexcept</emphasis>
|
|
500         {
|
|
501             std::vector<bool> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">exceptions</link>;
|
|
502
|
|
503             <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">int</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> : constants)
|
|
504                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">exceptions</link>.push_back(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> != 0);
|
|
505
|
|
506             <emphasis role="keywordtype">double</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">ans</link>{ 0 };
|
|
507             <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">int</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> = <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link>; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> >= 0; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>--)
|
|
508             {
|
|
509                 <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">exceptions</link>[<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>])
|
|
510                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">ans</link> += constants[<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>] * POW(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">x_val</link>, (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lrgst_expo</link> - <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>));
|
|
511             }
|
|
512
|
|
513             <emphasis role="keywordflow">return</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">ans</link>;
|
|
514         }
|
|
515
|
|
516         <emphasis role="keyword">template</emphasis><<emphasis role="keywordtype">int</emphasis> lrgst_expo>
|
|
517         <emphasis role="keyword">inline</emphasis> std::vector<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">Function<lrgst_expo>::solve_x</link>(<emphasis role="keyword">const</emphasis> <emphasis role="keywordtype">double</emphasis>& y_val, <emphasis role="keyword">const</emphasis> <link linkend="_struct_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_g_a___options">GA_Options</link>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>)<emphasis role="keyword"> const</emphasis>
|
|
518 <emphasis role="keyword">        </emphasis>{
|
|
519             <emphasis role="comment">// Create initial random solutions</emphasis>
|
|
520             std::random_device <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">device</link>;
|
|
521             std::uniform_real_distribution<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">unif</link>(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.min_range, <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.max_range);
|
|
522             std::vector<GA_Solution<lrgst_expo>> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>;
|
|
523
|
|
524             <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.resize(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.data_size);
|
|
525             <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">unsigned</emphasis> <emphasis role="keywordtype">int</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> = 0; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link> < <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.sample_size; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>++)
|
|
526                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>[<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">i</link>] = (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">GA_Solution<lrgst_expo></link>{0, <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">unif</link>(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">device</link>), y_val});
|
|
527
|
|
528             <emphasis role="keywordflow">for</emphasis> (<emphasis role="keywordtype">unsigned</emphasis> <emphasis role="keywordtype">int</emphasis> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">count</link> = 0; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">count</link> < <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.num_of_generations; <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">count</link>++)
|
|
529             {
|
|
530                 std::generate(std::execution::par, <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.begin() + <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.sample_size, <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.end(), [&<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">unif</link>, &<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">device</link>, &y_val]() {
|
|
531                     return GA_Solution<lrgst_expo>{0, unif(device), y_val};
|
|
532                     });
|
|
533
|
|
534
|
|
535                 <emphasis role="comment">// Run our fitness function</emphasis>
|
|
536                 <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">s</link> : <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>) { <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">s</link>.fitness(constants); }
|
|
537
|
|
538                 <emphasis role="comment">// Sort our solutions by rank</emphasis>
|
|
539                 std::sort(std::execution::par, <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.begin(), <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.end(),
|
|
540                     [](<emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lhs</link>, <emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">rhs</link>) {
|
|
541                         return lhs.rank > rhs.rank;
|
|
542                     });
|
|
543
|
|
544                 <emphasis role="comment">// Take top solutions</emphasis>
|
|
545                 std::vector<GA_Solution<lrgst_expo>> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>;
|
|
546                 std::copy(
|
|
547                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.begin(),
|
|
548                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.begin() + <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.sample_size,
|
|
549                     std::back_inserter(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>)
|
|
550                 );
|
|
551                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.clear();
|
|
552
|
|
553                 <emphasis role="keywordflow">if</emphasis> (<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">count</link> + 1 == <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.num_of_generations)
|
|
554                 {
|
|
555                     std::copy(
|
|
556                         <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.begin(),
|
|
557                         <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.end(),
|
|
558                         std::back_inserter(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>)
|
|
559                     );
|
|
560                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.clear();
|
|
561                     <emphasis role="keywordflow">break</emphasis>;
|
|
562                 }
|
|
563
|
|
564                 <emphasis role="comment">// Mutate the top solutions by %</emphasis>
|
|
565                 std::uniform_real_distribution<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">m</link>((1 - <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.mutation_percentage), (1 + <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.mutation_percentage));
|
|
566                 std::for_each(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.begin(), <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.end(), [&<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">m</link>, &<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">device</link>](<emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">s</link>) {
|
|
567                     s.x *= m(device);
|
|
568                     });
|
|
569
|
|
570                 <emphasis role="comment">// Cross over not needed as it's only one value</emphasis>
|
|
571
|
|
572                 std::copy(
|
|
573                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.begin(),
|
|
574                     <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.end(),
|
|
575                     std::back_inserter(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>)
|
|
576                 );
|
|
577                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">sample</link>.clear();
|
|
578                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.resize(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">options</link>.data_size);
|
|
579             }
|
|
580
|
|
581             std::sort(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.begin(), <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>.end(),
|
|
582                 [](<emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">lhs</link>, <emphasis role="keyword">const</emphasis> <emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">rhs</link>) {
|
|
583                     return lhs.x < rhs.x;
|
|
584                 });
|
|
585
|
|
586             std::vector<double> <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">ans</link>;
|
|
587             <emphasis role="keywordflow">for</emphasis> (<emphasis role="keyword">auto</emphasis>& <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">s</link> : <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">solutions</link>)
|
|
588             {
|
|
589                 <link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">ans</link>.push_back(<link linkend="_class_j_r_a_m_p_e_r_s_a_d_1_1_e_x_p_o_n_e_n_t_i_a_l_1_1_function">s</link>.x);
|
|
590             }
|
|
591             <emphasis role="keywordflow">return</emphasis> ans;
|
|
592         }
|
|
593     }
|
|
594 }
|
|
595
|
|
596 <emphasis role="preprocessor">#endif </emphasis><emphasis role="comment">// !JONATHAN_RAMPERSAD_EXPONENTIAL_H_</emphasis>
|
|
</programlisting></section>
|