1 /* ///////////////////////////////////////////////////////////////////////
2 * File: fopti_ga_test.h
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
15 /* ///////////////////////////////////////////////////////////////////////
18 EXTL_INTELLIGENCE_BEGIN_WHOLE_NAMESPACE
19 EXTL_TEST_NAME_BEGIN_NAMESPACE(fopti_ga_test
)
21 /* ///////////////////////////////////////////////////////////////////////
26 // for maximum optimization
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
)
41 // x <= 20 => (x - 20) <= 0
42 static double ga1_cons_2(double x
)
47 // for minimum optimization
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*/)
62 // x <= 100 => (x - 100) <= 0
63 static double ga2_cons_2(double x
, double /*y*/)
68 // y >= -100 => -1 * (y + 100) <= 0
69 static double ga2_cons_3(double /*x*/, double y
)
74 // y <= 100 => (y - 100) <= 0
75 static double ga2_cons_4(double /*x*/, double y
)
80 // for maximum optimization
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*/)
97 // x <= 100 => (x - 100) <= 0
98 static double ga3_cons_2(double x
, double /*y*/)
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
)
115 // for minimum optimization
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*/)
129 // y >= 0 => -1 * (y) <= 0
130 static double ga4_cons_2(double /*x*/, double 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
);
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
))
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
))
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
))
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
))
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 /* //////////////////////////////////////////////////////////////////// */