1 /* ///////////////////////////////////////////////////////////////////////
2 * File: fopti_penalty_traits.h
7 * Brief: The fopti_penalty_traits class
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_INTELLIGENCE_GA_FOPTI_PENALTY_TRAITS_H
14 #define EXTL_INTELLIGENCE_GA_FOPTI_PENALTY_TRAITS_H
16 /*!\file fopti_penalty_traits.h
17 * \brief The fopti_penalty_traits class
20 # error fopti_penalty_traits.h need be supported by c++.
23 /* ///////////////////////////////////////////////////////////////////////
27 #include "fopti_constraints.h"
28 #include "fopti_func_traits.h"
30 /* ///////////////////////////////////////////////////////////////////////
31 * ::extl::intelligence namespace
33 EXTL_INTELLIGENCE_BEGIN_WHOLE_NAMESPACE
35 /*!\brief fopti_penalty_traits class
37 * \param Idl The individual type
38 * \param Func The function type
40 * \ingroup extl_group_intelligence
42 template< typename_param_k Idl
43 , typename_param_k Func
45 class fopti_penalty_traits
46 : public penalty_traits_base
<fopti_penalty_traits
<Idl
, Func
>, Idl
>
51 typedef penalty_traits_base
<fopti_penalty_traits
<Idl
, Func
>, Idl
> base_type
;
52 typedef fopti_penalty_traits class_type
;
53 typedef typename_type_k
base_type::individual_type individual_type
;
54 typedef typename_type_k
base_type::size_type size_type
;
55 typedef typename_type_k
base_type::bool_type bool_type
;
56 typedef typename_type_k
base_type::index_type index_type
;
57 typedef typename_type_k
base_type::int_type int_type
;
58 typedef typename_type_k
base_type::float_type float_type
;
59 typedef Func func_type
;
60 typedef fopti_func_traits
<individual_type
, func_type
> func_traits_type
;
61 typedef fopti_constraints
<func_traits_type
> constraints_type
;
64 /// \name Constructors
67 constraints_type m_cons
;
73 constraints_type
const& constraints() const { return m_cons
; }
74 constraints_type
& constraints() { return m_cons
; }
75 void constraints(constraints_type
const& cons
) { m_cons
= cons
; }
80 float_type
unfitness(index_type i
, individual_type
const& idl
)
82 float_type unfit
= constraints()[i
].fvalue(idl
);
83 return (unfit
< 0)? 0 : unfit
;
87 // pena = 1 - avg(unfit_i / (unfit_max + 1))
89 void do_penalty(individual_type
& idl
)
91 size_type n
= constraints().size();
94 // calculates the maximum
97 float_type no_max
= unfitness(i
, idl
);
98 for (i
= 1; i
< n
; ++i
)
100 float_type no
= unfitness(i
, idl
);
101 no_max
= (no
> no_max
)? no
: no_max
;
103 if (no_max
== 0) return ;
106 for (i
= 0; i
< n
; ++i
)
108 // If only one individual need penalty
109 // then (unfitness(i, idl) / no_max) always is one and pena will be unchanged.
110 // But pena will be decreased with the unfitness if using (no_max + 1).
111 sum
+= unfitness(i
, idl
) / (no_max
+ 1);
114 // pena is in range: [0, 1]
115 float_type pena
= 1 - sum
/ n
;
117 //printf("%f %f %f %f\n", idl.value(0), pena, idl.fitness(), idl.fitness() * pena);
119 // the penalty of the fitness
120 idl
.fitness(idl
.fitness() * pena
);
124 /* ///////////////////////////////////////////////////////////////////////
125 * ::extl::intelligence namespace
127 EXTL_INTELLIGENCE_END_WHOLE_NAMESPACE
129 /* //////////////////////////////////////////////////////////////////// */
130 #endif /* EXTL_INTELLIGENCE_GA_FOPTI_PENALTY_TRAITS_H */
131 /* //////////////////////////////////////////////////////////////////// */