Skip to content

Commit

Permalink
add: Genome SNP
Browse files Browse the repository at this point in the history
  • Loading branch information
AsPJT committed Jun 11, 2024
1 parent dc33b18 commit 37e62c2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Library/PAX_MAHOROBA/LocationPoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,9 @@ namespace paxs {
// if (lli.lpe == MurMur3::calcHash("agent1"))
{
// const std::size_t pop_original = settlement.getFarmingPopulation(); // settlement.getPopulation();
const float pop_original = settlement.getFarmingPopulation() / float(settlement.getPopulation()) * 75.0f; // settlement.getPopulation();
//const float pop_original = settlement.getFarmingPopulation() / float(settlement.getPopulation()) * 75.0f; // settlement.getPopulation();
//const float pop_original = settlement.getMostMtDNA() / 27.0f * 75.0f; // settlement.getPopulation();
const float pop_original = settlement.getSNP() * 75.0f; // settlement.getPopulation();

const std::uint_least8_t pop = (pop_original >= 75) ? 75 : static_cast<std::uint_least8_t>(pop_original);
paxg::Circle(draw_pos,
Expand Down
20 changes: 16 additions & 4 deletions Library/PAX_SAPIENTICA/Simulation/Genome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ namespace paxs {
yDNA = value;
}

std::uint_least8_t getSNP() const noexcept {
return SNP;
}

void setSNP(const std::uint_least8_t value) noexcept {
SNP = value;
}

constexpr std::uint_least8_t getGender() const noexcept {
return chromosome.getGender();
}
Expand All @@ -65,13 +73,15 @@ namespace paxs {
std::uniform_int_distribution<> dist((std::numeric_limits<std::uint_least8_t>::min)(), (std::numeric_limits<std::uint_least8_t>::max)());
genome.setMtDNA(static_cast<std::uint_least8_t>(dist(engine)));
genome.setYDNA(static_cast<std::uint_least8_t>(dist(engine)));
genome.setSNP(static_cast<std::uint_least8_t>(dist(engine)));
return genome;
}
static Genome generateRandomSetMtDNA(std::mt19937& engine, const std::uint_least8_t mtdna_) noexcept {
static Genome generateRandomSetMtDNA(std::mt19937& engine, const std::uint_least8_t mtdna_, const std::uint_least8_t snp_) noexcept {
Genome genome;
genome.setChromosome(Chromosome::generateRandom(engine));

std::uniform_int_distribution<> dist((std::numeric_limits<std::uint_least8_t>::min)(), (std::numeric_limits<std::uint_least8_t>::max)());
genome.setSNP(snp_);
genome.setMtDNA(mtdna_);
genome.setYDNA(static_cast<std::uint_least8_t>(dist(engine)));
return genome;
Expand All @@ -88,6 +98,7 @@ namespace paxs {
else {
genome.setYDNA(father.getYDNA());
}
genome.setSNP(static_cast<std::uint_least8_t>((int(mother.getSNP()) + int(father.getSNP())) / 2));
return genome;
}

Expand All @@ -96,9 +107,10 @@ namespace paxs {
}

private:
Chromosome chromosome;
std::uint_least8_t mtDNA;
std::uint_least8_t yDNA;
Chromosome chromosome{};
std::uint_least8_t SNP = 0;
std::uint_least8_t mtDNA = 0;
std::uint_least8_t yDNA = 0;
};
}

Expand Down
11 changes: 11 additions & 0 deletions Library/PAX_SAPIENTICA/Simulation/Settlement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,17 @@ namespace paxs {
return farming_population;
}

/// @brief Get the SNP.
/// @brief SNP を取得
double getSNP() const noexcept {
std::uint_least64_t snp = 0;

for (std::size_t i = 0; i < agents.size(); ++i) {
snp += static_cast<std::uint_least64_t>(agents[i].cgetGenome().getSNP());
}
return static_cast<double>(snp) / static_cast<double>(agents.size()) / 255.0;
}

/// @brief Get the most mtDNA.
/// @brief 最多 mtDNA を取得
std::size_t getMostMtDNA() const noexcept {
Expand Down
20 changes: 15 additions & 5 deletions Library/PAX_SAPIENTICA/Simulation/SettlementSimulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace paxs {
void init() {
settlement_grids.clear();
initRandomizeSettlements();
randomizeSettlements(0, 0, 255);
randomizeSettlements(0, 0, 255, 0/*縄文人は SNP:0*/);
}

/// @brief Run the simulation for the specified number of steps.
Expand Down Expand Up @@ -131,6 +131,8 @@ namespace paxs {
std::size_t sat_num = 0; // 集落数
std::vector < std::vector<int>> mtdna_num(90, std::vector<int>(256, 0)); // mtDNA 数
std::size_t ryopop[90]{};
std::size_t ryoset[90]{};
double ryosnp[90]{};

// 地名を描画
for (const auto& agent : getSettlementGrids()) {
Expand All @@ -141,6 +143,8 @@ namespace paxs {
const std::uint_least8_t ryo_id = environment->template getData<std::uint_least8_t>(MurMur3::calcHash("gbank"), settlement.getPosition());
if (ryo_id < 90) {
ryopop[ryo_id]+= settlement.getPopulation(); // 地区ごとに人口数を増加させる
ryosnp[ryo_id]+= settlement.getSNP(); // 地区ごとに SNP を増加させる
++(ryoset[ryo_id]);

// mtDNA ごとにカウント
for (int popi = 0; popi < settlement.cgetAgents().size(); ++popi) {
Expand All @@ -151,8 +155,11 @@ namespace paxs {
}
pop_ofs << step_count << '\t' << sat_num << '\t' << pop_num << '\t';
pop_mtdna_ofs << step_count << '\t' << sat_num << '\t' << pop_num << '\t';
pop_snp_ofs << step_count << '\t' << sat_num << '\t' << pop_num << '\t';
for (int i = 0; i < 90; ++i) {
ryosnp[i] /= static_cast<double>(ryoset[i]);
pop_ofs << ryopop[i] << '\t';
pop_snp_ofs << ryosnp[i] << '\t';
for (int j = 0; j < 27; ++j) {
if (int(mtdna_num[i][j]) == 0) continue;
pop_mtdna_ofs << japan_provinces->getMtDNA_Name(j) << ':' << int(mtdna_num[i][j]) << '/';
Expand All @@ -161,6 +168,7 @@ namespace paxs {
}
pop_ofs << step_count << '\n';
pop_mtdna_ofs << step_count << '\n';
pop_snp_ofs << step_count << '\n';
}

std::vector<std::tuple<std::uint_least32_t, Vector2, Vector2>> move_list;
Expand Down Expand Up @@ -192,7 +200,7 @@ namespace paxs {
}
// 前901年から稲作文化開始
if (step_count > SimulationConstants::getInstance()->steps_per_year * 200) {
randomizeSettlements(1, 255, 0);
randomizeSettlements(1, 255, 0, 255/*渡来人は SNP:255*/);
}

m_start_time = std::chrono::system_clock::now(); // 婚姻計測開始
Expand Down Expand Up @@ -320,6 +328,7 @@ namespace paxs {

std::ofstream pop_ofs = std::ofstream("pop.txt");
std::ofstream pop_mtdna_ofs = std::ofstream("pop_mtdna.txt");
std::ofstream pop_snp_ofs = std::ofstream("pop_snp.txt");

/// @brief ()
/// @brief 集落をランダムに配置する前の初期化処理
Expand Down Expand Up @@ -372,7 +381,8 @@ namespace paxs {
void randomizeSettlements(
std::size_t ad200,
std::uint_least8_t farming,
std::uint_least8_t hunter_gatherer
std::uint_least8_t hunter_gatherer,
std::uint_least8_t snp_
) noexcept {

// 地区と人口のマップ
Expand Down Expand Up @@ -439,7 +449,7 @@ namespace paxs {

settlement.resizeAgents(settlement_population);
for (int i = 0; i < settlement_population; ++i) {
Genome genome = Genome::generateRandomSetMtDNA(gen, japan_provinces->getMtDNA((farming>0)? 73/*toraijin*/ : district_id, gen));
Genome genome = Genome::generateRandomSetMtDNA(gen, japan_provinces->getMtDNA((farming>0)? 73/*toraijin*/ : district_id, gen), snp_);
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 Down Expand Up @@ -492,7 +502,7 @@ namespace paxs {
for (auto& settlement : settlements) {
std::vector<Agent> agents(add_population);
for (int i = 0; i < add_population; ++i) {
Genome genome = Genome::generateRandomSetMtDNA(gen, japan_provinces->getMtDNA((farming > 0) ? 73/*toraijin*/ : district_id, gen));
Genome genome = Genome::generateRandomSetMtDNA(gen, japan_provinces->getMtDNA((farming > 0) ? 73/*toraijin*/ : district_id, gen), snp_);
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 Down

0 comments on commit 37e62c2

Please sign in to comment.