#!/usr/bin/env python """ LICENSE Attribution Non-Commercial Share Alike cc by-nc-sa http://creativecommons.org/licenses/by-nc-sa/3.0 Attribution: Scott Hendrickson http://drskippy.net """ from copy import copy from Partitioner import * from MultiPlayerGame import * class MultiPlayerNegotiationsModel: def __init__(self, d): self.data = d self.set = Set(self.data) self.setPartitions = SetPartitions(self.set) def createModelEntity(self, a, data = None): """ Within a set (or subset), the outcome of a game may be modeled as a new single entity with the weighted outcome position, the cumulative influence and the average Salience of the set. a is a list of entities that must be a subset of data.""" if data is None: data = self.data d = {} for entity in a: d[entity] = copy(data[entity]) p = MultiPlayerGame(d, maxPos = 100) avgPos, balOfPowPos = p.getAveragePosition() newEntityName = '_' newInf, maxInf = 0.0, -sys.maxint sumSal, maxSal = 0.0, -sys.maxint for entity in a: newEntityName += entity + "+" tmpInf = float(data[entity][INF_IDX]) newInf += tmpInf if tmpInf > maxInf: maxInf = tmpInf tmpSal = float(data[entity][SAL_IDX]) sumSal += tmpSal if tmpSal > maxSal: maxSal = tmpSal newEntityName = newEntityName[:-1] + '_' avgSal = sumSal/len(a) # Use average salience to as the salience of the new position # Combine influence for influence at new position #newSal = avgSal # Use salience weighted to maxSal as the salience of the new position # Combine influence for influence at new position if len(a) > 1: newSal = (0.2*(sumSal-maxSal)/(len(a)-1) + 0.8*(maxSal)) else: newSal = maxSal entityData = [avgPos, newInf, newSal] return newEntityName, entityData, p def modelGames(self): """ Play out game on data. Game is a list of lists of the keys in the data structure. """ result = {'minPos': [sys.maxint, None, 0], 'maxPos': [-sys.maxint, None, 0]} cnt = 1 for game in self.setPartitions.getPartitions(): roundFinalGame = {} for entity in game: if len(entity) > 1: newEntity, entityParams, entityObj = self.createModelEntity(entity) roundFinalGame[newEntity] = entityParams else: roundFinalGame[entity[0]] = self.data[entity[0]] entityName, entityData, entityObj = self.createModelEntity(roundFinalGame, roundFinalGame) print "<= GAME %d => \n%s\n"%(cnt,str(entityObj)) cnt += 1 if result['minPos'][0] > entityData[0]: result['minPos'] = [entityData[0], entityObj, cnt ] if result['maxPos'][0] < entityData[0]: result['maxPos'] = [entityData[0], entityObj, cnt ] return result if __name__ == '__main__': # [P, I, S] data = { 'd2': [ 15, 20, 80], 'ctcust': [ 35, 50, 5], 'ctsal': [ 35, 20, 5], 'er': [ 50, 10, 20], 'eng': [ 60, 40, 99], 'd1': [ 75, 80, 99], 'me': [ 75, 80, 99], 'adv': [100, 80, 20], 'inv': [100, 80, 5], 'legal': [100, 70, 95] } m = MultiPlayerNegotiationsModel(data) n,d,p = m.createModelEntity(data, data) print p # m = MultiPlayerNegotiationsModel(data) result = m.modelGames() for k in result: print ''.join([k, ' : %2.5f'%result[k][0]]) print str(result[k][1]) print ''.join(['(Game # : %d)\n'%result[k][2]])