- Main Page
- Classes
- Files

- File List

# OptFrame/Modules/CheckModule.hpp

    00001 #ifndef CHECKMODULE_HPP_
    00002 #define CHECKMODULE_HPP_
    00003
    00004 #include "../OptFrameModule.hpp"
    00005 #include "../Util/Timer.hpp"
    00006
    00007 template<class R, class M>
    00008 class CheckModule: public OptFrameModule<R, M>
    00009 {
    00010 public:
    00011         string id()
    00012         {
    00013                 return "check";
    00014         }
    00015         string usage()
    00016         {
    00017                 return "check [initsol id | loadsol id] evaluator ns_seq_list";
    00018         }
    00019
    00020         void run(vector<OptFrameModule<R, M>*> all_modules, HeuristicFactory<R, M>* factory, map<string, string>* dictionary, string input)
    00021         {
    00022                 cout << "check: " << input << endl;
    00023                 Scanner scanner(input);
    00024
    00025                 if (!scanner.hasNext())
    00026                 {
    00027                         cout << "Usage: " << usage() << endl;
    00028                         return;
    00029                 }
    00030
    00031                 string sol = scanner.next();
    00032
    00033                 if ((sol != "initsol") && (sol != "loadsol"))
    00034                 {
    00035                         cout << "First parameter must be either 'initsol' or 'loadsol'!" << endl;
    00036                         cout << "Usage: " << usage() << endl;
    00037                         return;
    00038                 }
    00039
    00040                 string id = scanner.next();
    00041
    00042                 Solution<R>* s = NULL;
    00043
    00044                 if (sol == "loadsol")
    00045                 {
    00046                         Scanner s2(sol + " " + id);
    00047                         s = &factory->read_loadsol(&s2)->clone();
    00048                 }
    00049
    00050                 if (sol == "initsol")
    00051                 {
    00052                         Scanner s2(sol + " " + id);
    00053                         cout << "Step 1: Testing solution generator... ";
    00054                         InitialSolution<R>* initsol = factory->read_initsol(&s2);
    00055                         s = &initsol->generateSolution();
    00056                         if (!s)
    00057                         {
    00058                                 cout << "NULL Solution. [Failed]" << endl;
    00059                                 return;
    00060                         }
    00061                         cout << "[Ok]" << endl;
    00062                 }
    00063
    00064                 Evaluator<R, M>* eval = factory->read_ev(&scanner);
    00065                 vector<NS<R, M>*> ns_list = factory->read_ns_list(&scanner);
    00066                 vector<NSSeq<R, M>*> ns_seq_list;
    00067                 for (int i = 0; i < ns_list.size(); i++)
    00068                         ns_seq_list.push_back((NSSeq<R, M>*) ns_list[i]);
    00069
    00070                 cout << endl;
    00071
    00072                 // ==========================================================
    00073                 // ==========================================================
    00074
    00075                 cout << "Step 2: Testing Solution methods" << endl;
    00076
    00077                 cout << "2.1 - Representation [Ok]" << endl;
    00078
    00079                 Solution<R>* s2 = &s->clone();
    00080                 if (&s2->getR() == &s->getR())
    00081                 {
    00082                         cout << "Error! Solution has the SAME representation object. Maybe a pointer-based representation?" << endl;
    00083                         return;
    00084                 }
    00085                 cout << "2.2 - Representation Copy [Ok]" << endl;
    00086
    00087                 delete s2;
    00088
    00089                 int numTests = 5;
    00090                 double totalCopyTime = 0;
    00091                 Timer* timer = new Timer(false);
    00092
    00093                 while (totalCopyTime < 1.0)
    00094                 {
    00095                         numTests *= 2;
    00096                         for (int i = 0; i < numTests; i++)
    00097                         {
    00098                                 s2 = &s->clone();
    00099                                 delete s2;
    00100                         }
    00101
    00102                         totalCopyTime = timer->now();
    00103                         delete timer;
    00104                         timer = new Timer(false);
    00105                 }
    00106
    00107                 delete timer;
    00108
    00109                 double tPerCopy = totalCopyTime / numTests;
    00110
    00111                 cout << "2.3 - Results" << endl;
    00112                 cout << "Number of tests: " << numTests << endl;
    00113                 cout << "Total test time: " << totalCopyTime << " seconds" << endl;
    00114                 cout << "Time per copy: " << tPerCopy << " seconds" << endl;
    00115                 cout << "Printing solution: " << endl;
    00116                 s->print();
    00117                 cout << "clone() and print() methods [Ok]" << endl;
    00118
    00119                 cout << endl;
    00120
    00121                 // ==========================================================
    00122                 // ==========================================================
    00123
    00124                 cout << "Step 3: Testing evaluation methods" << endl;
    00125
    00126                 cout << "3.1 - Evaluate ";
    00127                 Evaluation<M>* e = &eval->evaluate(*s);
    00128                 cout << "[Ok]" << endl;
    00129
    00130                 cout << "3.2 - Evaluation Value = " << e->evaluation() << " ";
    00131                 cout << "[Ok]" << endl;
    00132
    00133                 cout << "3.3 - Evaluation print()" << endl;
    00134                 e->print();
    00135                 cout << "[Ok]" << endl;
    00136
    00137                 cout << "3.4 - Re-evaluation" << endl;
    00138                 eval->evaluate(*e, *s);
    00139                 e->print();
    00140                 delete e;
    00141                 cout << "[Ok]" << endl;
    00142
    00143                 numTests = 5;
    00144                 double totalEvalTime = 0;
    00145                 timer = new Timer(false);
    00146
    00147                 while (totalEvalTime < 1.0)
    00148                 {
    00149                         numTests *= 2;
    00150                         for (int i = 0; i < numTests; i++)
    00151                         {
    00152                                 e = &eval->evaluate(*s);
    00153                                 delete e;
    00154                         }
    00155
    00156                         totalEvalTime = timer->now();
    00157                         delete timer;
    00158                         timer = new Timer(false);
    00159                 }
    00160
    00161                 double tPerEval = totalEvalTime / numTests;
    00162
    00163                 cout << "3.5 - Evaluation Results" << endl;
    00164                 cout << "Number of tests: " << numTests << endl;
    00165                 cout << "Total test time: " << totalEvalTime << " seconds" << endl;
    00166                 cout << "Time per evaluation: " << tPerEval << " seconds" << endl;
    00167
    00168                 cout << endl;
    00169
    00170                 // ==========================================================
    00171                 // ==========================================================
    00172
    00173                 cout << "Step 4: Testing neighborhoods (" << ns_seq_list.size() << ")" << endl;
    00174
    00175                 /*
    00176                 cout << "4.1 - Number of neighbors " << endl;
    00177                 for (int i = 0; i < ns_seq_list.size(); i++)
    00178                 {
    00179                         cout << "neighborhood " << i << ": ";
    00180                         ns_seq_list[i]->print();
    00181
    00182                         double s_total = 0;
    00183                         double s_applied = 0;
    00184
    00185                         ns_seq_list[i]->init(s);
    00186
    00187                         while (ns_seq_list[i]->hasNext(s))
    00188                         {
    00189                                 Move<R, M>* m = ns_seq_list[i]->next(s);
    00190                                 s_total++;
    00191
    00192                                 if (m->canBeApplied(s)) // Sum of useful moves
    00193                                         s_applied++;
    00194
    00195                                 if (m->canBeApplied(s)) // First -> checking moves and reverse moves
    00196                                 {
    00197                                         Evaluation<M>* e2 = eval->evaluate(s);
    00198
    00199                                         Move<R, M>* rev = m->apply(s);
    00200                                         if (!rev)
    00201                                         {
    00202                                                 cout << "Problem! Reverse move is NULL!" << endl;
    00203                                                 cout << "Move: ";
    00204                                                 m->print();
    00205                                                 return;
    00206                                         }
    00207                                         Move<R, M>* new_m = rev->apply(s);
    00208                                         delete new_m;
    00209
    00210                                         Evaluation<M>* e3 = eval->evaluate(s);
    00211
    00212                                         if (e2->evaluation() != e3->evaluation())
    00213                                         {
    00214                                                 cout << "Problem with reverse move." << endl;
    00215                                                 cout << "Move: ";
    00216                                                 m->print();
    00217                                                 cout << "Reverse: ";
    00218                                                 rev->print();
    00219                                                 cout << "First evaluation: " << e2->evaluation() << endl;
    00220                                                 cout << "Second evaluation: " << e3->evaluation() << endl;
    00221
    00222                                                 delete e2;
    00223                                                 delete e3;
    00224                                                 delete rev;
    00225                                                 delete m;
    00226                                                 return;
    00227                                         }
    00228                                 }
    00229
    00230                                 if (m->canBeApplied(s)) // Second -> checking evaluation and reevaluation
    00231                                 {
    00232                                         Evaluation<M>* e1 = eval->evaluate(s);
    00233                                         double f1 = e1->evaluation();
    00234
    00235                                         Move<R, M>* rev = m->apply(e1, s);
    00236                                         if (!rev)
    00237                                         {
    00238                                                 cout << "Problem! Reverse move is NULL!" << endl;
    00239                                                 cout << "Move: ";
    00240                                                 m->print();
    00241                                                 return;
    00242                                         }
    00243                                         Move<R, M>* new_m = rev->apply(e1, s);
    00244                                         delete new_m;
    00245
    00246                                         Evaluation<M>* e2 = eval->evaluate(s);
    00247
    00248                                         if (e1->evaluation() != e2->evaluation())
    00249                                         {
    00250                                                 cout << "Problem with evaluation and reevaluation." << endl;
    00251                                                 cout << "Move: ";
    00252                                                 m->print();
    00253                                                 cout << "Reverse: ";
    00254                                                 rev->print();
    00255                                                 cout << "Reevaluation value: " << e1->evaluation() << endl;
    00256                                                 cout << "Evaluation value: " << e2->evaluation() << endl;
    00257
    00258                                                 delete e1;
    00259                                                 delete e2;
    00260                                                 delete rev;
    00261                                                 delete m;
    00262                                                 return;
    00263                                         }
    00264                                 }
    00265
    00266                                 delete m;
    00267                         }
    00268
    00269                         cout << "Number of moves: " << s_total << endl;
    00270                         cout << "Number of useful moves (can be applied): " << s_applied << endl;
    00271                         cout << "Percentage of useful moves: " << (s_applied / s_total) * 100 << "%" << endl;
    00272                         if (s_applied < 0.01 * s_total)
    00273                                 cout << "Warning: useful moves less than 1% of total moves." << endl;
    00274                 }
    00275
    00276                 cout << "4.2 - Best Improvement " << endl;
    00277                 for (int i = 0; i < ns_seq_list.size(); i++)
    00278                 {
    00279                         cout << "neighborhood " << i << ": ";
    00280                         s2 = s->clone();
    00281                         Solution<R>* s3 = ns_seq_list[i]->bestImprovement(eval, s2);
    00282                         s3->print();
    00283                         delete s2;
    00284                         delete s3;
    00285                 }
    00286                 cout << "[Ok]" << endl;
    00287
    00288                 cout << "4.3 - First Improvement " << endl;
    00289                 for (int i = 0; i < ns_seq_list.size(); i++)
    00290                 {
    00291                         cout << "neighborhood " << i << ": ";
    00292                         s2 = s->clone();
    00293                         Solution<R>* s3 = ns_seq_list[i]->firstImprovement(eval, s2);
    00294                         s3->print();
    00295                         delete s2;
    00296                         delete s3;
    00297                 }
    00298                 cout << "[Ok]" << endl;
    00299                 */
    00300
    00301                 // ==========================================================
    00302                 // ==========================================================
    00303
    00304                 cout << "Tests finished successfully!" << endl;
    00305         }
    00306
    00307 };
    00308
    00309 #endif /* CHECKMODULE_HPP_ */

All Classes Functions