remove \r
[extl.git] / extl / intelligence / ga / fopti / test / fopti_ga_test.h
blob4fa3fdbb1618fa77850a3c7fe158b8c786e827a6
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: fopti_ga_test.h
4 * Created: 08.11.13
5 * Updated: 08.11.13
7 * Brief: Unit-testing
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_INTELLIGENCE_GA_FOPTI_GA_TEST_H
13 #define EXTL_INTELLIGENCE_GA_FOPTI_GA_TEST_H
14 #include "../fopti.h"
15 /* ///////////////////////////////////////////////////////////////////////
16 * unit_test namespace
18 EXTL_INTELLIGENCE_BEGIN_WHOLE_NAMESPACE
19 EXTL_TEST_NAME_BEGIN_NAMESPACE(fopti_ga_test)
21 /* ///////////////////////////////////////////////////////////////////////
22 * Unit-testing
24 struct fopti_ga_test
26 // for maximum optimization
27 // [-1, 20]
28 // x=19.850052
29 // max f(x)=21.850026
30 static double ga1_func(double x)
32 return x * sin(10 * 3.1415926 * x) + 2.0;
35 // x >= -1 => -1 * (x + 1) <= 0
36 static double ga1_cons_1(double x)
38 return -1 *(x + 1);
41 // x <= 20 => (x - 20) <= 0
42 static double ga1_cons_2(double x)
44 return (x - 20);
47 // for minimum optimization
48 // [-100, 100]
49 // (x, y): (-0.0898, 0.7126) or (0.0898, -0.7126)
50 // min f(x, y)=-1.031628
51 static double ga2_func(double x, double y)
53 return (4 - 2.1 * x * x + x * x * x * x / 3) * x * x + x * y + (-4 + 4 * y * y) * y * y;
56 // x >= -100 => -1 * (x + 100) <= 0
57 static double ga2_cons_1(double x, double /*y*/)
59 return -1 *(x + 100);
62 // x <= 100 => (x - 100) <= 0
63 static double ga2_cons_2(double x, double /*y*/)
65 return (x - 100);
68 // y >= -100 => -1 * (y + 100) <= 0
69 static double ga2_cons_3(double /*x*/, double y)
71 return -1 *(y + 100);
74 // y <= 100 => (y - 100) <= 0
75 static double ga2_cons_4(double /*x*/, double y)
77 return (y - 100);
80 // for maximum optimization
81 // [-100, 100]
82 // (x, y): (0, 0)
83 // min f(x, y)=1
84 static double ga3_func(double x, double y)
86 double a = sin(sqrt(x * x + y * y));
87 double b = (1 + 0.001 * (x * x + y * y));
88 return 0.5 - (a * a - 0.5) / (b * b);
91 // x >= -100 => -1 * (x + 100) <= 0
92 static double ga3_cons_1(double x, double /*y*/)
94 return -1 *(x + 100);
97 // x <= 100 => (x - 100) <= 0
98 static double ga3_cons_2(double x, double /*y*/)
100 return (x - 100);
103 // y >= -100 => -1 * (y + 100) <= 0
104 static double ga3_cons_3(double /*x*/, double y)
106 return -1 *(y + 100);
109 // y <= 100 => (y - 100) <= 0
110 static double ga3_cons_4(double /*x*/, double y)
112 return (y - 100);
115 // for minimum optimization
116 // [0, ...]
117 // (x, y): (1, 1)
118 // min f(x, y)=10
119 static double ga4_func(double x, double y)
121 return x * x + y * y + 8;
123 // x >= 0 => -1 * (x) <= 0
124 static double ga4_cons_1(double x, double /*y*/)
126 return -1 * x;
129 // y >= 0 => -1 * (y) <= 0
130 static double ga4_cons_2(double /*x*/, double y)
132 return -1 * y ;
135 // x * x - y >= 0 => -1 * (x * x - y) <= 0
136 static double ga4_cons_3(double x, double y)
138 return -1 * (x * x - y);
141 // -x - y * y + 2 = 0 => (-x - y * y + 2)^2 = 0
142 static double ga4_cons_4(double x, double y)
144 return (2 - x - y * y) * (2 - x - y * y);
147 fopti_ga_test()
149 EXTL_TEST_TRACE(_T("\nfopti_ga_test:"));
151 typedef func_ptr<double (*)(double)> ga1_func_type;
152 typedef fopti_ga<ga1_func_type> ga1_ga_type;
153 typedef ga1_ga_type::scopes_type ga1_scopes_type;
154 typedef ga1_scopes_type::scope_type ga1_scope_type;
155 typedef ga1_ga_type::constraints_type ga1_constraints_type;
157 ga1_ga_type ga1_ga ( ga1_func_type(&ga1_func)
158 , ga1_scopes_type(ga1_scope_type(-1, 20))
159 , ga1_constraints_type(ga1_func_type(&ga1_cons_1), ga1_func_type(&ga1_cons_2))
161 ga1_ga.generate();
163 typedef func_ptr<double (*)(double, double)> ga2_func_type;
164 typedef fopti_ga<ga2_func_type, e_false_v> ga2_ga_type;
165 typedef ga2_ga_type::scopes_type ga2_scopes_type;
166 typedef ga2_scopes_type::scope_type ga2_scope_type;
167 typedef ga2_ga_type::constraints_type ga2_constraints_type;
169 ga2_ga_type ga2_ga ( ga2_func_type(&ga2_func)
170 , ga2_scopes_type(ga2_scope_type(-100, 100), ga2_scope_type(-100, 100))
171 , ga2_constraints_type(ga2_func_type(&ga2_cons_1), ga2_func_type(&ga2_cons_2), ga2_func_type(&ga2_cons_3), ga2_func_type(&ga2_cons_4))
173 ga2_ga.generate();
175 typedef func_ptr<double (*)(double, double)> ga3_func_type;
176 typedef fopti_ga<ga3_func_type> ga3_ga_type;
177 typedef ga3_ga_type::scopes_type ga3_scopes_type;
178 typedef ga3_scopes_type::scope_type ga3_scope_type;
179 typedef ga3_ga_type::constraints_type ga3_constraints_type;
181 ga3_ga_type ga3_ga ( ga3_func_type(&ga3_func)
182 , ga3_scopes_type(ga3_scope_type(-100, 100), ga3_scope_type(-100, 100))
183 , ga3_constraints_type(ga3_func_type(&ga3_cons_1), ga3_func_type(&ga3_cons_2), ga3_func_type(&ga3_cons_3), ga3_func_type(&ga3_cons_4))
185 ga3_ga.generate();
187 typedef func_ptr<double (*)(double, double)> ga4_func_type;
188 typedef fopti_ga<ga4_func_type, e_false_v> ga4_ga_type;
189 typedef ga4_ga_type::scopes_type ga4_scopes_type;
190 typedef ga4_scopes_type::scope_type ga4_scope_type;
191 typedef ga4_ga_type::constraints_type ga4_constraints_type;
193 ga4_ga_type ga4_ga ( ga4_func_type(&ga4_func)
194 , ga4_scopes_type(ga4_scope_type(0, 100), ga4_scope_type(0, 100))
195 , ga4_constraints_type(ga4_func_type(&ga4_cons_1), ga4_func_type(&ga4_cons_2), ga4_func_type(&ga4_cons_3), ga4_func_type(&ga4_cons_4))
197 ga4_ga.generate();
201 fopti_ga_test g_fopti_ga_test;
203 /* ///////////////////////////////////////////////////////////////////////
204 * unit_test namespace
206 EXTL_TEST_NAME_END_NAMESPACE(fopti_ga_test)
207 EXTL_INTELLIGENCE_END_WHOLE_NAMESPACE
209 /* //////////////////////////////////////////////////////////////////// */
210 #endif /* EXTL_INTELLIGENCE_GA_FOPTI_GA_TEST_H */
211 /* //////////////////////////////////////////////////////////////////// */