def tabu_search(perm): solution = copy.deepcopy(perm) best_candidate = copy.deepcopy(perm) tabu_list = [] distances = [] no_of_neighbours = 10 #arbitrary starting value max_tabu_size = 20 #arbitrary starting value j = 0 #Adding starting value to the List tabu_list.append(solution) #Termination Criteria while ( j < 1000 ): #arbitrary starting value, the higher the number the better the solution i = 0 neighbours = [] for i in range( no_of_neighbours ): #Generates 10 different random solutions/neighbours which is used later to find the best generate_neighbour = generate_random_neighbour(best_candidate) neighbours.append(generate_neighbour) #Iterates through the neighbours to find the latest best solution for neighbour in neighbours: if (neighbour not in tabu_list and evaluate(neighbour) < evaluate(best_candidate)): best_candidate = neighbour #Evaluates the best solution in comparison to the current one if (evaluate(best_candidate) < evaluate(solution)): solution = best_candidate distances.append(evaluate(solution))
def hill_climbing(iteration): solution = [] neigh_solution = [] list_of_values = [] sol_distance = 0 neigh_distance = 0 #Initial random solution solution = generate_random_solution() #plot_colours(test_colours, solution, 'Initial Hill Climb') #Loop for specified number of iterations for i in range(iteration): #Evaluate total distance of solution sol_distance = evaluate(solution) #Generate random neighbour to current solution neigh_solution = generate_random_neighbour(solution) #Evaluate total distance of neighbour solution neigh_distance = evaluate(neigh_solution) #Update solution if neighbour is better if (neigh_distance < sol_distance): solution = copy.deepcopy(neigh_solution) list_of_values.append(neigh_distance) #Return a list of the distance values at each iteration, and the best solution found return solution, list_of_values
def multi_hc(tries): curr_solution = [] valList = [] curr_dist = 0 best_dist = 1000 #arbitrary starting value, just has to be greater than any possible distance best_solution = [] best_sol_list = [] dist_list = [] for i in range(tries): #Hill Climb curr_solution, valList = hill_climbing(500) #Calculate total distance of current solution curr_dist = evaluate(curr_solution) #Determine if new solution is better, if it is copy it into the 'best' variables if (curr_dist < best_dist): best_dist = curr_dist best_solution = copy.deepcopy(curr_solution) best_sol_list = copy.deepcopy(valList) dist_list.append(curr_dist) return dist_list, best_solution, best_dist, best_sol_list
parser = argparse.ArgumentParser() parser.add_argument('--max_samples', default=25000, type=int, help='maximum number of conversation pairs to use') parser.add_argument('--max_length', default=40, type=int, help='maximum sentence length') parser.add_argument('--batch_size', default=64, type=int) parser.add_argument('--num_layers', default=2, type=int) parser.add_argument('--num_units', default=512, type=int) parser.add_argument('--d_model', default=256, type=int) parser.add_argument('--num_heads', default=8, type=int) parser.add_argument('--dropout', default=0.1, type=float) parser.add_argument('--activation', default='relu', type=str) parser.add_argument('--epochs', default=20, type=int) hparams = parser.parse_args() dataset, tokenizer = get_dataset(hparams) model = transformer(hparams) model.load_weights('Test/cp.ckpt') model.save("Test\model") evaluate(hparams, model, tokenizer)