def testingILS(original_matrix, original_samples): bestResult = np.inf bestTime = 0 bestGroup = [] startGroup = [] regretGroups = [] random_groups = [] random_groups = greedMethod(20, original_matrix, original_samples) regretGroups = steepest_cashe_and_list(random_groups, original_matrix) for i in range(1000): print("START", i) start = time.time() random_groups = small_perturbate(regretGroups, 10) regretGroups = steepest_cashe_and_list(random_groups, original_matrix) elapsedTime = time.time() - start print(str(elapsedTime) + " sec") result, _, _ = count_result(regretGroups, original_matrix) print(str(result) + " result") if bestResult > result: bestResult = result bestTime = elapsedTime bestGroup = regretGroups.copy() startGroup = random_groups.copy() print("BEST") print(bestTime) print(bestResult) print("BEST") return bestGroup, startGroup
def compare_pairs_with_the_best(original_matrix, original_samples): bestResult = np.inf bestGroup = [] results = [] result_groups = [] similarity_with_best = [] similarity_with_others = [] for i in range(500): print("START", i) regretGroups = [] random_groups = [] random_groups = greedMethod(20, original_matrix, original_samples) #regretGroups = greedy(random_groups.copy(), original_matrix) regretGroups = steepest_cashe_and_list(random_groups, original_matrix) result, _, _ = count_result(regretGroups, original_matrix) print(str(result) + " result") results.append(result) result_groups.append(regretGroups) if bestResult > result: bestResult = result bestGroup = regretGroups.copy() best_id = results.index(bestResult) result_groups.pop(best_id) results.pop(best_id) for group in result_groups: similarity_with_best.append(compare_pairs2(group, bestGroup)) for i in range(len(result_groups)): for j in range(i + 1, len(result_groups)): result += compare_pairs2(result_groups[i], result_groups[j]) result = result / (len(result_groups) - 1) similarity_with_others.append(result) print(similarity_with_best, similarity_with_others, results) with_best_perason = scipy.stats.pearsonr(results, similarity_with_best) print(with_best_perason) with_others_pearson = scipy.stats.pearsonr(results, similarity_with_others) print(with_others_pearson) plt.plot(results, similarity_with_best, 'ro') plt.title("Podobieństwo względem najlepszego optimum lokalnego") plt.xlabel("Wartość funkcji celu") plt.ylabel("Podobieństwo") plt.savefig('similarity_with_best.png') plt.show() plt.title("Średnie podobieństwo względem pozostałych optimów lokalnych") plt.xlabel("Wartość funkcji celu") plt.ylabel("Podobieństwo") plt.plot(results, similarity_with_others, 'bo') plt.savefig('similarity_with_others.png') plt.show()
def testingMSLS(original_matrix, original_samples): best_times = [] best_groups = [] best_results = [] for j in range(10): bestResult = np.inf bestTime = 0 bestGroup = [] startGroup = [] regretGroups = [] random_groups = [] for i in range(100): print("START", j, i) random_groups = greedMethod(20, original_matrix, original_samples) start = time.time() regretGroups = steepest_cashe_and_list(random_groups, original_matrix) elapsedTime = time.time() - start print(str(elapsedTime) + " sec") result, _, _ = count_result(regretGroups, original_matrix) print(str(result) + " result") if bestResult > result: bestResult = result bestTime = elapsedTime bestGroup = regretGroups.copy() startGroup = random_groups.copy() print("BEST") print(bestTime) print(bestResult) print("BEST") best_times.append(bestTime) best_groups.append(bestGroup.copy()) best_results.append(bestResult) print("***RESULTS***") print('MIN: ', np.min(best_results)) print('MAX: ', np.max(best_results)) print('MEAN: ', np.average(best_results)) print('STD: ', np.std(best_results)) print("***TIME***") print('MIN: ', np.min(best_times)) print('MAX: ', np.max(best_times)) print('MEAN: ', np.average(best_times)) print('STD: ', np.std(best_times)) return bestGroup, startGroup
if __name__ == '__main__': no_groups = 20 fileName = "data/objects20_06.data" # fileName = "data/objects.data" # fileName = "data/test.data" # I read data from file samples = loadInstance(fileName) # II prepare coordinates data x_samples = [pair[0] for pair in samples] y_samples = [pair[1] for pair in samples] # III calculate matrix with euclidean distances matrix = prepareMatrix(samples) # IV Groups from first project - Greedy greed_groups = greedMethod(no_groups, matrix, samples) print(count_result(greed_groups, matrix)) #drawRegret(x_samples, y_samples, greed_groups) # V Start local search - steepest # steepest_groups = steepest(greed_groups, matrix) # drawRegret(x_samples, y_samples, steepest_groups) # print(count_result(steepest_groups, matrix)) # TESTING MSLS best, random = testingILS(matrix, samples) drawRegret(x_samples, y_samples, random) drawRegret(x_samples, y_samples, best)