# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0


import numpy as np
import matplotlib.pyplot as plt
import os

from run_experiment_general import run_experiment

####################################################################################
### Script for creating the plots of the experiments of Section 6.1 / Appendix G ###
####################################################################################


### SET PARAMETERS ###
data_set = 'IndianLiver'  # possible choices: 'Adult', 'Drug', 'IndianLiver'
dist_function = 'euclidean'  # see https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html
# for possible choices; we used 'euclidean', 'cityblock' or 'chebyshev'
nr_runs = 25  # number of runs --- we report average results
k_range = np.hstack(([2, 5, 10, 15], np.arange(20, 105, 10)))  # range of k values
legend_ind = True  # if True, plot legend
########################


results_folder = 'Results/' + data_set

title_str = {'Adult': 'Adult data set (1000 points)',
             'Drug': 'Drug Consumption (1885 points)',
             'IndianLiver': 'Indian Liver Patient (579 points)'}[data_set]
save_str = {'Adult': 'AdultData', 'Drug': 'DrugData', 'IndianLiver': 'IndianLiver'}[data_set]
met_for_print = {'euclidean': 'Euclidean', 'cityblock': 'Manhattan', 'chebyshev': 'Chebyshev'}[dist_function]

print('Dataset=' + title_str + ', Distance function=' + met_for_print)

if (not os.path.exists(
        results_folder + '/' + save_str + '_NrReplicates=' + str(nr_runs) + '_distance=' + dist_function + '.npz')):
    run_experiment(data_set, dist_function, k_range, nr_runs)

with np.load(results_folder + '/' + save_str + '_NrReplicates=' + str(
        nr_runs) + '_distance=' + dist_function + '.npz') as data:
    n = data['n']
    nr_unf_random = data['nr_unf_random']
    nr_unf_kmeans = data['nr_unf_kmeans']
    nr_unf_single_linkage = data['nr_unf_single_linkage']
    nr_unf_average_linkage = data['nr_unf_average_linkage']
    nr_unf_complete_linkage = data['nr_unf_complete_linkage']
    nr_unf_kmedoids = data['nr_unf_kmedoids']
    nr_unf_single_linkage_GreedyPruning = data['nr_unf_single_linkage_GreedyPruning']
    nr_unf_average_linkage_GreedyPruning = data['nr_unf_average_linkage_GreedyPruning']
    nr_unf_complete_linkage_GreedyPruning = data['nr_unf_complete_linkage_GreedyPruning']
    nr_unf_single_linkage_GreedyPruning_criterion2 = data['nr_unf_single_linkage_GreedyPruning_criterion2']
    nr_unf_average_linkage_GreedyPruning_criterion2 = data['nr_unf_average_linkage_GreedyPruning_criterion2']
    nr_unf_complete_linkage_GreedyPruning_criterion2 = data['nr_unf_complete_linkage_GreedyPruning_criterion2']
    #
    maxviol_random = data['maxviol_random']
    maxviol_kmeans = data['maxviol_kmeans']
    maxviol_single_linkage = data['maxviol_single_linkage']
    maxviol_average_linkage = data['maxviol_average_linkage']
    maxviol_complete_linkage = data['maxviol_complete_linkage']
    maxviol_kmedoids = data['maxviol_kmedoids']
    maxviol_single_linkage_GreedyPruning = data['maxviol_single_linkage_GreedyPruning']
    maxviol_average_linkage_GreedyPruning = data['maxviol_average_linkage_GreedyPruning']
    maxviol_complete_linkage_GreedyPruning = data['maxviol_complete_linkage_GreedyPruning']
    maxviol_single_linkage_GreedyPruning_criterion2 = data['maxviol_single_linkage_GreedyPruning_criterion2']
    maxviol_average_linkage_GreedyPruning_criterion2 = data['maxviol_average_linkage_GreedyPruning_criterion2']
    maxviol_complete_linkage_GreedyPruning_criterion2 = data['maxviol_complete_linkage_GreedyPruning_criterion2']
    #
    meanviol_random = data['meanviol_random']
    meanviol_kmeans = data['meanviol_kmeans']
    meanviol_single_linkage = data['meanviol_single_linkage']
    meanviol_average_linkage = data['meanviol_average_linkage']
    meanviol_complete_linkage = data['meanviol_complete_linkage']
    meanviol_kmedoids = data['meanviol_kmedoids']
    meanviol_single_linkage_GreedyPruning = data['meanviol_single_linkage_GreedyPruning']
    meanviol_average_linkage_GreedyPruning = data['meanviol_average_linkage_GreedyPruning']
    meanviol_complete_linkage_GreedyPruning = data['meanviol_complete_linkage_GreedyPruning']
    meanviol_single_linkage_GreedyPruning_criterion2 = data['meanviol_single_linkage_GreedyPruning_criterion2']
    meanviol_average_linkage_GreedyPruning_criterion2 = data['meanviol_average_linkage_GreedyPruning_criterion2']
    meanviol_complete_linkage_GreedyPruning_criterion2 = data['meanviol_complete_linkage_GreedyPruning_criterion2']
    #
    cost_random = data['cost_random']
    cost_kmeans = data['cost_kmeans']
    cost_single_linkage = data['cost_single_linkage']
    cost_average_linkage = data['cost_average_linkage']
    cost_complete_linkage = data['cost_complete_linkage']
    cost_kmedoids = data['cost_kmedoids']
    cost_single_linkage_GreedyPruning = data['cost_single_linkage_GreedyPruning']
    cost_average_linkage_GreedyPruning = data['cost_average_linkage_GreedyPruning']
    cost_complete_linkage_GreedyPruning = data['cost_complete_linkage_GreedyPruning']
    cost_single_linkage_GreedyPruning_criterion2 = data['cost_single_linkage_GreedyPruning_criterion2']
    cost_average_linkage_GreedyPruning_criterion2 = data['cost_average_linkage_GreedyPruning_criterion2']
    cost_complete_linkage_GreedyPruning_criterion2 = data['cost_complete_linkage_GreedyPruning_criterion2']

linewidth = 2.8
markersize = 12

plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, np.mean(nr_unf_kmeans, axis=0), 'b*--', linewidth=linewidth, markersize=markersize, label='k-means++')
plt.plot(k_range, np.mean(nr_unf_kmedoids, axis=0), color='tab:brown', markersize=markersize, linestyle='dashed',
         marker='*', linewidth=linewidth, label='k-medoids')
# plt.plot(k_range, np.mean(nr_unf_kcenter, axis=0), 'r*--', linewidth=linewidth, markersize=markersize, label='k-center')
# plt.plot(k_range, np.mean(nr_unf_kcenter_FAIR,axis=0), color='tab:olive', markersize=markersize, linestyle='dashed',
#             marker='*', linewidth=linewidth, label='k-center GF')
# plt.plot(k_range, nr_unf_single_linkage, 'm*-.', linewidth=linewidth, markersize=markersize, label='Single link.')
plt.plot(k_range, nr_unf_average_linkage, 'c*-.', linewidth=linewidth, markersize=markersize, label='Avg. link.')
plt.plot(k_range, nr_unf_complete_linkage, color='tab:orange', markersize=markersize, linestyle='dashdot',
         marker='*', linewidth=linewidth, label='Compl. link.')

plt.xticks(k_range)
locs, labels = plt.yticks()
if data_set == 'Drug':
    plt.yticks(np.arange(0, locs[-1] + 1, 100))
else:
    plt.yticks(np.arange(0, locs[-1] + 1, 20))
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('#Uns', fontsize=20)

plt.savefig(results_folder + '/' + save_str + '_NrUns_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' + str(
    nr_runs) + '_distance=' +
            dist_function + '.png')
plt.close()

# ###################################################


plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, np.mean(maxviol_kmeans, axis=0), 'b*--', linewidth=linewidth, markersize=markersize,
         label='k-means++')
plt.plot(k_range, np.mean(maxviol_kmedoids, axis=0), color='tab:brown', markersize=markersize, linestyle='dashed',
         marker='*', linewidth=linewidth, label='k-medoids')
# plt.plot(k_range, np.mean(maxviol_kcenter, axis=0), 'r*--', linewidth=linewidth, markersize=markersize,label='k-center')
# plt.plot(k_range, np.mean(maxviol_kcenter_FAIR,axis=0), color='tab:olive', markersize=markersize, linestyle='dashed',
#             marker='*', linewidth=linewidth, label='k-center GF')
# plt.plot(k_range, maxviol_single_linkage, 'm*-.', linewidth=linewidth, markersize=markersize,label='Single link.')
plt.plot(k_range, maxviol_average_linkage, 'c*-.', linewidth=linewidth, markersize=markersize, label='Avg. link.')
plt.plot(k_range, maxviol_complete_linkage, color='tab:orange', markersize=markersize, linestyle='dashdot',
         marker='*', linewidth=linewidth, label='Compl. link.')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(max(0, locs[0] - 0.2), locs[-1] + 0.1, (locs[-1]) / 10))
bottom, top = plt.ylim()
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('MaxVi', fontsize=20)

plt.savefig(results_folder + '/' + save_str + '_MaxViol_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' + str(nr_runs) +
            '_distance=' + dist_function + '.png')
plt.close()

# ###################################################


plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, np.mean(meanviol_kmeans, axis=0), 'b*--', linewidth=linewidth, markersize=markersize,
         label='k-means++')
plt.plot(k_range, np.mean(meanviol_kmedoids, axis=0), color='tab:brown', markersize=markersize, linestyle='dashed',
         marker='*', linewidth=linewidth, label='k-medoids')
# plt.plot(k_range, np.mean(meanviol_kcenter, axis=0), 'r*--', linewidth=linewidth, markersize=markersize,label='k-center')
# plt.plot(k_range, np.mean(meanviol_kcenter_FAIR,axis=0), color='tab:olive', markersize=markersize, linestyle='dashed',
#             marker='*', linewidth=linewidth, label='k-center GF')
# plt.plot(k_range, meanviol_single_linkage, 'm*-.', linewidth=linewidth, markersize=markersize,label='Single link.')
plt.plot(k_range, meanviol_average_linkage, 'c*-.', linewidth=linewidth, markersize=markersize, label='Avg. link.')
plt.plot(k_range, meanviol_complete_linkage, color='tab:orange', markersize=markersize, linestyle='dashdot',
         marker='*', linewidth=linewidth, label='Compl. link.')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(max(0, locs[0] - 0.2), locs[-1] + 0.1, (locs[-1]) / 10))
bottom, top = plt.ylim()
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('MeanVi', fontsize=20)

plt.savefig(results_folder + '/' + save_str + '_MeanViol_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' + str(nr_runs) +
            '_distance=' + dist_function + '.png')
plt.close()

# ###################################################


plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, np.log10(np.mean(cost_kmeans, axis=0)), 'b*--', linewidth=linewidth, markersize=markersize,
         label='k-means++')
plt.plot(k_range, np.log10(np.mean(cost_kmedoids, axis=0)), color='tab:brown', markersize=markersize,
         linestyle='dashed',
         marker='*', linewidth=linewidth, label='k-medoids')
# plt.plot(k_range, np.log10( np.mean(cost_kcenter, axis=0)), 'r*--',linewidth=linewidth,markersize=markersize,label='k-center')
# plt.plot(k_range, np.log10(np.mean(cost_kcenter_FAIR,axis=0)), color='tab:olive', markersize=markersize,
#               linestyle='dashed', marker='*', linewidth=linewidth, label='k-center GF')
# plt.plot(k_range, cost_single_linkage, 'm*-.', linewidth=linewidth,markersize=markersize, label='Single link.')
plt.plot(k_range, np.log10(cost_average_linkage), 'c*-.', linewidth=linewidth, markersize=markersize,
         label='Avg. link.')
plt.plot(k_range, np.log10(cost_complete_linkage), color='tab:orange', markersize=markersize, linestyle='dashdot',
         marker='*', linewidth=linewidth, label='Compl. link.')

if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('Co', fontsize=20)
plt.ticklabel_format(style='plain')

plt.savefig(results_folder + '/' + save_str + '_Cost_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' + str(nr_runs) +
            '_distance=' + dist_function + '.png')
plt.close()

# ###################################################


plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, nr_unf_single_linkage, 'm*-.', linewidth=linewidth, markersize=markersize, label='Single link.')
plt.plot(k_range, nr_unf_single_linkage_GreedyPruning, 'mo--', linewidth=linewidth, markersize=markersize,
         label='Single #U')
plt.plot(k_range, nr_unf_single_linkage_GreedyPruning_criterion2, 'mD:', markersize=markersize, linewidth=linewidth,
         label='Single MV')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(0, locs[-1] + 1, 20))
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('#Uns', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_NrUns_SingleLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, nr_unf_average_linkage, 'c*-.', linewidth=linewidth, markersize=markersize, label='Avg. link.')
plt.plot(k_range, nr_unf_average_linkage_GreedyPruning, 'co--', linewidth=linewidth, markersize=markersize,
         label='Avg. #U')
plt.plot(k_range, nr_unf_average_linkage_GreedyPruning_criterion2, 'cD:', markersize=markersize, linewidth=linewidth,
         label='Avg. MV')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(0, locs[-1] + 1, 20))
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('#Uns', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_NrUns_AverageLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, nr_unf_complete_linkage, color='tab:orange', markersize=markersize, linestyle='dashdot',
         marker='*', linewidth=linewidth, label='Compl. link.')
plt.plot(k_range, nr_unf_complete_linkage_GreedyPruning, color='tab:orange', markersize=markersize, linestyle='dashed',
         marker='o', linewidth=linewidth, label='Compl. #U')
plt.plot(k_range, nr_unf_complete_linkage_GreedyPruning_criterion2, color='tab:orange', markersize=markersize,
         linestyle='dotted',
         marker='D', linewidth=linewidth, label='Compl. MV')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(0, locs[-1] + 1, 20))
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('#Uns', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_NrUns_CompleteLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

# ################################################


plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, maxviol_single_linkage, 'm*-.', linewidth=linewidth, markersize=markersize, label='Single link.')
plt.plot(k_range, maxviol_single_linkage_GreedyPruning, 'mo--', linewidth=linewidth, markersize=markersize,
         label='Single #U')
plt.plot(k_range, maxviol_single_linkage_GreedyPruning_criterion2, 'mD:', markersize=markersize, linewidth=linewidth,
         label='Single MV')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(1.0, locs[-1] + 0.1, (locs[-1] - 1) / 10.))
bottom, top = plt.ylim()
plt.ylim((0.8, top))
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('MaxVi', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_MaxViol_SingleLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, maxviol_average_linkage, 'c*-.', linewidth=linewidth, markersize=markersize, label='Avg. link.')
plt.plot(k_range, maxviol_average_linkage_GreedyPruning, 'co--', linewidth=linewidth, markersize=markersize,
         label='Avg. #U')
plt.plot(k_range, maxviol_average_linkage_GreedyPruning_criterion2, 'cD:', markersize=markersize, linewidth=linewidth,
         label='Avg. MV')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(1.0, locs[-1] + 0.1, (locs[-1] - 1) / 10.))
bottom, top = plt.ylim()
plt.ylim((0.8, top))
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('MaxVi', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_MaxViol_AverageLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, maxviol_complete_linkage, color='tab:orange', markersize=markersize, linestyle='dashdot',
         marker='*', linewidth=linewidth, label='Compl. link.')
plt.plot(k_range, maxviol_complete_linkage_GreedyPruning, color='tab:orange', markersize=markersize, linestyle='dashed',
         marker='o', linewidth=linewidth, label='Compl. #U')
plt.plot(k_range, maxviol_complete_linkage_GreedyPruning_criterion2, color='tab:orange', markersize=markersize,
         linestyle='dotted',
         marker='D', linewidth=linewidth, label='Compl. MV')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(1.0, locs[-1] + 0.1, (locs[-1] - 1) / 10.))
bottom, top = plt.ylim()
plt.ylim((0.8, top))
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('MaxVi', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_MaxViol_CompleteLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

# ################################################


plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, meanviol_single_linkage, 'm*-.', linewidth=linewidth, markersize=markersize, label='Single link.')
plt.plot(k_range, meanviol_single_linkage_GreedyPruning, 'mo--', linewidth=linewidth, markersize=markersize,
         label='Single #U')
plt.plot(k_range, meanviol_single_linkage_GreedyPruning_criterion2, 'mD:', markersize=markersize, linewidth=linewidth,
         label='Single MV')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(0, locs[-1] + 0.1, (locs[-1] / 10)))
bottom, top = plt.ylim()
plt.ylim((0., top))
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('MeanVi', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_Mean_SingleLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, meanviol_average_linkage, 'c*-.', linewidth=linewidth, markersize=markersize, label='Avg. link.')
plt.plot(k_range, meanviol_average_linkage_GreedyPruning, 'co--', linewidth=linewidth, markersize=markersize,
         label='Avg. #U')
plt.plot(k_range, meanviol_average_linkage_GreedyPruning_criterion2, 'cD:', markersize=markersize, linewidth=linewidth,
         label='Avg. MV')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(0, locs[-1] + 0.1, (locs[-1]) / 10.))
bottom, top = plt.ylim()  # return the current ylim
plt.ylim((0, top))
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('MeanVi', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_Mean_AverageLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, meanviol_complete_linkage, color='tab:orange', markersize=markersize, linestyle='dashdot',
         marker='*', linewidth=linewidth, label='Compl. link.')
plt.plot(k_range, meanviol_complete_linkage_GreedyPruning, color='tab:orange', markersize=markersize,
         linestyle='dashed',
         marker='o', linewidth=linewidth, label='Compl. #U')
plt.plot(k_range, meanviol_complete_linkage_GreedyPruning_criterion2, color='tab:orange', markersize=markersize,
         linestyle='dotted', marker='D', linewidth=linewidth, label='Compl. MV')

plt.xticks(k_range)
locs, labels = plt.yticks()
plt.yticks(np.arange(0, locs[-1] + 0.1, (locs[-1]) / 10.))
bottom, top = plt.ylim()
plt.ylim((0., top))
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('MeanVi', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_Mean_CompleteLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

# ################################################


plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, np.log10(cost_single_linkage), 'm*-.', linewidth=linewidth, markersize=markersize,
         label='Single link.')
plt.plot(k_range, np.log10(cost_single_linkage_GreedyPruning), 'mo--', linewidth=linewidth, markersize=markersize,
         label='Single #U')
plt.plot(k_range, np.log10(cost_single_linkage_GreedyPruning_criterion2), 'mD:', markersize=markersize,
         linewidth=linewidth,
         label='Single MV')

plt.xticks(k_range)
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('Co', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_Cost_SingleLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, np.log10(cost_average_linkage), 'c*-.', linewidth=linewidth, markersize=markersize,
         label='Avg. link.')
plt.plot(k_range, np.log10(cost_average_linkage_GreedyPruning), 'co--', linewidth=linewidth, markersize=markersize,
         label='Avg. #U')
plt.plot(k_range, np.log10(cost_average_linkage_GreedyPruning_criterion2), 'cD:', markersize=markersize,
         linewidth=linewidth,
         label='Avg. MV')

plt.xticks(k_range)
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('Co', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_Cost_AverageLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()

plt.figure(constrained_layout=True, figsize=(6.5, 3.5))
plt.plot(k_range, np.log10(cost_complete_linkage), color='tab:orange', markersize=markersize, linestyle='dashdot',
         marker='*', linewidth=linewidth, label='Compl. link.')
plt.plot(k_range, np.log10(cost_complete_linkage_GreedyPruning), color='tab:orange', markersize=markersize,
         linestyle='dashed', marker='o', linewidth=linewidth, label='Compl. #U')
plt.plot(k_range, np.log10(cost_complete_linkage_GreedyPruning_criterion2), color='tab:orange', markersize=markersize,
         linestyle='dotted', marker='D', linewidth=linewidth, label='Compl. MV')

plt.xticks(k_range)
if legend_ind == True:
    plt.legend(loc='best', fontsize=13)
plt.title(title_str + ' --- ' + met_for_print + ' metric', fontsize=14)
plt.xlabel('k', fontsize=20)
plt.ylabel('Co', fontsize=20)

plt.savefig(
    results_folder + '/' + save_str + 'HEURISTICS_Cost_CompleteLink_AsFunctionOfk_n=' + str(n) + '_NrReplicates=' +
    str(nr_runs) + '_distance=' + dist_function + '.png')
plt.close()
