Skip to content

Commit

Permalink
fix: Replacing genes with genomes
Browse files Browse the repository at this point in the history
  • Loading branch information
AsPJT committed May 22, 2024
1 parent 0d5c540 commit e2d4a8d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 62 deletions.
4 changes: 2 additions & 2 deletions Library/PAX_SAPIENTICA/Simulation/Chromosome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint_least64_t> dist(std::numeric_limits<std::uint_least64_t>::min(), std::numeric_limits<std::uint_least64_t>::max());
std::uniform_int_distribution<std::uint_least64_t> dist((std::numeric_limits<std::uint_least64_t>::min)(), (std::numeric_limits<std::uint_least64_t>::max)());
std::uint_least64_t random_value = dist(engine);

Chromosome child;
Expand All @@ -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<std::uint_least8_t>::min(), std::numeric_limits<std::uint_least8_t>::max());
std::uniform_int_distribution<> dist((std::numeric_limits<std::uint_least8_t>::min)(), (std::numeric_limits<std::uint_least8_t>::max)());

Chromosome random_chromosome;
for (std::uint_least8_t i = 0; i < chromosome_length; ++i) {
Expand Down
38 changes: 19 additions & 19 deletions Library/PAX_SAPIENTICA/Simulation/Gene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

namespace paxs {

/// @brief 遺伝子
class Gene {
/// @brief ゲノム
class Genome {
public:
Gene() = default;
Genome() = default;

const Chromosome& cgetChromosome() const noexcept {
return chromosome;
Expand Down Expand Up @@ -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<std::uint_least8_t>::min(), std::numeric_limits<std::uint_least8_t>::max());
gene.setMtDNA(dist(engine));
gene.setYDNA(dist(engine));
return gene;
std::uniform_int_distribution<> dist((std::numeric_limits<std::uint_least8_t>::min)(), (std::numeric_limits<std::uint_least8_t>::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;
}

Expand Down
10 changes: 5 additions & 5 deletions Library/PAX_SAPIENTICA/Simulation/Settlement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<std::uint_least64_t>::generate(),
0, // TODO: 名前ID
0,
kanakuma_life_span.setLifeSpan(gene.getGender(), *gen),
gene
kanakuma_life_span.setLifeSpan(genome.getGender(), *gen),
genome
));
}
}
Expand Down
26 changes: 13 additions & 13 deletions Library/PAX_SAPIENTICA/Simulation/SettlementAgent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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を取得
Expand Down Expand Up @@ -71,23 +71,23 @@ 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 &&
name_id == a.name_id &&
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?
Expand All @@ -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?
Expand Down Expand Up @@ -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; }
Expand All @@ -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; // 結婚相手のゲノム
};
}

Expand Down
12 changes: 6 additions & 6 deletions Library/PAX_SAPIENTICA/Simulation/SettlementSimulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,16 @@ 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<int>(set_lifespan - 1) }; // 性別の乱数分布

settlement.setAgent(Agent(UniqueIdentification<std::uint_least64_t>::generate(),
0, // TODO: 名前ID
lifespan_dist(gen),
set_lifespan,
gene), static_cast<std::size_t>(i));
genome), static_cast<std::size_t>(i));
}

// 令制国の人口を減らす
Expand Down Expand Up @@ -373,8 +373,8 @@ namespace paxs {
for (auto& settlement : settlements) {
std::vector<Agent> 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<int>(set_lifespan - 1) }; // 性別の乱数分布

Expand All @@ -383,7 +383,7 @@ namespace paxs {
0, // TODO: 名前ID
lifespan_dist(gen),
set_lifespan,
gene
genome
);
}
settlement.addAgents(agents);
Expand Down
34 changes: 17 additions & 17 deletions Projects/UnitTest/Source/Simulations/GeneUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@
#include <PAX_SAPIENTICA/Simulation/SimulationConst.hpp>

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<std::uint_least8_t>::max());
EXPECT_GE(genome.cgetChromosome().get(i), 0);
EXPECT_LE(genome.cgetChromosome().get(i), std::numeric_limits<std::uint_least8_t>::max());
}
EXPECT_GE(gene.getMtDNA(), 0);
EXPECT_LE(gene.getMtDNA(), std::numeric_limits<std::uint_least8_t>::max());
EXPECT_GE(gene.getYDNA(), 0);
EXPECT_LE(gene.getYDNA(), std::numeric_limits<std::uint_least8_t>::max());
EXPECT_GE(genome.getMtDNA(), 0);
EXPECT_LE(genome.getMtDNA(), std::numeric_limits<std::uint_least8_t>::max());
EXPECT_GE(genome.getYDNA(), 0);
EXPECT_LE(genome.getYDNA(), std::numeric_limits<std::uint_least8_t>::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);
Expand Down

0 comments on commit e2d4a8d

Please sign in to comment.