1 /* ///////////////////////////////////////////////////////////////////////
7 * Brief: The Genetic Algoritm Base Class
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_INTELLIGENCE_GA_GA_BASE_H
14 #define EXTL_INTELLIGENCE_GA_GA_BASE_H
17 * \brief The Genetic Algoritm Base Class
20 # error ga_base.h need be supported by c++.
23 /* ///////////////////////////////////////////////////////////////////////
28 /* ///////////////////////////////////////////////////////////////////////
29 * ::extl::intelligence namespace
31 EXTL_INTELLIGENCE_BEGIN_WHOLE_NAMESPACE
33 /*!\brief fitness_traits_base class
35 * \param Gt The ga_traits type
37 * \ingroup extl_group_intelligence
39 template<typename_param_k Gt
>
45 typedef ga_base class_type
;
46 typedef Gt ga_traits_type
;
47 typedef typename_type_k
ga_traits_type::size_type size_type
;
48 typedef typename_type_k
ga_traits_type::bool_type bool_type
;
49 typedef typename_type_k
ga_traits_type::float_type float_type
;
50 typedef typename_type_k
ga_traits_type::index_type index_type
;
51 typedef typename_type_k
ga_traits_type::int_type int_type
;
52 typedef typename_type_k
ga_traits_type::population_type population_type
;
53 typedef typename_type_k
ga_traits_type::individual_type individual_type
;
59 ga_traits_type m_ga_traits
;
60 population_type m_pop
;
66 /// Generates a best population
67 bool_type
generate(population_type
& pop
)
69 // initialize ga_traits
70 ga_traits().init_traits();
73 EXTL_ASSERT(ga_traits().is_valid());
74 EXTL_ASSERT(pop
.size() > 0);
75 if (!ga_traits().is_valid() || pop
.size() <= 0) return e_false_v
;
77 // initialize child population
78 population_type child_pop
= pop
;
79 population_type
* pchild_pop
= &child_pop
;
80 population_type
* pparent_pop
= &pop
;
82 size_type max_g
= ga_traits().max_g();
83 for (index_type i
= 0; i
< max_g
; ++i
)
85 // calculate the fitnesses of the population
86 ga_traits().fitness(*pparent_pop
);
88 // find out the best result
89 if (ga_traits().is_stop(*pparent_pop
)) break;
91 // reserve best individual
92 pchild_pop
->individual(0) = pparent_pop
->best();
94 // prepare ga_traits for internal recurrence
95 ga_traits().prepare(*pparent_pop
);
97 // coverover and mutation
98 size_type select_n
= pchild_pop
->size() - 1;
99 size_type start_n
= (pchild_pop
->size() % 2)? 1 : (pchild_pop
->individual(1) = pchild_pop
->individual(0), 2);
100 for (size_type j
= start_n
; j
< select_n
; ++j
)
102 // select two individuals from the population
103 pchild_pop
->individual(j
) = ga_traits().select(*pparent_pop
);
104 pchild_pop
->individual(j
+ 1) = ga_traits().select(*pparent_pop
);
107 ga_traits().cross(pchild_pop
->individual(j
), pchild_pop
->individual(j
+ 1));
110 ga_traits().mutate(pchild_pop
->individual(j
));
111 ga_traits().mutate(pchild_pop
->individual(j
+ 1));
115 ga_traits().local_search(*pchild_pop
);
118 pchild_pop
->generation(pparent_pop
->generation() + 1);
119 std_swap(pchild_pop
, pparent_pop
);
122 // update this population and return it
123 ga_traits().fitness(*pparent_pop
);
124 if (pparent_pop
!= &pop
) pop
= *pparent_pop
;
126 #if defined(EXTL_INTELLIGENCE_GA_GA_TEST_ENABLE) && \
127 !defined(EXTL_INTELLIGENCE_GA_UGA_TEST_ENABLE)
128 // report detail for debugging
132 EXTL_ASSERT(ga_traits().is_valid());
136 /// Generates a best population
139 // initialize population
140 m_pop
.resize(ga_traits().pop_size());
141 ga_traits().init_pop(m_pop
);
143 return generate(m_pop
);
150 /// Returns const ga_traits
151 ga_traits_type
const& ga_traits() const { return m_ga_traits
; }
152 /// Returns ga_traits
153 ga_traits_type
& ga_traits() { return m_ga_traits
; }
155 /// Returns the best individual
156 individual_type
const& best() const { return population().best(); }
157 /// Returns the population at last generation
158 population_type
const& population() const { return m_pop
; }
160 /// Returns the generation of the best population
161 size_type
const generation() const { return population().generation(); }
165 #ifdef EXTL_INTELLIGENCE_GA_GA_TEST_ENABLE
166 void report(population_type
const& pop
)
168 EXTL_TEST_TRACE(_T("g:%d fmin:%f fmax:%f fsum:%f favg:%f"),
169 pop
.generation(), pop
.fmin(), pop
.fmax(), pop
.fsum(), pop
.favg());
171 //EXTL_TEST_TRACE(_T("best: x=%f fvalue:%f"),
172 // pop.best().value(0), ga_traits().fvalue(pop.best()));
174 /*EXTL_TEST_TRACE(_T("detail:"));
175 for (size_type i = 0; i < pop.size(); ++i)
178 _T("x=%f fvalue:%f fit:%f pro:%f"),
179 pop.individual(i).value(0),
180 ga_traits().fvalue(pop.individual(i)),
181 pop.individual(i).fitness(),
182 pop.individual(i).probability());
185 EXTL_TEST_TRACE(_T("\n"));
191 /* ///////////////////////////////////////////////////////////////////////
192 * ::extl::intelligence namespace
194 EXTL_INTELLIGENCE_END_WHOLE_NAMESPACE
196 /* //////////////////////////////////////////////////////////////////// */
197 #endif /* EXTL_INTELLIGENCE_GA_GA_BASE_H */
198 /* //////////////////////////////////////////////////////////////////// */