12.夏普利值应用

import itertools
from collections import Counter


"""
夏普利值的应用1

我们将夏普利值应用在基于替代用途测试(alternative uses test)的合作博弈中。在测试中,每个人都必须为一种常见的物品想出一些新的用途,比如砖块。
想象一下有三个人参加了某替代用途测试,分别是阿伦、贝蒂和卡洛斯。测试要求他们想出区块链的替代用途。阿伦和卡洛斯分别提出了6个想法,每个人的创造
力得分均为6;贝蒂则提出了7个想法,因而得到7分。他们这三个人组成的团队的总创造力得分为9,因为总共有9个不同的想法(不同人提出的想法,有些是重合的)。
为了计算夏普利值,可以写下这个团队能够形成的所有6种可能的排序,而且只有当某个人为团队提供了独特的想法时才“给分”,然后再对所有6种情况求平均值。
"""

# 0.构建字典
SV_dict = {
    "Allen" : {"data", "medical", "law", "supervision", "art", "science"},
    "Betty" : {"data", "medical", "law", "password", "supervision", "penalty", "audit"},
    "Carlos" : {"data", "medical", "law", "password", "penalty", "audit"}
}

# 1.所有可能的组合
combins = list(itertools.permutations(["Allen","Betty", "Carlos"], 3))

# 2.包含n个字典的夏普利值列表
def Shapley_Values_list():
    Shapley_Values = []
    for combin in combins:
        Shapley_Value = {}
        pre_values = set()
        for item in combin:
            Shapley_Value[item] = len(SV_dict[item] - pre_values) / len(combins)
            pre_values.update(SV_dict[item])
        Shapley_Values.append(Shapley_Value)

    return Shapley_Values

# 3.计算每个人的夏普利值
Shapley_Values = Shapley_Values_list()
counter = Counter()
for dic in Shapley_Values:
    counter += Counter(dic)
# print(dict(counter))

"""
夏普利值应用2

If Ram is eating alone, he would pay 800
If Abhiraj is eating alone, he would pay 560
If Pranav is eating alone, he would pay 700
If Ram and Abhiraj both eat alone, they would pay 800
If Ram and Pranav both eat alone, they would pay 850
If Abhiraj and Pranav both eat alone, they would pay 720
If Ram, Abhiraj, and Pranav all eat together, they would pay 900

"""
# 1.构建字典
dict1 = {
    ("Ram",):800,
    ("Abhiraj",):560,
    ("Pranav",):700,
    ("Ram", "Abhiraj"):800,
    ("Abhiraj", "Ram"):800,
    ("Ram", "Pranav"):850,
    ("Pranav", "Ram"):850,
    ("Abhiraj", "Pranav"):720,
    ("Pranav", "Abhiraj"):720,
    ("Ram", "Abhiraj", "Pranav"):900
}

# 2.所有可能的组合
combins1 = list(itertools.permutations(["Ram","Abhiraj", "Pranav"], 3))

# 3.包含n个字典的夏普利值列表
sv_list = []
for combin1 in combins1:
    sv_dict = {}
    names = ()
    pre_values1 = 0

    for item1 in combin1:
        names += (item1,)
        if len(names) == 3:
            sv_dict[item1] = (900 - pre_values1) / len(combins1)
        else:
            sv_dict[item1] = (dict1[names] - pre_values1) / len(combins1)
            pre_values1 = dict1[names]
    
    sv_list.append(sv_dict)

# 4.计算每个人的夏普利值
counter1 = Counter()
for dic1 in sv_list:
    counter1 += Counter(dic1)
print(dict(counter1))
{'Ram': 391.6666666666667, 'Pranav': 301.6666666666667, 'Abhiraj': 206.66666666666669}

 

posted @ 2020-11-09 13:11  止一  阅读(636)  评论(0编辑  收藏  举报