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-2015 Shark Development Team
15  *
16  * <BR><HR>
17  * This file is part of Shark.
18  * <http://image.diku.dk/shark/>
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 */
55  /**
56  * \brief Default c'tor, initializes the penalty factor to \f$10^{-6}\f$.
57  */
59 
60  /**
61  * \brief Evaluates the supplied function on the supplied individual
62  *
63  * \param [in] f The function to be evaluated.
64  * \param [in] individual The individual to evaluate the function for.
65  */
66  template<typename Function, typename IndividualType>
67  void operator()( Function const& f, IndividualType& individual ) const {
68 
69  if( f.isFeasible( individual.searchPoint() ) ) {
70  individual.unpenalizedFitness() = f.eval( individual.searchPoint() );
71  individual.penalizedFitness() = individual.unpenalizedFitness();
72  return;
73  }
74 
75  typename Function::SearchPointType t( individual.searchPoint() );
76  f.closestFeasible( t );
77 
78  individual.unpenalizedFitness() = f.eval( t );
79  individual.penalizedFitness() = individual.unpenalizedFitness();
80 
81  penalize(individual.searchPoint(),t,individual.penalizedFitness() );
82  }
83 
84  /**
85  * \brief Evaluates The function on individuals in the range [first,last]
86  *
87  * \param [in] f The function to be evaluated.
88  * \param [in] begin first indivdual in the range to be evaluated
89  * \param [in] end iterator pointing directly beehind the last individual to be evaluated
90  */
91  template<typename Function, typename Iterator>
92  void operator()( Function const& f, Iterator begin, Iterator end ) const {
93  for(Iterator pos = begin; pos != end; ++pos){
94  (*this)(f,*pos);
95  }
96  }
97 
98  template<class SearchPointType>
99  void penalize(SearchPointType const& s, SearchPointType const& t, double& fitness)const{
100  fitness += m_penaltyFactor * norm_sqr( t - s );
101  }
102 
103  template<class SearchPointType>
104  void penalize(SearchPointType const& s, SearchPointType const& t, RealVector& fitness)const{
105  fitness += m_penaltyFactor * norm_sqr( t - s );
106  }
107 
108  /**
109  * \brief Stores/loads the evaluator's state.
110  * \tparam Archive The type of the archive.
111  * \param [in,out] archive The archive to use for loading/storing.
112  * \param [in] version Currently unused.
113  */
114  template<typename Archive>
115  void serialize( Archive & archive, const unsigned int version ) {
116  archive & m_penaltyFactor;
117  }
118 
119  double m_penaltyFactor; ///< Penalty factor \f$\alpha\f$, default value: \f$10^{-6}\f$ .
120 
121 };
122 }
123 
124 
125 #endif