This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
AICup/Lectures/Student_lecture 2.ipynb

871 lines
350 KiB
Text
Raw Normal View History

2020-10-01 16:03:04 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
2020-11-01 18:24:26 +00:00
"## Second Lab\n",
"\n",
"What we are going to do today:\n",
"- Recap of the previous Lab\n",
"- Define the nearest neighbor method and use it\n",
"- Define the Best Nearest Neighbors and use it \n",
"- Define the Multi Fragment and use it\n",
"- Finally compare these Constructive Methods"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This cell below is simply importing some useful stuff for later"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import glob\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"from time import time as t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Recap\n",
"### The problems\n",
"As we saw last time, we have 12 problems and two have an optimal solution"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"- ch130.tsp\n",
"- d198.tsp\n",
"- eil76.tsp\n",
"- fl1577.tsp\n",
"- kroA100.tsp\n",
"- lin318.tsp\n",
"- pcb442.tsp\n",
"- pr439.tsp\n",
"- rat783.tsp\n",
"- u1060.tsp\n"
]
}
],
"source": [
"problems = glob.glob('../problems/*.tsp')\n",
"for prob in problems:\n",
" if prob in [\"../problems/eil76.tsp\", \"../problems/kroA100.tsp\"]:\n",
" print(f\"- {prob[12:]} with opt\", sep='\\t')\n",
" else:\n",
" print(f\"- {prob[12:]}\", sep='\\t')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### ProblemInstance Class\n",
"Last time we implemented an ProblemInstance class that we can use to load a problem. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from src.io_tsp import ProblemInstance"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"#############################\n",
"name: eil76\n",
"nPoints: 76\n",
"best_sol: 538.0\n",
"exist optimal: True\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAHiCAYAAAAwHB+eAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO3df4wc533f8c8nNJ2cHKcnWSeBOsWhBChMGjMW3Y1r9wpDppJQDlSJIKzUBhIwgVv1D6O10ITx0UXhxqjLKwgEyV8BVOcHAzt2ZJk+KRYQheBFdUAUDpY+xZQjM4JtmdaKJs+Wr3LcayIx3/6xs9SRPd3t3s3OMzPP+wUcdnf27ub77I/53M587xlHhAAAQLV+IHUBAADkiAAGACABAhgAgAQIYAAAEiCAAQBIgAAGACABAhhoCdsfsv2x4vpO22H7NanrArA2AhhoiYj4bxHxb9a6z/aXbf/dqq+Xbf/pqvu32f6vtp+3/T3bi7Ynq6seyA9/HQMZiIifGly3bUlflfTpVd/ym5L+haS3Szon6ack/d8qawRywydgoGFs32T7M7aXbH/d9n8olv8X2x8f4le8Q9INkj5T/Ny1kh6Q9G8j4hvR91REEMDAGBHAQIPY/gFJfyrpryVNS7pT0gO2943waw5Kejgivl/c3i3pZUnvtv0t239r+/1l1g3g/8cuaKBZfkbSVER8pLj9Ndv/Q9J7JH1jox+2fY2kd0u6Z9XimyX9E0k/LukWSbdJOmn7byPiRJnFA3gFAQw0y49Jusn28qpl2yT9pYYIYEkHJL0g6X+uWrZSXH4kIlYkfcn2pyT9giQCGBgTdkEDzfJNSV+PiMlVX6+PiF8Y8ucPSvqjuPI0aF8qLjk1GlAhAhholr+S9KLtD9qeKP596E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6+vhZ1g+gVMME8GskvUXS70bEHknfV39381Bs32+7a7u7tLS0yTKBajy/vDLSctYPYLOGCeDnJD0XEV8obj+sfiBfsL1DkorLi2v9cEQ8GBGdiOhMTU2VUTMwNjdNToy0nPUD2KwNAzgiviXpm7Z3FYvulPQ3kh6VdLBYdlDSI2OpEFhlfrGnmbkF3TL7mGbmFjS/2Cv19x/at0sT27ddsWxi+zYd2rfrVX6iXLmvH8jJsF3Q/17SJ4oO6K9J+lX1w/sh2++TdE7SfeMpEeirokFo8HtSdQHnvn4gJ46IylbW6XSi2+1Wtj60y8zcgnprHIucnpzQqdm9CSoCgPXZPh0RnbXuYyYsNAYNQgDahABGY9AgBKBNCGA0Bg1CANpk2CYsIDkahAC0CQGMRtm/Z5rABdAK7IIGACABAhgAgAQIYAAAEuAYMADURNPOxdy0euuGAAaAGmjauZibVm8dsQsaAGqgaediblq9dUQAA0ANNG2q1abVW0cEMADUQNOmWm1avXVEAANADTRtqtWm1VtH2Tdh1bWLr651ARiPpk212rR66yjr8wFf3cUn9f+CO3Jgd9IXUV3rAgCMhvMBv4q6dvHVtS4AQHmyDuC6dvHVtS4AQHmyDuC6dvHVtS4AQHmyDuC6dvHVtS4AQHmy7oKuaxdfXesCAJQn6y5oAADGiS5oAABqhgAGACABAhgAgAQIYAAAEiCAAQBIgAAGACABAhgAgAQIYAAAEsh6JiyUi3MYA8DwCGCU4upzGPeWV3T4+BlJIoQBYA3sgkYpOIcxAIyGAEYpOIcxAIyGAEYpOIcxAIyGAEYpOIcxAIyGJiyUgnMYA8BoCGCUZv+eaQIXAIbELmgAABIggAEASIAABgAgAY4BAzXDlJ5AHghgoEaY0hPIB7uggRphSk8gHwQwUCNM6QnkgwAGaoQpPYF8DHUM2Pazkr4n6ZKklyOiY/s6SX8iaaekZyX9YkR8dzxlAtVJ2QR1aN+uK44BS0z
"text/plain": [
"<Figure size 576x576 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0. 15. 23. ... 18. 23. 25.]\n",
" [15. 0. 24. ... 7. 12. 15.]\n",
" [23. 24. 0. ... 31. 21. 20.]\n",
" ...\n",
" [18. 7. 31. ... 0. 17. 20.]\n",
" [23. 12. 21. ... 17. 0. 3.]\n",
" [25. 15. 20. ... 20. 3. 0.]]\n"
]
}
],
"source": [
"example_problem = \"../problems/eil76.tsp\"\n",
"p_inst = ProblemInstance(example_problem)\n",
"p_inst.print_info()\n",
"p_inst.plot_data()\n",
"print(p_inst.dist_matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### TSPSolver Class\n",
"Even here, instead of reimplementing the class everytime, we can import the one I prepared before.\n",
"This implementation is ready to receive a lot of different types of problems"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from src.TSP_solver import TSPSolver\n",
"from src.constructive_algorithms import random_method"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The class implements the following methods:\n",
"..."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"### solving with ['random'] ####\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAHiCAYAAAAwHB+eAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOydd5gTxRvHv3NHr9IE6dJFFKRXQWkC0otUARFBBMRKVbBg4yf2TrNiQVA4sNMFQUCp0qX3ctxxB9cyvz++GTfJpWySTXI55vM8ee6S7M7ObnbnnbeOkFJCo9FoNBpNeImJdAc0Go1Go7kW0QJYo9FoNJoIoAWwRqPRaDQRQAtgjUaj0WgigBbAGo1Go9FEAC2ANRqNRqOJAFoAB4gQYpIQYpb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eDZqUTwAoarcUmTnmfAD97BPjvABW2D8/CmCVy29dQEr5oGM3/OjySVDzBMAAOMf3oAVEArhVSlkItLa4/gau1/kEzJEDHH8K2d9vg/XPrbuxMtujBbBnNgJIEEKMF0LkFUwdqiWEaGBmZyFEWTCwwik1RTJYYQ2AyUKI3IKBSPcAiHPTRi7BPM7CUso00Aek0m2+ADBUCFHHPgi+AGCDlPKQhy59AWAsqJ06Rhm+D2C6MqMJIUoIIbrav1sA4G7BIJ9coB/P4z0jpdwHzo4Hgv7oBFAj6QnPAvg0gLL29sPNadC374mCoDZ0VQjREJy8mGEOgN1SyldcPv8MQGchRHv7/ZRHMEiprN0isQnAM/bfvTmcTa3uWAVgNIxru9LlvTt8nbO/5AL9jmcBpAshOgBo530XIpk
"text/plain": [
"<Figure size 576x576 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"best solution with len 2484.0 \n",
"with a total time of 0.0 seconds\n"
]
}
],
"source": [
"available_solvers = {\"random\": random_method}\n",
"\n",
"solver = TSPSolver(\"random\", p_inst, available_solvers)\n",
"start = t()\n",
"solver.compute_solution(return_value=False, verbose=True)\n",
"end = t()\n",
"\n",
"solver.plot_solution()\n",
"print(f\"best solution with len {solver.found_length} \\nwith a total time of {np.round(end - start, 5)} seconds\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Let's analyse the performances of the random Method\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAsAAAAIECAYAAAAXVphNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOydd3hUVfrHv3dqyiSTRgpJ6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AZEU0kdrpwJ1K8luE6IaoxzAGE/f0VE+xis6MXVBfzojhUyPVxqeelZlxoz0Up8+6HP5ESMSYwA5gR8URapwtHIiGdQEy4TohyhrmeA6JNBtQ5m8P+/oqRW2zKGbkdvaBii2I6avu48HklxZo0jQsdtShl3n1GKGAGMIPBCNt0ArWE04QoZ5h7vEC9sxlAeN1ftV5aqcWm2Mjt7AUVWxQHoqaPb148we95rdpZqPgeYbtt70Up8+4zQgkzgBkMRtimEygR7hMizSAPx/sbjJdWzsgVGkCRuKC6GJHr44QZL2/3e15CtLbb9liUMu8+I5QwA5gR9kTKpqxIJlzTCZQI1wmRZpifrWxATaM74NjOur9t9dKqzXXubIOfjR8+5Pr4sql98fcvjkum7XgoBqxUu23vRalSyg2DoRZmADPCnkjZlHWxEE7pBICyAROO4W6aYb7/XDVuenVX2Nzftqa9KEUNHpyUg+c//yngfR1t8LPxIxBxG5zYLxUT+6VSjePn5w7Dko8OqG63HbEo1fJMmbEcHJfCfWOFMC4hIkmUPxjR80i6vlAQ6usN1+Ib4VrkQoljJQ7c+uYe5NtqwHEcMqzRYXV/21o0Qqk
"text/plain": [
"<Figure size 800x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"best solution 2146.0 on 1000 trials,\n",
"with a total time of 0.6 seconds\n"
]
}
],
"source": [
"\n",
"number_of_trials = 1000\n",
"found_lens = []\n",
"start = t()\n",
"for _ in range(number_of_trials):\n",
" solver.compute_solution(return_value=False, verbose=False)\n",
" found_lens.append(solver.found_length)\n",
"end = t()\n",
"fig=plt.figure(figsize=(8,6), dpi= 100, facecolor='w', edgecolor='k')\n",
"plt.ylabel(\"Solution Length\")\n",
"plt.xlabel(\"Tries\")\n",
"plt.plot(found_lens, \"*\",label=\"Random\")\n",
"plt.plot(np.ones(number_of_trials)*p_inst.best_sol, \"r-\",label=\"Optimal\")\n",
"plt.legend(loc=\"center right\")\n",
"plt.show()\n",
"\n",
"print(f\"best solution {min(found_lens)} on {number_of_trials} trials,\\nwith a total time of {np.round(end - start, 2)} seconds\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"-------------\n",
"## Constructive Algorithm\n",
"### Nearest Neighbor Method"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def nn( instance_, starting_node=0): \n",
" # TODO\n",
" \n",
" # [...]\n",
" tour.append(starting_node)\n",
" return np.array(tour)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"### solving with ['nn'] ####\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAHiCAYAAAAwHB+eAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOydeZyN5fvHP/cg+5oluyRFilB2oyQSaUGWioa0UN9C9r2UtIgklCUkWYpKSPY1jXWSkH3Pvi8z5vr98TnnN2Oa5Yw559zPOed6v17nNWd9nuuceZ7nc9/XfS1GRKAoiqIoin8Js22AoiiKooQiKsCKoiiKYgEVYEVRFEWxgAqwoiiKolhABVhRFEVRLKACrCiKoigWUAF2CMaYXsaYr1z3SxhjxBiT3rZdblz2lPLBdvcaYx5J4zbmGWPaJPP6RGPMux5uq5gx5oIxJp033ut6vaQH27rhf57Sd7oZjDEDjDFT0riN1saYX5N5vY4x5mBa9uFvfHVsK0pKqAA7BBF5T0TaJ/aaMWar60LuvsUYY36K93o6Y8y7xpjDxpjzxpiNxphc/rPeLiLymIh8DQDGmLbGmJVp2NZ+EckmItdT+15jzFJjTPsE78kmIrtvwo7//05OQkS+EZFH3Y9VvJyPMSajMWacMWZfvOvDYwnek8UYM8oYc8IYc9YYszyZ7U0xxhwxxpwzxux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bCHNfrmcDz+wI46VgD/vZKIpgbvZmKoiiKN3B5HTqKSEsP3/8qGGAW7sF724JBejXTZqViE50BK4qi+AARWZmc+BpjChpjahh
"text/plain": [
"<Figure size 576x576 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"best solution with len 680.0 \n",
"with a total time of 0.00199 seconds\n"
]
}
],
"source": [
"available_solvers = {\"random\": random_method,\n",
" \"nn\":nn}\n",
"\n",
"solver = TSPSolver(\"nn\", p_inst, available_solvers)\n",
"start = t()\n",
"solver.compute_solution(return_value=False, verbose=True)\n",
"end = t()\n",
"\n",
"solver.plot_solution()\n",
"print(f\"best solution with len {solver.found_length} \\nwith a total time of {np.round(end - start, 5)} seconds\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "A25UdIeTJOp8"
},
"source": [
"### Best Nearest Neighbors"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "i_cC5eA-XT0B"
},
"outputs": [],
"source": [
"def best_nn(instance):\n",
" # TODO\n",
" \n",
" # [...]\n",
" return solution\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 534
},
"id": "lRAGk5zEXW0U",
"outputId": "493f692e-ef0c-4a94-dfc9-6726e9a5ac43"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"### solving with ['best_nn'] ####\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAHiCAYAAAAwHB+eAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOyddZxUZRfHf4cUkC5pkFBCQOlGBYQ1EBUEC0QEDAQLAV8LQUFeFFTM18AEQTEXl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HYKTPLSYiJUWks7NtKoCbhEk+ecA4YYrHj3NBOwu6Iuc7btf9oBWfkgLeD6C8s/9gsx+M7adEQQBHHOXbGFy8pIePAWw0xrx20fNfALhZRG5wjqdLnOSY8o5HYjmAl5zfvSWAm9N4n3ngosz33c696HFypPWZ08M9IlLLWZANBzDVsLQrxc8nIqWFiU0FAJwH3dy+Y9nKMSAi94hIScfC9SV/xYOL3BtF5Hpn4fukI/OiFHb1Faj4bkfSxe17AIaKSG3n/QqLSNc0ZJorKSf7fQNgoIiUE5YtPpNoWx4wtnsQQJyIdALQIZl9+I6vVgBuQtJrQWI5corIJaDBlsP5LXMn2p7H2S4AcjvbU7w2pHQ9zMy+wh3XfrBA4lxQbgbde9vAFf7/wPhtergXwB+Owr2YHqD1ehiMwzxnmAWY0n62i8gJ0H13jyPfLDD+9i24Mq6K1OPTX4Pu4dkXWUfjAfwI4HcROQkmZDVx3mMdgEfAi8peAEdB6zY15oEuzZ2JHguAVSm8fjaAdQD2iUh2Wm3JMR6
"text/plain": [
"<Figure size 576x576 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"best solution with len 601.0 \n",
"with a total time of 0.05389 seconds\n"
]
}
],
"source": [
"available_solvers = {\"random\": random_method,\n",
" \"nn\": nn,\n",
" \"best_nn\": best_nn}\n",
"\n",
"solver = TSPSolver(\"best_nn\", p_inst, available_solvers)\n",
"start = t()\n",
"solver.compute_solution(return_value=False, verbose=True)\n",
"end = t()\n",
"\n",
"solver.plot_solution()\n",
"print(f\"best solution with len {solver.found_length} \\nwith a total time of {np.round(end - start, 5)} seconds\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uXtOOk25XXpR"
},
"source": [
"### Multi Fragment"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"id": "1oo-Zv7vXdTq"
},
"outputs": [],
"source": [
"def check_if_edge_available(n1, n2, sol):\n",
" return (len(sol[str(n1)]) < 2) and (len(sol[str(n2)]) < 2)\n",
"\n",
"def check_if_not_close(edge_to_append, sol):\n",
" n1, n2 = edge_to_append\n",
" from_city = n2\n",
" if len(sol[str(from_city)]) == 0:\n",
" return True\n",
" partial_tour = [from_city]\n",
" iter_num = 0\n",
" while True:\n",
" if len(sol[str(from_city)]) == 1:\n",
" if from_city == n1:\n",
" return_value = False\n",
" break\n",
" elif iter_num > 1:\n",
" return_value = True\n",
" break\n",
" else:\n",
" from_city = sol[str(from_city)][0]\n",
" partial_tour.append(from_city)\n",
" iter_num += 1\n",
" else:\n",
" for node_connected in sol[str(from_city)]:\n",
" if node_connected not in partial_tour:\n",
" from_city = node_connected\n",
" partial_tour.append(node_connected)\n",
" iter_num +=1\n",
" return return_value\n",
"\n",
"def create_solution(start_sol, sol):\n",
" assert len(start_sol)==2, \"too many cities with just one link\"\n",
" n1, n2 = start_sol\n",
" from_city = n2\n",
" sol_list = [n1, n2]\n",
" iter_num = 0\n",
" while iter_num <300:\n",
" for node_connected in sol[str(from_city)]:\n",
" iter_num += 1\n",
" if node_connected not in sol_list:\n",
" from_city = node_connected\n",
" sol_list.append(node_connected)\n",
" sol_list.append(n1)\n",
" return sol_list\n",
"\n",
"def mf(instance):\n",
" mat = np.copy(instance.dist_matrix)\n",
" mat = np.triu(mat)\n",
" mat[mat == 0] = 100000\n",
" # TODO\n",
" \n",
" #[...]\n",
" return create_solution(start_list, solution)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 516
},
"id": "-iTyocncXg0d",
"outputId": "9a48f544-d4ef-4f57-f786-789006ca5432"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"### solving with ['multi_fragment'] ####\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAHiCAYAAAAwHB+eAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0R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f8xze7uddfoVETkkIhudi4ydYKJf95TvM8a8DqA26KZPedEWUMJNAe8F3UpXedwKikhaijI1+gH4NMUPs8659+rHEpEVInIH+Af+HjypA3Rbua5gYYzJD14Z709lHRvBA7cjgD6gQnaxF4xBeX7PPCKyH3SrlPfYRj5nG2kRA6AlqIRjwFjXDUjfIrN50Ga07S/B2E55ESkMXumajFZqjMkL/lbjRWSOx6K9AD5Lsa/zi8gr4L4u4vyOLiqkKThP3KtAl956x3L7E8DjoEV6LCM5A4DrYiKfx2ulUnujH0j526b3Wx4EPVMA/v/3S+84T4+9AC6BeQeu37iQeCRmZlLu5AvppRgjIjXBRM7bQJfyAQBFHQ+XiwpI5Xzg8BWA3s4FfV4ACz3kj0lxjBYQkQe9lTEFKfet8XwOem4EQF0RKQR6iVL+x8p7PK4AftdAI0ghlzFmDHhO7eB4uKwSbgp4OYAzxpgnjTF5ndKh2saYxt582BhTDsCNoIvp/xGR7QD+ADDKGJPbGFMDTDj5KZV15DKs4yzsuLrOgNYpwBPKAGNMfceyfQnAMhHZlYZIXwIYClqnnhnZ7wF40eWOMsaUMMbc4SybCeA2Y0wLY0wuME6Y5u8sIttAa+A/YDz6DGg5dEPaCvgwgHLO+gPNYTC2nxYFQaviojHmevDixRs+ArBZRF5L8frnADobY252jqc8TrlDOccjsRLAGOd3bwF
"text/plain": [
"<Figure size 576x576 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"best solution with len 600.0 \n",
"with a total time of 0.00597 seconds\n"
]
}
],
"source": [
"available_solvers = {\"random\": random_method,\n",
" \"nn\":nn,\n",
" \"best_nn\":best_nn,\n",
" \"multi_fragment\": mf\n",
" }\n",
"\n",
"solver = TSPSolver(\"multi_fragment\", p_inst, available_solvers)\n",
"start = t()\n",
"solver.compute_solution(return_value=False, verbose=True)\n",
"end = t()\n",
"\n",
"solver.plot_solution()\n",
"print(f\"best solution with len {solver.found_length} \\nwith a total time of {np.round(end - start, 5)} seconds\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lp9RXLX9Ye1M"
},
"source": [
"### Comparison Constructive methods"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 239
},
"id": "jZ4R8UZgYed7",
"outputId": "28475799-f823-42e9-91ad-b9c3ed029bdd",
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 4/4 [00:01<00:00, 2.94it/s, using multi_fragment for d198] \n"
]
}
],
"source": [
"from time import time as t\n",
"import pandas as pd\n",
"from tqdm import tqdm\n",
"\n",
"available_solvers = {\"random\": random_method,\n",
" \"nn\":nn,\n",
" \"best_nn\":best_nn,\n",
" \"multi_fragment\": mf\n",
" }\n",
"\n",
"show_plots = False\n",
"verbose = False\n",
"problems = [\"../problems/eil76.tsp\", \"../problems/kroA100.tsp\", \"../problems/ch130.tsp\", \"../problems/d198.tsp\"]\n",
"methods = available_solvers.keys()\n",
"results = []\n",
"index = []\n",
"with tqdm(problems) as tq:\n",
" for filename in tq:\n",
" p_inst = ProblemInstance(filename)\n",
" if verbose:\n",
" print(\"\\n\\n#############################\")\n",
" p_inst.print_info()\n",
" if show_plots:\n",
" p_inst.plot_data()\n",
"\n",
" for method in methods:\n",
" tq.set_postfix_str(f\"using {method} for {p_inst.name}\")\n",
" solver = TSPSolver(method, p_inst, available_solvers)\n",
" start = t()\n",
" solver.compute_solution(return_value=False, verbose=verbose)\n",
" end = t()\n",
" if verbose: \n",
" print(f\"the total length for the solution found is {solver.found_length}\",\n",
" f\"while the optimal length is {p_inst.best_sol}\",\n",
" f\"the gap is {solver.gap} %\",\n",
" f\"the solution is found in {np.round(end - start, 5)} seconds\",sep=\"\\n\")\n",
" index.append((filename[12:], method))\n",
" results.append([solver.found_length, p_inst.best_sol, solver.gap, end - start])\n",
"\n",
" if show_plots:\n",
" solver.plot_solution()\n",
"\n",
" if p_inst.exist_opt and show_plots:\n",
" solver = TSPSolver(\"optimal\", p_inst)\n",
" solver.solved = True\n",
" solver.solution = np.concatenate([p_inst.optimal_tour, [p_inst.optimal_tour[0]]])\n",
" solver.plot_solution()\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"id": "v8iJZ1QtY6WM"
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>tour length</th>\n",
" <th>optimal solution</th>\n",
" <th>gap</th>\n",
" <th>time to solve</th>\n",
" </tr>\n",
" <tr>\n",
" <th>problem</th>\n",
" <th>method</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"4\" valign=\"top\">eil76.tsp</th>\n",
" <th>random</th>\n",
" <td>2616.0</td>\n",
" <td>538.0</td>\n",
" <td>386.25</td>\n",
" <td>0.000970</td>\n",
" </tr>\n",
" <tr>\n",
" <th>nn</th>\n",
" <td>680.0</td>\n",
" <td>538.0</td>\n",
" <td>26.39</td>\n",
" <td>0.000997</td>\n",
" </tr>\n",
" <tr>\n",
" <th>best_nn</th>\n",
" <td>601.0</td>\n",
" <td>538.0</td>\n",
" <td>11.71</td>\n",
" <td>0.049906</td>\n",
" </tr>\n",
" <tr>\n",
" <th>multi_fragment</th>\n",
" <td>600.0</td>\n",
" <td>538.0</td>\n",
" <td>11.52</td>\n",
" <td>0.005983</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"4\" valign=\"top\">kroA100.tsp</th>\n",
" <th>random</th>\n",
" <td>163891.0</td>\n",
" <td>21282.0</td>\n",
" <td>670.09</td>\n",
" <td>0.001030</td>\n",
" </tr>\n",
" <tr>\n",
" <th>nn</th>\n",
" <td>27807.0</td>\n",
" <td>21282.0</td>\n",
" <td>30.66</td>\n",
" <td>0.001961</td>\n",
" </tr>\n",
" <tr>\n",
" <th>best_nn</th>\n",
" <td>24815.0</td>\n",
" <td>21282.0</td>\n",
" <td>16.60</td>\n",
" <td>0.101760</td>\n",
" </tr>\n",
" <tr>\n",
" <th>multi_fragment</th>\n",
" <td>24287.0</td>\n",
" <td>21282.0</td>\n",
" <td>14.12</td>\n",
" <td>0.007005</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"4\" valign=\"top\">ch130.tsp</th>\n",
" <th>random</th>\n",
" <td>44209.0</td>\n",
" <td>6110.0</td>\n",
" <td>623.55</td>\n",
" <td>0.001029</td>\n",
" </tr>\n",
" <tr>\n",
" <th>nn</th>\n",
" <td>7578.0</td>\n",
" <td>6110.0</td>\n",
" <td>24.03</td>\n",
" <td>0.002980</td>\n",
" </tr>\n",
" <tr>\n",
" <th>best_nn</th>\n",
" <td>6880.0</td>\n",
" <td>6110.0</td>\n",
" <td>12.60</td>\n",
" <td>0.206479</td>\n",
" </tr>\n",
" <tr>\n",
" <th>multi_fragment</th>\n",
" <td>7849.0</td>\n",
" <td>6110.0</td>\n",
" <td>28.46</td>\n",
" <td>0.017952</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"4\" valign=\"top\">d198.tsp</th>\n",
" <th>random</th>\n",
" <td>188837.0</td>\n",
" <td>15780.0</td>\n",
" <td>1096.69</td>\n",
" <td>0.001047</td>\n",
" </tr>\n",
" <tr>\n",
" <th>nn</th>\n",
" <td>18975.0</td>\n",
" <td>15780.0</td>\n",
" <td>20.25</td>\n",
" <td>0.005039</td>\n",
" </tr>\n",
" <tr>\n",
" <th>best_nn</th>\n",
" <td>17565.0</td>\n",
" <td>15780.0</td>\n",
" <td>11.31</td>\n",
" <td>0.694035</td>\n",
" </tr>\n",
" <tr>\n",
" <th>multi_fragment</th>\n",
" <td>17111.0</td>\n",
" <td>15780.0</td>\n",
" <td>8.43</td>\n",
" <td>0.065004</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" tour length optimal solution gap \\\n",
"problem method \n",
"eil76.tsp random 2616.0 538.0 386.25 \n",
" nn 680.0 538.0 26.39 \n",
" best_nn 601.0 538.0 11.71 \n",
" multi_fragment 600.0 538.0 11.52 \n",
"kroA100.tsp random 163891.0 21282.0 670.09 \n",
" nn 27807.0 21282.0 30.66 \n",
" best_nn 24815.0 21282.0 16.60 \n",
" multi_fragment 24287.0 21282.0 14.12 \n",
"ch130.tsp random 44209.0 6110.0 623.55 \n",
" nn 7578.0 6110.0 24.03 \n",
" best_nn 6880.0 6110.0 12.60 \n",
" multi_fragment 7849.0 6110.0 28.46 \n",
"d198.tsp random 188837.0 15780.0 1096.69 \n",
" nn 18975.0 15780.0 20.25 \n",
" best_nn 17565.0 15780.0 11.31 \n",
" multi_fragment 17111.0 15780.0 8.43 \n",
"\n",
" time to solve \n",
"problem method \n",
"eil76.tsp random 0.000970 \n",
" nn 0.000997 \n",
" best_nn 0.049906 \n",
" multi_fragment 0.005983 \n",
"kroA100.tsp random 0.001030 \n",
" nn 0.001961 \n",
" best_nn 0.101760 \n",
" multi_fragment 0.007005 \n",
"ch130.tsp random 0.001029 \n",
" nn 0.002980 \n",
" best_nn 0.206479 \n",
" multi_fragment 0.017952 \n",
"d198.tsp random 0.001047 \n",
" nn 0.005039 \n",
" best_nn 0.694035 \n",
" multi_fragment 0.065004 "
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index = pd.MultiIndex.from_tuples(index, names=['problem', 'method'])\n",
"\n",
"df = pd.DataFrame(results, index=index, columns=[\"tour length\", \"optimal solution\", \"gap\", \"time to solve\"])\n",
"df"
2020-10-01 16:03:04 +00:00
]
}
],
"metadata": {
"kernelspec": {
2020-11-01 18:24:26 +00:00
"display_name": "PyCharm (AI2020BsC)",
2020-10-01 16:03:04 +00:00
"language": "python",
2020-11-01 18:24:26 +00:00
"name": "pycharm-61970693"
2020-10-01 16:03:04 +00:00
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
2020-11-01 18:24:26 +00:00
}