Skip to content

Commit

Permalink
Merge pull request #36 from AsPJT/feature/guinpen98/ref-simulation
Browse files Browse the repository at this point in the history
ref: Simulationのデータ構造見直しおよびテスト
  • Loading branch information
guinpen98 authored Jul 12, 2023
2 parents 447bf2d + 84d42a6 commit 51f372a
Show file tree
Hide file tree
Showing 19 changed files with 980 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/cmake-unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ jobs:
uses: actions/checkout@v3

- name: Checkout submodules
run: git submodule update --init --recursive ExternalLibrary/googletest
run: |
git submodule update --init --recursive ExternalLibrary/googletest
- name: Install dependencies
run: |
Expand Down
1 change: 1 addition & 0 deletions Data/Map/XYZTile/IPCC/Image/IPCC
Submodule IPCC added at b8a13d
1 change: 1 addition & 0 deletions Data/Map/XYZTile/Slope/Data/SlopeData20230514
Submodule SlopeData20230514 added at 787ca5
4 changes: 4 additions & 0 deletions Library/PAX_SAPIENTICA/Simulation/Agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ namespace paxs {
/// @brief エージェントが死んでいるかどうかを返す
constexpr bool isDead() const noexcept { return age >= life_span; }

/// @brief Get the agent's age.
/// @brief エージェントの年齢を取得する
constexpr std::uint_least32_t getAge() const noexcept { return age; }

/// @brief Update the agent's age.
/// @brief エージェントの年齢を更新する
constexpr void updateAge() noexcept {
Expand Down
32 changes: 21 additions & 11 deletions Library/PAX_SAPIENTICA/Simulation/Data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <regex>
#include <stdexcept>
#include <unordered_map>

#include <PAX_SAPIENTICA/File.hpp>
#include <PAX_SAPIENTICA/Logger.hpp>
Expand Down Expand Up @@ -49,6 +49,7 @@ namespace paxs {
using Vector2 = paxs::Vector2<int>;
explicit Data(const std::string& file_path, const std::string name, const Vector2& start_position, const Vector2& end_position, const int default_z, const int pj_z, const DataType data_type) : name(name), start_position(start_position), end_position(end_position), default_z(default_z), pj_z(pj_z), data_type(data_type) {
z_mag = std::pow(2, default_z - pj_z);
column_size = (end_position.x - start_position.x + 1) * pixel_size * z_mag;

try {
load(file_path);
Expand All @@ -68,7 +69,8 @@ namespace paxs {
default_z(other.default_z),
pj_z(other.pj_z),
z_mag(other.z_mag),
data_type(other.data_type) {}
data_type(other.data_type),
column_size(other.column_size) {}

// Copy assignment operator
constexpr Data& operator=(const Data& other) noexcept {
Expand All @@ -79,6 +81,9 @@ namespace paxs {
data = other.data;
default_z = other.default_z;
pj_z = other.pj_z;
z_mag = other.z_mag;
data_type = other.data_type;
column_size = other.column_size;
}
return *this;
}
Expand All @@ -87,7 +92,7 @@ namespace paxs {
/// @brief 指定した位置の値を取得する
constexpr T getValue(const Vector2& position) const noexcept {
const Vector2 converted_position = position * z_mag;
auto itr = data.find(converted_position);
auto itr = data.find(convertVector2ToIndex(converted_position));
if(itr == data.end()) {
return static_cast<T>(0);
}
Expand All @@ -97,11 +102,12 @@ namespace paxs {
Vector2 start_position; // シミュレーションの左上の座標
Vector2 end_position; // シミュレーションの右下の座標
std::string name; // データの名前
std::map<Vector2, T> data; // データ
std::unordered_map<std::uint_least64_t, T> data; // データ
int default_z; // データのz値
int pj_z; // シミュレーションのz値
double z_mag; // シミュレーションのz値からデータのz値に変換するときの倍率
DataType data_type; // データの型
int column_size; // シミュレーションの列数

/// @brief Load the file.
/// @brief ファイルのロード
Expand Down Expand Up @@ -156,8 +162,8 @@ namespace paxs {
continue;
}

const Vector2 default_position = (xyz_position * pixel_size - start_position * (pixel_size * z_mag));
if(default_position.x < 0 || default_position.y < 0 || default_position.x > (end_position.x - start_position.x) * pixel_size || default_position.y > (end_position.y - start_position.y) * pixel_size) {
const Vector2 default_position = xyz_position * pixel_size - start_position * z_mag * pixel_size;
if(default_position.x < 0 || default_position.y < 0 || default_position.x > (end_position.x - start_position.x) * pixel_size * z_mag || default_position.y > (end_position.y - start_position.y) * pixel_size * z_mag) {
++file_count;
continue;
}
Expand All @@ -183,9 +189,9 @@ namespace paxs {
// T型に変換
try {
if(data_type == DataType::u8 || data_type == DataType::u32)
data[position] = static_cast<T>(std::stoi(values[x]));
data[convertVector2ToIndex(position)] = static_cast<T>(std::stoi(values[x]));
else if(data_type == DataType::f32)
data[position] = static_cast<T>(std::stod(values[x]));
data[convertVector2ToIndex(position)] = static_cast<T>(std::stod(values[x]));
} catch (const std::invalid_argument&/*ia*/) {
// str is not convertible to double
continue;
Expand Down Expand Up @@ -222,8 +228,8 @@ namespace paxs {
continue;
}

const Vector2 default_position = (xyz_position * pixel_size - start_position * (pixel_size * z_mag));
if(default_position.x < 0 || default_position.y < 0 || default_position.x > (end_position.x - start_position.x) * pixel_size || default_position.y > (end_position.y - start_position.y) * pixel_size) {
const Vector2 default_position = xyz_position * pixel_size - start_position * z_mag * pixel_size;
if(default_position.x < 0 || default_position.y < 0 || default_position.x > (end_position.x - start_position.x) * pixel_size * z_mag || default_position.y > (end_position.y - start_position.y) * pixel_size * z_mag) {
++file_count;
continue;
}
Expand All @@ -242,7 +248,7 @@ namespace paxs {
for(std::size_t x = 0;x < file[y].size();++x) {
const Vector2 position = default_position + Vector2((int)x, (int)y);
// T型に変換
data[position] = static_cast<T>(file[y][x]);
data[convertVector2ToIndex(position)] = static_cast<T>(file[y][x]);
}
}
++file_count;
Expand All @@ -269,6 +275,10 @@ namespace paxs {
throw std::runtime_error(message);
}
}

std::uint_least64_t convertVector2ToIndex(const Vector2& position) const noexcept {
return position.x + (std::uint_least64_t)position.y * column_size;
}
};
}

Expand Down
45 changes: 43 additions & 2 deletions Library/PAX_SAPIENTICA/Simulation/Environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,50 @@ namespace paxs {
return std::get<Data<U>>(data_map.at(key)).getValue(position);
}

/// @brief Get data.
/// @brief Is it possible to live?
/// @brief 居住可能かどうかの判定
/// @details It is land and the slope is less than a certain value.
/// @details 陸地であるかつ、傾斜が一定以下であること
bool isLive(const Vector2& position) const {
try {
return isLand(position) && getSlope(position) <= 5;
} catch (const std::exception&) {
Logger logger("Save/error_log.txt");
const std::string message = "Failed to judge live";
logger.log(Logger::Level::ERROR, __FILE__, __LINE__, message);
throw std::runtime_error(message);
}
}

/// @brief Get slope.
/// @brief 傾斜の取得
float getSlope(const Vector2& position) const {
try {
return getData<float>("slope", position);
} catch (const std::exception&) {
Logger logger("Save/error_log.txt");
const std::string message = "Failed to get slope";
logger.log(Logger::Level::ERROR, __FILE__, __LINE__, message);
throw std::runtime_error(message);
}
}

/// @brief Get elevation.
/// @brief 標高の取得
float getElevation(const Vector2& position) const {
try {
return getData<float>("elevation", position);
} catch (const std::exception&) {
Logger logger("Save/error_log.txt");
const std::string message = "Failed to get elevation";
logger.log(Logger::Level::ERROR, __FILE__, __LINE__, message);
throw std::runtime_error(message);
}
}

/// @brief Is it land?
/// @brief 陸地かどうかの判定
bool isLand(const Vector2& position) const {
virtual bool isLand(const Vector2& position) const {
try {
auto value = getData<std::uint_least8_t>("gbank", position);
return static_cast<int>(value) == static_cast<int>('1');
Expand Down
4 changes: 2 additions & 2 deletions Library/PAX_SAPIENTICA/Simulation/Simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace paxs {
/// @brief Initialize the simulator.
/// @brief エージェントの初期化
/// @details エージェントをクリアし、指定された数だけランダムに配置する
// TODO: とりあえず陸地にランダムに設置しているため、改修必須
// TODO: とりあえず傾斜の緩やかな陸地にランダムに設置しているため、改修必須
void init() {
std::cout << "Initializing..." << std::endl;
clearAgents();
Expand Down Expand Up @@ -127,7 +127,7 @@ namespace paxs {
StatusDisplayer::displayProgressBar(i, agent_count);
Vector2 position = Vector2(x_dist(gen), y_dist(gen));
try {
while(!environment.isLand(position)) {
while(!environment.isLive(position)) {
position = Vector2(x_dist(gen), y_dist(gen));
}
} catch (const std::runtime_error&) {
Expand Down
2 changes: 1 addition & 1 deletion Library/PAX_SAPIENTICA/Type/Vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace paxs {
return *this;
}
constexpr Vector2<T> operator*(const double& t) const noexcept {
return Vector2<T>(x * T(t), y * T(t));
return Vector2<T>(x * t, y * t);
}
constexpr Vector2<T>& operator*=(const double& t) noexcept {
x *= t;
Expand Down
1 change: 1 addition & 0 deletions Project/IncludeTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ foreach(TEST_SOURCE ${TEST_SOURCES})

if(NOT EXISTS ${LIBRARY_PATH})
file(REMOVE ${TEST_SOURCE})
continue()
endif()

string(REPLACE "${TEST_SOURCE_DIR}/" "" TARGET_NAME ${TEST_SOURCE})
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 51f372a

Please sign in to comment.