-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptcontestB5.py
More file actions
103 lines (76 loc) · 3.79 KB
/
optcontestB5.py
File metadata and controls
103 lines (76 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import sys
import datetime
from Annealing import *
from Evolutive import *
from RandomAlgorithm import *
from operator import itemgetter
def show_results(n_runs, cores, p_e):
res = []
for _ in range(n_runs):
res.append(p_e(cores, 25))
print(f"\tMean: {sum(res)/len(res)}")
print(f"\tBest: {min(res)}")
def evolutive_worker(params):
mutation, ini_sol, time_, data_ = params[1]
pop_ = Population(len(ini_sol[0]), 50, data_, ini_sol)
return evolutive_algorithm(data_, pop_,
time_=time_, elite_size=1, mut_ratio=mutation, diversify_size=0, not_improving_limit=False,
sel_f=median_selection,
elite_f=get_elite,
rep_f=ox_reproduction,
mut_f=mutate)
def parallel_evolutive(n_processes, mutation, ini_sol, time_, data_):
p = Pool(processes=n_processes)
params = mutation, ini_sol, time_, data_
results_ = p.map(evolutive_worker, [(i, params) for i in range(n_processes)])
solutions, fmeds = zip(*results_)
p.close()
idx_best = min(enumerate(fmeds), key=itemgetter(1))[0]
return results_[idx_best]
# def annealing_worker(params):
# tries, t_ini_factor, alpha, initial_solution, time_, data_ = params
# return simulated_annealing(tries, t_ini_factor, alpha, initial_solution, time_, data_)
def annealing_worker(params):
tries, t_ini_factor, alpha, initial_solution, time_, data_ = params
presolution, presolution_fmed = simulated_annealing(tries, t_ini_factor, alpha, initial_solution, time_, data_)
solution, solution_fmed = local_best_search(presolution, presolution_fmed, data_, 60-time_) # El último parámetro es el tiempo de búsqueda local
#print("Prelocal: ", presolution_fmed, "Postlocal:", solution_fmed)
return solution, solution_fmed
def parallel_annealing(n_processes, tries, t_ini_factor, alpha, time_, data_):
p = Pool(processes=n_processes)
params = []
for process in range(n_processes):
params.append([tries, t_ini_factor, alpha, create_random_solution(len(data_)), time_, data_])
results_ = p.map(annealing_worker, params)
solutions, fmeds = zip(*results_)
# print(fmeds)
# print("Mean:", np.mean(fmeds))
# print("Min:", np.min(fmeds))
# print(tries, t_ini_factor, alpha, file=open("output.txt", "a"))
# print("", file=open("output.txt", "a"))
p.close()
idx_best = min(enumerate(fmeds), key=itemgetter(1))[0]
return results_[idx_best]
def test_params():
t_factor = [0.1, 0.5, 0.9]
alpha_list = [1, 10, 20]
n_neighbours_list = [3, 5, 10]
n_tries_list = [5, 50]
times = [(60, 0), (50, 10), (30, 30)]
searches_list = [local_best_search, local_best_first_search]
for t_factor, alpha, n_neighbours, n_tries, times_, search_method in list(itertools.product(t_factor, alpha_list, n_neighbours_list, n_tries_list, times, searches_list)):
print("t_factor, alpha, n_neighbours, n_tries, times_, search_method: ", t_factor, alpha, n_neighbours, n_tries, times_, search_method, file=open("output.txt", "a"))
annealing_results = parallel_annealing(3, n_tries, t_factor, alpha, n_neighbours, times_[0])
searcb_result = local_best_first_search(*annealing_results, read_file("Datasets/Doc11.txt"), times_[1])
print(annealing_results, file=open("output.txt", "a"))
print(searcb_result, file=open("output.txt", "a"))
print("", file=open("output.txt", "a"))
if __name__ == '__main__':
time_ini = time.time()
file_path = sys.argv[1]
data = read_file(file_path)
solution, solution_fmed = parallel_annealing(4, 10, 1000, 0.5, 53, data)
time_fin = time.time()
print("Best fmed:", solution_fmed)
print("Execution time:", time_fin-time_ini)
print("Best solution:", solution)