1 /* ///////////////////////////////////////////////////////////////////////
2 * File: probability_traits.h
7 * Brief: The probability_traits class
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_INTELLIGENCE_GA_PROBABILITY_TRAITS_H
14 #define EXTL_INTELLIGENCE_GA_PROBABILITY_TRAITS_H
16 /*!\file probability_traits.h
17 * \brief The probability_traits class
20 # error probability_traits.h need be supported by c++.
23 /* ///////////////////////////////////////////////////////////////////////
28 /* ///////////////////////////////////////////////////////////////////////
29 * ::extl::intelligence namespace
31 EXTL_INTELLIGENCE_BEGIN_WHOLE_NAMESPACE
33 /*!\brief proportional_probability_traits
35 * \param Pop The population type
37 * \ingroup extl_group_intelligence
39 template<typename_param_k Pop
>
40 class proportional_probability_traits
45 typedef Pop population_type
;
46 typedef typename_type_k
population_type::individual_type individual_type
;
47 typedef typename_type_k
population_type::size_type size_type
;
48 typedef typename_type_k
population_type::bool_type bool_type
;
49 typedef typename_type_k
population_type::float_type float_type
;
50 typedef typename_type_k
population_type::index_type index_type
;
51 typedef typename_type_k
population_type::int_type int_type
;
57 /// p = fit_i / fit_sum
58 void probability(population_type
& pop
)
60 EXTL_ASSERT(pop
.size() > 0);
62 size_type n
= pop
.size();
67 for (size_type i
= 0; i
< n
; ++i
)
69 pop
.individual(i
).probability(pop
.individual(i
).fitness() / pop
.fsum());
74 for (size_type i
= 0; i
< n
; ++i
)
76 pop
.individual(i
).probability(1);
83 /*!\brief proportional_probability_traits
85 * \param Pop The population type
86 * \param SP The selection pressure(SP%)
88 * \ingroup extl_group_intelligence
90 template< typename_param_k Pop
91 , e_size_t SP
/* = 30*/
93 struct rank_based_probability_traits
98 typedef Pop population_type
;
99 typedef typename_type_k
population_type::individual_type individual_type
;
100 typedef typename_type_k
population_type::size_type size_type
;
101 typedef typename_type_k
population_type::bool_type bool_type
;
102 typedef typename_type_k
population_type::float_type float_type
;
103 typedef typename_type_k
population_type::index_type index_type
;
104 typedef typename_type_k
population_type::int_type int_type
;
105 typedef typename_type_k buffer_selector
<index_type
>::buffer_type index_buffer_type
;
112 population_type
& m_pop
;
115 pred(population_type
& pop
)
121 bool_type
operator()(index_type lhs
, index_type rhs
)
123 return m_pop
.individual(lhs
).fitness() > m_pop
.individual(rhs
).fitness();
130 /// fitness sort => p = q * (1 - q)^i
131 void probability(population_type
& pop
)
133 size_type pop_n
= pop
.size();
134 EXTL_ASSERT(pop_n
> 0);
137 index_buffer_type
indices(pop
.size());
140 for (i
= 0; i
< pop_n
; ++i
) indices
[i
] = i
;
142 // fitness sort in descending order
143 //std_sort(indices.data(), indices.data() + indices.size(), pred(pop));
144 std_sort(indices
.begin(), indices
.end(), pred(pop
));
146 // calculate probability
147 float_type q
= float_type(SP
) / 100; // selection pressure
148 //float_type q = pop.fsum() > 0? (pop.individual(indices[0]).fitness() / pop.fsum()) : 0.9;
149 float_type p
= q
* (1 - q
);
150 for (i
= 0; i
< pop_n
; ++i
)
152 pop
.individual(indices
[i
]).probability(p
);
159 /* ///////////////////////////////////////////////////////////////////////
160 * ::extl::intelligence namespace
162 EXTL_INTELLIGENCE_END_WHOLE_NAMESPACE
164 /* //////////////////////////////////////////////////////////////////// */
165 #endif /* EXTL_INTELLIGENCE_GA_PROBABILITY_TRAITS_H */
166 /* //////////////////////////////////////////////////////////////////// */