edoFileSnapshot.cpp
00001 /*
00002 The Evolving Distribution Objects framework (EDO) is a template-based,
00003 ANSI-C++ evolutionary computation library which helps you to write your
00004 own estimation of distribution algorithms.
00005 
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 This library is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public
00017 License along with this library; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 Copyright (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2001
00021 Copyright (C) 2010 Thales group
00022 */
00023 /*
00024 Authors:
00025     todos@geneura.ugr.es
00026     Marc Schoenauer <Marc.Schoenauer@polytechnique.fr>
00027     Martin Keijzer <mkeijzer@dhi.dk>
00028     Johann Dréo <johann.dreo@thalesgroup.com>
00029     Caner Candan <caner.candan@thalesgroup.com>
00030 */
00031 
00032 #include <cstdlib>
00033 
00034 #include <iostream>
00035 #include <fstream>
00036 #include <stdexcept>
00037 
00038 #include <utils/edoFileSnapshot.h>
00039 #include <utils/compatibility.h>
00040 #include <utils/eoParam.h>
00041 
00042 edoFileSnapshot::edoFileSnapshot(std::string dirname,
00043                                unsigned int frequency /*= 1*/,
00044                                std::string filename /*= "gen"*/,
00045                                std::string delim /*= " "*/,
00046                                unsigned int counter /*= 0*/,
00047                                bool rmFiles /*= true*/,
00048                                bool saveFilenames /*= true*/)
00049     : _dirname(dirname), _frequency(frequency),
00050       _filename(filename), _delim(delim),
00051       _counter(counter), _saveFilenames(saveFilenames),
00052       _descOfFiles( NULL ), _boolChanged(true)
00053 {
00054     std::string s = "test -d " + _dirname;
00055 
00056     int res = system(s.c_str());
00057 
00058     // test for (unlikely) errors
00059 
00060     if ( (res == -1) || (res == 127) )
00061         {
00062             throw std::runtime_error("Problem executing test of dir in eoFileSnapshot");
00063         }
00064 
00065     // now make sure there is a dir without any genXXX file in it
00066     if (res)                    // no dir present
00067         {
00068             s = std::string("mkdir ") + _dirname;
00069         }
00070     else if (!res && rmFiles)
00071         {
00072             s = std::string("/bin/rm -f ") + _dirname+ "/" + _filename + "*";
00073         }
00074     else
00075         {
00076             s = " ";
00077         }
00078 
00079     int dummy;
00080     dummy = system(s.c_str());
00081     // all done
00082 
00083     _descOfFiles = new std::ofstream( std::string(dirname + "/list_of_files.txt").c_str() );
00084 
00085 }
00086 
00087 edoFileSnapshot::~edoFileSnapshot()
00088 {
00089     delete _descOfFiles;
00090 }
00091 
00092 void edoFileSnapshot::setCurrentFileName()
00093 {
00094     std::ostringstream oscount;
00095     oscount << _counter;
00096     _currentFileName = _dirname + "/" + _filename + oscount.str();
00097 }
00098 
00099 eoMonitor& edoFileSnapshot::operator()(void)
00100 {
00101     if (_counter % _frequency)
00102         {
00103             _boolChanged = false;  // subclass with gnuplot will do nothing
00104             _counter++;
00105             return (*this);
00106         }
00107     _counter++;
00108     _boolChanged = true;
00109     setCurrentFileName();
00110 
00111     std::ofstream os(_currentFileName.c_str());
00112 
00113     if (!os)
00114         {
00115             std::string str = "edoFileSnapshot: Could not open " + _currentFileName;
00116             throw std::runtime_error(str);
00117         }
00118 
00119     if ( _saveFilenames )
00120         {
00121             *_descOfFiles << _currentFileName.c_str() << std::endl;
00122         }
00123 
00124     return operator()(os);
00125 }
00126 
00127 eoMonitor& edoFileSnapshot::operator()(std::ostream& os)
00128 {
00129     iterator it = vec.begin();
00130 
00131     os << (*it)->getValue();
00132 
00133     for ( ++it; it != vec.end(); ++it )
00134         {
00135             os << _delim.c_str() << (*it)->getValue();
00136         }
00137 
00138     os << '\n';
00139 
00140     return *this;
00141 }
 All Classes Functions Variables Typedefs