1 /* ///////////////////////////////////////////////////////////////////////
2 * File: fitness_traits_base.h
7 * Brief: The fitness_traits_base class
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_INTELLIGENCE_GA_FITNESS_TRAITS_BASE_H
14 #define EXTL_INTELLIGENCE_GA_FITNESS_TRAITS_BASE_H
16 /*!\file fitness_traits_base.h
17 * \brief The fitness_traits_base class
20 # error fitness_traits_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 D The derived type
36 * \param Pop The population type
37 * \param Pen The penalty_traits type
39 * \ingroup extl_group_intelligence
41 template< typename_param_k D
42 , typename_param_k Pop
43 , typename_param_k Pen
45 class fitness_traits_base
50 typedef D derived_type
;
51 typedef fitness_traits_base class_type
;
52 typedef Pop population_type
;
53 typedef typename_type_k
population_type::individual_type individual_type
;
54 typedef typename_type_k
individual_type::size_type size_type
;
55 typedef typename_type_k
individual_type::bool_type bool_type
;
56 typedef typename_type_k
individual_type::index_type index_type
;
57 typedef typename_type_k
individual_type::int_type int_type
;
58 typedef typename_type_k
individual_type::float_type float_type
;
59 typedef Pen penalty_traits_type
;
66 penalty_traits_type m_pt
;
72 penalty_traits_type
const& penalty_traits() const { return m_pt
; }
73 penalty_traits_type
& penalty_traits() { return m_pt
; }
76 /// \name Constructors
80 : m_is_valid(e_false_v
), m_pt()
85 void fitness_impl(individual_type
& idl
)
87 derive().do_fitness(idl
);
88 penalty_traits().penalty(idl
);
97 m_is_valid
= e_true_v
;
99 /// calculate the fitness of individuals
100 void fitness(population_type
& pop
)
102 EXTL_ASSERT(pop
.size() > 0);
103 EXTL_ASSERT(derive().is_valid());
106 fitness_impl(pop
.individual(0));
107 pop
.fsum(pop
.individual(0).fitness());
108 pop
.fmin(pop
.individual(0).fitness());
109 pop
.fmax(pop
.individual(0).fitness());
111 size_type best_index
= 0;
112 size_type worst_index
= 0;
113 pop
.individual(0).is_best(e_false_v
);
114 pop
.individual(0).is_worst(e_false_v
);
116 size_type n
= pop
.size();
118 for (i
= 1; i
< n
; ++i
)
121 fitness_impl(pop
.individual(i
));
123 // fitness minimum, maximum, sum
124 pop
.fmin(pop
.individual(i
).fitness() < pop
.fmin()? (worst_index
= i
, pop
.individual(i
).fitness()) : pop
.fmin());
125 pop
.fmax(pop
.individual(i
).fitness() > pop
.fmax()? (best_index
= i
, pop
.individual(i
).fitness()) : pop
.fmax());
126 pop
.fsum(pop
.fsum() + pop
.individual(i
).fitness());
128 pop
.individual(i
).is_best(e_false_v
);
129 pop
.individual(i
).is_worst(e_false_v
);
133 pop
.favg(pop
.fsum() / n
);
135 // best/worst individual
136 pop
.individual(best_index
).is_best(e_true_v
);
137 pop
.individual(worst_index
).is_worst(e_true_v
);
140 bool is_valid() const
147 derived_type
const& derive() const { return static_cast<derived_type
const&>(*this); }
148 derived_type
& derive() { return static_cast<derived_type
&>(*this); }
151 /* ///////////////////////////////////////////////////////////////////////
152 * ::extl::intelligence namespace
154 EXTL_INTELLIGENCE_END_WHOLE_NAMESPACE
156 /* //////////////////////////////////////////////////////////////////// */
157 #endif /* EXTL_INTELLIGENCE_GA_FITNESS_TRAITS_BASE_H */
158 /* //////////////////////////////////////////////////////////////////// */