Skip to content

Commit

Permalink
Merge pull request #100 from AsPJT/feature_simulation_mini_data_type
Browse files Browse the repository at this point in the history
シミュレーションの最適化(型の小型化)
  • Loading branch information
AsPJT authored Jun 15, 2024
2 parents d8a798a + 4995d02 commit cbf31d6
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 143 deletions.
4 changes: 2 additions & 2 deletions Library/PAX_MAHOROBA/LocationPoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ namespace paxs {
public:

void draw(const double jdn,
std::unordered_map<std::uint_least64_t, paxs::SettlementGrid>& agents,
std::unordered_map<SettlementGridsType, paxs::SettlementGrid>& agents,
const double map_view_width, const double map_view_height, const double map_view_center_x, const double map_view_center_y,

std::size_t& pop_num, // 人口数
Expand Down Expand Up @@ -680,7 +680,7 @@ namespace paxs {
// 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.getMostMtDNA() / 27.0f * 75.0f; // settlement.getPopulation();
const float pop_original = settlement.getSNP() * 75.0f; // settlement.getPopulation();
const double pop_original = settlement.getSNP() * 75.0; // 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
17 changes: 4 additions & 13 deletions Library/PAX_SAPIENTICA/Simulation/Chromosome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ namespace paxs {
return os;
}

constexpr std::uint_least8_t getGender() const noexcept {
return gender;
}

void setGender(const std::uint_least8_t value) noexcept {
gender = value;
}

bool operator==(const Chromosome& rhs) const noexcept {
for (std::uint_least8_t i = 0; i < chromosome_length; ++i) {
if (chromosome[i] != rhs.chromosome[i]) {
Expand All @@ -83,9 +75,9 @@ namespace paxs {
else {
child.set(i, father.get(i - 1 + (random_value % 2)));
}
if (i == chromosome_length - 1) {
child.setGender(random_value % 2);
}
//if (i == chromosome_length - 1) {
// child.setGender(random_value % 2);
//}
random_value >>= 1;
}
return child;
Expand All @@ -98,13 +90,12 @@ namespace paxs {
for (std::uint_least8_t i = 0; i < chromosome_length; ++i) {
random_chromosome.set(i, static_cast<std::uint_least8_t>(dist(engine)));
}
random_chromosome.setGender(dist(engine) % 2);
//random_chromosome.setGender(dist(engine) % 2);
return random_chromosome;
}

private:
std::array<std::uint_least8_t, chromosome_length> chromosome;
std::uint_least8_t gender;
};
}

Expand Down
22 changes: 11 additions & 11 deletions Library/PAX_SAPIENTICA/Simulation/Data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace paxs {
/// @brief 指定した位置の値を取得する
constexpr DataType getValue(const Vector2& position) const noexcept {
const Vector2 converted_position = position * z_mag;
auto itr = data.find(converted_position.toU64());
auto itr = data.find(converted_position.to(DataGridsType{}));
if(itr == data.end()) {
return static_cast<DataType>(0);
}
Expand All @@ -82,7 +82,7 @@ namespace paxs {
/// @brief Get the data of the key.
/// @brief キーをvectorで取得
/// @return keyのvector
constexpr void getKeys(std::vector<std::uint64_t>& keys) const noexcept {
constexpr void getKeys(std::vector<DataGridsType>& keys) const noexcept {
keys.reserve(data.size());
for (const auto& [key, value] : data) {
keys.emplace_back(key);
Expand All @@ -91,7 +91,7 @@ namespace paxs {

private:
std::string name; // データの名前
std::unordered_map<std::uint_least64_t, DataType> data; // データ
std::unordered_map<DataGridsType, DataType> data; // データ
int default_z; // データのz値
double z_mag; // シミュレーションのz値からデータのz値に変換するときの倍率
int column_size; // シミュレーションの列数
Expand Down Expand Up @@ -156,7 +156,7 @@ namespace paxs {
for(std::size_t py = 0;py < pixel_size;++py) {
for(std::size_t px = 0;px < pixel_size;++px) {
const Vector2 position = default_position + Vector2((GridType)px, (GridType)py);
data[position.toU64()] = static_cast<DataType>(tmp_data[py * pixel_size + px]);
data[position.to(DataGridsType{})] = static_cast<DataType>(tmp_data[py * pixel_size + px]);
}
}

Expand Down Expand Up @@ -207,7 +207,7 @@ namespace paxs {
for(std::size_t y = 0;y < pixel_size;++y) {
for(std::size_t x = 0;x < pixel_size;++x) {
const Vector2 position = default_position + Vector2((GridType)x, (GridType)y);
data[position.toU64()] = static_cast<DataType>(tmp_data[y * pixel_size + x]);
data[position.to(DataGridsType{})] = static_cast<DataType>(tmp_data[y * pixel_size + x]);
}
}

Expand Down Expand Up @@ -262,9 +262,9 @@ namespace paxs {
if constexpr (std::is_same<DataType, std::uint_least8_t>::value || std::is_same<DataType, std::uint_least32_t>::value) {
int value = std::stoi(values[px]);
if(value == 0) continue;
data[position.toU64()] = static_cast<DataType>(value);
data[position.to(DataGridsType{})] = static_cast<DataType>(value);
} else if constexpr (std::is_same<DataType, float>::value) {
data[position.toU64()] = static_cast<DataType>(std::stod(values[px]));
data[position.to(DataGridsType{})] = static_cast<DataType>(std::stod(values[px]));
}
} catch (const std::invalid_argument&/*ia*/) {
PAXS_WARNING("File contains invalid value: " + file_name);
Expand Down Expand Up @@ -334,9 +334,9 @@ namespace paxs {
if constexpr (std::is_same<DataType, std::uint_least8_t>::value || std::is_same<DataType, std::uint_least32_t>::value) {
int value = std::stoi(values[x]);
if(value == 0) continue;
data[position.toU64()] = static_cast<DataType>(value);
data[position.to(DataType{})] = static_cast<DataType>(value);
} else if constexpr (std::is_same<DataType, float>::value) {
data[position.toU64()] = static_cast<DataType>(std::stod(values[x]));
data[position.to(DataType{})] = static_cast<DataType>(std::stod(values[x]));
}
} catch (const std::invalid_argument&/*ia*/) {
PAXS_WARNING("File contains invalid value: " + file_name);
Expand Down Expand Up @@ -395,7 +395,7 @@ namespace paxs {
// T型に変換
if (file[y][x] == '0') continue;
const Vector2 position = default_position + Vector2((GridType)x, (GridType)y);
data[position.toU64()] = static_cast<DataType>(file[y][x]);
data[position.to(DataGridsType{})] = static_cast<DataType>(file[y][x]);
}
}
++file_count;
Expand Down Expand Up @@ -456,7 +456,7 @@ namespace paxs {
}
if(!is_contain_one) continue;
const Vector2 position = default_position + Vector2(static_cast<GridType>(x / z_mag), static_cast<GridType>(y / z_mag));
data[position.toU64()] = static_cast<DataType>('1');
data[position.to(DataGridsType{})] = static_cast<DataType>('1');
}
}
++file_count;
Expand Down
2 changes: 1 addition & 1 deletion Library/PAX_SAPIENTICA/Simulation/Environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace paxs {

/// @brief Get the land position list.
/// @brief 陸の位置リストの取得
void getLandPositions(std::vector<std::uint64_t>& keys) const {
void getLandPositions(std::vector<DataGridsType>& keys) const {
std::get<Data<std::uint_least8_t>>(*data_map.at(MurMur3::calcHash("gbank"))).getKeys(keys);
}

Expand Down
53 changes: 43 additions & 10 deletions Library/PAX_SAPIENTICA/Simulation/Genome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,34 @@ namespace paxs {

/// @brief ゲノム
class Genome {
#ifndef USING_CHROMOSOME
using Chromosome = std::uint_least8_t;
#endif // USING_CHROMOSOME
public:
Genome() = default;

const Chromosome& cgetChromosome() const noexcept {
#ifdef USING_CHROMOSOME
return chromosome;
#else
return yDNA;
#endif // USING_CHROMOSOME
}

Chromosome& getChromosome() noexcept {
#ifdef USING_CHROMOSOME
return chromosome;
#else
return yDNA;
#endif // USING_CHROMOSOME
}

void setChromosome(const Chromosome& value) noexcept {
#ifdef USING_CHROMOSOME
chromosome = value;
#else
yDNA = value;
#endif // USING_CHROMOSOME
}

std::uint_least8_t getMtDNA() const noexcept {
Expand All @@ -62,37 +77,47 @@ namespace paxs {
SNP = value;
}

constexpr std::uint_least8_t getGender() const noexcept {
return chromosome.getGender();
constexpr bool isFemale() const noexcept {
return (yDNA == 0);
}

constexpr bool isMale() const noexcept {
return (yDNA != 0);
}

static Genome generateRandom(std::mt19937& engine) noexcept {
Genome genome;
#ifdef USING_CHROMOSOME
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)());
#endif // USING_CHROMOSOME
const bool is_female = ((engine() % 2) == 0);
std::uniform_int_distribution<> dist(1, (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.setYDNA((is_female) ? 0 : 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_, const std::uint_least8_t snp_) noexcept {
Genome genome;
#ifdef USING_CHROMOSOME
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)());
#endif // USING_CHROMOSOME
const bool is_female = ((engine() % 2) == 0);
genome.setSNP(snp_);
genome.setMtDNA(mtdna_);
genome.setYDNA(static_cast<std::uint_least8_t>(dist(engine)));
genome.setYDNA((is_female) ? 0 : 1/*static_cast<std::uint_least8_t>(dist(engine))*/);
return genome;
}

static Genome generateFromParents(std::mt19937& engine, const Genome& mother, const Genome& father) noexcept {
Genome genome;
#ifdef USING_CHROMOSOME
genome.setChromosome(Chromosome::generateFromParents(engine, mother.cgetChromosome(), father.cgetChromosome()));
#endif // USING_CHROMOSOME
const bool is_female = ((engine() % 2) == 0);

genome.setMtDNA(mother.getMtDNA());
if (genome.cgetChromosome().getGender() == female_value) {
if (is_female) {
genome.setYDNA(mother.getYDNA());
}
else {
Expand All @@ -103,11 +128,19 @@ namespace paxs {
}

bool operator==(const Genome& rhs) const noexcept {
return chromosome == rhs.chromosome && mtDNA == rhs.mtDNA && yDNA == rhs.yDNA;
return
#ifdef USING_CHROMOSOME
chromosome == rhs.chromosome &&
#endif // USING_CHROMOSOME
mtDNA == rhs.mtDNA &&
yDNA == rhs.yDNA;
}

private:
#ifdef USING_CHROMOSOME
Chromosome chromosome{};
#endif // USING_CHROMOSOME

std::uint_least8_t SNP = 0;
std::uint_least8_t mtDNA = 0;
std::uint_least8_t yDNA = 0;
Expand Down
10 changes: 5 additions & 5 deletions Library/PAX_SAPIENTICA/Simulation/KanakumaLifeSpan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ namespace paxs {

/// @brief 英語未翻訳
/// @brief 寿命を決定する
AgeType setAdultLifeSpan(const std::uint_least8_t gender_, std::mt19937& gen) {
AgeType setAdultLifeSpan(const bool is_male_, std::mt19937& gen) {
// もし大人だったら
if (gender_ == 0) { // 女性の場合
if (!is_male_) { // 女性の場合
const AgeType adult_type = static_cast<AgeType>(life_male_adult_num(gen));

if (adult_type <= 14) { // 成年
Expand All @@ -61,7 +61,7 @@ namespace paxs {
// 老年
return static_cast<AgeType>(life_older_exp_dist(gen));
}
else if (gender_ == 1) { // 男性の場合
else { // 男性の場合
const AgeType adult_type = static_cast<AgeType>(life_female_adult_num(gen));

if (adult_type <= 19) { // 成年
Expand All @@ -77,7 +77,7 @@ namespace paxs {

/// @brief 英語未翻訳
/// @brief 寿命を決定する
AgeType setLifeSpan(const std::uint_least8_t gender_, std::mt19937& gen) {
AgeType setLifeSpan(const bool is_male_, std::mt19937& gen) {

if (life_person_num(gen) <= 37) { // もし子供だったら
const AgeType child_type = static_cast<AgeType>(life_child_num(gen));
Expand All @@ -95,7 +95,7 @@ namespace paxs {
return static_cast<AgeType>(life_young_exp_dist(gen));
}
// もし大人だったら
return setAdultLifeSpan(gender_, gen);
return setAdultLifeSpan(is_male_, gen);
}

};
Expand Down
Loading

0 comments on commit cbf31d6

Please sign in to comment.