From 4204f416b947b7367e26bd81095181db487d7d96 Mon Sep 17 00:00:00 2001 From: jelmert Date: Tue, 21 Nov 2017 18:58:01 +0100 Subject: [PATCH 1/2] Small changes --- model.py | 2 +- model/equalgain.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/model.py b/model.py index e7b93d7..1e0301a 100644 --- a/model.py +++ b/model.py @@ -21,7 +21,7 @@ if args.p != 'None': p = decimal.Decimal(args.p) - model_name = 'equal-' + round(p, 2) + model_name = 'equal-' + str(round(p, 2)) else: p = None model_name = 'equal' diff --git a/model/equalgain.py b/model/equalgain.py index 8e889a9..55670d5 100644 --- a/model/equalgain.py +++ b/model/equalgain.py @@ -21,6 +21,7 @@ def __init__(self, model: 'EqualGainModel', actor: Actor, demand_issue: Issue, s self.z = '-' self.u = '-' self.v = '-' + self.eu_max = '-' def randomized_gain(self, u, v, z, exchange_ratio): @@ -31,13 +32,14 @@ def randomized_gain(self, u, v, z, exchange_ratio): self.z = z self.u = u self.v = v + self.opposite_actor.u = '-' self.opposite_actor.v = '-' self.opposite_actor.z = '-' eui_max = None - if v > 0.5: # V < 0.5: + if v < 0.5: # V < 0.5: exchange_ratio_zero_i = calculations.exchange_ratio_by_expected_utility(exchange_ratio, self.supply.salience, self.demand.salience) @@ -45,6 +47,10 @@ def randomized_gain(self, u, v, z, exchange_ratio): eui_max = abs(calculations.expected_utility(self, exchange_ratio_zero_i, exchange_ratio)) eui = p * z * (eui_max - eu) + eu + + self.eu_max = eui_max + self.opposite_actor.eu_max = '-' + else: eui = eu - p * z * eu @@ -339,8 +345,9 @@ def csv_row(self, head=False): "u", "v", "z", + "max eu", "eu", - "gain", + "gain p=0", "nbs 0", "nbs 1", "delta nbs", @@ -360,8 +367,9 @@ def csv_row(self, head=False): "u", "v", "z", + "max eu", "eu", - "gain", + "gain p=0", "nbs 0", "nbs 1", "delta nbs", @@ -395,6 +403,7 @@ def csv_row(self, head=False): exchange.i.u, exchange.i.v, exchange.i.z, + exchange.i.eu_max, exchange.i.eu, exchange.gain, exchange.i.nbs_0, @@ -416,6 +425,7 @@ def csv_row(self, head=False): exchange.j.u, exchange.j.v, exchange.j.z, + exchange.j.eu_max, exchange.j.eu, exchange.gain, exchange.j.nbs_0, From b6c07b6e9be4059f7a9cff576c241ce0edfb870d Mon Sep 17 00:00:00 2001 From: jelmert Date: Thu, 23 Nov 2017 15:29:29 +0100 Subject: [PATCH 2/2] Small changes --- model/equalgain.py | 53 +++++++++++++++------------- model/helpers/csvparser.py | 2 +- model/observers/issue_development.py | 36 +++++++++++++------ 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/model/equalgain.py b/model/equalgain.py index 55670d5..37df888 100644 --- a/model/equalgain.py +++ b/model/equalgain.py @@ -23,7 +23,7 @@ def __init__(self, model: 'EqualGainModel', actor: Actor, demand_issue: Issue, s self.v = '-' self.eu_max = '-' - def randomized_gain(self, u, v, z, exchange_ratio): + def randomized_gain(self, u, v, z, exchange_ratio, exchange_ratio_j): p = self.exchange.model.randomized_value @@ -33,24 +33,24 @@ def randomized_gain(self, u, v, z, exchange_ratio): self.u = u self.v = v - self.opposite_actor.u = '-' - self.opposite_actor.v = '-' - self.opposite_actor.z = '-' + self.opposite_actor.u = u + self.opposite_actor.v = v + self.opposite_actor.z = z - eui_max = None + exchange_ratio_zero_i = calculations.exchange_ratio_by_expected_utility(exchange_ratio, + self.supply.salience, + self.demand.salience) - if v < 0.5: # V < 0.5: - exchange_ratio_zero_i = calculations.exchange_ratio_by_expected_utility(exchange_ratio, - self.supply.salience, - self.demand.salience) - - eui_max = abs(calculations.expected_utility(self, exchange_ratio_zero_i, exchange_ratio)) + self.eu_max = abs(calculations.expected_utility(self, exchange_ratio_zero_i, exchange_ratio)) - eui = p * z * (eui_max - eu) + eu + exchange_ratio_zero_j = calculations.exchange_ratio_by_expected_utility(exchange_ratio_j, + self.opposite_actor.supply.salience, + self.opposite_actor.demand.salience) - self.eu_max = eui_max - self.opposite_actor.eu_max = '-' + self.opposite_actor.eu_max = abs(calculations.expected_utility(self, exchange_ratio_zero_j, exchange_ratio_j)) + if v < 0.5: # V < 0.5: + eui = p * z * (self.eu_max - eu) + eu else: eui = eu - p * z * eu @@ -143,8 +143,12 @@ def randomized_gain(self, u, v, z, exchange_ratio): self.opposite_actor.is_adjusted_by_nbs = True else: - raise Exception('Fail: when the shift for j is to large by a maximum shift of i, ' - 'the maximum shift of j has to result in a lower shift for i.') + print('check should not hit') + self.exchange.is_valid = False + return + # raise Exception('Fail: when the shift for j is to large by a maximum shift of i, ' + # 'the maximum shift of j has to result in a lower shift for i. data=' + str(self.exchange)) + else: # check if the shift does not exceed the NBS. if self.supply.position > self.opposite_actor.demand.position: @@ -197,12 +201,13 @@ def randomized_gain(self, u, v, z, exchange_ratio): self.is_adjusted_by_nbs = True if abs(eui - eui_check) > 1e-10: - raise Exception('Fail: the expected utility of the check ({0})' - ' does not match the expected utility ({1}), {2}.' - .format(eui_check, eui, - self.opposite_actor.is_adjusted_by_nbs)) + print('check does not fit') + # raise Exception('Fail: the expected utility of the check ({0})' + # ' does not match the expected utility ({1}), {2}.' + # .format(eui_check, eui, + # self.opposite_actor.is_adjusted_by_nbs)) - if eui_max is None: + if self.eu_max is None: # if they are not equal (occurs by p=0.0) if not abs(euj - eui) < 1e-10: if (euj - eui) < 1e-10: @@ -321,10 +326,10 @@ def calculate(self): z = decimal.Decimal(random.uniform(0, 1)) - if u > 0.5: - self.i.randomized_gain(u, v, z, self.dp) + if u < 0.5: + self.i.randomized_gain(u, v, z, self.dp, self.dq) else: - self.j.randomized_gain(u, v, z, self.dq) + self.j.randomized_gain(u, v, z, self.dq, self.dp) def csv_row(self, head=False): diff --git a/model/helpers/csvparser.py b/model/helpers/csvparser.py index 9898674..28b65d1 100644 --- a/model/helpers/csvparser.py +++ b/model/helpers/csvparser.py @@ -37,7 +37,7 @@ def read(self, filename): if not filename.startswith("/"): filename = "{1}".format(os.path.dirname(os.path.abspath(__file__)), filename) - with open(filename, 'rt') as csv_file: + with open(filename, 'rt', encoding='utf-8') as csv_file: # guess the document format dialect = csv.Sniffer().sniff(csv_file.read(1024)) diff --git a/model/observers/issue_development.py b/model/observers/issue_development.py index 4b2f3a5..82ab951 100644 --- a/model/observers/issue_development.py +++ b/model/observers/issue_development.py @@ -2,7 +2,7 @@ import csv import math import os -from collections import defaultdict +from collections import defaultdict, OrderedDict from typing import List from .. import base @@ -211,16 +211,20 @@ def after_iterations(self, repetition): if matplotlib_loaded: plt.plot(nbs_values, label='nbs') - for key, value in self.preference_history[issue].items(): + od = OrderedDict(sorted(self.preference_history[issue].items())) + + for key, value in od.items(): values = self._number_list_value(value) writer.writerow([key] + values) if matplotlib_loaded: plt.plot(values, label=self.model_ref.actors[key].name) if matplotlib_loaded: - plt.legend(loc='upper left') + lgd = plt.legend(loc='upper left', bbox_to_anchor=(1, 1)) plt.title(self.issue_obj) - plt.savefig('{0}/issues/{2}/{1}.png'.format(self.output_directory, self.issue_obj.name, repetition)) + plt.savefig('{0}/issues/{2}/{1}.png'.format(self.output_directory, self.issue_obj.name, repetition), + bbox_extra_artists=(lgd, ), + bbox_inches='tight') if self.write_voting_position: writer.writerow([]) @@ -229,7 +233,9 @@ def after_iterations(self, repetition): writer.writerow(["nbs"] + self._number_list_value(voting_nbs)) - for key, value in self.voting_history[issue].items(): + od = OrderedDict(sorted(self.voting_history[issue].items())) + + for key, value in od.items(): writer.writerow([key] + self._number_list_value(value)) writer.writerow([]) @@ -237,7 +243,9 @@ def after_iterations(self, repetition): writer.writerow(["nbs-var"] + preference_nbs_var) - for key, value in self.preference_loss[issue].items(): + od = OrderedDict(sorted(self.preference_loss[issue].items())) + + for key, value in od.items(): writer.writerow([key] + value) if self.write_voting_position: @@ -247,7 +255,9 @@ def after_iterations(self, repetition): writer.writerow(["nbs-var"] + voting_nbs_var) - for key, value in self.voting_loss[issue].items(): + od = OrderedDict(sorted(self.voting_loss[issue].items())) + + for key, value in od.items(): writer.writerow([key] + value) def after_repetitions(self): @@ -328,7 +338,7 @@ def after_repetitions(self): var_rows = [] - for actor, value in self.preference_history_sum[issue].items(): + for actor, value in sorted(self.preference_history_sum[issue].items()): row = [actor] var_row = [actor] @@ -339,7 +349,7 @@ def after_repetitions(self): var_row.append(math.sqrt(var)) if matplotlib_loaded: - plt.plot(avg_row, label=actor) + plt.plot(avg_row, label=actor.name) writer.writerow([actor] + avg_row) var_rows.append(var_row) @@ -352,6 +362,10 @@ def after_repetitions(self): writer.writerow(row) if matplotlib_loaded: - plt.legend(loc='upper left') + lgd = plt.legend(loc='upper left', bbox_to_anchor=(1, 1)) plt.title(self.issue_obj) - plt.savefig('{0}/issues/{2}/{1}.png'.format(self.output_directory, self.issue_obj.issue_id, 'summary')) + plt.savefig('{0}/issues/{2}/{1}.png'.format(self.output_directory, + self.issue_obj.issue_id, + 'summary'), + bbox_extra_artists=(lgd, ), + bbox_inches='tight')