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 #include "beagle/Beagle.hpp"
28 
29 #include <iostream>
30 #include <map>
31 #include <string>
32 
33 using namespace Beagle;
34 
35 
36 // This restrict the scope of the class to the actual file only.
37 namespace {
38 
39 typedef std::map< unsigned,unsigned int,std::less<unsigned int>,
40                   BEAGLE_STLALLOCATOR< std::pair<const unsigned int,unsigned int> > >
41         CountMap;
42 
43 /*!
44  *  \brief Internal class used to log individual size frequency usage.
45  */
46 class UsageCount : public Beagle::Object, public CountMap
47 {
48 public:
49   explicit UsageCount(unsigned int inDemeID=0, unsigned int inGeneration=0);
~UsageCount()50   virtual ~UsageCount() { }
51   void write(PACC::XML::Streamer& ioStreamer, bool inIndent=true) const;
52   unsigned int mDemeID;
53   unsigned int mGeneration;
54 };
55 
56 
57 /*!
58  *  \brief Construct usage count class.
59  */
UsageCount(unsigned int inDemeID,unsigned int inGeneration)60 UsageCount::UsageCount(unsigned int inDemeID,
61                        unsigned int inGeneration) :
62   mDemeID(inDemeID),
63   mGeneration(inGeneration)
64 { }
65 
66 
67 /*!
68  *  \brief Write usage count to the streamer.
69  *  \param ioStreamer XML streamer to write usage count to.
70  *  \param inIndent Whether XML output should be indented.
71  */
write(PACC::XML::Streamer & ioStreamer,bool inIndent) const72 void UsageCount::write(PACC::XML::Streamer& ioStreamer, bool inIndent) const
73 {
74   Beagle_StackTraceBeginM();
75   ioStreamer.openTag("IndividualSizeFrequencyUsageStats", inIndent);
76   ioStreamer.insertAttribute("deme", uint2str(mDemeID));
77   ioStreamer.insertAttribute("generation", uint2str(mGeneration));
78   for(const_iterator lIter=begin(); lIter!=end(); ++lIter) {
79     ioStreamer.openTag("IndividualSize", inIndent);
80     ioStreamer.insertAttribute("size", uint2str(lIter->first));
81     ioStreamer.insertAttribute("count", uint2str(lIter->second));
82     ioStreamer.closeTag();
83   }
84   ioStreamer.closeTag();
85   Beagle_StackTraceEndM("void UsageCount::write(PACC::XML::Streamer& ioStreamer, bool inIndent) const");
86 }
87 
88 }
89 
90 
91 /*!
92  *  \brief Construct individual size frequency display operator.
93  *  \param inName Name of the primitive.
94  */
IndividualSizeFrequencyStatsOp(Beagle::string inName)95 IndividualSizeFrequencyStatsOp::IndividualSizeFrequencyStatsOp(Beagle::string inName) :
96   Operator(inName)
97 { }
98 
99 
100 /*!
101  *  \brief Compute primitive usage statistics from deme.
102  *  \param ioDeme Deme to compute individual frequency on.
103  *  \param ioContext Evolutionary context.
104  */
operate(Beagle::Deme & ioDeme,Beagle::Context & ioContext)105 void IndividualSizeFrequencyStatsOp::operate(Beagle::Deme& ioDeme, Beagle::Context& ioContext)
106 {
107   Beagle_StackTraceBeginM();
108   // Compute usage statistics
109   UsageCount lCounter(ioContext.getDemeIndex(), ioContext.getGeneration());
110   for(unsigned int i=0; i<ioDeme.size(); ++i) {
111     CountMap::iterator lIter = lCounter.find(ioDeme[i]->size());
112     if(lIter == lCounter.end()) lCounter[ioDeme[i]->size()] = 1;
113     else ++(lIter->second);
114   }
115 
116   // Display usage statistics
117   Beagle_LogObjectM(
118     ioContext.getSystem().getLogger(),
119     Logger::eStats,
120     "stats", "Beagle::IndividualSizeFrequencyStatsOp",
121     lCounter
122   );
123   Beagle_StackTraceEndM("void IndividualSizeFrequencyStatsOp::operate(Beagle::Deme& ioDeme, Beagle::Context& ioContext)");
124 }
125