[new] set as template

This commit is contained in:
Wu Han 2021-01-09 17:09:32 +00:00
parent a7dbaffe06
commit 352639f91b
7 changed files with 135 additions and 281 deletions

View File

@ -30,11 +30,11 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"from utils.load_data import load_data\n", "from utils.load_data import load_data\n",
"from utils.load_data import log\n",
"from utils.visualize_tsp import plotTSP\n", "from utils.visualize_tsp import plotTSP\n",
"from utils.tsp import TSP\n", "\n",
"from utils.tsp import TSP_Bench\n", "from tsp import TSP_Bench_ONE\n",
"from utils.tsp import TSP_Bench_ALL" "from tsp import TSP_Bench_PATH\n",
"from tsp import TSP_Bench_ALL"
] ]
}, },
{ {
@ -291,7 +291,7 @@
" \"\"\"\n", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -322,7 +322,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -373,9 +373,9 @@
" solution.append(i)\n", " solution.append(i)\n",
" unvisited_list = list(range(0, self.N))\n", " unvisited_list = list(range(0, self.N))\n",
" cur_city = i\n", " cur_city = i\n",
"# print(\"[starting]\", i)\n", " # print(\"[starting]\", i)\n",
" for steps in range(self.N - 1):\n", " for steps in range(self.N - 1):\n",
"# print(unvisited_list)\n", " # print(unvisited_list)\n",
" unvisited_list.remove(cur_city)\n", " unvisited_list.remove(cur_city)\n",
" closest_neighbour = -1\n", " closest_neighbour = -1\n",
" shortest_distance = math.inf\n", " shortest_distance = math.inf\n",
@ -390,7 +390,7 @@
"\n", "\n",
" self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n", " self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n",
"\n", "\n",
" return self.best_solution, self.fitness_list" " return self.best_solution, self.fitness_list\n"
] ]
}, },
{ {
@ -408,7 +408,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -481,7 +481,7 @@
" self.best_solution = UCS_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n", " self.best_solution = UCS_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n",
" self.fitness_list.append(self.fitness(self.best_solution))\n", " self.fitness_list.append(self.fitness(self.best_solution))\n",
"\n", "\n",
" return self.best_solution, self.fitness_list" " return self.best_solution, self.fitness_list\n"
] ]
}, },
{ {
@ -503,7 +503,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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", " distances.append(self.dist(node, i))\n",
" return [x for _,x in sorted(zip(distances, MST))]\n", " return [x for _,x in sorted(zip(distances, MST))]\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -595,7 +595,7 @@
"\n", "\n",
" self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n", " self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n",
"\n", "\n",
" return self.best_solution, self.fitness_list" " return self.best_solution, self.fitness_list\n"
] ]
}, },
{ {
@ -613,7 +613,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", " distances.append(self.dist(node, i))\n",
" return [x for _,x in sorted(zip(distances, MST))]\n", " return [x for _,x in sorted(zip(distances, MST))]\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -700,7 +700,7 @@
" self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n", " self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n",
" self.fitness_list.append(self.fitness(self.best_solution))\n", " self.fitness_list.append(self.fitness(self.best_solution))\n",
"\n", "\n",
" return self.best_solution, self.fitness_list" " return self.best_solution, self.fitness_list\n"
] ]
}, },
{ {
@ -718,7 +718,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", "\n",
" self.log(\"Nothing to initialize in your model now\")\n", " self.log(\"Nothing to initialize in your model now\")\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -793,7 +793,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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": [], "outputs": [],
"source": [ "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", " if(files):\n",
" for f in files:\n", " for f in files:\n",
" print(str(root) + \"/\" + f)" " print(str(root) + \"/\" + f)"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tsp_path = './template/data'"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -867,7 +859,7 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"scrolled": true "scrolled": false
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
@ -1019,7 +1011,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.3" "version": "3.8.5"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -30,11 +30,11 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"from utils.load_data import load_data\n", "from utils.load_data import load_data\n",
"from utils.load_data import log\n",
"from utils.visualize_tsp import plotTSP\n", "from utils.visualize_tsp import plotTSP\n",
"from utils.tsp import TSP\n", "\n",
"from utils.tsp import TSP_Bench\n", "from tsp import TSP_Bench_ONE\n",
"from utils.tsp import TSP_Bench_ALL" "from tsp import TSP_Bench_PATH\n",
"from tsp import TSP_Bench_ALL"
] ]
}, },
{ {
@ -280,8 +280,8 @@
"source": [ "source": [
"import math\n", "import math\n",
"import random\n", "import random\n",
"import numpy as np\n",
"from model.base_model import Model\n", "from model.base_model import Model\n",
"import numpy as np\n",
"\n", "\n",
"class MyRandomModel(Model):\n", "class MyRandomModel(Model):\n",
" def __init__(self):\n", " def __init__(self):\n",
@ -293,7 +293,7 @@
" \"\"\"\n", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -302,6 +302,7 @@
" solution = np.random.permutation(self.N).tolist()\n", " solution = np.random.permutation(self.N).tolist()\n",
" random_solutions.append(solution)\n", " random_solutions.append(solution)\n",
" self.fitness_list.append(self.fitness(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", " self.best_solution = random_solutions[self.fitness_list.index(min(self.fitness_list))]\n",
" return self.best_solution, self.fitness_list" " return self.best_solution, self.fitness_list"
] ]
@ -325,7 +326,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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", " distances.append(self.dist(node, i))\n",
" return [x for _,x in sorted(zip(distances, MST))]\n", " return [x for _,x in sorted(zip(distances, MST))]\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\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", " 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", " 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", " UCS_losses[i] = cur_fit\n",
"# if(UCS_losses[i] < min_loss):\n", " # if(UCS_losses[i] < min_loss):\n",
"# min_loss = UCS_losses[i]\n", " # min_loss = UCS_losses[i]\n",
" break\n", " break\n",
" self.best_solution = UCS_solutions[ UCS_losses.index(min(UCS_losses)) ]\n", " self.best_solution = UCS_solutions[ UCS_losses.index(min(UCS_losses)) ]\n",
" self.fitness_list.append(self.fitness(self.best_solution))\n", " self.fitness_list.append(self.fitness(self.best_solution))\n",
@ -440,7 +441,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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", " distances.append(self.dist(node, i))\n",
" return [x for _,x in sorted(zip(distances, MST))]\n", " return [x for _,x in sorted(zip(distances, MST))]\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -499,7 +500,7 @@
" \n", " \n",
" # Breadth First: Set each city as starting point, then go to next city simultaneously\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", " 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", " unvisited_list = list(range(0, self.N))\n",
" # For each search path\n", " # For each search path\n",
" for i in range(0, self.N):\n", " for i in range(0, self.N):\n",
@ -538,7 +539,7 @@
" \n", " \n",
" self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n", " self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ]\n",
"\n", "\n",
" return self.best_solution, self.fitness_list" " return self.best_solution, self.fitness_list\n"
] ]
}, },
{ {
@ -556,7 +557,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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 math\n",
"import random\n", "import random\n",
"from model.base_model import Model\n", "from model.base_model import Model\n",
"import numpy as np\n",
"\n", "\n",
"class MyModel(Model):\n", "class MyModel(Model):\n",
" def __init__(self):\n", " def __init__(self):\n",
@ -721,9 +721,10 @@
" Put your initialization here.\n", " Put your initialization here.\n",
" \"\"\"\n", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n",
" self.log(\"Nothing to initialize in your model now\")\n", " self.log(\"Nothing to initialize in your model now\")\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -757,7 +758,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", " if(files):\n",
" for f in files:\n", " for f in files:\n",
" print(str(root) + \"/\" + f)" " print(str(root) + \"/\" + f)"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tsp_path = './template/data'"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -946,7 +939,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.3" "version": "3.8.5"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -9,7 +9,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -40,7 +40,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -57,26 +57,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": null,
"metadata": { "metadata": {
"scrolled": true "scrolled": true
}, },
"outputs": [ "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"
]
}
],
"source": [ "source": [
"res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds, x2_bounds, x3_bounds, x4_bounds])\n", "res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds, x2_bounds, x3_bounds, x4_bounds])\n",
"\n", "\n",
@ -110,7 +95,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -125,26 +110,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"metadata": { "metadata": {
"scrolled": false "scrolled": false
}, },
"outputs": [ "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"
]
}
],
"source": [ "source": [
"res = linprog(c, A_eq=A, b_eq=b, bounds=[x0_bounds, x1_bounds, x2_bounds,])\n", "res = linprog(c, A_eq=A, b_eq=b, bounds=[x0_bounds, x1_bounds, x2_bounds,])\n",
"\n", "\n",
@ -180,7 +150,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -194,26 +164,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": null,
"metadata": { "metadata": {
"scrolled": true "scrolled": true
}, },
"outputs": [ "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"
]
}
],
"source": [ "source": [
"res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds])\n", "res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds])\n",
"\n", "\n",

View File

@ -30,11 +30,11 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"from utils.load_data import load_data\n", "from utils.load_data import load_data\n",
"from utils.load_data import log\n",
"from utils.visualize_tsp import plotTSP\n", "from utils.visualize_tsp import plotTSP\n",
"from utils.tsp import TSP\n", "\n",
"from utils.tsp import TSP_Bench\n", "from tsp import TSP_Bench_ONE\n",
"from utils.tsp import TSP_Bench_ALL" "from tsp import TSP_Bench_PATH\n",
"from tsp import TSP_Bench_ALL"
] ]
}, },
{ {
@ -289,7 +289,7 @@
" \"\"\"\n", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -320,7 +320,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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", "\n",
" self.iteration = 0\n", " self.iteration = 0\n",
"\n", "\n",
" def init(self, nodes, *args):\n", " def init(self, nodes):\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n", " \n",
" T, stopping_temperature, alpha = args\n", " # Set hyper-parameters\n",
" T = -1\n",
" stopping_temperature = -1\n",
" alpha = 0.99\n",
"\n", "\n",
" self.T = math.sqrt(self.N) if T == -1 else T\n", " self.T = math.sqrt(self.N) if T == -1 else T\n",
" self.alpha = 0.995 if alpha == -1 else alpha\n", " self.alpha = 0.995 if alpha == -1 else alpha\n",
@ -409,7 +412,7 @@
" if random.random() < self.p_accept(candidate_fitness):\n", " if random.random() < self.p_accept(candidate_fitness):\n",
" self.cur_fitness, self.cur_solution = candidate_fitness, candidate\n", " self.cur_fitness, self.cur_solution = candidate_fitness, candidate\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Execute simulated annealing algorithm.\n", " Execute simulated annealing algorithm.\n",
" \"\"\"\n", " \"\"\"\n",
@ -444,18 +447,6 @@
"tsp_file = './template/data/simple/ulysses16.tsp'" "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", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -464,7 +455,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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", " Put your initialization here.\n",
" \"\"\"\n", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n",
" self.log(\"Nothing to initialize in your model now\")\n", " self.log(\"Nothing to initialize in your model now\")\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
" self.log(\"Naive Random Solution\")\n",
" self.best_solution = np.random.permutation(self.N).tolist()\n", " self.best_solution = np.random.permutation(self.N).tolist()\n",
" self.fitness_list.append(self.fitness(self.best_solution))\n", " self.fitness_list.append(self.fitness(self.best_solution))\n",
"\n", "\n",
@ -541,7 +532,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", " if(files):\n",
" for f in files:\n", " for f in files:\n",
" print(str(root) + \"/\" + f)" " print(str(root) + \"/\" + f)"
@ -577,15 +569,6 @@
" fig.suptitle(title, fontsize=20)" " fig.suptitle(title, fontsize=20)"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tsp_path = './template/data'"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -605,18 +588,6 @@
"plot_results(best_solutions, times, \"Random Model\")" "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", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -626,7 +597,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"print(\"Simulated Annealing\")\n", "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", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.3" "version": "3.8.5"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -30,11 +30,11 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"from utils.load_data import load_data\n", "from utils.load_data import load_data\n",
"from utils.load_data import log\n",
"from utils.visualize_tsp import plotTSP\n", "from utils.visualize_tsp import plotTSP\n",
"from utils.tsp import TSP\n", "\n",
"from utils.tsp import TSP_Bench\n", "from tsp import TSP_Bench_ONE\n",
"from utils.tsp import TSP_Bench_ALL" "from tsp import TSP_Bench_PATH\n",
"from tsp import TSP_Bench_ALL"
] ]
}, },
{ {
@ -289,7 +289,7 @@
" \"\"\"\n", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -320,7 +320,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", " self.distance += self.edges[self.tour[i]][self.tour[(i + 1) % self.num_nodes]].weight\n",
" return self.distance\n", " return self.distance\n",
"\n", "\n",
" def init(self, nodes, *args):\n", " def init(self, nodes):\n",
" super().init(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", "\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.colony_size = colony_size\n",
" self.elitist_weight = elitist_weight\n", " self.elitist_weight = elitist_weight\n",
" self.min_scaling_factor = min_scaling_factor\n", " self.min_scaling_factor = min_scaling_factor\n",
@ -439,7 +450,7 @@
"\n", "\n",
" def _acs(self, max_it):\n", " def _acs(self, max_it):\n",
" for step in range(0, 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", " for ant in self.ants:\n",
" self._add_pheromone(ant.find_tour(), ant.get_distance())\n", " self._add_pheromone(ant.find_tour(), ant.get_distance())\n",
" if ant.distance < self.global_best_distance:\n", " if ant.distance < self.global_best_distance:\n",
@ -452,7 +463,7 @@
"\n", "\n",
" def _elitist(self, max_it):\n", " def _elitist(self, max_it):\n",
" for step in range(0, 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", " for ant in self.ants:\n",
" self._add_pheromone(ant.find_tour(), ant.get_distance())\n", " self._add_pheromone(ant.find_tour(), ant.get_distance())\n",
" if ant.distance < self.global_best_distance:\n", " if ant.distance < self.global_best_distance:\n",
@ -475,8 +486,8 @@
" self._elitist(max_it)\n", " self._elitist(max_it)\n",
" else:\n", " else:\n",
" print(\"Un supported\")\n", " print(\"Un supported\")\n",
"# print('Sequence : <- {0} ->'.format(' - '.join(str(self.labels[i]) for i in self.global_best_tour)))\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('Total distance travelled to complete the tour : {0}\\n'.format(round(self.global_best_distance, 2)))\n",
"\n", "\n",
" return self.global_best_tour, self.fitness_list" " return self.global_best_tour, self.fitness_list"
] ]
@ -490,25 +501,6 @@
"tsp_file = './template/data/simple/ulysses16.tsp'" "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", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -517,7 +509,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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", " Put your initialization here.\n",
" \"\"\"\n", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n",
" self.log(\"Nothing to initialize in your model now\")\n", " self.log(\"Nothing to initialize in your model now\")\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
" self.log(\"Naive Random Solution\")\n",
" self.best_solution = np.random.permutation(self.N).tolist()\n", " self.best_solution = np.random.permutation(self.N).tolist()\n",
" self.fitness_list.append(self.fitness(self.best_solution))\n", " self.fitness_list.append(self.fitness(self.best_solution))\n",
"\n", "\n",
@ -583,7 +575,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", " if(files):\n",
" for f in files:\n", " for f in files:\n",
" print(str(root) + \"/\" + f)" " print(str(root) + \"/\" + f)"
@ -619,15 +612,6 @@
" fig.suptitle(title, fontsize=20)" " fig.suptitle(title, fontsize=20)"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tsp_path = './template/data'"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -647,25 +631,6 @@
"plot_results(best_solutions, times, \"Random Model\")" "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", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -673,7 +638,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"print(\"Ant Colony Optimization\")\n", "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", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.3" "version": "3.8.5"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -30,11 +30,11 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"from utils.load_data import load_data\n", "from utils.load_data import load_data\n",
"from utils.load_data import log\n",
"from utils.visualize_tsp import plotTSP\n", "from utils.visualize_tsp import plotTSP\n",
"from utils.tsp import TSP\n", "\n",
"from utils.tsp import TSP_Bench\n", "from tsp import TSP_Bench_ONE\n",
"from utils.tsp import TSP_Bench_ALL" "from tsp import TSP_Bench_PATH\n",
"from tsp import TSP_Bench_ALL"
] ]
}, },
{ {
@ -289,7 +289,7 @@
" \"\"\"\n", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
@ -320,7 +320,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", "class Gene: # City\n",
" def __init__(self, name, lat, lng):\n", " def __init__(self, name, lat, lng):\n",
" self.name = name\n", " self.name = name\n",
@ -417,19 +422,7 @@
" if route.fitness > fittest.fitness:\n", " if route.fitness > fittest.fitness:\n",
" fittest = route\n", " fittest = route\n",
"\n", "\n",
" return fittest" " return fittest\n",
]
},
{
"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",
"\n", "\n",
"class MyGAModel(Model):\n", "class MyGAModel(Model):\n",
" def __init__(self):\n", " def __init__(self):\n",
@ -495,7 +488,7 @@
" def selection(self, population, competitors_n):\n", " def selection(self, population, competitors_n):\n",
" return Population(sample(population.individuals, competitors_n)).get_fittest()\n", " return Population(sample(population.individuals, competitors_n)).get_fittest()\n",
"\n", "\n",
" def fit(self, max_it=20):\n", " def fit(self, max_it=100):\n",
" \"\"\"\n", " \"\"\"\n",
" Execute simulated annealing algorithm.\n", " Execute simulated annealing algorithm.\n",
" \"\"\"\n", " \"\"\"\n",
@ -543,7 +536,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"print(\"Genetic Algorithm\")\n", "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", " Put your initialization here.\n",
" \"\"\"\n", " \"\"\"\n",
" super().init(nodes)\n", " super().init(nodes)\n",
"\n",
" self.log(\"Nothing to initialize in your model now\")\n", " self.log(\"Nothing to initialize in your model now\")\n",
"\n", "\n",
" def fit(self, max_it):\n", " def fit(self, max_it=1000):\n",
" \"\"\"\n", " \"\"\"\n",
" Put your iteration process here.\n", " Put your iteration process here.\n",
" \"\"\"\n", " \"\"\"\n",
" self.log(\"Naive Random Solution\")\n",
" self.best_solution = np.random.permutation(self.N).tolist()\n", " self.best_solution = np.random.permutation(self.N).tolist()\n",
" self.fitness_list.append(self.fitness(self.best_solution))\n", " self.fitness_list.append(self.fitness(self.best_solution))\n",
"\n", "\n",
@ -639,7 +632,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", " if(files):\n",
" for f in files:\n", " for f in files:\n",
" print(str(root) + \"/\" + f)" " print(str(root) + \"/\" + f)"
@ -675,15 +669,6 @@
" fig.suptitle(title, fontsize=20)" " fig.suptitle(title, fontsize=20)"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tsp_path = './template/data/medium'"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -712,7 +697,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"print(\"Genetic Algorithm\")\n", "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\")" "plot_results(best_solutions, times, \"Genetic Algorithm\")"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -755,7 +733,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.3" "version": "3.8.5"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -4,8 +4,8 @@ docker build -t com2014-tsp .
## Windows ## 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 ## 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