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 <format>
11#include <string>
12#include <utility>
13
14#include <boost/thread.hpp>
15
16#include "array_util.h"
17#include "cassert_define.h"
18#include "cmdio_util.h"
19#include "graph_search_const.h"
20#include "map_state.h"
21#include "string_util.h"
22
23
24namespace designlab
25{
26
28 std::unique_ptr<GraphTreeCreator>&& graph_tree_creator_ptr,
29 std::unique_ptr<IGraphSearcher>&& graph_searcher_ptr,
30 const int max_depth,
31 const int max_node_num) :
32 graph_tree_creator_ptr_(std::move(graph_tree_creator_ptr)),
33 graph_searcher_ptr_(std::move(graph_searcher_ptr)),
34 graph_tree_{ 1000 },
35 graph_tree_array_(InitializeGraphTreeArray(kThreadNum, max_node_num)),
36 max_depth_(max_depth)
37{
38 assert(graph_tree_creator_ptr_ != nullptr);
39 assert(graph_searcher_ptr_ != nullptr);
40 assert(max_depth_ > 0);
41 assert(max_node_num > 0);
42 assert(graph_tree_array_.size() == kThreadNum);
43}
44
46 const RobotStateNode& current_node,
47 const MapState& map_state,
48 const RobotOperation& operation,
49 RobotStateNode* output_node)
50{
51 assert(current_node.IsLootNode());
52 assert(output_node != nullptr);
53
54 // 初期化処理を行う.
55 DividedMapState divided_map;
56 divided_map.Init(map_state, current_node.center_of_mass_global_coord);
57
58 graph_tree_creator_ptr_->Init(divided_map);
59
60 // グラフ探索をするための,歩容パターングラフを生成する.
61 graph_tree_.Reset();
62 graph_tree_.AddNode(current_node);
63
64 const GraphSearchResult create_result =
65 graph_tree_creator_ptr_->CreateGraphTree(0, 1, &graph_tree_);
66
67 if (create_result.result != enums::Result::kSuccess) { return create_result; }
68
69 CmdIOUtil::DebugOutput("Graph tree generation has been completed to depth 1.");
71 "The number of nodes in the graph tree is {}.",
72 graph_tree_.GetGraphSize());
73
74 // 深さ0のノードを配列にコピーする.
75 for (int i = 0; i < kThreadNum; i++)
76 {
77 graph_tree_array_[i].Reset();
78 graph_tree_array_[i].AddNode(current_node);
79 }
80
81 // 深さ1のノードを配列に分けてコピーする.
82 for (int i = 1; i < graph_tree_.GetGraphSize(); i++)
83 {
84 if (graph_tree_.GetNode(i).depth == 1)
85 {
86 // i を kThreadNum で割った余り番目の配列にコピーする.
87 graph_tree_array_[i % kThreadNum].AddNode(graph_tree_.GetNode(i));
88 }
89 }
90
91 // スレッドを分けて,最大深さまで探索する.
92 boost::thread_group thread_group;
93
94 for (size_t i = 0; i < kThreadNum; i++)
95 {
96 if (graph_tree_array_[i].GetGraphSize() > 1)
97 {
100 "Starts graph tree generation in thread {}.", i);
103 "The number of nodes explored in thread {} is {}.",
104 i, graph_tree_array_[i].GetGraphSize());
105
106 thread_group.create_thread(
107 boost::bind(
109 graph_tree_creator_ptr_.get(),
110 1,
111 max_depth_,
112 &graph_tree_array_[i]));
113 }
114 }
115
116 thread_group.join_all(); // 全てのスレッドが終了するまで待機する.
117
118 CmdIOUtil::DebugOutput("Graph tree generation is complete.\n");
119
120 for (size_t i = 0; i < kThreadNum; i++)
121 {
124 "The number of nodes created in thread {} is {}.",
125 i, graph_tree_array_[i].GetGraphSize());
126 }
127
128
129 // グラフ探索を行う.
130 CmdIOUtil::DebugOutput("Evaluates graph trees.");
131
132 const auto [search_result, _, next_node] =
133 graph_searcher_ptr_->SearchGraphTreeVector(
134 graph_tree_array_, operation, divided_map, max_depth_);
135
136 if (search_result.result != enums::Result::kSuccess)
137 {
138 CmdIOUtil::DebugOutput("Failed to evaluate the graph tree.");
139 return search_result;
140 }
141
142 (*output_node) = next_node;
143
145 "Graph tree evaluation is completed. Graph search succeeded.");
146
147 return { enums::Result::kSuccess, std::string("") };
148}
149
150std::vector<GaitPatternGraphTree>
151GaitPatternGeneratorThread::InitializeGraphTreeArray(
152 const int thread_num, const int max_node_num) const
153{
154 std::vector<GaitPatternGraphTree> graph_tree_array;
155
156 for (int i = 0; i < thread_num; i++)
157 {
158 graph_tree_array.emplace_back(max_node_num / thread_num);
159 }
160
161 return graph_tree_array;
162}
163
164} // namespace designlab
static void FormatOutput(OutputDetail detail, const std::format_string< Args... > str, Args &&... args)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
Definition cmdio_util.h:117
static void DebugOutput(const std::string &str)
コマンドラインに文字を出力する関数.Debug用の出力.
Definition cmdio_util.h:77
マップを格子状に分割して管理するクラス.
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:32
@ kDebug
デバッグ時のみ出力,一番優先度が低い.
Definition com_type.h:24
グラフ探索の結果を表す構造体.
enums::Result result
成功か失敗か.
探索において目標となる座標や角度,評価する値についてまとめた構造体.
グラフ構造のためのノード(頂点).旧名 LNODE
constexpr bool IsLootNode() const
自身が根ノードであるか判定する.
Vector3 center_of_mass_global_coord
[4 * 3 = 12byte] グローバル座標系における重心の位置.旧名 GCOM
int depth
[4 byte] 自身の深さ.一番上の親が深さ0となる.