#ifndef _RECORDER #define _RECORDER #include #include #include #include #include #include namespace fs = std::filesystem; template struct Recorder { bool verbose; std::string name; T global_best_task; T current_best_task; long long global_best_score; long long current_best_score; Recorder(std::string name = "") : name(name) { verbose = false; global_best_score = 0; current_best_score = 0; } void set_verbose(bool verbose = true) { this->verbose = verbose; } void submit(T &task) { auto task_score = task.score(); if (task_score > global_best_score) { std::cout << "Best improved: " << task_score << " <-- " << global_best_score << std::endl; current_best_score = global_best_score = task_score; current_best_task = global_best_task = task; if (name == "") { std::cout << "Warning: No name assigned for this recorder." << std::endl; return; } fs::path tasks = "tasks"; std::fstream fs_score, fs_submit, fs_info; fs_score.open(tasks / (name + ".score")); fs_score << task_score << std::endl; fs_score.close(); fs_submit.open(tasks / (name + ".out")); fs_info.open(tasks / (name + ".info")); task.save(fs_submit, fs_info); fs_submit.close(); fs_info.close(); } else if (task_score > current_best_score) { std::cout << "Current improved: " << task_score << " <-- " << current_best_score << std::endl; current_best_score = task_score; current_best_task = task; if (verbose) { task.save(std::cout, std::cerr); } } } void load(T &task) { fs::path tasks = "tasks"; std::ifstream fs_score, fs_submit, fs_info; auto cur_name = tasks / (name + ".score"); fs_score.open(tasks / (name + ".score")); fs_score >> global_best_score; fs_score.close(); fs_submit.open(tasks / (name + ".out")); fs_info.open(tasks / (name + ".info")); task.load(fs_submit, fs_info); fs_submit.close(); fs_info.close(); std::cout << "Loaded Task: " << name << " Score: " << global_best_score << std::endl; } }; template void print(T &task) { task.save(std::cout, std::cerr); } #endif