|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 8, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import heapq\n", |
| 10 | + "romania = {}\n" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 9, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "class PriorityQueue:\n", |
| 20 | + " def __init__(self) -> None:\n", |
| 21 | + " self.cities = []\n", |
| 22 | + "\n", |
| 23 | + " def isEmpty(self):\n", |
| 24 | + " if self.cities == []:\n", |
| 25 | + " return True\n", |
| 26 | + " return False\n", |
| 27 | + "\n", |
| 28 | + " def check(self):\n", |
| 29 | + " print(self.cities)\n", |
| 30 | + "\n", |
| 31 | + " def push(self, city, cost):\n", |
| 32 | + " heapq.heappush(self.cities, (cost, city))\n", |
| 33 | + " \n", |
| 34 | + " def pop(self):\n", |
| 35 | + " return heapq.heappop(self.cities)[1]\n" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 10, |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "class CityNode:\n", |
| 45 | + " def __init__(self, city, distance) -> None:\n", |
| 46 | + " self.city = str(city)\n", |
| 47 | + " self.distance = str(distance)" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": 11, |
| 53 | + "metadata": {}, |
| 54 | + "outputs": [], |
| 55 | + "source": [ |
| 56 | + "def makeDict():\n", |
| 57 | + " file = open(\"Jalan.txt\", 'r')\n", |
| 58 | + " for str in file:\n", |
| 59 | + " delimeter = str.split(',')\n", |
| 60 | + " city1 = delimeter[0]\n", |
| 61 | + " city2 = delimeter[1]\n", |
| 62 | + " dist = delimeter[2]\n", |
| 63 | + " romania.setdefault(city1, []).append(CityNode(city2, dist))\n", |
| 64 | + " romania.setdefault(city2, []).append(CityNode(city1, dist))\n", |
| 65 | + " " |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 12, |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [], |
| 73 | + "source": [ |
| 74 | + "def makeHeuristicDict():\n", |
| 75 | + " h = {}\n", |
| 76 | + " file = open(\"HeuristicJalan.txt\", 'r')\n", |
| 77 | + " for str in file:\n", |
| 78 | + " delimeter = str.strip().split(',')\n", |
| 79 | + " node = delimeter[0].strip()\n", |
| 80 | + " sld = int(delimeter[1].strip()) # Jalan lurus\n", |
| 81 | + " h[node] = sld\n", |
| 82 | + " return h\n", |
| 83 | + "\n", |
| 84 | + "def heuristic(node, values):\n", |
| 85 | + " return values[node]\n" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 13, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "def aStar(start, end):\n", |
| 95 | + " path = {}\n", |
| 96 | + " distance = {}\n", |
| 97 | + " q = PriorityQueue()\n", |
| 98 | + " h = makeHeuristicDict()\n", |
| 99 | + "\n", |
| 100 | + " q.push(start, 0)\n", |
| 101 | + " distance[start] = 0\n", |
| 102 | + " path[start] = None\n", |
| 103 | + " expandList = []\n", |
| 104 | + "\n", |
| 105 | + " while q.isEmpty() == False:\n", |
| 106 | + " curr = q.pop()\n", |
| 107 | + " expandList.append(curr)\n", |
| 108 | + "\n", |
| 109 | + " if curr == end:\n", |
| 110 | + " break\n", |
| 111 | + "\n", |
| 112 | + " for new in romania[curr]:\n", |
| 113 | + " graph_cost = distance[curr] + int(new.distance)\n", |
| 114 | + " # print(new.city, new.distance, \"now : \" + str(distance[curr]), graph_cost)\n", |
| 115 | + "\n", |
| 116 | + " if new.city not in distance or graph_cost < distance[new.city]:\n", |
| 117 | + " distance[new.city] = graph_cost\n", |
| 118 | + " f_cost = graph_cost + heuristic(new.city, h)\n", |
| 119 | + " q.push(new.city, f_cost)\n", |
| 120 | + " path[new.city] = curr\n", |
| 121 | + " \n", |
| 122 | + " printOutput(start, end, path, distance, expandList)\n", |
| 123 | + "\n", |
| 124 | + "def printOutput(start, end, path, distance, expandList):\n", |
| 125 | + " finalpath = []\n", |
| 126 | + " i = end\n", |
| 127 | + "\n", |
| 128 | + " while (path.get(i) != None):\n", |
| 129 | + " finalpath.append(i)\n", |
| 130 | + " i = path[i]\n", |
| 131 | + " finalpath.append(start)\n", |
| 132 | + " finalpath.reverse()\n", |
| 133 | + "\n", |
| 134 | + " print(\"Membuat Program A* (A star) sederhana\")\n", |
| 135 | + " print(\"\\tSurabaya => Tulungagung\")\n", |
| 136 | + " print(\"==============================================================================================================\")\n", |
| 137 | + " print(\"Kota yg mungkin dijelajah \\t\\t: \" + str(expandList))\n", |
| 138 | + " print(\"Jumlah kemungkinan kota \\t\\t: \" + str(len(expandList)))\n", |
| 139 | + " print(\"==============================================================================================================\")\n", |
| 140 | + " print(\"Kota yg dilewati dg jarak terpendek\\t: \" + str(finalpath))\n", |
| 141 | + " print(\"Jumlah kota yang dilewati \\t\\t: \" + str(len(finalpath)))\n", |
| 142 | + " print(\"Total Jarak yang ditempuh \\t\\t: \" + str(distance[end]))\n", |
| 143 | + " print(\"==============================================================================================================\")" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": 14, |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [ |
| 151 | + { |
| 152 | + "name": "stdout", |
| 153 | + "output_type": "stream", |
| 154 | + "text": [ |
| 155 | + "Membuat Program A* (A star) sederhana\n", |
| 156 | + "\tSurabaya => Tulungagung\n", |
| 157 | + "==============================================================================================================\n", |
| 158 | + "Kota yg mungkin dijelajah \t\t: ['Surabaya', 'Gresik', 'Tuban', 'Sidoarjo', 'Malang', 'Krian', 'Kediri', 'Rembang', 'Jombang', 'Ponorogo', 'Klaten', 'Mojokerto', 'Demak', 'Kertosono', 'Demak', 'Madiun', 'Tulungagung']\n", |
| 159 | + "Jumlah kemungkinan kota \t\t: 17\n", |
| 160 | + "==============================================================================================================\n", |
| 161 | + "Kota yg dilewati dg jarak terpendek\t: ['Surabaya', 'Gresik', 'Tuban', 'Mojokerto', 'Kertosono', 'Madiun', 'Tulungagung']\n", |
| 162 | + "Jumlah kota yang dilewati \t\t: 7\n", |
| 163 | + "Total Jarak yang ditempuh \t\t: 520\n", |
| 164 | + "==============================================================================================================\n" |
| 165 | + ] |
| 166 | + } |
| 167 | + ], |
| 168 | + "source": [ |
| 169 | + "def main():\n", |
| 170 | + " src = \"Surabaya\"\n", |
| 171 | + " dst = \"Tulungagung\"\n", |
| 172 | + " makeDict()\n", |
| 173 | + " aStar(src, dst)\n", |
| 174 | + "\n", |
| 175 | + "if __name__ == \"__main__\":\n", |
| 176 | + " main()" |
| 177 | + ] |
| 178 | + } |
| 179 | + ], |
| 180 | + "metadata": { |
| 181 | + "kernelspec": { |
| 182 | + "display_name": "Python 3", |
| 183 | + "language": "python", |
| 184 | + "name": "python3" |
| 185 | + }, |
| 186 | + "language_info": { |
| 187 | + "codemirror_mode": { |
| 188 | + "name": "ipython", |
| 189 | + "version": 3 |
| 190 | + }, |
| 191 | + "file_extension": ".py", |
| 192 | + "mimetype": "text/x-python", |
| 193 | + "name": "python", |
| 194 | + "nbconvert_exporter": "python", |
| 195 | + "pygments_lexer": "ipython3", |
| 196 | + "version": "3.9.6" |
| 197 | + }, |
| 198 | + "orig_nbformat": 4 |
| 199 | + }, |
| 200 | + "nbformat": 4, |
| 201 | + "nbformat_minor": 2 |
| 202 | +} |
0 commit comments