00001 #ifndef EXPORT_HPP_
00002 #define EXPORT_HPP_
00003
00004 #include "../OptFrameModule.hpp"
00005
00006 #include <stdio.h>
00007
00008 template<class R, class M>
00009 class ExportModule: public OptFrameModule<R, M>
00010 {
00011 public:
00012 string id()
00013 {
00014 return "export";
00015 }
00016
00017 string usage()
00018 {
00019 string out = "\texport initsol id output_filename\n";
00020 out += "\texport loadsol id output_filename\nWHERE:\n";
00021 out += "\tid can assume two different type values:\n";
00022 out += "\t\t1 - an specific solution id,\n";
00023
00024 out += "\t\t2 - the special character (*) meaning that all solutions will be saved.\n";
00025
00026 return out;
00027 }
00028
00029 void run(vector<OptFrameModule<R, M>*> all_modules,
00030 HeuristicFactory<R, M>* factory, map<string, string>* dictionary,
00031 string input)
00032 {
00033
00034 Scanner scanner(input);
00035
00036 if (!scanner.hasNext())
00037 {
00038 cout << "Usage: " << usage() << endl;
00039 return;
00040 }
00041
00042 string sol = scanner.next();
00043
00044 if (sol != "initsol" && sol != "loadsol")
00045 {
00046 cout << "First parameter must be either 'initsol' or 'loadsol'!"
00047 << endl;
00048 cout << "Usage: " << usage() << endl;
00049 return;
00050 }
00051
00052 string id = scanner.next();
00053
00054 string filename = scanner.next();
00055
00056 FILE * pFile;
00057
00058 pFile = fopen(filename.c_str(), "w");
00059
00060 if (id == "*")
00061 {
00062 if (sol == "initsol")
00063 {
00064 for (int i = 0; i < (factory->initsol_size()); i++)
00065 {
00066 stringstream i_temp;
00067 i_temp << i;
00068
00069 Scanner s2((sol + " " + i_temp.str()));
00070
00071 Solution<R>* s =
00072 &(factory->read_initsol(&s2))->generateSolution();
00073
00074 stringstream stream;
00075
00076 stream << s->getR() << endl;
00077
00078 fprintf(pFile, "%s", (stream.str()).c_str());
00079 }
00080 }
00081 else
00082 {
00083 for (int i = 0; i < (factory->loadsol_size()); i++)
00084 {
00085 stringstream i_temp;
00086 i_temp << i;
00087
00088 Scanner s2((sol + " " + i_temp.str()));
00089
00090 Solution<R>* s = factory->read_loadsol(&s2);
00091
00092 stringstream stream;
00093
00094 stream << s->getR() << endl;
00095
00096 fprintf(pFile, "%s", (stream.str()).c_str());
00097 }
00098 }
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 else
00166 {
00167 Scanner s2(sol + " " + id);
00168
00169 Solution<R>* s;
00170
00171 if (sol == "initsol")
00172 s = &(factory->read_initsol(&s2))->generateSolution();
00173 else
00174 s = factory->read_loadsol(&s2);
00175
00176 stringstream stream;
00177
00178 stream << s->getR() << endl;
00179
00180 fprintf(pFile, "%s", (stream.str()).c_str());
00181
00182 }
00183
00184 fclose(pFile);
00185 }
00186 };
00187
00188 #endif