def run_test_min_edge(min_edge_weight, nb_clusters): # create instance instance = create_instance(min_edge_weight) separated_nodes = [ node for node in ['node_16', 'node_65', 'node_39', 'node_57', 'node_68'] if node in instance.get_all_nodes() ] min_cluster_weight = int(instance.total_exchanges() / nb_clusters * 0.25) # create model and solve model = LSModel(instance, nb_clusters=nb_clusters, separated_nodes=separated_nodes, min_cluster_weight=min_cluster_weight) model.solve() model.print_solution() instance.print_kpis() # save instance with solution instance.set_solution(model.solution) instance.save('instance_min_edge_weight_{}_w_{}_clusters.pickle'.format( min_edge_weight, nb_clusters)) # save a graphical representation of the solution display_graph( instance, 'solution_min_edge_weight_{}_w_{}_clusters.png'.format( min_edge_weight, nb_clusters))
def test_redraw_solutions(self): instances = {} for w in [1, 20]: # [1, 5, 10, 20]: instances[w] = {} for c in [5, 8]: instance_name = 'instance_min_w_{}_max_c_{}'.format(w, c) pickle_path = str(current_path) \ + '/../openbackend_clustering/resources/{}.pickle'.format(instance_name) parser = InstancePickleParser(pickle_path) instance = parser.create_instance() instance.compress_solution() instances[w][c] = instance logging.info('self = [w == 20] and [c == 8]') logging.info('other = [w == 20] and [c == 5]') instances[20][8].compare_solutions(instances[20][5]) logging.info('self = [w == 20] and [c == 8] ') logging.info('other = [w == 1] and [c == 8]') instances[20][8].compare_solutions(instances[1][8]) logging.info('self = [w == 20] and [c == 5]') logging.info('other = [w == 1] and [c == 5]') instances[20][5].compare_solutions(instances[1][5]) for w in [1, 20]: # [1, 5, 10, 20]: for c in [5, 8]: instance_name = 'instance_min_w_{}_max_c_{}'.format(w, c) logging.info('kpis for instance: {}'.format(instance_name)) instances[w][c].print_kpis() display_graph(instances[w][c], '{}.png'.format(instance_name), graph_rad=6 if w == 1 else 5)
def _experimentation_plan(self, separated_nodes_plan, nb_clusters_plan, optimize_min_cluster_weight_plan, min_cluster_weight_plan, ignore_plan, path, parser_class): plan_nb = 0 parser = parser_class(path) for separated_nodes, nb_clusters, optimize_min_cluster_weight, min_cluster_weight, ignore in \ itertools.product(separated_nodes_plan, nb_clusters_plan, optimize_min_cluster_weight_plan, min_cluster_weight_plan, ignore_plan): if separated_nodes and nb_clusters < 5: continue if optimize_min_cluster_weight and min_cluster_weight is not None: continue file_name = 'sep[{}]-nb[{}]-opt_min[{}]-min_w[{}]-ignore[{}]'.format( len(separated_nodes) != 0, nb_clusters, optimize_min_cluster_weight, min_cluster_weight, len(ignore)) instance = parser.create_instance(ignore=ignore) min_cluster_weight = None if min_cluster_weight is None \ else int(instance.total_exchanges() / nb_clusters * min_cluster_weight) plan_nb += 1 logging.info('plan nb: {}'.format(plan_nb)) logging.info(' - separated_nodes: {}'.format(separated_nodes)) logging.info(' - nb_clusters: {}'.format(nb_clusters)) logging.info(' - optimize_min_cluster_weight: {}'.format( optimize_min_cluster_weight)) logging.info( ' - min_cluster_weight: {}'.format(min_cluster_weight)) logging.info(' - ignore: {}'.format(ignore)) logging.info(' - file_name: {}'.format(file_name)) model = LSModel( instance, nb_clusters=nb_clusters, separated_nodes=separated_nodes, force_nb_clusters=True, min_cluster_weight=min_cluster_weight, optimize_min_cluster_weight=optimize_min_cluster_weight) model.solve() if model.infeasible: logging.info('NOT FEASIBLE') continue model.print_solution() instance.print_kpis() # save instance with solution instance.set_solution(model.solution) instance.save('{}.pickle'.format(file_name)) # save graphical representation of the solution display_graph(instance, '{}.png'.format(file_name))
def test_model_matrix_instance_3(self): model = LSModel(self.instance_3, nb_clusters=3) model.solve() model.print_solution() self.instance_3.print_kpis() # save instance with solution self.instance_3.set_solution(model.solution) self.instance_3.save('instance_3.pickle') # load saved instance and check if equality holds parser = InstancePickleParser('instance_3.pickle') pickled_instance = parser.create_instance() self.assertEqual(self.instance_3, pickled_instance) # save a graphical representation of the solution display_graph(self.instance_3, 'solution_instance_3.png')
def test_model_matrix_instance_5(self): model = LSModel(self.instance_5, nb_clusters=5, separated_nodes=['node_0', 'node_4'], min_cluster_size=2) model.solve() model.print_solution() self.instance_5.print_kpis() # save instance with solution self.instance_5.set_solution(model.solution) self.instance_5.save('instance_5.pickle') # load saved instance and check if equality holds parser = InstancePickleParser('instance_5.pickle') pickled_instance = parser.create_instance() self.assertEqual(self.instance_5, pickled_instance) # save a graphical representation of the solution display_graph(self.instance_5, 'solution_instance_5.png')
def test_load_instance_results(self): parser = InstancePickleParser('instance.pickle') pickled_instance = parser.create_instance() # save a graphical representation of the solution display_graph(pickled_instance, 'solution_instance_pickle.png') pickled_instance.print_kpis()