From 352639f91bd92e2077eb32b64be522e7e6b0158d Mon Sep 17 00:00:00 2001 From: Wu Han Date: Sat, 9 Jan 2021 17:09:32 +0000 Subject: [PATCH] [new] set as template --- Workshop - 1 (Random, BFS, DFS, DP).ipynb | 60 ++++++------- Workshop - 2 (UCS, A, Hill-Climbing).ipynb | 53 +++++------- Workshop - 3 (LP).ipynb | 65 +++----------- Workshop - 4 (TSP SA).ipynb | 71 +++++----------- Workshop - 5 (ACO, PSO).ipynb | 99 +++++++--------------- Workshop - 6 (GA, SOM).ipynb | 64 +++++--------- template/README.md | 4 +- 7 files changed, 135 insertions(+), 281 deletions(-) diff --git a/Workshop - 1 (Random, BFS, DFS, DP).ipynb b/Workshop - 1 (Random, BFS, DFS, DP).ipynb index 4a28814..f7a1dd4 100644 --- a/Workshop - 1 (Random, BFS, DFS, DP).ipynb +++ b/Workshop - 1 (Random, BFS, DFS, DP).ipynb @@ -30,11 +30,11 @@ "outputs": [], "source": [ "from utils.load_data import load_data\n", - "from utils.load_data import log\n", "from utils.visualize_tsp import plotTSP\n", - "from utils.tsp import TSP\n", - "from utils.tsp import TSP_Bench\n", - "from utils.tsp import TSP_Bench_ALL" + "\n", + "from tsp import TSP_Bench_ONE\n", + "from tsp import TSP_Bench_PATH\n", + "from tsp import TSP_Bench_ALL" ] }, { @@ -291,7 +291,7 @@ " \"\"\"\n", " super().init(nodes)\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -322,7 +322,7 @@ "metadata": {}, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyRandomModel, max_it=100)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyRandomModel)" ] }, { @@ -361,7 +361,7 @@ " \"\"\"\n", " super().init(nodes)\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -373,9 +373,9 @@ " solution.append(i)\n", " unvisited_list = list(range(0, self.N))\n", " cur_city = i\n", - "# print(\"[starting]\", i)\n", + " # print(\"[starting]\", i)\n", " for steps in range(self.N - 1):\n", - "# print(unvisited_list)\n", + " # print(unvisited_list)\n", " unvisited_list.remove(cur_city)\n", " closest_neighbour = -1\n", " shortest_distance = math.inf\n", @@ -390,7 +390,7 @@ "\n", " self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n", "\n", - " return self.best_solution, self.fitness_list" + " return self.best_solution, self.fitness_list\n" ] }, { @@ -408,7 +408,7 @@ "metadata": {}, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyDFSModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyDFSModel)" ] }, { @@ -447,7 +447,7 @@ " \"\"\"\n", " super().init(nodes)\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -481,7 +481,7 @@ " self.best_solution = UCS_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n", " self.fitness_list.append(self.fitness(self.best_solution))\n", "\n", - " return self.best_solution, self.fitness_list" + " return self.best_solution, self.fitness_list\n" ] }, { @@ -503,7 +503,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyBFSModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyBFSModel)" ] }, { @@ -560,7 +560,7 @@ " distances.append(self.dist(node, i))\n", " return [x for _,x in sorted(zip(distances, MST))]\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -595,7 +595,7 @@ "\n", " self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n", "\n", - " return self.best_solution, self.fitness_list" + " return self.best_solution, self.fitness_list\n" ] }, { @@ -613,7 +613,7 @@ "metadata": {}, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyDPDModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyDPDModel)" ] }, { @@ -661,7 +661,7 @@ " distances.append(self.dist(node, i))\n", " return [x for _,x in sorted(zip(distances, MST))]\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -700,7 +700,7 @@ " self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n", " self.fitness_list.append(self.fitness(self.best_solution))\n", "\n", - " return self.best_solution, self.fitness_list" + " return self.best_solution, self.fitness_list\n" ] }, { @@ -718,7 +718,7 @@ "metadata": {}, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyDPBModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyDPBModel)" ] }, { @@ -759,7 +759,7 @@ "\n", " self.log(\"Nothing to initialize in your model now\")\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -793,7 +793,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyModel)" ] }, { @@ -811,21 +811,13 @@ }, "outputs": [], "source": [ - "for root, _, files in os.walk('./template/data'):\n", + "tsp_path = './template'\n", + "for root, _, files in os.walk(tsp_path + '/data'):\n", " if(files):\n", " for f in files:\n", " print(str(root) + \"/\" + f)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "tsp_path = './template/data'" - ] - }, { "cell_type": "code", "execution_count": null, @@ -867,7 +859,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "scrolled": true + "scrolled": false }, "outputs": [], "source": [ @@ -1019,7 +1011,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.8.5" } }, "nbformat": 4, diff --git a/Workshop - 2 (UCS, A, Hill-Climbing).ipynb b/Workshop - 2 (UCS, A, Hill-Climbing).ipynb index c7c44bf..0384a83 100644 --- a/Workshop - 2 (UCS, A, Hill-Climbing).ipynb +++ b/Workshop - 2 (UCS, A, Hill-Climbing).ipynb @@ -30,11 +30,11 @@ "outputs": [], "source": [ "from utils.load_data import load_data\n", - "from utils.load_data import log\n", "from utils.visualize_tsp import plotTSP\n", - "from utils.tsp import TSP\n", - "from utils.tsp import TSP_Bench\n", - "from utils.tsp import TSP_Bench_ALL" + "\n", + "from tsp import TSP_Bench_ONE\n", + "from tsp import TSP_Bench_PATH\n", + "from tsp import TSP_Bench_ALL" ] }, { @@ -280,8 +280,8 @@ "source": [ "import math\n", "import random\n", - "import numpy as np\n", "from model.base_model import Model\n", + "import numpy as np\n", "\n", "class MyRandomModel(Model):\n", " def __init__(self):\n", @@ -293,7 +293,7 @@ " \"\"\"\n", " super().init(nodes)\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -302,6 +302,7 @@ " solution = np.random.permutation(self.N).tolist()\n", " random_solutions.append(solution)\n", " self.fitness_list.append(self.fitness(solution))\n", + "\n", " self.best_solution = random_solutions[self.fitness_list.index(min(self.fitness_list))]\n", " return self.best_solution, self.fitness_list" ] @@ -325,7 +326,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyRandomModel, max_it=100)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyRandomModel)" ] }, { @@ -373,7 +374,7 @@ " distances.append(self.dist(node, i))\n", " return [x for _,x in sorted(zip(distances, MST))]\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -414,8 +415,8 @@ " coord_0, coord_1 = self.coords[UCS_solutions[i][k % N]], self.coords[UCS_solutions[i][(k + 1) % N]]\n", " cur_fit += math.sqrt((coord_0[0] - coord_1[0]) ** 2 + (coord_0[1] - coord_1[1]) ** 2)\n", " UCS_losses[i] = cur_fit\n", - "# if(UCS_losses[i] < min_loss):\n", - "# min_loss = UCS_losses[i]\n", + " # if(UCS_losses[i] < min_loss):\n", + " # min_loss = UCS_losses[i]\n", " break\n", " self.best_solution = UCS_solutions[ UCS_losses.index(min(UCS_losses)) ]\n", " self.fitness_list.append(self.fitness(self.best_solution))\n", @@ -440,7 +441,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyUCSModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyUCSModel)" ] }, { @@ -486,7 +487,7 @@ " distances.append(self.dist(node, i))\n", " return [x for _,x in sorted(zip(distances, MST))]\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -499,7 +500,7 @@ " \n", " # Breadth First: Set each city as starting point, then go to next city simultaneously\n", " for step in range(0, self.N - 1):\n", - "# print(\"[step]\", step)\n", + " # print(\"[step]\", step)\n", " unvisited_list = list(range(0, self.N))\n", " # For each search path\n", " for i in range(0, self.N):\n", @@ -538,7 +539,7 @@ " \n", " self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n", "\n", - " return self.best_solution, self.fitness_list" + " return self.best_solution, self.fitness_list\n" ] }, { @@ -556,7 +557,7 @@ "metadata": {}, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyASModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyASModel)" ] }, { @@ -682,7 +683,7 @@ "metadata": {}, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyHillClimbModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyHillClimbModel)" ] }, { @@ -710,7 +711,6 @@ "import math\n", "import random\n", "from model.base_model import Model\n", - "import numpy as np\n", "\n", "class MyModel(Model):\n", " def __init__(self):\n", @@ -721,9 +721,10 @@ " Put your initialization here.\n", " \"\"\"\n", " super().init(nodes)\n", + "\n", " self.log(\"Nothing to initialize in your model now\")\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -757,7 +758,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyModel)" ] }, { @@ -773,21 +774,13 @@ "metadata": {}, "outputs": [], "source": [ - "for root, _, files in os.walk('./template/data'):\n", + "tsp_path = './template'\n", + "for root, _, files in os.walk(tsp_path + '/data'):\n", " if(files):\n", " for f in files:\n", " print(str(root) + \"/\" + f)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "tsp_path = './template/data'" - ] - }, { "cell_type": "code", "execution_count": null, @@ -946,7 +939,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.8.5" } }, "nbformat": 4, diff --git a/Workshop - 3 (LP).ipynb b/Workshop - 3 (LP).ipynb index 213dabf..561ed93 100644 --- a/Workshop - 3 (LP).ipynb +++ b/Workshop - 3 (LP).ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -57,26 +57,11 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " con: array([], dtype=float64)\n", - " fun: -17.999999976555795\n", - " message: 'Optimization terminated successfully.'\n", - " nit: 4\n", - " slack: array([5.8 , 0.00000002, 0. ])\n", - " status: 0\n", - " success: True\n", - " x: array([4.19999999, 1.2 , 0. , 0. , 0. ])\n" - ] - } - ], + "outputs": [], "source": [ "res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds, x2_bounds, x3_bounds, x4_bounds])\n", "\n", @@ -110,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -125,26 +110,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "scrolled": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " con: array([ 0. , -4.56053464])\n", - " fun: -0.6099904044751114\n", - " message: 'The algorithm terminated successfully and determined that the problem is infeasible.'\n", - " nit: 4\n", - " slack: array([], dtype=float64)\n", - " status: 2\n", - " success: False\n", - " x: array([0.2893146 , 0.75265113, 0.60865936])\n" - ] - } - ], + "outputs": [], "source": [ "res = linprog(c, A_eq=A, b_eq=b, bounds=[x0_bounds, x1_bounds, x2_bounds,])\n", "\n", @@ -180,7 +150,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -194,26 +164,11 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " con: array([], dtype=float64)\n", - " fun: -8.999999998556428\n", - " message: 'Optimization terminated successfully.'\n", - " nit: 4\n", - " slack: array([4., 0., 4.])\n", - " status: 0\n", - " success: True\n", - " x: array([3., 0.])\n" - ] - } - ], + "outputs": [], "source": [ "res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds])\n", "\n", diff --git a/Workshop - 4 (TSP SA).ipynb b/Workshop - 4 (TSP SA).ipynb index c6f8dc7..f5c957c 100644 --- a/Workshop - 4 (TSP SA).ipynb +++ b/Workshop - 4 (TSP SA).ipynb @@ -30,11 +30,11 @@ "outputs": [], "source": [ "from utils.load_data import load_data\n", - "from utils.load_data import log\n", "from utils.visualize_tsp import plotTSP\n", - "from utils.tsp import TSP\n", - "from utils.tsp import TSP_Bench\n", - "from utils.tsp import TSP_Bench_ALL" + "\n", + "from tsp import TSP_Bench_ONE\n", + "from tsp import TSP_Bench_PATH\n", + "from tsp import TSP_Bench_ALL" ] }, { @@ -289,7 +289,7 @@ " \"\"\"\n", " super().init(nodes)\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -320,7 +320,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyRandomModel, max_it=100)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyRandomModel)" ] }, { @@ -355,10 +355,13 @@ "\n", " self.iteration = 0\n", "\n", - " def init(self, nodes, *args):\n", + " def init(self, nodes):\n", " super().init(nodes)\n", - "\n", - " T, stopping_temperature, alpha = args\n", + " \n", + " # Set hyper-parameters\n", + " T = -1\n", + " stopping_temperature = -1\n", + " alpha = 0.99\n", "\n", " self.T = math.sqrt(self.N) if T == -1 else T\n", " self.alpha = 0.995 if alpha == -1 else alpha\n", @@ -409,7 +412,7 @@ " if random.random() < self.p_accept(candidate_fitness):\n", " self.cur_fitness, self.cur_solution = candidate_fitness, candidate\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Execute simulated annealing algorithm.\n", " \"\"\"\n", @@ -444,18 +447,6 @@ "tsp_file = './template/data/simple/ulysses16.tsp'" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set hyper-parameters\n", - "T = -1\n", - "stopping_T = -1\n", - "alpha = 0.99" - ] - }, { "cell_type": "code", "execution_count": null, @@ -464,7 +455,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MySAModel, T, stopping_T, alpha, max_it=1000)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MySAModel)" ] }, { @@ -504,13 +495,13 @@ " Put your initialization here.\n", " \"\"\"\n", " super().init(nodes)\n", + "\n", " self.log(\"Nothing to initialize in your model now\")\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", - " self.log(\"Naive Random Solution\")\n", " self.best_solution = np.random.permutation(self.N).tolist()\n", " self.fitness_list.append(self.fitness(self.best_solution))\n", "\n", @@ -541,7 +532,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyModel)" ] }, { @@ -557,7 +548,8 @@ "metadata": {}, "outputs": [], "source": [ - "for root, _, files in os.walk('./template/data'):\n", + "tsp_path = './template'\n", + "for root, _, files in os.walk(tsp_path + '/data'):\n", " if(files):\n", " for f in files:\n", " print(str(root) + \"/\" + f)" @@ -577,15 +569,6 @@ " fig.suptitle(title, fontsize=20)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "tsp_path = './template/data'" - ] - }, { "cell_type": "code", "execution_count": null, @@ -605,18 +588,6 @@ "plot_results(best_solutions, times, \"Random Model\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set hyper-parameters\n", - "T = -1\n", - "stopping_T = -1\n", - "alpha = 0.8" - ] - }, { "cell_type": "code", "execution_count": null, @@ -626,7 +597,7 @@ "outputs": [], "source": [ "print(\"Simulated Annealing\")\n", - "best_solutions, fitness_lists, times = TSP_Bench_ALL(tsp_path, MySAModel, T, stopping_T, alpha, max_it=1000)" + "best_solutions, fitness_lists, times = TSP_Bench_ALL(tsp_path, MySAModel)" ] }, { @@ -671,7 +642,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.8.5" } }, "nbformat": 4, diff --git a/Workshop - 5 (ACO, PSO).ipynb b/Workshop - 5 (ACO, PSO).ipynb index 5ac4b8a..f54c550 100644 --- a/Workshop - 5 (ACO, PSO).ipynb +++ b/Workshop - 5 (ACO, PSO).ipynb @@ -30,11 +30,11 @@ "outputs": [], "source": [ "from utils.load_data import load_data\n", - "from utils.load_data import log\n", "from utils.visualize_tsp import plotTSP\n", - "from utils.tsp import TSP\n", - "from utils.tsp import TSP_Bench\n", - "from utils.tsp import TSP_Bench_ALL" + "\n", + "from tsp import TSP_Bench_ONE\n", + "from tsp import TSP_Bench_PATH\n", + "from tsp import TSP_Bench_ALL" ] }, { @@ -289,7 +289,7 @@ " \"\"\"\n", " super().init(nodes)\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -320,7 +320,7 @@ "metadata": {}, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyRandomModel, max_it=100)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyRandomModel)" ] }, { @@ -405,11 +405,22 @@ " self.distance += self.edges[self.tour[i]][self.tour[(i + 1) % self.num_nodes]].weight\n", " return self.distance\n", "\n", - " def init(self, nodes, *args):\n", + " def init(self, nodes):\n", " super().init(nodes)\n", - " mode, colony_size, elitist_weight, min_scaling_factor, alpha, beta, rho, pheromone_deposit_weight, initial_pheromone, labels = args\n", "\n", - " self.mode = mode[0]\n", + " # Set hypter-parameters\n", + " mode=['ACS']\n", + " colony_size=10\n", + " elitist_weight=1.0\n", + " min_scaling_factor=0.001\n", + " alpha=1.0\n", + " beta=3.0\n", + " rho=0.1\n", + " pheromone_deposit_weight=1.0\n", + " initial_pheromone=1.0\n", + " labels = None\n", + "\n", + " self.mode = str(mode[0]) \n", " self.colony_size = colony_size\n", " self.elitist_weight = elitist_weight\n", " self.min_scaling_factor = min_scaling_factor\n", @@ -439,7 +450,7 @@ "\n", " def _acs(self, max_it):\n", " for step in range(0, max_it):\n", - "# print('[step]', step)\n", + " # print('[step]', step)\n", " for ant in self.ants:\n", " self._add_pheromone(ant.find_tour(), ant.get_distance())\n", " if ant.distance < self.global_best_distance:\n", @@ -452,7 +463,7 @@ "\n", " def _elitist(self, max_it):\n", " for step in range(0, max_it):\n", - "# print('[step]', step)\n", + " # print('[step]', step)\n", " for ant in self.ants:\n", " self._add_pheromone(ant.find_tour(), ant.get_distance())\n", " if ant.distance < self.global_best_distance:\n", @@ -475,8 +486,8 @@ " self._elitist(max_it)\n", " else:\n", " print(\"Un supported\")\n", - "# print('Sequence : <- {0} ->'.format(' - '.join(str(self.labels[i]) for i in self.global_best_tour)))\n", - "# print('Total distance travelled to complete the tour : {0}\\n'.format(round(self.global_best_distance, 2)))\n", + " # print('Sequence : <- {0} ->'.format(' - '.join(str(self.labels[i]) for i in self.global_best_tour)))\n", + " # print('Total distance travelled to complete the tour : {0}\\n'.format(round(self.global_best_distance, 2)))\n", "\n", " return self.global_best_tour, self.fitness_list" ] @@ -490,25 +501,6 @@ "tsp_file = './template/data/simple/ulysses16.tsp'" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set hypter-parameters\n", - "mode='ACS', \n", - "colony_size=10\n", - "elitist_weight=1.0\n", - "min_scaling_factor=0.001\n", - "alpha=1.0\n", - "beta=3.0\n", - "rho=0.1\n", - "pheromone_deposit_weight=1.0\n", - "initial_pheromone=1.0\n", - "labels = None" - ] - }, { "cell_type": "code", "execution_count": null, @@ -517,7 +509,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyACOModel, mode, colony_size, elitist_weight, min_scaling_factor, alpha, beta, rho, pheromone_deposit_weight, initial_pheromone, labels, max_it=1000, timeout=300)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyACOModel, timeout=300)" ] }, { @@ -546,13 +538,13 @@ " Put your initialization here.\n", " \"\"\"\n", " super().init(nodes)\n", + "\n", " self.log(\"Nothing to initialize in your model now\")\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", - " self.log(\"Naive Random Solution\")\n", " self.best_solution = np.random.permutation(self.N).tolist()\n", " self.fitness_list.append(self.fitness(self.best_solution))\n", "\n", @@ -583,7 +575,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyModel)" ] }, { @@ -599,7 +591,8 @@ "metadata": {}, "outputs": [], "source": [ - "for root, _, files in os.walk('./template/data'):\n", + "tsp_path = './template'\n", + "for root, _, files in os.walk(tsp_path + '/data'):\n", " if(files):\n", " for f in files:\n", " print(str(root) + \"/\" + f)" @@ -619,15 +612,6 @@ " fig.suptitle(title, fontsize=20)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "tsp_path = './template/data'" - ] - }, { "cell_type": "code", "execution_count": null, @@ -647,25 +631,6 @@ "plot_results(best_solutions, times, \"Random Model\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set hypter-parameters\n", - "mode='ACS', \n", - "colony_size=10\n", - "elitist_weight=1.0\n", - "min_scaling_factor=0.001\n", - "alpha=1.0\n", - "beta=3.0\n", - "rho=0.1\n", - "pheromone_deposit_weight=1.0\n", - "initial_pheromone=1.0\n", - "labels = None" - ] - }, { "cell_type": "code", "execution_count": null, @@ -673,7 +638,7 @@ "outputs": [], "source": [ "print(\"Ant Colony Optimization\")\n", - "best_solutions, fitness_lists, times = TSP_Bench_ALL(tsp_path, MyACOModel, mode, colony_size, elitist_weight, min_scaling_factor, alpha, beta, rho, pheromone_deposit_weight, initial_pheromone, labels, max_it=100, timeout=600)" + "best_solutions, fitness_lists, times = TSP_Bench_ALL(tsp_path, MyACOModel)" ] }, { @@ -709,7 +674,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.8.5" } }, "nbformat": 4, diff --git a/Workshop - 6 (GA, SOM).ipynb b/Workshop - 6 (GA, SOM).ipynb index ac84033..9c6d996 100644 --- a/Workshop - 6 (GA, SOM).ipynb +++ b/Workshop - 6 (GA, SOM).ipynb @@ -30,11 +30,11 @@ "outputs": [], "source": [ "from utils.load_data import load_data\n", - "from utils.load_data import log\n", "from utils.visualize_tsp import plotTSP\n", - "from utils.tsp import TSP\n", - "from utils.tsp import TSP_Bench\n", - "from utils.tsp import TSP_Bench_ALL" + "\n", + "from tsp import TSP_Bench_ONE\n", + "from tsp import TSP_Bench_PATH\n", + "from tsp import TSP_Bench_ALL" ] }, { @@ -289,7 +289,7 @@ " \"\"\"\n", " super().init(nodes)\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", @@ -320,7 +320,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyRandomModel, max_it=100)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyRandomModel)" ] }, { @@ -345,6 +345,11 @@ "metadata": {}, "outputs": [], "source": [ + "import math\n", + "import random\n", + "from model.base_model import Model\n", + "from random import randint, sample\n", + "\n", "class Gene: # City\n", " def __init__(self, name, lat, lng):\n", " self.name = name\n", @@ -417,19 +422,7 @@ " if route.fitness > fittest.fitness:\n", " fittest = route\n", "\n", - " return fittest" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import math\n", - "import random\n", - "from model.base_model import Model\n", - "from random import randint, sample\n", + " return fittest\n", "\n", "class MyGAModel(Model):\n", " def __init__(self):\n", @@ -495,7 +488,7 @@ " def selection(self, population, competitors_n):\n", " return Population(sample(population.individuals, competitors_n)).get_fittest()\n", "\n", - " def fit(self, max_it=20):\n", + " def fit(self, max_it=100):\n", " \"\"\"\n", " Execute simulated annealing algorithm.\n", " \"\"\"\n", @@ -543,7 +536,7 @@ "outputs": [], "source": [ "print(\"Genetic Algorithm\")\n", - "best_solution, fitness_list, time = TSP_Bench(tsp_problem, MyGAModel, max_it=200)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_problem, MyGAModel, timeout=300)" ] }, { @@ -602,13 +595,13 @@ " Put your initialization here.\n", " \"\"\"\n", " super().init(nodes)\n", + "\n", " self.log(\"Nothing to initialize in your model now\")\n", "\n", - " def fit(self, max_it):\n", + " def fit(self, max_it=1000):\n", " \"\"\"\n", " Put your iteration process here.\n", " \"\"\"\n", - " self.log(\"Naive Random Solution\")\n", " self.best_solution = np.random.permutation(self.N).tolist()\n", " self.fitness_list.append(self.fitness(self.best_solution))\n", "\n", @@ -639,7 +632,7 @@ }, "outputs": [], "source": [ - "best_solution, fitness_list, time = TSP_Bench(tsp_file, MyModel)" + "best_solution, fitness_list, time = TSP_Bench_ONE(tsp_file, MyModel)" ] }, { @@ -655,7 +648,8 @@ "metadata": {}, "outputs": [], "source": [ - "for root, _, files in os.walk('./template/data'):\n", + "tsp_path = './template'\n", + "for root, _, files in os.walk(tsp_path + '/data'):\n", " if(files):\n", " for f in files:\n", " print(str(root) + \"/\" + f)" @@ -675,15 +669,6 @@ " fig.suptitle(title, fontsize=20)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "tsp_path = './template/data/medium'" - ] - }, { "cell_type": "code", "execution_count": null, @@ -712,7 +697,7 @@ "outputs": [], "source": [ "print(\"Genetic Algorithm\")\n", - "best_solutions, fitness_lists, times = TSP_Bench_ALL(tsp_path, MyGAModel, max_it=100, timeout=600)" + "best_solutions, fitness_lists, times = TSP_Bench_ALL(tsp_path, MyGAModel)" ] }, { @@ -724,13 +709,6 @@ "plot_results(best_solutions, times, \"Genetic Algorithm\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "code", "execution_count": null, @@ -755,7 +733,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.8.5" } }, "nbformat": 4, diff --git a/template/README.md b/template/README.md index 487f313..26981ed 100644 --- a/template/README.md +++ b/template/README.md @@ -4,8 +4,8 @@ docker build -t com2014-tsp . ## Windows -docker container run -v $PWD/requirements.txt:/tsp/requirements.txt -v $PWD/output/:/output/ com2014-tsp +docker container run -v $PWD/output/:/output/ com2014-tsp ## Linux -docker container run -v $PWD/requirements.txt:/tsp/requirements.txt -v $PWD/output/:/output/ com2014-tsp +docker container run -v $PWD/output/:/output/ com2014-tsp