Coverage for /home/simon/Git/preflibtools/preflibtools/instances/convert.py: 0%
18 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-22 11:34 +0200
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-22 11:34 +0200
1""" This module presents procedures to convert instances from one type to another.
2"""
3from preflibtools.properties.basic import pairwise_scores
6def order_to_pwg(instance):
7 """ Takes as input an instances and returns a string describing a pairwise graph.
9 :param instance: The instance to convert.
10 :type instance: :class:`preflibtools.instances.preflibinstance.OrdinalInstance`
11 """
12 header = str(instance.num_alternatives) + "\n"
13 for alt, alt_name in instance.alternatives_name.items():
14 header += str(alt) + "," + str(alt_name) + "\n"
15 header = header
17 scores = pairwise_scores(instance)
18 pairwise_relation = ""
19 sum_vote_count = 0
20 num_unique = 0
21 for alt_name1, score_dict in scores.items():
22 for alt_name2, score in score_dict.items():
23 pairwise_relation += str(score) + "," + str(alt_name1) + "," + str(alt_name2) + "\n"
24 num_unique += 1
25 sum_vote_count += score
26 pairwise_relation = pairwise_relation[:-1]
28 count_line = str(instance.num_voters) + "," + str(sum_vote_count) + "," + str(num_unique) + "\n"
30 return header + count_line + pairwise_relation