PenalizingEvaluator.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief PenalizingEvaluator
6 
7 
8  *
9  *
10  * \author T. Voss, O.Krause
11  * \date 2014
12  *
13  *
14  * \par Copyright 1995-2017 Shark Development Team
15  *
16  * <BR><HR>
17  * This file is part of Shark.
18  * <http://shark-ml.org/>
19  *
20  * Shark is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU Lesser General Public License as published
22  * by the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * Shark is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public License
31  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
32  *
33  */
34 //===========================================================================
35 
36 #ifndef SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_EVALUATION_PENALIZING_EVALUATOR_H
37 #define SHARK_ALGORITHMS_DIRECT_SEARCH_OPERATORS_EVALUATION_PENALIZING_EVALUATOR_H
38 
39 #include <shark/LinAlg/Base.h>
40 
41 namespace shark {
42 /**
43 * \brief Penalizing evaluator for scalar objective functions.
44 *
45 * Evaluates the supplied single-objective function \f$f\f$ for the search point \f$s\f$
46 * according to:
47 * \f{align*}{
48 * y & = & f( s' )\\
49 * y' & = & f( s' ) + \alpha \vert\vert s - s' \vert\vert_2^2
50 * \f}
51 * where \f$s'\f$ is the repaired version of \f$s\f$ if \f$s\f$ is not feasible and equal to \f$s\f$ otherwise.
52 * The default value of \f$\alpha\f$ is \f$10^{-6}\f$.
53 *
54 * This Evaluator can also handle noisy functions by applying reevaluations of a single point on f and
55 * averaging the results.
56 */
58  /**
59  * \brief Default c'tor, initializes the penalty factor to \f$10^{-6}\f$.
60  */
62 
63  /**
64  * \brief Evaluates the supplied function on the supplied individual
65  *
66  * \param [in] f The function to be evaluated.
67  * \param [in] individual The individual to evaluate the function for.
68  */
69  template<typename Function, typename IndividualType>
70  void operator()( Function const& f, IndividualType& individual ) const {
71  typename Function::SearchPointType t( individual.searchPoint() );
72  if( !f.isFeasible( t ) ) {
73  f.closestFeasible( t );
74  }
75 
76  individual.unpenalizedFitness() = f.eval( t );
77  for(std::size_t k = 1; k < m_numEvaluations; ++k){
78  individual.unpenalizedFitness() += f.eval(t);
79  }
80  individual.unpenalizedFitness() /= m_numEvaluations;
81  individual.penalizedFitness() = individual.unpenalizedFitness();
82  penalize(individual.searchPoint(),t,individual.penalizedFitness() );
83  }
84 
85  /**
86  * \brief Evaluates The function on individuals in the range [first,last]
87  *
88  * \param [in] f The function to be evaluated.
89  * \param [in] begin first indivdual in the range to be evaluated
90  * \param [in] end iterator pointing directly beehind the last individual to be evaluated
91  */
92  template<typename Function, typename Iterator>
93  void operator()( Function const& f, Iterator begin, Iterator end ) const {
94  for(Iterator pos = begin; pos != end; ++pos){
95  (*this)(f,*pos);
96  }
97  }
98 
99  template<class SearchPointType>
100  void penalize(SearchPointType const& s, SearchPointType const& t, double& fitness)const{
101  fitness += m_penaltyFactor * norm_sqr( t - s );
102  }
103 
104  template<class SearchPointType>
105  void penalize(SearchPointType const& s, SearchPointType const& t, RealVector& fitness)const{
106  fitness += m_penaltyFactor * norm_sqr( t - s );
107  }
108 
109 
110  /**
111  * \brief Stores/loads the evaluator's state.
112  * \tparam Archive The type of the archive.
113  * \param [in,out] archive The archive to use for loading/storing.
114  * \param [in] version Currently unused.
115  */
116  template<typename Archive>
117  void serialize( Archive & archive, const unsigned int version ) {
118  archive & m_penaltyFactor;
119  }
120 
121  double m_penaltyFactor; ///< Penalty factor \f$\alpha\f$, default value: \f$10^{-6}\f$ .
122  std::size_t m_numEvaluations;///< Number of Evaluations on a noisy function
123 
124 };
125 }
126 
127 
128 #endif