00001 #ifndef EXECMODULE_HPP_
00002 #define EXECMODULE_HPP_
00003
00004 #include "../OptFrameModule.hpp"
00005
00006 template<class R, class M>
00007 class ExecModule: public OptFrameModule<R, M>
00008 {
00009 public:
00010 string id()
00011 {
00012 return "exec";
00013 }
00014 string usage()
00015 {
00016 return "exec [ <initsol> id | <loadsol> id | <initpop> id popSize | <loadpop> id ] target_fo timelimit method [output_solution_name]";
00017 }
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 void run(vector<OptFrameModule<R, M>*> all_modules, HeuristicFactory<R, M>* factory, map<string, string>* dictionary, string input)
00028 {
00029 cout << "exec: " << input << endl;
00030 Scanner scanner(input);
00031
00032 if (!scanner.hasNext())
00033 {
00034 cout << "Usage: " << usage() << endl;
00035 return;
00036 }
00037
00038 string sol = scanner.next();
00039
00040 if ((sol != "initsol") && (sol != "loadsol") && (sol != "initpop") && (sol != "loadpop"))
00041 {
00042 cout << "First parameter must be either 'initsol', 'loadsol', 'initpop', 'loadpop'!" << endl;
00043 cout << "Usage: " << usage() << endl;
00044 return;
00045 }
00046
00047 string id = scanner.next();
00048
00049 Solution<R>* s = NULL;
00050 bool needDelete = false;
00051
00052 Population<R> *p = NULL;
00053 bool needDeletePop = false;
00054
00055 if (sol == "loadsol")
00056 {
00057 Scanner s2(sol + " " + id);
00058 s = factory->read_loadsol(&s2);
00059 }
00060
00061 if (sol == "initsol")
00062 {
00063 Scanner s2(sol + " " + id);
00064 InitialSolution<R>* initsol = factory->read_initsol(&s2);
00065 s = &initsol->generateSolution();
00066 needDelete = true;
00067 }
00068
00069 if (sol == "loadpop")
00070 {
00071 Scanner s2(sol + " " + id);
00072 p = factory->read_loadpop(&s2);
00073 }
00074
00075 if (sol == "initpop")
00076 {
00077 Scanner s2(sol + " " + id);
00078 InitialPopulation<R>* initpop = factory->read_initpop(&s2);
00079
00080 unsigned popSize = scanner.nextInt();
00081 p = &initpop->generatePopulation(popSize);
00082 }
00083
00084 double target_fo = scanner.nextDouble();
00085 double timelimit = scanner.nextDouble();
00086
00087 pair<Heuristic<R, M>*, string> method = factory->createHeuristic(scanner.rest());
00088 scanner = Scanner(method.second);
00089
00090
00091
00092 string s_new_id = "";
00093
00094 if (sol == "initsol" || sol == "loadsol")
00095 {
00096 Solution<R>* sFinal = &method.first->search(*s, timelimit, target_fo);
00097
00098 if (needDelete)
00099 delete s;
00100
00101 int new_id = factory->add_loadsol(sFinal);
00102
00103 stringstream str;
00104 str << "loadsol " << new_id;
00105 s_new_id = str.str();
00106
00107 cout << "'" << s_new_id << "' added." << endl;
00108 }
00109 else if (sol == "initpop" || sol == "loadpop")
00110 {
00111 Population<R> *pFinal;
00112 Population<R> *pAux;
00113
00114 pAux = &(p->clone());
00115
00116 pFinal = &method.first->search(*pAux, timelimit, target_fo);
00117
00118 for (unsigned i = 0; i < pAux->size(); i++)
00119 delete &(pAux->at(i));
00120
00121 int new_id = factory->add_loadpop(pFinal);
00122
00123 stringstream str;
00124 str << "loadpop " << new_id;
00125 s_new_id = str.str();
00126
00127 cout << "'" << s_new_id << "' added." << endl;
00128 }
00129
00130
00131
00132 if (scanner.hasNext())
00133 {
00134 string new_name = scanner.next();
00135 run_module("define", all_modules, factory, dictionary, new_name + " " + s_new_id);
00136 }
00137
00138 }
00139
00140 };
00141
00142 #endif