Skip to content

Commit

Permalink
Merge pull request #4 from foarsitter/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
foarsitter authored Nov 23, 2017
2 parents 64d7983 + b6c07b6 commit b22b4e4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 36 deletions.
2 changes: 1 addition & 1 deletion model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
61 changes: 38 additions & 23 deletions model/equalgain.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ 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):
def randomized_gain(self, u, v, z, exchange_ratio, exchange_ratio_j):

p = self.exchange.model.randomized_value

Expand All @@ -31,20 +32,25 @@ 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
self.opposite_actor.u = u
self.opposite_actor.v = v
self.opposite_actor.z = z

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)
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.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

Expand Down Expand Up @@ -137,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:
Expand Down Expand Up @@ -191,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:
Expand Down Expand Up @@ -315,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):

Expand All @@ -339,8 +350,9 @@ def csv_row(self, head=False):
"u",
"v",
"z",
"max eu",
"eu",
"gain",
"gain p=0",
"nbs 0",
"nbs 1",
"delta nbs",
Expand All @@ -360,8 +372,9 @@ def csv_row(self, head=False):
"u",
"v",
"z",
"max eu",
"eu",
"gain",
"gain p=0",
"nbs 0",
"nbs 1",
"delta nbs",
Expand Down Expand Up @@ -395,6 +408,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,
Expand All @@ -416,6 +430,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,
Expand Down
2 changes: 1 addition & 1 deletion model/helpers/csvparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
36 changes: 25 additions & 11 deletions model/observers/issue_development.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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([])
Expand All @@ -229,15 +233,19 @@ 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([])
writer.writerow(["Preference variance and loss of all actors"])

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:
Expand All @@ -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):
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -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')

0 comments on commit b22b4e4

Please sign in to comment.