|
| 1 | +/*############ A*(A-star) Algorithm ############## |
| 2 | + |
| 3 | + By @VictorKariuki |
| 4 | +
|
| 5 | + https://github.com/VictorKariuki |
| 6 | +
|
| 7 | +################################################*/ |
| 8 | + |
| 9 | + |
| 10 | +// create a list of numbers |
| 11 | +fanya list = unda(first,last,interval){ |
| 12 | + fanya list = [first]; |
| 13 | + fanya i = first + interval; |
| 14 | + wakati(i < last){ |
| 15 | + list.sukuma(i) |
| 16 | + i+=interval; |
| 17 | + } |
| 18 | + rudisha list; |
| 19 | +} |
| 20 | + |
| 21 | +// Maths functions |
| 22 | +// find the absolute value of a number |
| 23 | +fanya abs_namba = unda(namba){ |
| 24 | + kama(namba < 0){ |
| 25 | + rudisha -1 * namba; |
| 26 | + } |
| 27 | + |
| 28 | + rudisha namba; |
| 29 | +} |
| 30 | + |
| 31 | +// square a number |
| 32 | +fanya square = unda(n, i, j){ |
| 33 | + fanya kati = (i+j)/2; |
| 34 | + fanya mul = kati * kati; |
| 35 | + fanya abs_diff = abs_namba(mul-n); |
| 36 | + |
| 37 | + kama (mul == n || abs_diff < 0.00001){ |
| 38 | + rudisha kati; |
| 39 | + }au kama(mul < n){ |
| 40 | + rudisha square(n,kati,j) |
| 41 | + }au{ |
| 42 | + rudisha square(n,i,kati) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// find the square root of a number |
| 47 | +fanya sqrt = unda(namba){ |
| 48 | + kwa i ktk list(0,namba,1) { |
| 49 | + kama((i*i )== namba){ |
| 50 | + rudisha i; |
| 51 | + }au kama ((i*i )> namba){ |
| 52 | + rudisha square(namba,i-1,i) |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Main function |
| 58 | +fanya aStar = unda(start, goal) { |
| 59 | + // Initialize the open and closed lists |
| 60 | + fanya openList = [start]; |
| 61 | + fanya closedList = []; |
| 62 | + |
| 63 | + fanya reconstructPath = unda(node) { |
| 64 | + fanya path = [node]; |
| 65 | + wakati (node["parent"]) { |
| 66 | + path = [node["parent"]] + path; |
| 67 | + node = node["parent"]; |
| 68 | + } |
| 69 | + rudisha path; |
| 70 | + } |
| 71 | + |
| 72 | + fanya heuristic = unda(node1, node2) { |
| 73 | + // Calculate the Euclidean distance between the nodes' positions |
| 74 | + fanya dx = node1["x"] - node2["x"]; |
| 75 | + fanya dy = node1["y"] - node2["y"]; |
| 76 | + rudisha sqrt(dx * dx + dy * dy); |
| 77 | + } |
| 78 | + |
| 79 | + fanya findMinNode = unda(openList) { |
| 80 | + fanya i = 1; |
| 81 | + fanya minNode = openList[0]; |
| 82 | + |
| 83 | + wakati (i < openList.idadi()) { |
| 84 | + fanya node = openList[i]; |
| 85 | + kama (node["f"] < minNode["f"]) { |
| 86 | + minNode = node; |
| 87 | + } |
| 88 | + i++ |
| 89 | + } |
| 90 | + |
| 91 | + rudisha minNode; |
| 92 | + } |
| 93 | + |
| 94 | + fanya removeNodeFromArray = unda(array, node) { |
| 95 | + fanya newArray = []; |
| 96 | + fanya i = 1; |
| 97 | + wakati (i < array.idadi()) { |
| 98 | + kama (array[i] != node) { |
| 99 | + newArray.sukuma(array[i]); |
| 100 | + } |
| 101 | + i++; |
| 102 | + } |
| 103 | + rudisha newArray; |
| 104 | + } |
| 105 | + |
| 106 | + fanya urefu = unda(node1, node2) { |
| 107 | + // Assume all edges have a cost of 1 |
| 108 | + rudisha 1; |
| 109 | + } |
| 110 | + |
| 111 | + // Initialize the g and f scores of the starting node |
| 112 | + start["g"] = 0; |
| 113 | + start["f"] = start["g"] + heuristic(start, goal); |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + // Start the search loop |
| 118 | + wakati (openList.idadi() > 0) { |
| 119 | + // Find the node with the lowest f score in the open list |
| 120 | + fanya current = findMinNode(openList); |
| 121 | + |
| 122 | + // Check kama the goal node has been reached |
| 123 | + kama (current == goal) { |
| 124 | + rudisha reconstructPath(current); |
| 125 | + } |
| 126 | + |
| 127 | + // Move the current node from the open to the closed list |
| 128 | + openList = removeNodeFromArray(openList, current); |
| 129 | + |
| 130 | + closedList.sukuma(current); |
| 131 | + |
| 132 | + // Explore the neighbors of the current node |
| 133 | + kwa neighbor ktk current["neighbors"] { |
| 134 | + // Skip neighbors that are in the closed list |
| 135 | + kama (neighbor ktk closedList) { |
| 136 | + endelea |
| 137 | + } |
| 138 | + |
| 139 | + // Calculate the tentative g score of the neighbor |
| 140 | + fanya tentativeG = start["g"] + urefu(current, neighbor); |
| 141 | + |
| 142 | + // Check kama the neighbor is in the open list |
| 143 | + fanya tentativeIsBetter = sikweli; |
| 144 | + kama (!(neighbor ktk openList)) { |
| 145 | + openList.sukuma(neighbor); |
| 146 | + tentativeIsBetter = kweli; |
| 147 | + } au kama (tentativeG < neighbor["g"]) { |
| 148 | + tentativeIsBetter = kweli; |
| 149 | + } |
| 150 | + |
| 151 | + // Update the neighbor's g score kama the tentative score is better |
| 152 | + kama (tentativeIsBetter) { |
| 153 | + neighbor["g"] = tentativeG; |
| 154 | + neighbor["f"] = neighbor["g"] + heuristic(neighbor, goal); |
| 155 | + neighbor["parent"] = current; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // kama the open list is empty, no path was found |
| 161 | + rudisha tupu; |
| 162 | +} |
| 163 | + |
| 164 | +// Define the nodes of the graph |
| 165 | +fanya nodeA = { "x": 0, "y": 0, "neighbors": [] }; |
| 166 | +fanya nodeB = { "x": 1, "y": 2, "neighbors": [] }; |
| 167 | +fanya nodeC = { "x": 3, "y": 1, "neighbors": [] }; |
| 168 | +fanya nodeD = { "x": 4, "y": 3, "neighbors": [] }; |
| 169 | + |
| 170 | +// Define the edges between the nodes |
| 171 | +nodeA["neighbors"] = [nodeB]; |
| 172 | +nodeB["neighbors"] = [nodeA, nodeC]; |
| 173 | +nodeC["neighbors"] = [nodeB, nodeD]; |
| 174 | +nodeD["neighbors"] = [nodeC]; |
| 175 | + |
| 176 | +// Call the A* function with the start and goal nodes and the heuristic and distance functions |
| 177 | +//fanya path = aStar(nodeA, nodeC); |
| 178 | + |
| 179 | +andika(nodeA); |
0 commit comments