-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pathalgo.ino
113 lines (94 loc) · 3 KB
/
Pathalgo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const int totalNodes = 12;
const int totalPaths = 18;
struct Node {
int id;
int heuristic;
int gScore;
};
struct Edge {
int from;
int to;
int cost;
bool isWaterBlocked;
};
Edge paths[18] = {
{12, 23, 1, false}, {23, 34, 1, false}, {34, 45, 1, false},
{45, 16, 1, false}, {16, 36, 1, false}, {36, 73, 1, false},
{73, 74, 1, false}, {74, 84, 1, false}, {84, 85, 1, false},
{85, 9, 1, false}, {9, 10, 1, false}, {10, 11, 1, false},
{11, 12, 1, false}, {7, 10, 1, false}, {8, 11, 1, false},
{5, 12, 1, false}, {6, 10, 1, false}, {7, 8, 1, false}
};
int calculateHeuristic(int currentNode, int goalNode) {
// Replace this with your own heuristic calculation based on the goal node
return 0;
}
void aStar(int startNode, int goalNode) {
Node openSet[totalNodes];
int openSetSize = 0;
Node closedSet[totalNodes];
int closedSetSize = 0;
openSet[openSetSize++] = {startNode, calculateHeuristic(startNode, goalNode), 0};
while (openSetSize > 0) {
int currentIdx = 0;
for (int i = 1; i < openSetSize; ++i) {
if (openSet[i].gScore + openSet[i].heuristic < openSet[currentIdx].gScore + openSet[currentIdx].heuristic) {
currentIdx = i;
}
}
Node current = openSet[currentIdx];
if (current.id == goalNode) {
// Goal reached, implement your desired actions here
break;
}
// Move current node from open set to closed set
for (int i = currentIdx; i < openSetSize - 1; ++i) {
openSet[i] = openSet[i + 1];
}
openSetSize--;
// Loop through neighbors
for (int i = 0; i < totalPaths; ++i) {
if (paths[i].from == current.id && !paths[i].isWaterBlocked) {
int neighbor = paths[i].to;
int tentativeGScore = current.gScore + paths[i].cost;
// Check if neighbor is in closed set
bool inClosedSet = false;
for (int j = 0; j < closedSetSize; ++j) {
if (closedSet[j].id == neighbor) {
inClosedSet = true;
break;
}
}
// Check if neighbor is in open set or not
int neighborIdx = -1;
for (int j = 0; j < openSetSize; ++j) {
if (openSet[j].id == neighbor) {
neighborIdx = j;
break;
}
}
if (!inClosedSet || (inClosedSet && tentativeGScore < closedSet[neighborIdx].gScore)) {
if (!inClosedSet) {
openSet[openSetSize++] = {neighbor, calculateHeuristic(neighbor, goalNode), 0};
}
openSet[openSetSize - 1].gScore = tentativeGScore;
}
}
}
// Move current node to closed set
closedSet[closedSetSize++] = current;
}
}
void modifyWaterBlockage(int pathIndex, bool isBlocked) {
paths[pathIndex].isWaterBlocked = isBlocked;
}
void setup() {
// Initialize your setup code here
}
void loop() {
// Call your A* star function with appropriate start and goal nodes
aStar(12, 48);
// Modify water blockage if needed
modifyWaterBlockage(6, true); // Example: block the path from 73 to 74
// Add your loop code here
}