GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
gait_pattern_generator_thread.cpp
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
9
10#include <boost/thread.hpp>
11#include <format>
12#include <string>
13#include <utility>
14
15#include "cassert_define.h"
16#include "cmdio_util.h"
17#include "map_state.h"
18#include "string_util.h"
19
20namespace designlab {
21
23 std::unique_ptr<GraphTreeCreator>&& graph_tree_creator_ptr,
24 std::unique_ptr<IGraphSearcher>&& graph_searcher_ptr, const int max_depth,
25 const int max_node_num)
26 : graph_tree_creator_ptr_(std::move(graph_tree_creator_ptr)),
27 graph_searcher_ptr_(std::move(graph_searcher_ptr)),
28 graph_tree_{1000},
29 graph_tree_array_(InitializeGraphTreeArray(kThreadNum, max_node_num)),
30 max_depth_(max_depth) {
31 assert(graph_tree_creator_ptr_ != nullptr);
32 assert(graph_searcher_ptr_ != nullptr);
33 assert(max_depth_ > 0);
34 assert(max_node_num > 0);
35 assert(graph_tree_array_.size() == kThreadNum);
36}
37
39 const RobotStateNode& current_node, const MapState& map_state,
40 const RobotOperation& operation, RobotStateNode* output_node) {
41 assert(current_node.IsLootNode());
42 assert(output_node != nullptr);
43
44 // 初期化処理を行う.
45 DividedMapState divided_map;
46 divided_map.Init(map_state, current_node.center_of_mass_global_coord);
47
48 graph_tree_creator_ptr_->Init(divided_map);
49
50 // グラフ探索をするための,歩容パターングラフを生成する.
51 graph_tree_.Reset();
52 graph_tree_.AddNode(current_node);
53
54 const GraphSearchResult create_result =
55 graph_tree_creator_ptr_->CreateGraphTree(0, 1, &graph_tree_);
56
57 if (create_result.result != enums::Result::kSuccess) {
58 return create_result;
59 }
60
61 cmdio::DebugOutput("Graph tree generation has been completed to depth 1.");
63 "The number of nodes in the graph tree is {}.",
64 graph_tree_.GetGraphSize());
65
66 // 深さ0のノードを配列にコピーする.
67 for (int i = 0; i < kThreadNum; i++) {
68 graph_tree_array_[i].Reset();
69 graph_tree_array_[i].AddNode(current_node);
70 }
71
72 // 深さ1のノードを配列に分けてコピーする.
73 for (int i = 1; i < graph_tree_.GetGraphSize(); i++) {
74 if (graph_tree_.GetNode(i).depth == 1) {
75 // i を kThreadNum で割った余り番目の配列にコピーする.
76 graph_tree_array_[i % kThreadNum].AddNode(graph_tree_.GetNode(i));
77 }
78 }
79
80 // スレッドを分けて,最大深さまで探索する.
81 boost::thread_group thread_group;
82
83 for (size_t i = 0; i < kThreadNum; i++) {
84 if (graph_tree_array_[i].GetGraphSize() > 1) {
86 "Starts graph tree generation in thread {}.", i);
88 "The number of nodes explored in thread {} is {}.", i,
89 graph_tree_array_[i].GetGraphSize());
90
91 thread_group.create_thread(boost::bind(
92 &GraphTreeCreator::CreateGraphTree, graph_tree_creator_ptr_.get(), 1,
93 max_depth_, &graph_tree_array_[i]));
94 }
95 }
96
97 thread_group.join_all(); // 全てのスレッドが終了するまで待機する.
98
99 cmdio::DebugOutput("Graph tree generation is complete.\n");
100
101 for (size_t i = 0; i < kThreadNum; i++) {
103 "The number of nodes created in thread {} is {}.", i,
104 graph_tree_array_[i].GetGraphSize());
105 }
106
107 // グラフ探索を行う.
108 cmdio::DebugOutput("Evaluates graph trees.");
109
110 const auto [search_result, _, next_node] =
111 graph_searcher_ptr_->SearchGraphTreeVector(graph_tree_array_, operation,
112 divided_map, max_depth_);
113
114 if (search_result.result != enums::Result::kSuccess) {
115 cmdio::DebugOutput("Failed to evaluate the graph tree.");
116 return search_result;
117 }
118
119 (*output_node) = next_node;
120
122 "Graph tree evaluation is completed. Graph search succeeded.");
123
124 return {enums::Result::kSuccess, std::string("")};
125}
126
127std::vector<GaitPatternGraphTree>
128GaitPatternGeneratorThread::InitializeGraphTreeArray(
129 const int thread_num, const int max_node_num) const {
130 std::vector<GaitPatternGraphTree> graph_tree_array;
131
132 for (int i = 0; i < thread_num; i++) {
133 graph_tree_array.emplace_back(max_node_num / thread_num);
134 }
135
136 return graph_tree_array;
137}
138
139} // namespace designlab
マップを格子状に分割して管理するクラス.
void Init(const MapState &map_state, const Vector3 global_robot_com)
マップのデータを初期化する. ロボットの重心座標を中心にマップのデータを格子状に分割し, その中に存在する脚設置可能点を集める.
GaitPatternGeneratorThread()=delete
デフォルトコンストラクタは禁止.
GraphSearchResult GetNextNodeByGraphSearch(const RobotStateNode &current_node, const MapState &map_ref, const RobotOperation &operation, RobotStateNode *output_node) override
グラフ探索を行い,次の動作として最適なノードを返す.
constexpr void Reset()
グラフをリセットする.
void AddNode(const RobotStateNode &node)
ノードを追加する. 追加するノードは親ノードのインデックスと,depthの指定が適切にされている必要がある. これらが適切にされていない場合,アサーションに引っかかる....
const RobotStateNode & GetNode(const int index) const
グラフのノードの参照を返す.
constexpr int GetGraphSize() const
グラフのノードの総数を返す.
GraphSearchResult CreateGraphTree(int start_depth, int max_depth, GaitPatternGraphTree *graph_ptr) const
歩容パターングラフを作成する.
マップを表すクラス.
Definition map_state.h:29
void OutputF(OutputDetail detail, const std::format_string< Args... > str, Args &&... args)
コマンドラインに文字を出力する関数, format した文字列を出力する. SetOutputLimit() で設定した出力の許可範囲内であれば出力される.
Definition cmdio_util.h:91
void DebugOutput(const std::string &str)
コマンドラインに文字を出力する関数. Debug 用の出力.
Definition cmdio_util.h:51
@ kDebug
デバッグ時のみ出力,一番優先度が低い.
Definition com_type.h:21
グラフ探索の結果を表す構造体.
enums::Result result
成功か失敗か.
探索において目標となる座標や角度,評価する値についてまとめた構造体.
グラフ構造のためのノード(頂点).
constexpr bool IsLootNode() const
自身が根ノードであるか判定する.
Vector3 center_of_mass_global_coord
[4 * 3 = 12byte] グローバル座標系における重心の位置.旧名 GCOM
int depth
[4 byte] 自身の深さ.一番上の親が深さ0となる.