Skip to content

Commit

Permalink
heuristic method
Browse files Browse the repository at this point in the history
  • Loading branch information
linkliu committed Sep 24, 2024
1 parent 91ff5d7 commit 95a8ead
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"*.cpp",
"-lncursesw",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
"${fileDirname}/main"
],
"options": {
"cwd": "${fileDirname}"
Expand Down
26 changes: 13 additions & 13 deletions dijstra_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "dijstra_algorithm.h"
#include "mnode.h"
#include <chrono>
#include <cmath>
#include <functional>
#include <ncurses.h>
#include <queue>
Expand All @@ -9,6 +10,11 @@
using std::priority_queue;
using std::make_pair;

int DIJAlgorithm::Heuristic(const MNode& nodeA , const MNode& nodeB) const
{
return std::abs(nodeA.mapIndex_X - nodeB.mapIndex_X) + std::abs(nodeA.mapIndex_Y - nodeB.mapIndex_Y);
}

map<int, int> DIJAlgorithm::Resolve()
{
Map& aMap = GetMap();
Expand All @@ -24,13 +30,11 @@ map<int, int> DIJAlgorithm::Resolve()
return solveMap;
}
priority_queue<MNode, vector<MNode>, std::greater<MNode>> waveQueue;
map<int, int> curCostMap;
curCostMap.insert(make_pair(aMap.GetNodeNum(startNode), 0));
waveQueue.push(startNode);
int meetIndex = 0;
while(!waveQueue.empty())
{
const MNode checkNode = waveQueue.top();
waveQueue.pop();
if(checkNode == endNode)
{
break;
Expand All @@ -40,27 +44,23 @@ map<int, int> DIJAlgorithm::Resolve()
{
int ckNodeNum = aMap.GetNodeNum(checkNode);
int nextNodeNum = aMap.GetNodeNum(nextNode);
int nextCurCost = curCostMap[ckNodeNum] + nextNode.nodeCost;
map<int, int>::const_iterator citer = curCostMap.find(nextNodeNum);
if(citer == curCostMap.cend() || nextCurCost < citer->second)
map<int, int>::const_iterator citer = solveMap.find(nextNodeNum);
if(citer == solveMap.cend() && nextNode != startNode)
{
curCostMap[nextNodeNum] = nextCurCost;
nextNode.CurCostSetter(nextCurCost);
nextNode.MISetter(meetIndex);
nextNode.CurCostSetter(Heuristic(endNode, nextNode));
if(nextNode!=startNode && nextNode!=endNode)
{
// nextNode.NStateSetter(ENodeState::FINDDING);
// aMap.Draw(nextNode, EDrawType::STATE);
aMap.Draw(nextNode.curCost, nextNode.mapIndex_Y, nextNode.mapIndex_X);
aMap.Draw(nextNode.index, nextNode.mapIndex_Y, nextNode.mapIndex_X);
refresh();
}
std::this_thread::sleep_for(std::chrono::milliseconds(30));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
waveQueue.push(nextNode);
solveMap.insert(make_pair(nextNodeNum, ckNodeNum));
meetIndex += 1;
}
}
waveQueue.pop();
// waveQueue.pop();
}
return solveMap;
}
Expand Down
2 changes: 2 additions & 0 deletions dijstra_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _DIJ_ALGORITHM_H
#include "algorithm.h"
#include "map.h"
#include "mnode.h"
#include <map>
#include <vector>
using std::map;
Expand All @@ -14,5 +15,6 @@ class DIJAlgorithm : public Algorithm
DIJAlgorithm(Map& _map, MNode& _start, MNode& _end):Algorithm(_map, _start, _end){}
map<int, int> Resolve() override;
vector<int> FindPath(map<int, int>&) override;
int Heuristic(const MNode&, const MNode&)const;
};
#endif
1 change: 1 addition & 0 deletions map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ void Map::buildNodes(map<int, MNode>& nMap)
MNode node = MNode(ipair.first, ipair.second);
node.NTypeSetter(ENodeType::NORMAL);
node.NStateSetter(ENodeState::NONE);
node.index = i;
nodeMap.insert({i, node});
}
}
Expand Down
18 changes: 2 additions & 16 deletions mnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,12 @@ struct MNode

bool operator<(const MNode& node) const
{
if(curCost < node.curCost)
{
return true;
}
else
{
return meetIndex < node.meetIndex;
}
return curCost < node.curCost;
}

bool operator>(const MNode& node) const
{
if(curCost > node.curCost)
{
return true;
}
else
{
return meetIndex > node.meetIndex;
}
return curCost > node.curCost;
}

void NTypeSetter(ENodeType ntype)
Expand Down

0 comments on commit 95a8ead

Please sign in to comment.