add TUI & tweak params

Signed-off-by: szdytom <szdytom@qq.com>
This commit is contained in:
方而静 2024-01-19 16:02:08 +08:00
parent 2b82fb1c7f
commit 148a0e46d5

View File

@ -11,8 +11,10 @@ std::mt19937 rng(std::random_device{}());
__builtin_unreachable(); \ __builtin_unreachable(); \
} while (false); \ } while (false); \
const int species_limit = 8; const int species_limit = 16;
const int population_limit = 10; const int population_limit = 10;
const int protect_age = 30;
const int stable_limit = 10;
const int SPECIES_NAME_LEN = 4; const int SPECIES_NAME_LEN = 4;
struct Gene { struct Gene {
@ -207,19 +209,19 @@ inline Species mutation(const Species &sc) {
res.tactic = tactic; res.tactic = tactic;
switch (tactic) { switch (tactic) {
case 0: case 0:
mutationRandomHole(res, sc.population[0], 60); mutationRandomHole(res, sc.population[0], 80);
break; break;
case 1: case 1:
mutationRowCl(res, sc.population[0], 2); mutationRowCl(res, sc.population[0], 3);
break; break;
case 2: case 2:
mutationColCl(res, sc.population[0], 2); mutationColCl(res, sc.population[0], 3);
break; break;
case 3: case 3:
mutationSqrCl(res, sc.population[0], 3); mutationSqrCl(res, sc.population[0], 5);
break; break;
case 4: case 4:
mutationSqrCl(res, sc.population[0], 5); mutationSqrCl(res, sc.population[0], 7);
break; break;
} }
} while (res.empty()); } while (res.empty());
@ -289,6 +291,20 @@ inline void inherit(Species &s) {
s.population.resize(population_limit); s.population.resize(population_limit);
} }
namespace term {
inline void clear()
{
printf("\033[2J\033[0;0H");
}
inline void setColor(int color)
{
printf("\033[%dm", color);
}
}
int main() { int main() {
std::vector<Species> biosphere; std::vector<Species> biosphere;
int history_best_w; int history_best_w;
@ -298,6 +314,7 @@ int main() {
std::fclose(frecord); std::fclose(frecord);
std::uniform_real_distribution<float> d01(0, 1); std::uniform_real_distribution<float> d01(0, 1);
int new_record_counter = 0;
for (int iter_id = 1; ; ++iter_id) { for (int iter_id = 1; ; ++iter_id) {
int best_w = 0; int best_w = 0;
@ -310,10 +327,10 @@ int main() {
std::vector<Species> Hnxt, Lnxt; std::vector<Species> Hnxt, Lnxt;
for (auto &s : biosphere) { for (auto &s : biosphere) {
if (s.stable_age <= std::max(5, 15 * s.best_w / best_w) if (s.stable_age < std::max(3, stable_limit * s.best_w / best_w)
|| s.best_w == best_w) || s.best_w == best_w)
{ {
if (s.best_w < best_w || s.age <= 35) if (s.best_w < best_w || s.age < protect_age)
Hnxt.push_back(std::move(s)); Hnxt.push_back(std::move(s));
else else
Lnxt.push_back(std::move(s)); Lnxt.push_back(std::move(s));
@ -343,20 +360,39 @@ int main() {
bool broke_record = best_w > history_best_w; bool broke_record = best_w > history_best_w;
if (broke_record) { if (broke_record) {
history_best_w = best_w; history_best_w = best_w;
printf("New Record: %d!\n", best_w); new_record_counter = 5;
std::FILE *f = fopen("best.txt", "w"); std::FILE *f = fopen("best.txt", "w");
writeBiosphere(biosphere, best_w, f); writeBiosphere(biosphere, best_w, f);
fclose(f); fclose(f);
} }
if (true) { if (true) {
term::clear();
printf("Iterated for %d times.\n", iter_id); printf("Iterated for %d times.\n", iter_id);
printf("Current Best=%d, History Best=%d, Species:\n", best_w, history_best_w); printf("Current Best=%d, History Best=%d, Species:\n", best_w, history_best_w);
for (auto &s : biosphere) { for (auto &s : biosphere) {
printf(" - %s[%d]: best=%d stable=%d age=%d.\n", s.name 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
printf(" - %s[%d]: best=%d\tstable=%d\tage=%d.\n", s.name
, s.tactic, s.best_w, s.stable_age, s.age); , s.tactic, s.best_w, s.stable_age, s.age);
term::setColor(0);
} }
if (new_record_counter > 0) {
term::setColor(32);
printf("New Record: %d\n", best_w);
term::setColor(0);
new_record_counter -= 1;
}
fflush(stdout);
std::FILE *f = fopen("current.txt", "w"); std::FILE *f = fopen("current.txt", "w");
writeBiosphere(biosphere, best_w, f); writeBiosphere(biosphere, best_w, f);
fclose(f); fclose(f);