00001 #ifndef EMPIRICALMODULE_HPP_
00002 #define EMPIRICALMODULE_HPP_
00003
00004 #include "../OptFrameModule.hpp"
00005
00006 template<class R, class M>
00007 class EmpiricalModule: public OptFrameModule<R, M>
00008 {
00009 public:
00010 string id()
00011 {
00012 return "empirical";
00013 }
00014 string usage()
00015 {
00016 string u = "empirical N T TF BF ISG EVAL METHOD OUTPUTFILE\n WHERE:\n";
00017 u += "N is the number of tests to be executed;\n";
00018 u += "T is the timelimit, in seconds, for each test; (0 for no timelimit)\n";
00019 u += "TF is the target evaluation function value;\n";
00020 u += "BF is the best known evaluation function value;\n";
00021 u += "ISG is the initial solution generator; (e.g. initsol 0)\n";
00022 u += "EVAL is the main evaluator; (e.g. ev 0)\n";
00023 u += "METHOD is the method to be tested with its own parameters;\n";
00024 u += "OUTPUTFILE is the output file.\n";
00025
00026 return u;
00027 }
00028
00029 void run(vector<OptFrameModule<R, M>*> all_modules, HeuristicFactory<R, M>* factory, map<string, string>* dictionary, string input)
00030 {
00031 Scanner scanner(input);
00032
00033 if (!scanner.hasNext())
00034 {
00035 cout << "Usage: " << usage() << endl;
00036 return;
00037 }
00038
00039 int n = scanner.nextInt();
00040 int t = scanner.nextDouble();
00041 double tf = scanner.nextDouble();
00042 double bf = scanner.nextDouble();
00043 InitialSolution<R>* initsol = factory->read_initsol(&scanner);
00044 Evaluator<R, M>* eval = factory->read_ev(&scanner);
00045 pair<Heuristic<R, M>*, string> method = factory->createHeuristic(scanner.rest());
00046
00047 Heuristic<R, M>* h = method.first;
00048
00049 string filename = method.second;
00050
00051 if (t == 0)
00052 t = 1000000000;
00053
00054 long timelimit = t;
00055
00056 if (bf == 0)
00057 bf = 0.00001;
00058
00059 FILE* file = fopen(filename.c_str(), "a");
00060 if (!file)
00061 {
00062 cout << "Error creating file '" << filename << "'" << endl;
00063 return;
00064 }
00065
00066 double t_now = 0;
00067 double fo_now = 0;
00068
00069 vector<double> time_spent;
00070
00071 for (int i = 0; i < n; i++)
00072 {
00073 long seed = time(NULL) + i;
00074 fprintf(file, "%ld\t", seed);
00075
00076 cout << "Test " << i << " {seed=" << seed << "}... Running";
00077 Timer t(false);
00078
00079 Solution<R>* s = &initsol->generateSolution();
00080 t_now = t.now();
00081 fo_now = eval->evaluate(*s).evaluation();
00082 fprintf(file, "%f\t%.3f\t", fo_now, t_now);
00083
00084 Solution<R>* s2 = &h->search(*s, timelimit, tf);
00085 t_now = t.now();
00086 fo_now = eval->evaluate(*s2).evaluation();
00087 fprintf(file, "%f\t%.3f\t", fo_now, t_now);
00088 time_spent.push_back(t_now);
00089
00090 cout << "... Finished! (" << t.now() << "secs.)" << endl;
00091
00092 delete s;
00093 delete s2;
00094
00095 fprintf(file, "\n");
00096 }
00097
00098 sort(time_spent.begin(), time_spent.end());
00099
00100 fprintf(file, "EMPIRICAL PROBABILITY DISTRIBUTION:\ni\ttime(i)\tp(i)=(i-0.5)/N\n0\t0\t0\n");
00101 for (int i = 1; i <= n; i++)
00102 fprintf(file, "%i\t%.3f\t%.3f\n", i, time_spent[i - 1], (i - 0.5) / n);
00103
00104 fprintf(file, "PARAMETERS:%s\n", input.c_str());
00105
00106 fclose(file);
00107 }
00108
00109 };
00110
00111 #endif