Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59a45a4

Browse files
committed
done
1 parent 536bc90 commit 59a45a4

File tree

2 files changed

+183
-98
lines changed

2 files changed

+183
-98
lines changed

experiment.ipynb

Lines changed: 41 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,32 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 123,
5+
"execution_count": 4,
66
"id": "4b3f41d2-acc8-42eb-ab23-468b3f020416",
77
"metadata": {},
88
"outputs": [
99
{
1010
"name": "stdout",
1111
"output_type": "stream",
1212
"text": [
13-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O')\n",
14-
"4962.250202429876\n",
15-
"STATE ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O')\n",
16-
"NSTATE ('E', 'B', 'F', 'S', 'D', 'L', 'T', 'P', 'H', 'R', 'Z', 'N', 'I', 'G', 'M', 'V', 'A', 'C', 'U', 'O')\n",
17-
"new_state ('E', 'B', 'F', 'S', 'D', 'L', 'T', 'P', 'H', 'R', 'Z', 'N', 'I', 'G', 'M', 'V', 'A', 'C', 'U', 'O')\n",
18-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') E\n",
19-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') E E\n",
20-
"4962.250202429876\n",
21-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') B\n",
22-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') B B\n",
23-
"4962.250202429876\n",
24-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') F\n",
25-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') F F\n",
26-
"4962.250202429876\n",
27-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') S\n",
28-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') S S\n",
29-
"4962.250202429876\n",
30-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') D\n",
31-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') D D\n",
32-
"4962.250202429876\n",
33-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') L\n",
34-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') L L\n",
35-
"4962.250202429876\n",
36-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') T\n",
37-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') T T\n",
38-
"4962.250202429876\n",
39-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') P\n",
40-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') P P\n",
41-
"4962.250202429876\n",
42-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') H\n",
43-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') H H\n",
44-
"4962.250202429876\n",
45-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') R\n",
46-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') R R\n",
47-
"4962.250202429876\n",
48-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') Z\n",
49-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') Z Z\n",
50-
"4962.250202429876\n",
51-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') N\n",
52-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') N N\n",
53-
"4962.250202429876\n",
54-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') I\n",
55-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') I I\n",
56-
"4962.250202429876\n",
57-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') G\n",
58-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') G G\n",
59-
"4962.250202429876\n",
60-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') M\n",
61-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') M M\n",
62-
"4962.250202429876\n",
63-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') V\n",
64-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') V V\n",
65-
"4962.250202429876\n",
66-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') A\n",
67-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') A A\n",
68-
"4962.250202429876\n",
69-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') C\n",
70-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') C C\n",
71-
"4962.250202429876\n",
72-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') U\n",
73-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') U U\n",
74-
"4962.250202429876\n",
75-
"('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') O\n",
76-
"action cost ('E', 'B', 'F', 'S', 'M', 'G', 'I', 'N', 'Z', 'R', 'H', 'P', 'T', 'L', 'D', 'V', 'A', 'C', 'U', 'O') O O\n",
77-
"4962.250202429876\n",
78-
"STATE E\n",
79-
"NSTATE ('E',)\n",
80-
"new_state ('E',)\n",
81-
"E E\n",
82-
"action cost E E E\n"
13+
"optimal route 1715.211603318795\n",
14+
"initial route ('A', 'T', 'L', 'S', 'D', 'C', 'P', 'G', 'B', 'U', 'E', 'H', 'V', 'I', 'N', 'F', 'R', 'M', 'O', 'Z')\n",
15+
"initial route cost 2024.8555347300094\n"
8316
]
8417
},
8518
{
86-
"ename": "TypeError",
87-
"evalue": "unsupported operand type(s) for +: 'float' and 'NoneType'",
88-
"output_type": "error",
89-
"traceback": [
90-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
91-
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
92-
"Cell \u001b[0;32mIn[123], line 76\u001b[0m\n\u001b[1;32m 73\u001b[0m \u001b[38;5;28mprint\u001b[39m(cost(initial_route))\n\u001b[1;32m 75\u001b[0m r0 \u001b[38;5;241m=\u001b[39m TSP(initial \u001b[38;5;241m=\u001b[39m initial_route)\n\u001b[0;32m---> 76\u001b[0m path_states(\u001b[43muniform_cost_search\u001b[49m\u001b[43m(\u001b[49m\u001b[43mr0\u001b[49m\u001b[43m)\u001b[49m) \n",
93-
"File \u001b[0;32m~/aima-python/search_2.py:151\u001b[0m, in \u001b[0;36muniform_cost_search\u001b[0;34m(problem)\u001b[0m\n\u001b[1;32m 149\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21muniform_cost_search\u001b[39m(problem):\n\u001b[1;32m 150\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSearch nodes with minimum path cost first.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m--> 151\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mbest_first_search\u001b[49m\u001b[43m(\u001b[49m\u001b[43mproblem\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mf\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mg\u001b[49m\u001b[43m)\u001b[49m\n",
94-
"File \u001b[0;32m~/aima-python/search_2.py:101\u001b[0m, in \u001b[0;36mbest_first_search\u001b[0;34m(problem, f)\u001b[0m\n\u001b[1;32m 99\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m problem\u001b[38;5;241m.\u001b[39mis_goal(node\u001b[38;5;241m.\u001b[39mstate):\n\u001b[1;32m 100\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m node\n\u001b[0;32m--> 101\u001b[0m \u001b[43m\u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchild\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mexpand\u001b[49m\u001b[43m(\u001b[49m\u001b[43mproblem\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 102\u001b[0m \u001b[43m \u001b[49m\u001b[43ms\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mchild\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstate\u001b[49m\n\u001b[1;32m 103\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43ms\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mreached\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchild\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath_cost\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m<\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mreached\u001b[49m\u001b[43m[\u001b[49m\u001b[43ms\u001b[49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath_cost\u001b[49m\u001b[43m:\u001b[49m\n",
95-
"File \u001b[0;32m~/aima-python/search_2.py:49\u001b[0m, in \u001b[0;36mexpand\u001b[0;34m(problem, node)\u001b[0m\n\u001b[1;32m 47\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m action \u001b[38;5;129;01min\u001b[39;00m problem\u001b[38;5;241m.\u001b[39mactions(s):\n\u001b[1;32m 48\u001b[0m s1 \u001b[38;5;241m=\u001b[39m problem\u001b[38;5;241m.\u001b[39mresult(s, action)\n\u001b[0;32m---> 49\u001b[0m cost \u001b[38;5;241m=\u001b[39m \u001b[43mnode\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath_cost\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mproblem\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43maction_cost\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maction\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ms1\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 50\u001b[0m \u001b[38;5;28;01myield\u001b[39;00m Node(s1, node, action, cost)\n",
96-
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'float' and 'NoneType'"
19+
"name": "stderr",
20+
"output_type": "stream",
21+
"text": [
22+
"Exception ignored in: <bound method IPythonKernel._clean_thread_parent_frames of <ipykernel.ipkernel.IPythonKernel object at 0x7fd35e1c6190>>\n",
23+
"Traceback (most recent call last):\n",
24+
" File \"/usr/local/lib/python3.11/dist-packages/ipykernel/ipkernel.py\", line 775, in _clean_thread_parent_frames\n",
25+
" def _clean_thread_parent_frames(\n",
26+
"\n",
27+
"KeyboardInterrupt: \n",
28+
"\n",
29+
"KeyboardInterrupt\n",
30+
"\n"
9731
]
9832
}
9933
],
@@ -108,6 +42,10 @@
10842
"\n",
10943
"distances = {}\n",
11044
"\n",
45+
"correct_route = ['A', 'T', 'L', 'M', 'D', 'C', 'P',\n",
46+
" 'G', 'B', 'U', 'E', 'H', 'V', 'I', \n",
47+
" 'N', 'F', 'R', 'S', 'O', 'Z']\n",
48+
"\n",
11149
"for city in romania.keys():\n",
11250
" distances[city] = {}\n",
11351
"\n",
@@ -125,55 +63,60 @@
12563
" c += distances[route[0]][route[-1]]\n",
12664
" return c\n",
12765
"\n",
66+
"print(\"optimal route\", cost(correct_route))\n",
67+
"\n",
12868
"class TSP(Problem):\n",
12969
" \n",
13070
" def two_opt(self, state):\n",
131-
" \"\"\" Neighbour generating function for Traveling Salesman Problem \"\"\"\n",
132-
" print(\"STATE\", state)\n",
13371
" neighbour_state = state[:]\n",
13472
" left = random.randint(0, len(neighbour_state) - 1)\n",
13573
" right = random.randint(0, len(neighbour_state) - 1)\n",
13674
" if left > right:\n",
13775
" left, right = right, left\n",
138-
"\n",
13976
" neighbour_list = list(neighbour_state)\n",
14077
" x = neighbour_list[left: right + 1]\n",
14178
" neighbour_list[left: right + 1] = x[::-1]\n",
14279
" neighbour_state = tuple(neighbour_list)\n",
143-
" print(\"NSTATE\", neighbour_state)\n",
14480
" return neighbour_state\n",
14581
" \n",
14682
" def is_goal(self, state):\n",
147-
" return False\n",
83+
" return cost(state) < 2000\n",
14884
" \n",
14985
" def actions(self, state): \n",
15086
" \"\"\"The places neighboring `state`.\"\"\"\n",
151-
" new_state = self.two_opt(state)\n",
152-
" print(\"new_state\", new_state)\n",
153-
" return new_state\n",
87+
" new_states = set()\n",
88+
" for i in range(6):\n",
89+
" new_state = self.two_opt(state)\n",
90+
" new_states.add(new_state)\n",
91+
" return new_states\n",
15492
" \n",
15593
" def result(self, state, action):\n",
15694
" \"\"\"Go to the `action` place, if the map says that is possible.\"\"\"\n",
157-
" print(state, action)\n",
15895
" return action\n",
15996
" \n",
16097
" def action_cost(self, s, action, s1):\n",
16198
" \"\"\"The distance (cost) to go from s to s1.\"\"\"\n",
162-
" print(\"action cost\", s, action, s1)\n",
163-
" if(type(s) == tuple):\n",
164-
" print(cost(s))\n",
165-
" return cost(s)\n",
99+
" if(type(action) == tuple):\n",
100+
" return cost(action)\n",
166101
"\n",
167102
"\n",
168103
"initial_route = list(romania.keys())\n",
169104
"random.shuffle(initial_route)\n",
105+
"\n",
106+
"initial_route = ['A', 'T', 'L', 'S', 'D', 'C', 'P',\n",
107+
" 'G', 'B', 'U', 'E', 'H', 'V', 'I', \n",
108+
" 'N', 'F', 'R', 'M', 'O', 'Z']\n",
109+
"\n",
110+
"\n",
170111
"initial_route = tuple(initial_route)\n",
171112
"\n",
172-
"print(initial_route)\n",
173-
"print(cost(initial_route))\n",
113+
"print(\"initial route\", initial_route)\n",
114+
"print(\"initial route cost\", cost(initial_route))\n",
174115
"\n",
175116
"r0 = TSP(initial = initial_route)\n",
176-
"path_states(uniform_cost_search(r0)) "
117+
"path = path_states(uniform_cost_search(r0)) \n",
118+
"print(cost(path[-1]))\n",
119+
"print(path[-1])"
177120
]
178121
},
179122
{

mini_exp.ipynb

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 18,
6+
"id": "afb7219d-2eed-4626-8a44-a84d1166ff0c",
7+
"metadata": {},
8+
"outputs": [
9+
{
10+
"name": "stdout",
11+
"output_type": "stream",
12+
"text": [
13+
"initial route ('E', 'T', 'V', 'G', 'D', 'N', 'A', 'O', 'R')\n",
14+
"initial route cost 2774.5916941471914\n",
15+
"optimum_path ('D', 'T', 'A', 'O', 'R', 'N', 'V', 'E', 'G')\n",
16+
"optimum_cost 1492.2438454600285\n",
17+
"('T', 'A', 'O', 'N', 'V', 'E', 'G', 'R', 'D')\n",
18+
"1480.2605250041886\n"
19+
]
20+
}
21+
],
22+
"source": [
23+
"from search_2 import *\n",
24+
"import numpy as np\n",
25+
"\n",
26+
"romania = {'A': ( 76, 497), 'D': (160, 296),\n",
27+
" 'E': (558, 294), 'G': (368, 257),\n",
28+
" 'N': (407, 561), 'R': (227, 412),\n",
29+
" 'T': ( 83, 414), 'V': (535, 473),\n",
30+
" 'O': (117, 580)}\n",
31+
"\n",
32+
"distances = {}\n",
33+
"\n",
34+
"optimum_path = ('D', 'T', 'A', 'O', 'R', 'N', 'V', 'E', 'G')\n",
35+
"\n",
36+
"for city in romania.keys():\n",
37+
" distances[city] = {}\n",
38+
"\n",
39+
"for name_1, coordinates_1 in romania.items():\n",
40+
" for name_2, coordinates_2 in romania.items():\n",
41+
" distances[name_1][name_2] = np.linalg.norm(\n",
42+
" [coordinates_1[0] - coordinates_2[0], coordinates_1[1] - coordinates_2[1]])\n",
43+
" distances[name_2][name_1] = np.linalg.norm(\n",
44+
" [coordinates_1[0] - coordinates_2[0], coordinates_1[1] - coordinates_2[1]])\n",
45+
"\n",
46+
"def cost(route):\n",
47+
" c = 0\n",
48+
" for i in range(len(route)-1):\n",
49+
" c += distances[route[i]][route[i+1]]\n",
50+
" c += distances[route[0]][route[-1]]\n",
51+
" return c\n",
52+
"\n",
53+
"class TSP(Problem):\n",
54+
" \n",
55+
" def two_opt(self, state):\n",
56+
" neighbour_state = state[:]\n",
57+
" left = random.randint(0, len(neighbour_state) - 1)\n",
58+
" right = random.randint(0, len(neighbour_state) - 1)\n",
59+
" if left > right:\n",
60+
" left, right = right, left\n",
61+
" neighbour_list = list(neighbour_state)\n",
62+
" x = neighbour_list[left: right + 1]\n",
63+
" neighbour_list[left: right + 1] = x[::-1]\n",
64+
" neighbour_state = tuple(neighbour_list)\n",
65+
" return neighbour_state\n",
66+
" \n",
67+
" def is_goal(self, state):\n",
68+
" return cost(state) < 1500\n",
69+
" \n",
70+
" def actions(self, state): \n",
71+
" \"\"\"The places neighboring `state`.\"\"\"\n",
72+
" new_states = set()\n",
73+
" for i in range(6):\n",
74+
" new_state = self.two_opt(state)\n",
75+
" new_states.add(new_state)\n",
76+
" return new_states\n",
77+
" \n",
78+
" def result(self, state, action):\n",
79+
" \"\"\"Go to the `action` place, if the map says that is possible.\"\"\"\n",
80+
" return action\n",
81+
" \n",
82+
" def action_cost(self, s, action, s1):\n",
83+
" \"\"\"The distance (cost) to go from s to s1.\"\"\"\n",
84+
" if(type(action) == tuple):\n",
85+
" return cost(action)\n",
86+
"\n",
87+
"\n",
88+
"initial_route = list(romania.keys())\n",
89+
"random.shuffle(initial_route)\n",
90+
"\n",
91+
"initial_route = tuple(initial_route)\n",
92+
"\n",
93+
"print(\"initial route\", initial_route)\n",
94+
"print(\"initial route cost\", cost(initial_route))\n",
95+
"print(\"optimum_path\", optimum_path)\n",
96+
"print(\"optimum_cost\", cost(optimum_path))\n",
97+
"\n",
98+
"r0 = TSP(initial = initial_route)\n",
99+
"path = path_states(uniform_cost_search(r0)) \n",
100+
"print(path[-1])\n",
101+
"print(cost(path[-1]))"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"id": "ce7021ae-7527-45a7-9883-625637257572",
108+
"metadata": {},
109+
"outputs": [],
110+
"source": []
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": null,
115+
"id": "65cd45ac-cad0-49fe-a4fb-977b88fd667b",
116+
"metadata": {},
117+
"outputs": [],
118+
"source": []
119+
}
120+
],
121+
"metadata": {
122+
"kernelspec": {
123+
"display_name": "Python 3 (ipykernel)",
124+
"language": "python",
125+
"name": "python3"
126+
},
127+
"language_info": {
128+
"codemirror_mode": {
129+
"name": "ipython",
130+
"version": 3
131+
},
132+
"file_extension": ".py",
133+
"mimetype": "text/x-python",
134+
"name": "python",
135+
"nbconvert_exporter": "python",
136+
"pygments_lexer": "ipython3",
137+
"version": "3.11.2"
138+
}
139+
},
140+
"nbformat": 4,
141+
"nbformat_minor": 5
142+
}

0 commit comments

Comments
 (0)