remove \r
[extl.git] / extl / intelligence / ga / fitness_traits_base.h
blobd963083dd273e51efe4d0212fe85ba89ae8276ec
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: fitness_traits_base.h
4 * Created: 08.10.07
5 * Updated: 08.11.07
7 * Brief: The fitness_traits_base class
9 * [<Home>]
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
19 #ifndef __cplusplus
20 # error fitness_traits_base.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 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
47 /// \name Types
48 /// @{
49 public:
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;
60 /// @}
62 /// \name Members
63 /// @{
64 private:
65 bool_type m_is_valid;
66 penalty_traits_type m_pt;
67 /// @}
69 /// \name Traits
70 /// @{
71 public:
72 penalty_traits_type const& penalty_traits() const { return m_pt; }
73 penalty_traits_type& penalty_traits() { return m_pt; }
74 /// @}
76 /// \name Constructors
77 /// @{
78 public:
79 fitness_traits_base()
80 : m_is_valid(e_false_v), m_pt()
82 /// @}
84 private:
85 void fitness_impl(individual_type& idl)
87 derive().do_fitness(idl);
88 penalty_traits().penalty(idl);
91 /// \name Methods
92 /// @{
93 public:
94 /// initialization
95 void init()
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());
105 // init
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();
117 size_type i;
118 for (i = 1; i < n; ++i)
120 // calculate fitness
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);
132 // fitness average
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);
139 /// invariance
140 bool is_valid() const
142 return m_is_valid;
144 /// @}
146 private:
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 /* //////////////////////////////////////////////////////////////////// */