Back to Top page.

EGTProcessor.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003 $Id: EGTProcessor.h,v 1.7 2003/01/27 14:00:44 motegi Exp $
00004 Copyright (C) 2002 Higuchi Lab. All rights reserved.
00005 
00006 *****************************************************************************/
00007 #ifndef INCLUDE__EGPROCESSOR_H__FILE
00008 #define INCLUDE__EGPROCESSOR_H__FILE
00009 
00010 #include "defs.h"
00011 #include "env.h"
00012 #include "EGException.h"
00013 #include "EGGetOpt.h"
00014 #include "EGOptionAnalyzer.h"
00015 #include "EGRandom.h"
00016 #include <fstream>
00017 #include <string>
00018 #include <signal.h>    // for signal()
00019 #include "EGSignal.h"
00020 #include "EGFileIO.h"  // for MakeDirectory
00021 
00027 template<class TC>
00028 class EGTProcessor {
00029 public:
00030   typedef TC Control;
00031   int Execute(int argc, char** argv);
00032 
00033 private:
00034   Control ctrl_;
00035 };
00036 
00037 
00038 //============================================================================
00042 template<class TC>
00043 int EGTProcessor<TC>::Execute(int argc, char** argv)
00044 {
00045   try {
00046     signal(SIGINT, &SigIntHandler);
00047 
00048     EGGetOpt opt(argc, argv);      // Parses main function arguments.
00049     const char* filename = opt.GetFileName();
00050 
00051     EGOptionAnalyzer oa;           // Reads a setting file
00052     oa.analyzeOption(filename);
00053     
00054     EGFileIO::MakeDirectory(Env::Instance().WorkDir().c_str());
00055 
00056     std::string logfile = Env::Instance().WorkDir() + "/egg.log";
00057     std::ofstream fout(logfile.c_str());
00058     if (!fout) {
00059       throw EGException("Can't open file egg.log", __FILE__, __LINE__);
00060     }
00061     
00062     std::cout << "\nEvolutionary Graph Generation (EGG) System ";
00063     fout      << "\nEvolutionary Graph Generation (EGG) System ";
00064     std::cout << VERSION << '\n';
00065     fout      << VERSION << '\n';
00066     std::cout << "Copyright (C) 2002 - 2003 Higuchi Lab.\n\n" << std::endl;
00067     fout      << "Copyright (C) 2002 - 2003 Higuchi Lab.\n\n" << std::endl;
00068 
00069     unsigned uSeed = opt.GetSeed();
00070     if (uSeed == 0) {
00071       uSeed = time(0);
00072     }
00073     EGRandom::SetSeed(uSeed);
00074     std::cout << "Random seed... " << uSeed << std::endl;
00075     fout      << "Random seed... " << uSeed << std::endl;
00076     
00077     if (opt.IsLoad()) {
00078       std::cout << "Load resume file... " << std::flush;
00079       fout      << "Load resume file... " << std::flush;
00080       ctrl_.LoadControl();
00081       std::cout << "done\n";
00082       fout      << "done\n";
00083       std::cout << "Load target file... " << std::flush;
00084       fout      << "Load target file... " << std::flush;
00085       ctrl_.LoadTarget(Env::Instance().Input().c_str());
00086       std::cout << "done" << std::endl;
00087       fout      << "done" << std::endl;
00088     }
00089     else {
00090       std::cout << "Load target file... " << std::flush;
00091       fout      << "Load target file... " << std::flush;
00092       ctrl_.LoadTarget(Env::Instance().Input().c_str());
00093       std::cout << "done\n";
00094       fout      << "done\n";
00095       std::cout << "Initialize population... " << std::flush;
00096       fout      << "Initialize population... " << std::flush;
00097       ctrl_.InitControl();
00098       std::cout << "done" << std::endl;
00099       fout      << "done" << std::endl;
00100     }
00101     
00102     unsigned uMaxGen = opt.GetGenerations();
00103     if (uMaxGen) {
00104       Env::Instance().SetMaxGeneration(uMaxGen);
00105       std::cout << "Max. num. of generations... " << uMaxGen << std::endl;
00106       fout      << "Max. num. of generations... " << uMaxGen << std::endl;
00107     }
00108     
00109     ctrl_.Execute(fout);
00110 
00111     if (Env::Instance().GeneratePopSave()) {
00112       std::cout << "Save files... " << std::flush;
00113       fout      << "Save files... " << std::flush;
00114       ctrl_.SaveControl();
00115       std::cout << "done" << std::endl;
00116       fout      << "done" << std::endl;
00117     }
00118     std::cout << "EGG system has successfully finished." << std::endl;
00119     fout      << "EGG system has successfully finished." << std::endl;
00120     return 0;
00121   }
00122   catch(EGException& ex) {
00123     std::cerr << ex << std::endl;
00124   }
00125   catch(std::exception& e) {
00126     std::cerr << e.what() << std::endl;
00127   }
00128   catch(...) {
00129     std::cerr << "Unexpected Exception." << std::endl;
00130   }
00131   return -1;
00132 }
00133 
00134 #endif //INCLUDE__EGPROCESSOR_H__FILE