remove \r
[extl.git] / extl / intelligence / ga / probability_traits.h
blob149caef42e1458c346068030854fbb87b1dfa607
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: probability_traits.h
4 * Created: 08.09.09
5 * Updated: 08.09.11
7 * Brief: The probability_traits class
9 * [<Home>]
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
19 #ifndef __cplusplus
20 # error probability_traits.h need be supported by c++.
21 #endif
23 /* ///////////////////////////////////////////////////////////////////////
24 * Includes
26 #include "prefix.h"
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
42 /// \name Types
43 /// @{
44 public:
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;
52 /// @}
54 /// \name Methods
55 /// @{
56 public:
57 /// p = fit_i / fit_sum
58 void probability(population_type& pop)
60 EXTL_ASSERT(pop.size() > 0);
62 size_type n = pop.size();
64 // pop.fsum() != 0
65 if (pop.fsum())
67 for (size_type i = 0; i < n; ++i)
69 pop.individual(i).probability(pop.individual(i).fitness() / pop.fsum());
72 else
74 for (size_type i = 0; i < n; ++i)
76 pop.individual(i).probability(1);
80 /// @}
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
95 /// \name Types
96 /// @{
97 public:
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;
106 /// @}
108 private:
109 class pred
111 private:
112 population_type& m_pop;
114 public:
115 pred(population_type& pop)
116 : m_pop(pop)
120 public:
121 bool_type operator()(index_type lhs, index_type rhs)
123 return m_pop.individual(lhs).fitness() > m_pop.individual(rhs).fitness();
127 /// \name Methods
128 /// @{
129 public:
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);
136 // init indices
137 index_buffer_type indices(pop.size());
139 size_type i;
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);
153 p *= (1 - q);
156 /// @}
159 /* ///////////////////////////////////////////////////////////////////////
160 * ::extl::intelligence namespace
162 EXTL_INTELLIGENCE_END_WHOLE_NAMESPACE
164 /* //////////////////////////////////////////////////////////////////// */
165 #endif /* EXTL_INTELLIGENCE_GA_PROBABILITY_TRAITS_H */
166 /* //////////////////////////////////////////////////////////////////// */