Skip to content

Commit

Permalink
upd: Settlement random
Browse files Browse the repository at this point in the history
  • Loading branch information
AsPJT committed May 31, 2024
1 parent 2c71dcb commit 97a8957
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
9 changes: 4 additions & 5 deletions Library/PAX_SAPIENTICA/RandomSelector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace paxs {
/// @brief ランダムにベクトルから要素を選択
class RandomSelector {
public:
explicit RandomSelector() noexcept : gen(rd()) {}
explicit RandomSelector(std::mt19937* gen_) noexcept : gen(gen_) {}

/// @brief Randomly select elements from a vector.
/// @brief ベクトルから要素をランダムに選択
Expand All @@ -48,7 +48,7 @@ namespace paxs {
std::vector<T> result(vec);
for (std::size_t i = 0; i < num_elements; ++i) {
std::uniform_int_distribution<std::size_t> distribution(i, result.size() - 1);
std::size_t j = distribution(gen);
std::size_t j = distribution(*gen);
std::swap(result[i], result[j]);
}

Expand Down Expand Up @@ -78,14 +78,13 @@ namespace paxs {
}

private:
std::random_device rd;
std::mt19937 gen;
std::mt19937* gen;

std::size_t getRandomIndex(std::size_t num_elements, std::unordered_set<std::size_t>& used_indices) {
std::uniform_int_distribution<std::size_t> distribution(0, num_elements - 1);
std::size_t index;
do {
index = distribution(gen);
index = distribution(*gen);
} while (used_indices.find(index) != used_indices.end());
used_indices.insert(index);
return index;
Expand Down
16 changes: 8 additions & 8 deletions Library/PAX_SAPIENTICA/Simulation/Settlement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ namespace paxs {
}

// 女性と男性の組み合わせをランダムに選択
RandomSelector selector;
RandomSelector selector(gen);

// first: 女性のインデックス, second: 男性のインデックス
const auto marriageable_agents_index_pair = selector.select(marriageable_female_index.size(), male_settlement_pair.size());
Expand Down Expand Up @@ -382,16 +382,16 @@ namespace paxs {
/// @brief 既に移動したかどうか
bool is_moved = false;
/// @brief 集落id
std::uint_least32_t id;
std::uint_least32_t id = 0;
/// @brief 集落の座標
Vector2 position;
Vector2 position{};

std::mt19937* gen; // 乱数生成器
std::mt19937* gen{}; // 乱数生成器
std::uniform_real_distribution<float> random_dist{ 0.0f, 1.0f }; // 乱数分布

std::shared_ptr<Environment> environment; // 環境
std::shared_ptr<Environment> environment{}; // 環境
/// @brief エージェントの配列
std::vector<Agent> agents;
std::vector<Agent> agents{};

/// @brief Birth.
/// @brief 出産
Expand Down Expand Up @@ -507,7 +507,7 @@ namespace paxs {
return std::exp(-std::pow(std::log(x(age)), 2) / settlement::sigma_p_2_x_2) / (x(age) * settlement::sigma_x_sqrt_2_x_pi);
};

const float threshold = weight(age) * (0.98f / 101.8f);
const float threshold = static_cast<float>(weight(age)) * (0.98f / 101.8f);

return random_dist(*gen) < threshold;
}
Expand All @@ -520,7 +520,7 @@ namespace paxs {
return std::exp(-std::pow(std::log(x(age)), 2) / settlement::sigma_p_2_x_2) / (x(age) * settlement::sigma_x_sqrt_2_x_pi);
};

const float threshold = weight(age) * (16.0f / 101.8f);
const float threshold = static_cast<float>(weight(age)) * (16.0f / 101.8f);

return random_dist(*gen) < threshold;
}
Expand Down
12 changes: 9 additions & 3 deletions Projects/UnitTest/Source/RandomSelectorUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include <PAX_SAPIENTICA/RandomSelector.hpp>

TEST(RandomSelectorUnitTest, select1) {
paxs::RandomSelector selector;
std::random_device rd;
std::mt19937 gen(rd());
paxs::RandomSelector selector(&gen);

std::vector<int> vec = { 0, 1, 2, 3, 4, 5 };
std::vector<int> result = selector.select(vec, 3);
Expand All @@ -23,7 +25,9 @@ TEST(RandomSelectorUnitTest, select1) {
}

TEST(RandomSelectorUnitTest, select2) {
paxs::RandomSelector selector;
std::random_device rd;
std::mt19937 gen(rd());
paxs::RandomSelector selector(&gen);

std::vector<int> vec = { 0, 1, 2, 3, 4, 5 };
std::vector<int> result = selector.select(vec, 3);
Expand All @@ -44,7 +48,9 @@ TEST(RandomSelectorUnitTest, select2) {
}

TEST(RandomSelectorUnitTest, outOfRange) {
paxs::RandomSelector selector;
std::random_device rd;
std::mt19937 gen(rd());
paxs::RandomSelector selector(&gen);

std::vector<int> vec = { 0, 1, 2, 3, 4, 5 };
std::vector<int> result = {};
Expand Down

0 comments on commit 97a8957

Please sign in to comment.