Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions algorithms/astar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class AStar : public Search<State, Cost> {

struct Node;
struct NodeCompare;
using MinHeap = boost::heap::d_ary_heap<Node*, boost::heap::arity<2>, boost::heap::mutable_<true>, boost::heap::compare<NodeCompare>>;
using MinHeap = boost::heap::d_ary_heap<Node *, boost::heap::arity<5>, boost::heap::mutable_<true>, boost::heap::compare<NodeCompare>>;

public:
AStar(const ProblemInstance<State, Cost>* problemInstance, size_t extra_expansion_time) : Search<State, Cost>(problemInstance){
open = MinHeap();
closed = unordered_flat_map<State, Node*, HashFn>(0,
closed = unordered_flat_map<State, Node *, HashFn>(0,
[this](const State& state) {
return this->hash(state);
});
Expand All @@ -39,9 +39,11 @@ class AStar : public Search<State, Cost> {
AStar(const ProblemInstance<State, Cost>* problemInstance) : AStar(problemInstance, 0) {}

vector<State> findPath() override {
this->start();
nodes.reserve(10'000'000); // reserve 10 million nodes
nodes.reserve(1'000'000'000); // reserve 10 million nodes
open.reserve(10'000'000);
closed.reserve(10'000'000);
// Side note, push back is amortized O(1), so we can compare the speed loss of reserve vs push_back at some point
this->start();

nodes.emplace_back(this->problemInstance->initial_state,
0, this->heuristic(this->problemInstance->initial_state), nullptr);
Expand Down
2 changes: 1 addition & 1 deletion algorithms/cafe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CAFE : public Search<State, Cost> {

struct Node;
struct NodeCompare;
using d_ary_heap = boost::heap::d_ary_heap<Node*, boost::heap::arity<2>, boost::heap::mutable_<true>, boost::heap::compare<NodeCompare>>;
using d_ary_heap = boost::heap::d_ary_heap<Node*, boost::heap::arity<5>, boost::heap::mutable_<true>, boost::heap::compare<NodeCompare>>;
using handle_type = typename d_ary_heap::handle_type;

d_ary_heap open{};
Expand Down
2 changes: 1 addition & 1 deletion algorithms/kbfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class KBFS : public Search<State, Cost> {
ctpl::thread_pool threadPool;
struct Node;
struct NodeCompare;
using d_ary_heap = boost::heap::d_ary_heap<Node*, boost::heap::arity<2>, boost::heap::mutable_<true>, boost::heap::compare<NodeCompare>>;
using d_ary_heap = boost::heap::d_ary_heap<Node*, boost::heap::arity<5>, boost::heap::mutable_<true>, boost::heap::compare<NodeCompare>>;
using handle_type = typename d_ary_heap::handle_type;


Expand Down
7 changes: 6 additions & 1 deletion algorithms/search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ using Value = std::variant<int, long int, size_t, double, bool, std::string>;

template<typename State, typename Cost = float>
class Search {
private:
std::vector<State> successors;
public:

Search() {
Expand Down Expand Up @@ -46,7 +48,10 @@ class Search {

const ProblemInstance<State, Cost> *problemInstance;

inline std::vector<State> getSuccessors(const State& state) const { return problemInstance->getSuccessors(state); }
inline std::vector<State>& getSuccessors(const State& state) {
problemInstance->getSuccessors(state, successors);
return successors;
}
inline Cost heuristic(const State& state) const { return problemInstance->heuristic(state); }
inline Cost getCost(const State& state, const State& successor) const { return problemInstance->getCost(state, successor); }
inline size_t hash(const State& state) const { return problemInstance->hash(state); }
Expand Down
2 changes: 1 addition & 1 deletion algorithms/spastar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SPAStar : public Search<State, Cost> {

struct Node;
struct NodeCompare;
using MinHeap = boost::heap::d_ary_heap<Node*, boost::heap::arity<2>, boost::heap::mutable_<true>, boost::heap::compare<NodeCompare>>;
using MinHeap = boost::heap::d_ary_heap<Node*, boost::heap::arity<5>, boost::heap::mutable_<true>, boost::heap::compare<NodeCompare>>;

public:
SPAStar(const ProblemInstance<State, Cost>* problemInstance, size_t extra_expansion_time, size_t threadCount) : Search<State, Cost>(problemInstance){
Expand Down
5 changes: 3 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ int main(int argc, char* argv[]) {
if (algorithmChoice == "astar") {
if (problem == "tiles") {
using namespace SlidingPuzzle;
auto instance = SlidingTileInstance<State>::parseInput(std::cin);
AStar<State> searcher(&instance, extraExpansionTime);
auto instance = SlidingTileInstance<State, int>::parseInput(std::cin);
std::cerr << instance;
AStar<State, int> searcher(&instance, extraExpansionTime);
auto path = searcher.findPath();
// print_path(path);
} else if (problem == "path") {
Expand Down
5 changes: 2 additions & 3 deletions problems/path_finding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,13 @@ namespace Pathfinding {
return minDist;
}

inline vector<State> getSuccessors(const State& state) const override {
vector<State> successors;
inline void getSuccessors(const State& state, vector<State>& successors) const override {
successors.clear();
for (const auto& move : this->getValidMoves(state)) {
State newState = state;
applyMove(newState, move);
successors.push_back(newState);
}
return successors;
}

inline float getCost(const State&, const State&) const override {
Expand Down
2 changes: 1 addition & 1 deletion problems/problem_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ProblemInstance {
* @param state The state to get the successors for
* @return A vector of successor states
*/
virtual std::vector<State> getSuccessors(const State& state) const = 0;
virtual void getSuccessors(const State& state, std::vector<State>& successors) const = 0;

/**
* Get the heuristic value for a given state
Expand Down
Loading