From e2d4a8d85c222b9d64a14da9d72de3c3c6484dcb Mon Sep 17 00:00:00 2001 From: wanotaitei Date: Wed, 22 May 2024 18:09:29 +0900 Subject: [PATCH] fix: Replacing genes with genomes --- .../PAX_SAPIENTICA/Simulation/Chromosome.hpp | 4 +- Library/PAX_SAPIENTICA/Simulation/Gene.hpp | 38 +++++++++---------- .../PAX_SAPIENTICA/Simulation/Settlement.hpp | 10 ++--- .../Simulation/SettlementAgent.hpp | 26 ++++++------- .../Simulation/SettlementSimulator.hpp | 12 +++--- .../Source/Simulations/GeneUnitTest.cpp | 34 ++++++++--------- 6 files changed, 62 insertions(+), 62 deletions(-) diff --git a/Library/PAX_SAPIENTICA/Simulation/Chromosome.hpp b/Library/PAX_SAPIENTICA/Simulation/Chromosome.hpp index b0dd3cee0..72704da6e 100644 --- a/Library/PAX_SAPIENTICA/Simulation/Chromosome.hpp +++ b/Library/PAX_SAPIENTICA/Simulation/Chromosome.hpp @@ -73,7 +73,7 @@ namespace paxs { static Chromosome generateFromParents(const Chromosome& mother, const Chromosome& father) noexcept { std::random_device seed_gen; std::mt19937 engine(seed_gen()); - std::uniform_int_distribution dist(std::numeric_limits::min(), std::numeric_limits::max()); + std::uniform_int_distribution dist((std::numeric_limits::min)(), (std::numeric_limits::max)()); std::uint_least64_t random_value = dist(engine); Chromosome child; @@ -96,7 +96,7 @@ namespace paxs { static Chromosome generateRandom() noexcept { std::random_device seed_gen; std::mt19937 engine(seed_gen()); - std::uniform_int_distribution<> dist(std::numeric_limits::min(), std::numeric_limits::max()); + std::uniform_int_distribution<> dist((std::numeric_limits::min)(), (std::numeric_limits::max)()); Chromosome random_chromosome; for (std::uint_least8_t i = 0; i < chromosome_length; ++i) { diff --git a/Library/PAX_SAPIENTICA/Simulation/Gene.hpp b/Library/PAX_SAPIENTICA/Simulation/Gene.hpp index 93e312b91..1718edd21 100644 --- a/Library/PAX_SAPIENTICA/Simulation/Gene.hpp +++ b/Library/PAX_SAPIENTICA/Simulation/Gene.hpp @@ -21,10 +21,10 @@ namespace paxs { - /// @brief 遺伝子 - class Gene { + /// @brief ゲノム + class Genome { public: - Gene() = default; + Genome() = default; const Chromosome& cgetChromosome() const noexcept { return chromosome; @@ -58,35 +58,35 @@ namespace paxs { return chromosome.getGender(); } - static Gene generateRandom() noexcept { - Gene gene; - gene.setChromosome(Chromosome::generateRandom()); + static Genome generateRandom() noexcept { + Genome genome; + genome.setChromosome(Chromosome::generateRandom()); std::random_device seed_gen; std::mt19937 engine(seed_gen()); - std::uniform_int_distribution<> dist(std::numeric_limits::min(), std::numeric_limits::max()); - gene.setMtDNA(dist(engine)); - gene.setYDNA(dist(engine)); - return gene; + std::uniform_int_distribution<> dist((std::numeric_limits::min)(), (std::numeric_limits::max)()); + genome.setMtDNA(dist(engine)); + genome.setYDNA(dist(engine)); + return genome; } - static Gene generateFromParents(const Gene& mother, const Gene& father) noexcept { - Gene gene; - gene.setChromosome(Chromosome::generateFromParents(mother.cgetChromosome(), father.cgetChromosome())); + static Genome generateFromParents(const Genome& mother, const Genome& father) noexcept { + Genome genome; + genome.setChromosome(Chromosome::generateFromParents(mother.cgetChromosome(), father.cgetChromosome())); std::random_device seed_gen; std::mt19937 engine(seed_gen()); - gene.setMtDNA(mother.getMtDNA()); - if (gene.cgetChromosome().getGender() == SimulationConstants::getInstance()->female) { - gene.setYDNA(mother.getYDNA()); + genome.setMtDNA(mother.getMtDNA()); + if (genome.cgetChromosome().getGender() == SimulationConstants::getInstance()->female) { + genome.setYDNA(mother.getYDNA()); } else { - gene.setYDNA(father.getYDNA()); + genome.setYDNA(father.getYDNA()); } - return gene; + return genome; } - bool operator==(const Gene& rhs) const noexcept { + bool operator==(const Genome& rhs) const noexcept { return chromosome == rhs.chromosome && mtDNA == rhs.mtDNA && yDNA == rhs.yDNA; } diff --git a/Library/PAX_SAPIENTICA/Simulation/Settlement.hpp b/Library/PAX_SAPIENTICA/Simulation/Settlement.hpp index 84d823e5f..05c7f17c1 100644 --- a/Library/PAX_SAPIENTICA/Simulation/Settlement.hpp +++ b/Library/PAX_SAPIENTICA/Simulation/Settlement.hpp @@ -243,10 +243,10 @@ namespace paxs { Agent male_ = close_settlements[j].getAgentCopy(male_id); Agent& female_ = agents[marriageable_female_index[index_pair.first]]; - female_.marry(male_id, male_.cgetGenePtr()); + female_.marry(male_id, male_.cgetGenomePtr()); const std::uint_least64_t female_id = female_.getId(); - male_.marry(female_id, female_.cgetGenePtr()); + male_.marry(female_id, female_.cgetGenomePtr()); agents.emplace_back(male_); is_found = true; @@ -390,13 +390,13 @@ namespace paxs { if (random_dist(*gen) < 0.11f) continue; // TODO: 直す if (!agent.isMarried()) continue; - Gene gene = Gene::generateFromParents(agent.cgetGene(), agent.cgetPartnerGene()); + Genome genome = Genome::generateFromParents(agent.cgetGenome(), agent.cgetPartnerGenome()); children.emplace_back(Agent( UniqueIdentification::generate(), 0, // TODO: 名前ID 0, - kanakuma_life_span.setLifeSpan(gene.getGender(), *gen), - gene + kanakuma_life_span.setLifeSpan(genome.getGender(), *gen), + genome )); } } diff --git a/Library/PAX_SAPIENTICA/Simulation/SettlementAgent.hpp b/Library/PAX_SAPIENTICA/Simulation/SettlementAgent.hpp index fc8ee2e70..99089bc4a 100644 --- a/Library/PAX_SAPIENTICA/Simulation/SettlementAgent.hpp +++ b/Library/PAX_SAPIENTICA/Simulation/SettlementAgent.hpp @@ -38,8 +38,8 @@ namespace paxs { SettlementAgent() = default; explicit SettlementAgent(const std::uint_least64_t id, const std::uint_least32_t& name_id, - const std::uint_least32_t age, const std::uint_least32_t life_span, const Gene& gene) noexcept - : id(id), name_id(name_id), gender(gene.getGender()), age(age), life_span(life_span), gene(gene) {} + const std::uint_least32_t age, const std::uint_least32_t life_span, const Genome& genome) noexcept + : id(id), name_id(name_id), gender(genome.getGender()), age(age), life_span(life_span), genome(genome) {} /// @brief Get the id. /// @brief idを取得 @@ -71,13 +71,13 @@ namespace paxs { /// @brief エージェントの年齢をインクリメントする constexpr void incrementAge(const std::uint_least32_t n) noexcept { age += n; } - constexpr const Gene& cgetGene() const noexcept { return gene; } + constexpr const Genome& cgetGenome() const noexcept { return genome; } - constexpr const Gene* cgetGenePtr() const noexcept { return &gene; } + constexpr const Genome* cgetGenomePtr() const noexcept { return &genome; } - constexpr void setPartnerGene(const Gene& gene) noexcept { partner_gene = &gene; } + constexpr void setPartnerGenome(const Genome& genome) noexcept { partner_genome = &genome; } - constexpr const Gene& cgetPartnerGene() const noexcept { return *partner_gene; } + constexpr const Genome& cgetPartnerGenome() const noexcept { return *partner_genome; } constexpr bool operator==(const SettlementAgent& a) const noexcept { return id == a.id && @@ -85,9 +85,9 @@ namespace paxs { gender == a.gender && age == a.age && life_span == a.life_span && - gene == a.gene && + genome == a.genome && partner_id == a.partner_id && - partner_gene == a.partner_gene; + partner_genome == a.partner_genome; } /// @brief Is the agent married? @@ -96,10 +96,10 @@ namespace paxs { /// @brief Set the agent's marriage status. /// @brief 結婚する - void marry(const std::uint_least64_t partner_id_, const Gene* partner_gene_) noexcept { + void marry(const std::uint_least64_t partner_id_, const Genome* partner_genome_) noexcept { is_married = true; partner_id = partner_id_; - partner_gene = partner_gene_; + partner_genome = partner_genome_; } /// @brief Is the agent able to marry? @@ -132,7 +132,7 @@ namespace paxs { void divorce() noexcept { is_married = false; partner_id = 0; - partner_gene = nullptr; + partner_genome = nullptr; } std::uint_least8_t getBirthIntervalCount() const noexcept { return birth_interval_count; } @@ -151,8 +151,8 @@ namespace paxs { std::uint_least64_t id; // ID std::uint_least64_t partner_id = 0; // 結婚相手のID - Gene gene; // 遺伝子 - Gene const* partner_gene = nullptr; // 結婚相手の遺伝子 + Genome genome; // ゲノム + Genome const* partner_genome = nullptr; // 結婚相手のゲノム }; } diff --git a/Library/PAX_SAPIENTICA/Simulation/SettlementSimulator.hpp b/Library/PAX_SAPIENTICA/Simulation/SettlementSimulator.hpp index fce8c7ff9..946f9c9d1 100644 --- a/Library/PAX_SAPIENTICA/Simulation/SettlementSimulator.hpp +++ b/Library/PAX_SAPIENTICA/Simulation/SettlementSimulator.hpp @@ -324,8 +324,8 @@ namespace paxs { settlement.resizeAgents(settlement_population); for (int i = 0; i < settlement_population; ++i) { - Gene gene = Gene::generateRandom(); - const std::uint_least32_t set_lifespan = kanakuma_life_span.setLifeSpan(gene.getGender(), gen); + Genome genome = Genome::generateRandom(); + const std::uint_least32_t set_lifespan = kanakuma_life_span.setLifeSpan(genome.getGender(), gen); std::uniform_int_distribution<> lifespan_dist{ 0, static_cast(set_lifespan - 1) }; // 性別の乱数分布 @@ -333,7 +333,7 @@ namespace paxs { 0, // TODO: 名前ID lifespan_dist(gen), set_lifespan, - gene), static_cast(i)); + genome), static_cast(i)); } // 令制国の人口を減らす @@ -373,8 +373,8 @@ namespace paxs { for (auto& settlement : settlements) { std::vector agents(add_population); for (int i = 0; i < add_population; ++i) { - Gene gene = Gene::generateRandom(); - const std::uint_least32_t set_lifespan = kanakuma_life_span.setLifeSpan(gene.getGender(), gen); + Genome genome = Genome::generateRandom(); + const std::uint_least32_t set_lifespan = kanakuma_life_span.setLifeSpan(genome.getGender(), gen); std::uniform_int_distribution<> lifespan_dist{ 0, static_cast(set_lifespan - 1) }; // 性別の乱数分布 @@ -383,7 +383,7 @@ namespace paxs { 0, // TODO: 名前ID lifespan_dist(gen), set_lifespan, - gene + genome ); } settlement.addAgents(agents); diff --git a/Projects/UnitTest/Source/Simulations/GeneUnitTest.cpp b/Projects/UnitTest/Source/Simulations/GeneUnitTest.cpp index 5d188c397..2f9d87bc9 100644 --- a/Projects/UnitTest/Source/Simulations/GeneUnitTest.cpp +++ b/Projects/UnitTest/Source/Simulations/GeneUnitTest.cpp @@ -4,36 +4,36 @@ #include TEST (GeneUnitTest, getAndSet) { - paxs::Gene gene; - gene.setMtDNA(1); - EXPECT_EQ(gene.getMtDNA(), 1); - gene.setYDNA(1); - EXPECT_EQ(gene.getYDNA(), 1); + paxs::Genome genome; + genome.setMtDNA(1); + EXPECT_EQ(genome.getMtDNA(), 1); + genome.setYDNA(1); + EXPECT_EQ(genome.getYDNA(), 1); paxs::Chromosome chromosome = paxs::Chromosome::generateRandom(); - gene.setChromosome(chromosome); - EXPECT_EQ(gene.cgetChromosome(), chromosome); + genome.setChromosome(chromosome); + EXPECT_EQ(genome.cgetChromosome(), chromosome); } TEST (GeneUnitTest, generateRandom) { - paxs::Gene gene = paxs::Gene::generateRandom(); + paxs::Genome genome = paxs::Genome::generateRandom(); for (std::uint_least8_t i = 0; i < paxs::Chromosome::chromosome_length; ++i) { - EXPECT_GE(gene.cgetChromosome().get(i), 0); - EXPECT_LE(gene.cgetChromosome().get(i), std::numeric_limits::max()); + EXPECT_GE(genome.cgetChromosome().get(i), 0); + EXPECT_LE(genome.cgetChromosome().get(i), std::numeric_limits::max()); } - EXPECT_GE(gene.getMtDNA(), 0); - EXPECT_LE(gene.getMtDNA(), std::numeric_limits::max()); - EXPECT_GE(gene.getYDNA(), 0); - EXPECT_LE(gene.getYDNA(), std::numeric_limits::max()); + EXPECT_GE(genome.getMtDNA(), 0); + EXPECT_LE(genome.getMtDNA(), std::numeric_limits::max()); + EXPECT_GE(genome.getYDNA(), 0); + EXPECT_LE(genome.getYDNA(), std::numeric_limits::max()); } TEST (GeneUnitTest, generateFromParents) { - paxs::Gene mother; - paxs::Gene father; + paxs::Genome mother; + paxs::Genome father; for (std::uint_least8_t i = 0; i < paxs::Chromosome::chromosome_length; ++i) { mother.getChromosome().set(i, 1); father.getChromosome().set(i, 0); } - paxs::Gene child = paxs::Gene::generateFromParents(mother, father); + paxs::Genome child = paxs::Genome::generateFromParents(mother, father); for (std::uint_least8_t i = 0; i < paxs::Chromosome::chromosome_length; ++i) { if (i % 2 == 0) { EXPECT_EQ(child.cgetChromosome().get(i), 1);