lab 2 WIP
This commit is contained in:
parent
af0a5c7e4e
commit
6947aaacb1
5 changed files with 26 additions and 77 deletions
|
@ -9,7 +9,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## First Tutorial\n",
|
"## First Lab\n",
|
||||||
"\n",
|
"\n",
|
||||||
"What we are going to do today:\n",
|
"What we are going to do today:\n",
|
||||||
"- read TSP data\n",
|
"- read TSP data\n",
|
||||||
|
|
|
@ -1,60 +0,0 @@
|
||||||
{
|
|
||||||
"cells": [
|
|
||||||
{
|
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"## First Lecture\n",
|
|
||||||
"What we are going to do today:\n",
|
|
||||||
"- read TSP data\n",
|
|
||||||
"- store nodes in data structure\n",
|
|
||||||
"- plot raw data\n",
|
|
||||||
"- define distance function\n",
|
|
||||||
"- create distance matrix\n",
|
|
||||||
"- generate naive solution \n",
|
|
||||||
"- check if the solution is valid\n",
|
|
||||||
"- evaluate solution!"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"source": [
|
|
||||||
"### Read TSP data\n"
|
|
||||||
],
|
|
||||||
"metadata": {
|
|
||||||
"collapsed": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"### Read TSP data\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"metadata": {
|
|
||||||
"kernelspec": {
|
|
||||||
"display_name": "Python 3",
|
|
||||||
"language": "python",
|
|
||||||
"name": "python3"
|
|
||||||
},
|
|
||||||
"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
|
|
||||||
}
|
|
6
run.py
6
run.py
|
@ -1,7 +1,7 @@
|
||||||
import glob
|
import glob
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from src.io_tsp import ProblemInstance
|
from src.io_tsp import ProblemInstance
|
||||||
from src.TSP_solver import SolverTSP, available_improvers, available_solvers
|
from src.TSP_solver import TSPSolver, available_improvers, available_solvers
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ def run(show_plots=False, verbose=False):
|
||||||
|
|
||||||
for solver_name in solvers_names:
|
for solver_name in solvers_names:
|
||||||
for improve in improvers_names:
|
for improve in improvers_names:
|
||||||
solver = SolverTSP(solver_name, prob_instance)
|
solver = TSPSolver(solver_name, prob_instance)
|
||||||
use_solver_to_compute_solution(solver, improve, index, results, problem_path, verbose, show_plots)
|
use_solver_to_compute_solution(solver, improve, index, results, problem_path, verbose, show_plots)
|
||||||
for improve2 in [j for j in improvers_names if j not in [improve]]:
|
for improve2 in [j for j in improvers_names if j not in [improve]]:
|
||||||
use_solver_to_compute_solution(solver, improve2, index, results, problem_path, verbose, show_plots)
|
use_solver_to_compute_solution(solver, improve2, index, results, problem_path, verbose, show_plots)
|
||||||
|
@ -51,7 +51,7 @@ def run(show_plots=False, verbose=False):
|
||||||
solver.pop()
|
solver.pop()
|
||||||
|
|
||||||
if prob_instance.exist_opt and show_plots:
|
if prob_instance.exist_opt and show_plots:
|
||||||
solver = SolverTSP("optimal", prob_instance)
|
solver = TSPSolver("optimal", prob_instance)
|
||||||
solver.solved = True
|
solver.solved = True
|
||||||
solver.solution = np.concatenate([prob_instance.optimal_tour, [prob_instance.optimal_tour[0]]])
|
solver.solution = np.concatenate([prob_instance.optimal_tour, [prob_instance.optimal_tour[0]]])
|
||||||
solver.plot_solution()
|
solver.plot_solution()
|
||||||
|
|
|
@ -18,9 +18,15 @@ available_improvers = {"2-opt": loop2opt,
|
||||||
"simulated_annealing": sa}
|
"simulated_annealing": sa}
|
||||||
|
|
||||||
|
|
||||||
class SolverTSP:
|
class TSPSolver:
|
||||||
def __init__(self, algorithm_name, problem_instance):
|
def __init__(self, algorithm_name, problem_instance, passed_avail_solvers=None, passed_avail_improvers=None):
|
||||||
# assert algorithm_name in available_solvers, f"the {algorithm_name} initializer is not available currently."
|
# assert algorithm_name in available_solvers, f"the {algorithm_name} initializer is not available currently."
|
||||||
|
if passed_avail_improvers is None:
|
||||||
|
passed_avail_improvers = available_improvers
|
||||||
|
if passed_avail_solvers is None:
|
||||||
|
passed_avail_solvers = available_solvers
|
||||||
|
self.available_improvers = passed_avail_improvers
|
||||||
|
self.available_solvers = passed_avail_solvers
|
||||||
self.duration = np.inf
|
self.duration = np.inf
|
||||||
self.found_length = np.inf
|
self.found_length = np.inf
|
||||||
self.algorithm_name = algorithm_name
|
self.algorithm_name = algorithm_name
|
||||||
|
@ -31,7 +37,7 @@ class SolverTSP:
|
||||||
self.solution = None
|
self.solution = None
|
||||||
|
|
||||||
def bind(self, local_or_meta):
|
def bind(self, local_or_meta):
|
||||||
assert local_or_meta in available_improvers, f"the {local_or_meta} method is not available currently."
|
assert local_or_meta in self.available_improvers, f"the {local_or_meta} method is not available currently."
|
||||||
self.algorithms.append(local_or_meta)
|
self.algorithms.append(local_or_meta)
|
||||||
self.name_method += ", improved with " + local_or_meta
|
self.name_method += ", improved with " + local_or_meta
|
||||||
|
|
||||||
|
@ -45,11 +51,11 @@ class SolverTSP:
|
||||||
if verbose:
|
if verbose:
|
||||||
print(f"### solving with {self.algorithms} ####")
|
print(f"### solving with {self.algorithms} ####")
|
||||||
start_time = t()
|
start_time = t()
|
||||||
self.solution = available_solvers[self.algorithms[0]](self.problem_instance)
|
self.solution = self.available_solvers[self.algorithms[0]](self.problem_instance)
|
||||||
assert self.check_if_solution_is_valid(self.solution), "Error the solution is not valid"
|
assert self.check_if_solution_is_valid(), "Error the solution is not valid"
|
||||||
for i in range(1, len(self.algorithms)):
|
for i in range(1, len(self.algorithms)):
|
||||||
self.solution = available_improvers[self.algorithms[i]](self.solution, self.problem_instance)
|
self.solution = self.available_improvers[self.algorithms[i]](self.solution, self.problem_instance)
|
||||||
assert self.check_if_solution_is_valid(self.solution), "Error the solution is not valid"
|
assert self.check_if_solution_is_valid(), "Error the solution is not valid"
|
||||||
|
|
||||||
end_time = t()
|
end_time = t()
|
||||||
self.duration = np.around(end_time - start_time, 3)
|
self.duration = np.around(end_time - start_time, 3)
|
||||||
|
@ -68,11 +74,11 @@ class SolverTSP:
|
||||||
plt.plot(ordered_points[:, 1], ordered_points[:, 2], 'b-')
|
plt.plot(ordered_points[:, 1], ordered_points[:, 2], 'b-')
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
def check_if_solution_is_valid(self, solution):
|
def check_if_solution_is_valid(self):
|
||||||
# rights_values = np.sum(
|
# rights_values = np.sum(
|
||||||
# [self.check_validation(i, solution[:-1]) for i in np.arange(self.problem_instance.nPoints)])
|
# [self.check_validation(i, solution[:-1]) for i in np.arange(self.problem_instance.nPoints)])
|
||||||
rights_values = np.sum(
|
rights_values = np.sum(
|
||||||
[1 if np.sum(solution[:-1] == i) == 1 else 0 for i in np.arange(self.problem_instance.nPoints)])
|
[1 if np.sum(self.solution[:-1] == i) == 1 else 0 for i in np.arange(self.problem_instance.nPoints)])
|
||||||
return rights_values == self.problem_instance.nPoints
|
return rights_values == self.problem_instance.nPoints
|
||||||
|
|
||||||
# def check_validation(self, node, solution):
|
# def check_validation(self, node, solution):
|
||||||
|
|
|
@ -10,8 +10,10 @@ class ProblemInstance:
|
||||||
self.exist_opt = False
|
self.exist_opt = False
|
||||||
self.optimal_tour = None
|
self.optimal_tour = None
|
||||||
self.dist_matrix = None
|
self.dist_matrix = None
|
||||||
|
|
||||||
# read raw data
|
# read raw data
|
||||||
file_object = open(name_tsp)
|
self.file_name = name_tsp
|
||||||
|
file_object = open(self.file_name)
|
||||||
data = file_object.read()
|
data = file_object.read()
|
||||||
file_object.close()
|
file_object.close()
|
||||||
self.lines = data.splitlines()
|
self.lines = data.splitlines()
|
||||||
|
@ -30,9 +32,10 @@ class ProblemInstance:
|
||||||
self.points[i, 2] = line_i[2]
|
self.points[i, 2] = line_i[2]
|
||||||
|
|
||||||
self.create_dist_matrix()
|
self.create_dist_matrix()
|
||||||
if name_tsp in ["./problems/eil76.tsp", "./problems/kroA100.tsp"]:
|
if self.file_name in ["./problems/eil76.tsp", "./problems/kroA100.tsp", "../problems/eil76.tsp",
|
||||||
|
"../problems/kroA100.tsp"]:
|
||||||
self.exist_opt = True
|
self.exist_opt = True
|
||||||
file_object = open(name_tsp.replace(".tsp", ".opt.tour"))
|
file_object = open(self.file_name.replace(".tsp", ".opt.tour"))
|
||||||
data = file_object.read()
|
data = file_object.read()
|
||||||
file_object.close()
|
file_object.close()
|
||||||
lines = data.splitlines()
|
lines = data.splitlines()
|
||||||
|
@ -50,7 +53,7 @@ class ProblemInstance:
|
||||||
print('best_sol: ' + str(self.best_sol))
|
print('best_sol: ' + str(self.best_sol))
|
||||||
print('exist optimal: ' + str(self.exist_opt))
|
print('exist optimal: ' + str(self.exist_opt))
|
||||||
|
|
||||||
def plot_data(self,show_numbers=False):
|
def plot_data(self, show_numbers=False):
|
||||||
plt.figure(figsize=(8, 8))
|
plt.figure(figsize=(8, 8))
|
||||||
plt.title(self.name)
|
plt.title(self.name)
|
||||||
plt.scatter(self.points[:, 1], self.points[:, 2])
|
plt.scatter(self.points[:, 1], self.points[:, 2])
|
||||||
|
|
Reference in a new issue