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
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 cout << "Tests finished successfully!" << endl;
00305 }
00306
00307 };
00308
00309 #endif