1 /*
2  *  Open BEAGLE
3  *  Copyright (C) 2001-2007 by Christian Gagne and Marc Parizeau
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Contact:
20  *  Laboratoire de Vision et Systemes Numeriques
21  *  Departement de genie electrique et de genie informatique
22  *  Universite Laval, Quebec, Canada, G1K 7P4
23  *  http://vision.gel.ulaval.ca
24  *
25  */
26 
27 /*!
28  *  \file   OneMaxEvalOp.cpp
29  *  \brief  Implementation of the class OneMaxEvalOp.
30  *  \author Christian Gagne
31  *  \author Marc Parizeau
32  *  $Revision: 1.9.2.1 $
33  *  $Date: 2007/05/09 01:51:23 $
34  */
35 
36 #include "beagle/GA.hpp"
37 #include "OneMaxEvalOp.hpp"
38 
39 #include <cmath>
40 
41 using namespace Beagle;
42 
43 /*!
44  *  \brief Construct the individual evaluation operator for the OneMax problem.
45  */
OneMaxEvalOp()46 OneMaxEvalOp::OneMaxEvalOp() :
47   EvaluationOp("OneMaxEvalOp")
48 { }
49 
50 
51 /*!
52  *  \brief Evaluate the fitness of the given individual.
53  *  \param inIndividual Current individual to evaluate.
54  *  \param ioContext Evolutionary context.
55  *  \return Handle to the fitness value of the individual.
56  */
evaluate(Individual & inIndividual,Context & ioContext)57 Fitness::Handle OneMaxEvalOp::evaluate(Individual& inIndividual, Context& ioContext)
58 {
59   Beagle_AssertM(inIndividual.size() == 1);
60   GA::BitString::Handle lBitString = castHandleT<GA::BitString>(inIndividual[0]);
61   unsigned int lCount = 0;
62   for(unsigned int i=0; i<lBitString->size(); ++i) {
63     if((*lBitString)[i] == true) ++lCount;
64   }
65   return new FitnessSimple(float(lCount));
66 }
67