123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #ifndef _RECORDER
- #define _RECORDER
- #include <iostream>
- #include <fstream>
- #include <ostream>
- #include <istream>
- #include <streambuf>
- #include <filesystem>
- namespace fs = std::filesystem;
- template <typename T>
- 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 <typename T>
- void print(T &task)
- {
- task.save(std::cout, std::cerr);
- }
- #endif
|