Welcome, guest | Sign In | My Account | Store | Cart

A method for allocating costs 'fairly' amongst a group of friends who cooperate to their mutual advantage.

Python, 423 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# Author: James Collins

# Purpose: To allocate shares of cost amongst

# a group of friends who benifit by mutual cooperation.



from __future__ import division

from random import randint

from math import factorial

from copy import copy



# Travelling salesman solver

# global variables for tsp

qstart = 0 

qend = 0

queue = []

bestCost = 0

bestTrack = ''

def tsp(nodes, start):

    global qstart

    global qend

    global queue

    global bestCost

    global bestTrack

    n = len(nodes)

    if n == 0:

        return [], 0

    qstart = 0

    qend = n - 1

    queue   = copy(nodes) #range(n)

    bestCost = 9999999 # a very large number

    bestTrack = ''

    def recur(head, last, CostSoFar, track):

        global bestCost

        global bestTrack

        for j in  range( n-head):

            node = gett()

            track2 = track + node.initial

            NewCost = CostSoFar + metric(last, node)

            if NewCost < bestCost:

                if head < n-2:

                   recur(head+1, node, NewCost, track2)

                else:

                    node2 = gett()

                    finalCost = NewCost + metric(node, node2)

                    if finalCost < bestCost:

                        bestCost = finalCost

                        bestTrack = track2 + node2.initial

                    putt(node2)

            putt(node)

    def gett():

        global qstart

        x = queue[qstart]

        qstart += 1

        if qstart == n:

           qstart = 0

        return x

    def putt(x):

        global queue

        global qend

        qend += 1

        if qend == n:

           qend = 0

        queue[qend] = x

    recur(0, start, 0, '') # exhaustive search of all permutations

    return bestTrack, bestCost



# MAIN PROGRAM

people = []

class person:

    def __init__(self, name, x, y):

        self.name     = name

        self.initial  = name[0]

        self.x        = x # house location North/South

        self.y        = y # house location East/West

        self.bitplace = pow(2, len(people))

    def isElementOf(self, n): # If the ith bit in the index of

        # powerset (base 2)is 1 then it contains the ith person,

        # or else it dosnt.                

        if self.bitplace & n <> 0:

           return True

        return False                



def metric(a,b): # city grid

    return abs(a.x - b.x) + abs(a.y - b.y)



   

# random locations are being used for test purposes.

# Cost of travel is asssumed to be proportional to distance



pub = person('Shakespear', 5, 5)

people.append(person('Anne',   randint(1, 10), randint(1, 10)))

people.append(person('Brian',  randint(1, 10), randint(1, 10)))

people.append(person('Cindy',  randint(1, 10), randint(1, 10)))

people.append(person('David',  randint(1, 10), randint(1, 10)))

people.append(person('Elaine', randint(1, 10), randint(1, 10)))

#people.append(person('Francis', randint(1, 10), randint(1, 10)))

#people.append(person('Girlee', randint(1, 10), randint(1, 10)))

#people.append(person('Him', randint(1, 10), randint(1, 10)))

#people.append(person('Indy', randint(1, 10), randint(1, 10)))

#people.append(person('Jim', randint(1, 10), randint(1, 10)))

#people.append(person('Kathy', randint(1, 10), randint(1, 10))) # On my 3Gz Dual Core this takes 3min, mostly solving tsp



lenPeople = len(people)

class solution:

    def __init__(self, coalition):

        self.coalition = coalition

        self.size   = len(coalition)

        self.index  = len(powerset)

        self.split  = []

        if len(coalition) == 1: # individual

            self.cost = metric(coalition[0], pub)

            self.trk  = coalition[0].initial

            coalition[0].cost = self.cost

        else:

            self.trk, self.cost = tsp(coalition, pub)

        print 'route: ', self.trk, 'cost>',self.cost



powerset = []

# The size of the problem increases exponentialy

# w.r.t the number of people.

# Fortunately only about four people can fit in a taxi.

for i in range(pow(2, lenPeople)): # itterating over the power set

    newset = []

    for j in people:

        if j.isElementOf(i):

            newset.append(j)

    powerset.append(solution(newset))



def ShapleySub(q, p, setindex, personbit):

    s = q.size

    t = factorial(s) * factorial(lenPeople - s - 1)

    v = powerset[setindex + personbit]

    return t * ( v.cost - q.cost)

# Note: The Shapley value method assumes that there is no

# incentive for any party to defect from the grand

# coalition. This may not be true in this scenario as it

# may be more efficient for parties to travel seperately.

# In this aproach I calculate the minimum possible cost for the grand coalition

# allowing for seperate taxi travel and assign cost portions to each person

# as if there were no strategic politicing.

# For example if A can travel with B or C, but

# A, B and C cannot travel together. Both B and C are advantaged and A pays less

# than choosing to travel with either. So even if B ends up travelling home alone

# his travel is discounted by the others because of his ability to dispute

# the status quoe.



# test if union cost > sum cost of partitions

for setSize in range(2, lenPeople + 1):

    for p in powerset:

        if p.size == setSize:

            for i in range(1, pow(2, p.size-1)): # itterating all possible splits

                subset0s = 0

                subset1s = 0

                for j in range(p.size):

                    k = pow(2, j)

                    if k & i == 0: # note this coresponds to the bit places of the coalition not to the people list.

                       subset0s += p.coalition[j].bitplace # bitplace coresponds to the people list

                    else:

                       subset1s += p.coalition[j].bitplace

                if powerset[subset0s].cost + powerset[subset1s].cost < p.cost:

                   # subsets gain more by travelling seperately

                   print 'New costing for', p.trk, powerset[subset0s].cost + powerset[subset1s].cost, 'down from', p.cost

                   p.cost = powerset[subset0s].cost + powerset[subset1s].cost

                   p.split = [subset0s, subset1s]



                            

print 'Shapley values'

for p in people:

    Shapley = 0.0

    personbit = p.bitplace

    for q in powerset:

        setindex = q.index

        if not(p.isElementOf( setindex)):

            Shapley += ShapleySub(q, p, setindex, personbit)

    Shapley = Shapley / factorial(lenPeople)

    diff = p.cost - Shapley

    if p.cost > 0:

        perc = int(diff / p.cost * 100)

    else:

        perc = 0

    print "%s pays %6.2f saves %6.2f saving %d percent." %(p.name, Shapley, diff, perc) 



# Draws map

print

print '#######################'

for i in range(1, 11):

    line = '# '

    for j in range(1, 11):

         match = False 

         if i == pub.x and j == pub.y:

             line += pub.initial

             match = True

         for p in people:

             if p.x == i and p.y == j:

                 line += p.initial

                 match = True

         if not match:

             line += '. '

         else:

             line += ' '

    line += '#'

    print line

print '#######################'    



def AllocateTaxis(pset):

    if len(pset.split) == 0:

       print pset.trk, pset.cost

    else:

       AllocateTaxis(powerset[pset.split[0]])

       AllocateTaxis(powerset[pset.split[1]])

print

print 'Allocating taxis'

AllocateTaxis(powerset[len(powerset)-1])

The scenario is that a group of friends wish to travel home from the pub sharing taxis. The Shapley value is used to distribute costs within the grand coalition. Defection of individuals is possible but the assumption is made that there will be no strategic politicing. An unintuitive result sometimes occurs where one person will travel home alone but their fare will be discounted by the group because of the strategic location of their house.