From bd4c731c96576f5a250ba3825ffd62859e402030 Mon Sep 17 00:00:00 2001 From: Erel Segal-Halevi Date: Sun, 14 Apr 2024 10:16:21 +0300 Subject: [PATCH] fix examples --- examples/courses.md | 24 +++++++++---------- examples/input_formats.md | 30 ++++++++++-------------- fairpyx/algorithms/almost_egalitarian.py | 4 ++-- fairpyx/explanations.py | 19 +++++++++++++++ 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/examples/courses.md b/examples/courses.md index 362748a..abf397d 100644 --- a/examples/courses.md +++ b/examples/courses.md @@ -106,18 +106,18 @@ print(divide(fairpyx.algorithms.almost_egalitarian_with_donation, instance=rando ``` ``` -{'s1': ['c1', 'c3'], 's2': ['c1', 'c2'], 's3': ['c1', 'c2'], 's4': -['c1', 'c3'], 's5': ['c1', 'c2'], 's6': ['c2'], 's7': ['c2'], 's8': -['c3'], 's9': ['c3'], 's10': ['c3']} -{'s1': ['c3'], 's2': ['c2'], 's3': ['c2'], 's4': ['c3'], 's5': ['c2'], -'s6': ['c1', 'c2'], 's7': ['c1', 'c2'], 's8': ['c1', 'c3'], 's9': -['c1', 'c3'], 's10': ['c1', 'c3']} -{'s1': ['c1', 'c2', 'c3'], 's2': ['c1', 'c2', 'c3'], 's3': ['c1', -'c2', 'c3'], 's4': ['c2', 'c3'], 's5': ['c2', 'c3'], 's6': ['c1'], -'s7': ['c1'], 's8': [], 's9': [], 's10': []} -{'s1': ['c3'], 's2': ['c1', 'c2'], 's3': ['c1'], 's4': ['c3'], 's5': -['c3'], 's6': ['c1', 'c2'], 's7': ['c2', 'c3'], 's8': ['c2'], 's9': -['c1', 'c3'], 's10': ['c1', 'c2']} +{'s1': ['c2', 'c3'], 's2': ['c2', 'c3'], 's3': ['c1', 'c3'], 's4': +['c1', 'c3'], 's5': ['c1', 'c2'], 's6': ['c1', 'c3'], 's7': ['c1', +'c2'], 's8': ['c1', 'c2'], 's9': ['c1', 'c3'], 's10': ['c3']} +{'s1': ['c2'], 's2': ['c1', 'c2'], 's3': ['c1', 'c3'], 's4': ['c1', +'c3'], 's5': ['c1', 'c2'], 's6': ['c1', 'c3'], 's7': ['c2', 'c3'], +'s8': ['c2', 'c3'], 's9': ['c1', 'c3'], 's10': ['c1', 'c3']} +{'s1': ['c2', 'c3'], 's2': ['c2', 'c3'], 's3': ['c2', 'c3'], 's4': +['c1', 'c2', 'c3'], 's5': ['c1', 'c2', 'c3'], 's6': ['c1', 'c3'], +'s7': ['c1', 'c3'], 's8': ['c1'], 's9': ['c1'], 's10': ['c1']} +{'s1': ['c2'], 's2': ['c3'], 's3': ['c1', 'c3'], 's4': ['c1', 'c3'], +'s5': ['c1', 'c2'], 's6': ['c1', 'c2', 'c3'], 's7': ['c1', 'c2'], +'s8': ['c1', 'c3'], 's9': ['c1', 'c3'], 's10': ['c2', 'c3']} ``` diff --git a/examples/input_formats.md b/examples/input_formats.md index 7e92428..35a7b6e 100644 --- a/examples/input_formats.md +++ b/examples/input_formats.md @@ -68,12 +68,12 @@ print(allocation) ``` ``` -[[33 29 31 68 28 55 87 85 51 9 62 29] - [47 57 24 82 9 53 49 4 35 94 25 39] - [ 9 98 13 23 21 52 1 14 33 50 60 70] - [29 52 68 85 20 98 85 75 93 38 36 19] - [72 23 23 34 36 71 29 13 38 2 69 91]] -{0: [6, 7], 1: [3, 9], 2: [1, 10], 3: [2, 8], 4: [0, 4, 5, 11]} +[[11 51 26 40 92 22 25 9 26 44 52 98] + [58 55 4 57 48 3 24 24 51 84 97 2] + [ 6 5 31 66 74 95 11 21 81 28 98 28] + [ 9 50 8 9 37 24 42 22 90 15 36 11] + [43 73 76 60 30 20 4 11 1 54 68 86]] +{0: [4], 1: [0, 7, 9], 2: [5, 10], 3: [6, 8], 4: [1, 2, 3, 11]} ``` @@ -89,8 +89,7 @@ print(allocation) ``` ``` -No leafs - removing edge (2, 11) with minimum weight 0.17 -{0: [6, 7], 1: [3, 9], 2: [1, 10], 3: [5, 8], 4: [0, 11]} +{0: [4, 11], 1: [0, 9], 2: [5, 10], 3: [6, 8], 4: [1, 2]} ``` @@ -104,8 +103,7 @@ print(allocation) ``` ``` -No leafs - removing edge (0, 6) with minimum weight 0.15 -{0: [7], 1: [3, 9], 2: [1, 5, 10], 3: [6, 8], 4: [11]} +{0: [3], 1: [0, 9], 2: [4, 5, 10], 3: [1, 8], 4: [11]} ``` @@ -119,8 +117,7 @@ print(allocation) ``` ``` -No leafs - removing edge (2, 11) with minimum weight 0.17 -{0: [6, 7], 1: [3, 9], 2: [1, 10], 3: [5, 8], 4: [0, 11]} +{0: [4, 11], 1: [0, 9], 2: [5, 10], 3: [6, 8], 4: [1, 2]} ``` @@ -134,9 +131,8 @@ print(allocation) ``` ``` -No leafs - removing edge (0, 10) with minimum weight 0.05 -{0: [3, 6, 7, 8], 1: [0, 1, 3, 9], 2: [1, 9, 10, 11], 3: [5, 6, 7, 8], -4: [0, 5, 10, 11]} +{0: [1, 4, 9, 11], 1: [0, 3, 9, 10], 2: [4, 5, 8, 10], 3: [1, 5, 6, +8], 4: [0, 2, 3, 11]} ``` @@ -150,8 +146,8 @@ print(allocation) ``` ``` -{0: [3, 6, 7], 1: [1, 3, 9], 2: [1, 9, 10, 11], 3: [2, 5, 7, 8], 4: -[0, 4, 5, 11]} +{0: [1, 4, 11], 1: [0, 3, 7, 9], 2: [3, 5, 10], 3: [5, 6, 7, 8], 4: +[1, 2, 9, 11]} ``` diff --git a/fairpyx/algorithms/almost_egalitarian.py b/fairpyx/algorithms/almost_egalitarian.py index 1d6bebb..5f150a6 100644 --- a/fairpyx/algorithms/almost_egalitarian.py +++ b/fairpyx/algorithms/almost_egalitarian.py @@ -282,8 +282,8 @@ def remove_agent_from_graph(agent): edge_with_min_weight = min(fractional_allocation_graph.edges(), key=lambda edge:fractional_allocation_graph.weight(edge[0],edge[1])) agent_min_weight,item_min_weight = edge_with_min_weight min_weight = fractional_allocation_graph.weight(agent_min_weight,item_min_weight) - logger.warning("No leafs - removing edge %s with minimum weight %g", edge_with_min_weight, min_weight) - explanation_logger.info("There are no leaf nodes, but the edge %s has minimum weight %g, so it is removed.", edge_with_min_weight, min_weight, agents=agent_min_weight) + # logger.warning("No leafs - removing edge %s with minimum weight %g", edge_with_min_weight, min_weight) + explanation_logger.warning("There are no leaf nodes, but the edge %s has minimum weight %g, so it is removed.", edge_with_min_weight, min_weight, agents=agent_min_weight) remove_edge_from_graph(agent_min_weight,item_min_weight) iterated_maximum_matching(alloc) # Avoid waste diff --git a/fairpyx/explanations.py b/fairpyx/explanations.py index 674f478..d63cffe 100644 --- a/fairpyx/explanations.py +++ b/fairpyx/explanations.py @@ -54,6 +54,9 @@ class ExplanationLogger: def __init__(self, language='en'): self.language=language + def warning(self, message:str, *args, agents=None): + pass + def info(self, message:str, *args, agents=None): pass @@ -117,6 +120,12 @@ def info(self, message:str, *args, agents=None): else: # to one agent self.logger.info(str(agents)+": "+message.strip(), *args) + def warning(self, message:str, *args, agents=None): + if agents is None or not is_individual_agent(agents): # to all agents + self.logger.warning(message, *args) + else: # to one agent + self.logger.warning(str(agents)+": "+message.strip(), *args) + class ConsoleExplanationLogger(SingleExplanationLogger): """ @@ -160,6 +169,16 @@ def info(self, message:str, *args, agents=None): for agent in agents: self.map_agent_to_logger[agent].info(message, *args) + def warning(self, message:str, *args, agents=None): + if agents is None: + for agent,logger in self.map_agent_to_logger.items(): + logger.warning(message, *args) + elif is_individual_agent(agents): + self.map_agent_to_logger[agents].warning(message, *args) + else: + for agent in agents: + self.map_agent_to_logger[agent].warning(message, *args) + class FilesExplanationLogger(ExplanationLoggerPerAgent):