record iteration time

Signed-off-by: szdytom <szdytom@qq.com>
This commit is contained in:
方而静 2024-01-19 19:45:34 +08:00
parent 7d6bd6f085
commit 0c0f25a566

View File

@ -3,13 +3,13 @@
#include "pushbox.h"
#include "box-solver.h"
std::mt19937 rng(std::random_device{}());
#define compiler_assume(condition) \
do { \
if (!(condition)) \
__builtin_unreachable(); \
} while (false); \
} while (false);
using i64 = long long;
const int species_limit = 16;
const int population_limit = 10;
@ -17,6 +17,8 @@ const int protect_age = 30;
const int stable_limit = 10;
const int SPECIES_NAME_LEN = 4;
std::mt19937 rng(std::random_device{}());
struct Gene {
Maze m;
int w;
@ -82,17 +84,17 @@ inline Species loadSpecies(std::FILE *f) {
return res;
}
inline void loadBiosphere(std::vector<Species> &species, int &best_w, std::FILE *f) {
inline void loadBiosphere(std::vector<Species> &species, int &best_w, int &iter_n, i64 &tc, std::FILE *f) {
int n;
std::fscanf(f, "%d %d", &n, &best_w);
std::fscanf(f, "%d %d %d %lld", &n, &best_w, &iter_n, &tc);
species.reserve(n);
for (int i = 0; i < n; ++i)
species.push_back(loadSpecies(f));
}
inline void writeBiosphere(const std::vector<Species> &species, int &best_w, std::FILE *f) {
inline void writeBiosphere(const std::vector<Species> &species, int best_w, int iter_n, i64 tc, std::FILE *f) {
int n = species.size();
std::fprintf(f, "%d %d\n", n, best_w);
std::fprintf(f, "%d %d %d %lld\n", n, best_w, iter_n, tc);
for (int i = 0; i < n; ++i)
writeSpecies(species[i], f);
}
@ -307,15 +309,18 @@ inline void setColor(int color)
int main() {
std::vector<Species> biosphere;
int history_best_w;
int history_best_w, iter_start;
i64 time_cost_old = 0;
auto frecord = std::fopen("current.txt", "r");
loadBiosphere(biosphere, history_best_w, frecord);
loadBiosphere(biosphere, history_best_w, iter_start, time_cost_old, frecord);
std::fclose(frecord);
std::chrono::milliseconds time_cost(time_cost_old);
std::uniform_real_distribution<float> d01(0, 1);
int new_record_counter = 0;
for (int iter_id = 1; ; ++iter_id) {
for (int iter_id = iter_start + 1; ; ++iter_id) {
auto iter_st = std::chrono::steady_clock::now();
int best_w = 0;
# pragma omp parallel for num_threads(8)
@ -327,10 +332,9 @@ int main() {
std::vector<Species> Hnxt, Lnxt;
for (auto &s : biosphere) {
if (s.stable_age < std::max(3, stable_limit * s.best_w / best_w)
|| s.best_w == best_w)
{
if (s.best_w < best_w || s.age < protect_age)
const int sl = std::max(3, stable_limit * s.best_w / best_w);
if (s.stable_age < sl || s.best_w == best_w) {
if (s.best_w < best_w || s.age < protect_age || s.stable_age < sl)
Hnxt.push_back(std::move(s));
else
Lnxt.push_back(std::move(s));
@ -338,6 +342,7 @@ int main() {
}
biosphere.clear();
if (Lnxt.size()) {
shuffle(Lnxt.begin(), Lnxt.end(), rng);
biosphere.push_back(std::move(*std::max_element(Lnxt.begin(), Lnxt.end())));
}
@ -357,28 +362,35 @@ int main() {
for (auto &s : biosphere)
s.tick();
auto iter_ed = std::chrono::steady_clock::now();
time_cost += std::chrono::duration_cast<std::chrono::milliseconds>(iter_ed - iter_st);
bool broke_record = best_w > history_best_w;
if (broke_record) {
history_best_w = best_w;
new_record_counter = 5;
std::FILE *f = fopen("best.txt", "w");
writeBiosphere(biosphere, best_w, f);
fclose(f);
writeBiosphere(biosphere, best_w, iter_id, time_cost.count(), f);
std::freopen("log.txt", "a", f);
std::fprintf(f, "[%lldms] [%d]: %d\n", (i64)time_cost.count(), iter_id, best_w);
std::fclose(f);
}
if (true) {
term::clear();
printf("Iterated for %d times.\n", iter_id);
i64 tc_val = time_cost.count() / 1000;
auto tc_hr = tc_val / 3600, tc_min = (tc_val / 60) % 60, tc_sec = tc_val % 60;
printf("Iterated for %d times in %lld hour(s), %lld minute(s), %lld second(s).\n"
, iter_id, tc_hr, tc_min, tc_sec);
printf("Current Best=%d, History Best=%d, Species:\n", best_w, history_best_w);
for (auto &s : biosphere) {
if (s.best_w == best_w)
term::setColor(35); // purple
else if (s.stable_age > 5)
term::setColor(31); // red
else if (s.age <= 2)
term::setColor(34); // blue
else if (s.best_w >= best_w * 95 / 100)
term::setColor(32); // green
else if (s.stable_age == 1)
term::setColor(34); // blue
printf(" - %s[%d]: best=%d\tstable=%d\tage=%d.\n", s.name
, s.tactic, s.best_w, s.stable_age, s.age);
@ -394,7 +406,7 @@ int main() {
fflush(stdout);
std::FILE *f = fopen("current.txt", "w");
writeBiosphere(biosphere, best_w, f);
writeBiosphere(biosphere, best_w, iter_id, time_cost.count(), f);
fclose(f);
}
}