Skip to content

Commit

Permalink
Cleanup and final submission
Browse files Browse the repository at this point in the history
  • Loading branch information
croutons committed May 24, 2017
1 parent affa51c commit 6192ad6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 123 deletions.
138 changes: 34 additions & 104 deletions diner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,23 @@
import matplotlib.pyplot as plt


# Model the 'everyone pays for their own meal'

players = [n for n in range(0,500)]

"""
Cost layouts for different ratios:
e , c ; ue, uc
0.25 - [180,100;240,220]
0.33 - [80,50;90,80]
0.4 - [80,40;90,74]
0.5 - [30,10;50,40]
"""

# Costs
e = 30
c = 10
ue = 50
uc = 40

#cost = [n, for n*50 in range(0,10)]
max_pop = 1000
# Model the 'everyone pays for their own meal'

cost = []
cumul_cost = []
exp = []
cumul_exp = []

for n in players:
exp = exp+ [e]
cumul_exp.append(sum(exp))

n_cost = []
for number in range(10,1000,10):
players = [n for n in range(0,number)]
for population in range(10,max_pop+1,10):
players = [n for n in range(0,population)]
for n in players:
# 'Choose' Expensive option, as it is preferred
cost = cost + [e]
Expand All @@ -49,105 +33,62 @@
cost = cost+[c]
cumul_cost.append(sum(cost))
#print cost
final_cost=cumul_cost[number-1]
final_cost=cumul_cost[population-1]
n_cost.append(final_cost)
fig = plt.figure()
ax1 = fig.add_subplot(111)

xaxis = [x for x in range(10,1000,10)]
val = [5 for x in range(10,1000,10)]
ax1.plot(xaxis, n_cost,'r',label='Cheap')
#
util_c = uc-c
util_e = ue-e
print util_c
print util_e
#exp = sum(expensive)
#cheap = sum(cheap)
ax2 = ax1.twinx()
plot_c = [util_c for x in range(len(players))]
plot_e = [util_e for x in range(len(players))]
ax1.set_xlabel('Number of players')
xaxis = [x for x in range(10,max_pop+1,10)]
ax1.plot(xaxis, n_cost,'y',label='Individual')

#Setup axes
ax1.set_xlabel('Population of players')
ax1.set_ylabel('Cumulative cost of dinner')
ax2 = ax1.twinx()
ax2.set_ylabel('Utility')

ax2_ticks = [n for n in range(0,1000,10)]
print ax2_ticks
#ax2.set_yticks=ax2_ticks
#ax1.plot(players,cumul_cost,label='cumul_cost')
#ax2.plot(players, plot_c,label='Cheap utility')
#ax2.plot(players,plot_e,label='Expensive utility')
#plt.legend()
#fig.tight_layout()
#plt.show()
# Model the 'Split bill' scenario
#(cost[exp] + (n-1)*cost[cheap])/n - c > ue-uc

"""
If everyone can see what 3 other people are doing
then everyone acting at once is acting based on 1/3 of n-1
So, if we know that 1 other person is eating the expensive option,
we can factor that into our decision.
We've chosen the expensive option; however, now the number of
people choosing the expensive option has increased, according
to the 1 person who is eating the expensive option, we can
factor that in to our calculations:

(e + ((number-1)/3)*e)/number + (number-(1-2((number-1)/3))*c)/number

"""
#Model Shared Split Bill

cost = []
cumul_cost = []
n_cost = []
util_diff = []
for number in range(10,1000,10):
players = [n for n in range(0,number)]
#print players
for population in range(10,max_pop+1,10):
players = [n for n in range(0,population)]
for n in players:
# 'Choose' Expensive option, as it is preferred
cost = cost + [e]
cumul_cost.append(sum(cost))
if ((e+(number-1)*c)/number)-c > ue-uc:
if ((e+(population-1)*c)/population)-c > ue-uc:
cumul_cost.remove(sum(cost))
cost.remove(e)
cost = cost+[c]
cumul_cost.append(sum(cost))

final_cost=cumul_cost[number-1]
final_cost=cumul_cost[population-1]
n_cost.append(final_cost)

e = (e+(number-1)*c)/float(number)
e = (e+(population-1)*c)/float(population)
util_c = uc-c
util_e = ue-e

util_diff.append(((uc-c)-(ue-e))/number)
#plot_c = [util_c for x in range(number)]
#plot_e = [util_e for x in range(number)]
'''
#ax1.plot(players, cumul_cost, label='cumul_cost: '+ str(number))
ax2.plot(players, plot_c,'ro', label='Cheap utility: ' +str(number))
ax2.plot(players, plot_e, 'go',label='Expensive utility: '+str(number))
'''
xaxis = [x for x in range(10,1000,10)]
ax1.plot(xaxis, n_cost,'g', label='split bill')
ax2.plot(xaxis, util_diff,'c--', label='util_diff')
#plt.plot(players, cumul_cost)
#plt.plot(players, cumul_exp)
util_diff.append(((uc-c)-(ue-e))/population)


ax1.plot(xaxis, n_cost,'g', label='Shared')
ax2.plot(xaxis, util_diff,'c--', label='Utility Difference')

"""
Third round of testing
Model 'Guilt' population
"""
cost = []
cumul_cost = []
n_cost = []
util_diff = []
for number in range(10,1000,10):
players = [n for n in range(0,number)]
#print players
for population in range(10,max_pop+1,10):
players = [n for n in range(0,population)]
for n in players:
# 'Choose' Expensive option, as it is preferred
cost = cost + [e]
Expand All @@ -158,30 +99,19 @@
cost = cost+[c]
cumul_cost.append(sum(cost))

final_cost=cumul_cost[number-1]
final_cost=cumul_cost[population-1]
n_cost.append(final_cost)

e = (e+(number-1)*c)/float(number)
util_c = uc-c
util_e = ue-e
h1, l1 = ax1.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()

# ax1.plot(xaxis, n_cost,'b.', label='Guilt')
plt.title("Cumulative cost of dinner with respect to utility\n difference as a proportion of population size.")
# ax1.legend(loc=4)
# ax2.legend(loc=0)
ax1.legend(h1+h2, l1+l2, loc=4)

util_diff.append(((uc-c)-(ue-e))/number)
#plot_c = [util_c for x in range(number)]
#plot_e = [util_e for x in range(number)]
'''
#ax1.plot(players, cumul_cost, label='cumul_cost: '+ str(number))
ax2.plot(players, plot_c,'ro', label='Cheap utility: ' +str(number))
ax2.plot(players, plot_e, 'go',label='Expensive utility: '+str(number))
'''
xaxis = [x for x in range(10,1000,10)]
ax1.plot(xaxis, n_cost,'+', label='split bill')

plt.title('Cumulative cost of dinner with respect to utility\n difference as a proportion of population size.')
plt.legend()
plt.show()
#print num
#print (e+(num-1)*c)/float(num)-c
print ue-uc



Expand Down
22 changes: 3 additions & 19 deletions diners_cumul.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@


ratios = [0.25,0.33,0.4,0.5,0.75]
''',[130,90,150,120]'''

ratio_costs = [[180,100,240,220],[80,50,90,80],[110,70,120,104],[30,10,50,40],[130,90,150,120]]

ratio_results = []

players = [n for n in range(0,300)]

# e = 30
# c = 10
# ue = 50
# uc = 40

for ratio_list in ratio_costs:
cost = []
cumul_cost = []
Expand Down Expand Up @@ -59,13 +54,9 @@
plot_e = [util_e for x in range(len(players))]

ratio_results.append(cumul_cost)
#ax1.plot(players,cumul_cost,label='Self')

# ax2 = ax1.twinx()

# ax2.plot(players, plot_c, label='Cheap utility')
# ax2.plot(players, plot_e, label='Expensive utility(Self)')

#Model guilt

cost = []
cumul_cost = []
Expand All @@ -87,28 +78,21 @@
plot_e = [util_e for x in range(len(players))]
ratio_results.append(cumul_cost)

#ax1.plot(players,cumul_cost,label='Split')
# ax2.plot(players, plot_e, label='Expensive utility(Split)')
fig = plt.figure()
# ax1 = fig.add_subplot(111)

count = 0
for result in ratio_results:
print count
if count %2 is 0:
cheap, = plt.plot(players,result,color='b',label='Cheap')
print len(result)
players[-1]
result[-1]
ratios[count/2]
plt.text(players[-1], result[-1], str(ratios[(count/2)]))
else:
expensive, =plt.plot(players,result,'r--',label='Expensive')
plt.text(players[-1], result[-1], str(ratios[(count/2)]))

count = count + 1

plt.title('Cumulative cost of Expensive and Cheap meals\n for different Joy:Cost ratios for n=300')
plt.title("Cumulative cost of Expensive and Cheap meals\n for different Joy:Cost ratios for n=300\n with 'guilt'")
plt.xlabel('Number of players')
plt.ylabel('Cumulative Cost')
plt.legend(handles=[cheap, expensive])
Expand Down

0 comments on commit 6192ad6

Please sign in to comment.