-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mdkdoc15/matt_k
Added A* agent
- Loading branch information
Showing
6 changed files
with
225 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* @file AStarAgent.h | ||
* @author Matt Kight | ||
*/ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include "../core/AgentBase.hpp" | ||
#include "AgentLibary.h" | ||
#include <iostream> | ||
namespace cse491 | ||
{ | ||
namespace walle | ||
{ | ||
/** | ||
* Class that describes a AStarAgent class | ||
*/ | ||
class AStarAgent : public cse491::AgentBase | ||
{ | ||
private: | ||
std::vector<GridPosition> path; // Path this agent is taking | ||
cse491::GridPosition goal_position; // Where the agent wants to end up | ||
int recalculate_after_x_turns = 100; // How often agent recalculates moves | ||
int current_move_num = 0; // What move # we are currently on | ||
WorldBase *world = nullptr; | ||
|
||
public: | ||
AStarAgent(size_t id, const std::string &name) : AgentBase(id, name) | ||
{ | ||
} | ||
~AStarAgent() = default; | ||
|
||
/** | ||
* @brief Set the word object | ||
* | ||
* @param world world this agent is a part of | ||
*/ | ||
void SetWorld(WorldBase *world) { this->world = world; } | ||
|
||
/// @brief This agent needs a specific set of actions to function. | ||
/// @return Success. | ||
bool Initialize() override | ||
{ | ||
return HasAction("up") && HasAction("down") && HasAction("left") && HasAction("right"); | ||
} | ||
|
||
void SetGoalPosition(const GridPosition gp) | ||
{ | ||
goal_position = gp; | ||
} | ||
/// Choose the action to take a step in the appropriate direction. | ||
size_t SelectAction(const WorldGrid & /*grid*/, | ||
const type_options_t & /* type_options*/, | ||
const item_set_t & /* item_set*/, | ||
const agent_set_t & /* agent_set*/) override | ||
{ | ||
// TODO REMOVE THIS | ||
// We are taking an action so another turn has passed | ||
++(this->current_move_num); | ||
// If the last step failed, or we need a new path the then regenerate the path | ||
if (action_result == 0 || this->path.empty() || this->current_move_num > this->recalculate_after_x_turns) | ||
{ | ||
this->path = this->world->shortest_path(GetPosition(), this->goal_position); | ||
} | ||
// Return whatever action gets us closer to our goal | ||
if (!this->path.empty()) | ||
{ | ||
auto pos = path.back(); | ||
path.pop_back(); | ||
if (pos.GetX() == position.GetX() && pos.GetY() == position.GetY() - 1) | ||
return action_map["up"]; | ||
if (pos.GetX() == position.GetX() && pos.GetY() == position.GetY() + 1) | ||
return action_map["down"]; | ||
if (pos.GetX() == position.GetX() - 1 && pos.GetY() == position.GetY()) | ||
return action_map["left"]; | ||
if (pos.GetX() == position.GetX() + 1 && pos.GetY() == position.GetY()) | ||
return action_map["right"]; | ||
} | ||
return 0; // If no path then do not do anything | ||
} | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Created by Matthew Kight on 9/24/23. | ||
// | ||
#pragma once | ||
|
||
#include "../core/AgentBase.hpp" | ||
#include "../core/WorldBase.hpp" | ||
#include <vector> | ||
#include <map> | ||
#include <queue> | ||
#include <tuple> | ||
namespace cse491 | ||
{ | ||
namespace walle | ||
{ | ||
|
||
/** | ||
* @brief Node class to make A* search easier | ||
* | ||
*/ | ||
struct Node | ||
{ | ||
cse491::GridPosition position; // Where node is located | ||
int g; // Cost from start to current node | ||
int h; // Heuristic (estimated cost from current node to goal) | ||
std::shared_ptr<Node> parent; | ||
|
||
Node(cse491::GridPosition position, double g, double h, std::shared_ptr<Node> parent) | ||
: position(position), g(g), h(h), parent(parent) {} | ||
|
||
// Calculate the total cost (f) of the node | ||
int f() const | ||
{ | ||
return g + h; | ||
} | ||
}; | ||
|
||
/** | ||
* @brief Custom comparison function for priority queue | ||
* | ||
*/ | ||
struct CompareNodes | ||
{ | ||
bool operator()(const std::shared_ptr<walle::Node> a, const std::shared_ptr<walle::Node> b) const | ||
{ | ||
return a->f() > b->f(); | ||
} | ||
}; | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters