3 #ifndef GRAIL_PLANNER_H
4 #define GRAIL_PLANNER_H
6 #include "../../GrailData/DebugInfo/PlannerSnapshots.h"
7 #include "Action/Action.hh"
8 #include "WorldState/WorldState.hh"
25 using ConditionFunction = std::function<bool(
const WorldState&)>;
26 using PlannerHeuristic = std::function<double(
const WorldState&,
const std::vector<ConditionFunction>&)>;
38 PlannerHeuristic heuristic =
nullptr;
39 std::size_t iterationLimit = 500;
40 int maxPlanLength = -1;
41 double maxPlanCost = -1;
42 std::size_t initialNodeCapacity = 128;
47 std::vector<Action> actions{};
48 float totalCost = 0.0f;
54 void SetIterationLimit(std::size_t iterationLimit);
55 void SetMaxPlanLength(
int maxLength);
56 void SetMaxPlanCost(
double maxCost);
57 void SetHeuristic(PlannerHeuristic heuristic);
58 void ResetConfig(
const Config& config);
63 AbstractPlan GetPartialPlan()
const;
65 void Iterate(std::size_t iterationCount) noexcept;
66 void IterateWithSnapshot(std::size_t iterationCount,
68 std::size_t minDebugSnapshotIteration = 0,
69 std::size_t maxDebugSnapshotIteration = std::numeric_limits<std::size_t>::max())
71 void SetPlanFoundCallback(const std::function<
void(const AbstractPlan&)> callbackFunction);
72 void SetPlanningFailedCallback(const std::function<
void()> callbackFunction);
73 void Reset(std::unique_ptr<
WorldState> initialState);
74 bool IsComputing() const;
86 double distanceFromGoal = std::numeric_limits<double>::max();
87 std::shared_ptr<Edge> pathEdge =
nullptr;
89 Node(Node&& other) noexcept;
92 Node& operator=(Node&& other) noexcept;
97 Edge(std::size_t sourceIndex, std::size_t destinationIndex,
Action&& action);
98 Edge(Edge&& otherEdge) =
default;
100 std::size_t sourceIndex = 0;
101 std::size_t destinationIndex = 0;
107 EdgePriority(
const std::shared_ptr<Edge>& edge,
double dijkstraScore,
double aStarScore);
109 std::shared_ptr<Edge> edge;
110 double dijkstraScore;
114 class EdgePriorityComparer
117 bool operator()(
const EdgePriority& one,
const EdgePriority& other)
119 return one.aStarScore > other.aStarScore;
125 void AddNode(Node&& node);
126 bool IsGoalAchieved(
const WorldState& state)
const;
128 std::unique_ptr<WorldState> initialState;
129 Config plannerConfig = {};
131 std::vector<ConditionFunction> goalConditions = {};
132 std::function<void(
const AbstractPlan&)> onPlanFound =
nullptr;
133 std::function<void()> onPlanningFailed =
nullptr;
136 std::size_t nextNodeIndex = 0;
137 std::size_t nodeClosestToGoalIndex = std::numeric_limits<std::size_t>::max();
138 std::size_t lastChosenNodeIndex = std::numeric_limits<std::size_t>::max();
139 int maxNodeDepth = 0;
140 std::priority_queue<EdgePriority, std::vector<EdgePriority>, EdgePriorityComparer> edges{};
141 std::vector<Node> generatedNodes{};
142 std::vector<std::size_t> closedNodeIndices{};
143 std::size_t currentIteration = 0;
144 bool isPlanningFinished =
true;
148 #endif //GRAIL_PLANNER_H