def __init__(self, coords, temp, alpha, stopping_temp, stopping_iter): ''' animate the solution over time Parameters ---------- coords: array_like list of coordinates temp: float initial temperature alpha: float rate at which temp decreases stopping_temp: float temerature at which annealing process terminates stopping_iter: int interation at which annealing process terminates ''' self.coords = coords self.sample_size = len(coords) self.temp = temp self.alpha = alpha self.stopping_temp = stopping_temp self.stopping_iter = stopping_iter self.iteration = 1 self.dist_matrix = tsp_utils.vectorToDistMatrix(coords) self.curr_solution = tsp_utils.nearestNeighbourSolution( self.dist_matrix) self.best_solution = self.curr_solution self.solution_history = [self.curr_solution] self.curr_weight = self.weight(self.curr_solution) self.initial_weight = self.curr_weight self.min_weight = self.curr_weight self.weight_list = [self.curr_weight] self.acceptance_probability_list = [] self.temperature_list = [self.temp] self.random_solution = tsp_utils.randomSolution(self.dist_matrix) self.random_weight = self.weight(self.random_solution) print print "Random: ", self.random_weight print print "Greedy: ", self.curr_weight print
def __init__(self, adjacency_matrix, temp=-1, alpha=-1, stopping_temp=-1, stopping_iter=-1): ''' animate the solution over time Parameters ---------- coords: array_like list of coordinates temp: float initial temperature alpha: float rate at which temp decreases stopping_temp: float temerature at which annealing process terminates stopping_iter: int interation at which annealing process terminates ''' self.adjacency_matrix = adjacency_matrix self.sample_size = len(adjacency_matrix) self.temp = math.sqrt(self.sample_size) if temp == -1 else temp self.alpha = 0.995 if alpha == -1 else alpha self.stopping_temp = 1e-8 if stopping_temp == -1 else stopping_temp self.stopping_iter = 200000 if stopping_iter == -1 else stopping_iter self.iteration = 1 self.dist_matrix = adjacency_matrix self.curr_solution = tsp_utils.nearestNeighbourSolution( self.dist_matrix) self.best_solution = self.curr_solution self.solution_history = [self.curr_solution] self.curr_weight = self.weight(self.curr_solution) self.initial_weight = self.curr_weight self.min_weight = self.curr_weight self.weight_list = [self.curr_weight] print('Intial weight: ', self.curr_weight)