单次测试获得详细数据:
import random
#依次读入agentA,agentB对于分别生产牛奶与苹果的效率
agentA_apple_eff = int(input("agentA牛奶生产效率:"))
agentA_milk_eff = int(input("agentA苹果生产效率:"))
agentB_apple_eff = int(input("agentB牛奶生产效率:"))
agentB_milk_eff = int(input("agentB苹果生产效率:"))
#agentA,agentB生产牛奶的时间
agentA_apple_time = random.randint(0,24)
agentA_milk_time = 24-agentA_apple_time
agentB_apple_time = random.randint(0,24)
agentB_milk_time = 24-agentB_apple_time
#记录agentA,agentB在每次实验中的效用值以及生产时间
agentA_eff_lst = []
agentB_eff_lst = []
agentA_apple_time_lst = []
agentB_apple_time_lst = []
agentA_milk_time_lst = []
agentB_milk_time_lst = []
#默认试验次数为100次
ex_time = 100
#agentA与agentB所拥有的牛奶与苹果(分别表示牛奶与苹果)
agentA_have = [0,0]
agentB_have = [0,0]
#交易函数(当某人苹果比牛奶多且另一人牛奶比苹果多的时候,尽量交换多余部分)
def exchange():
if (agentA_have[0] > agentA_have[1]) :
if (agentB_have[0] < agentB_have[1]) :
exchange_num = min(agentA_have[0] - agentA_have[1],agentB_have[1] - agentB_have[0])
agentA_have[0] -= exchange_num//2
agentA_have[1] += exchange_num//2
agentB_have[0] += exchange_num//2
agentB_have[1] -= exchange_num//2
elif (agentA_have[0] < agentA_have[1]) :
if (agentB_have[0] > agentB_have[1]) :
exchange_num = min(agentA_have[1] - agentA_have[0],agentB_have[0] - agentB_have[1])
agentA_have[0] += exchange_num//2
agentA_have[1] -= exchange_num//2
agentB_have[0] -= exchange_num//2
agentB_have[1] += exchange_num//2
#获取效用函数值
def Get_Eff():
#global agentA_eff
#global agentB_eff
agentA_eff = 3*min(agentA_have[0],agentA_have[1]) + abs(agentA_have[0]-agentA_have[1])
agentB_eff = 3*min(agentB_have[0],agentB_have[1]) + abs(agentB_have[0]-agentB_have[1])
agentA_eff_lst.append(agentA_eff)
agentB_eff_lst.append(agentB_eff)
#根据生产效率与生产时间生产
def production():
agentA_have[0] = agentA_apple_eff*agentA_apple_time
agentA_have[1] = agentA_milk_eff*agentA_milk_time
agentB_have[0] = agentB_apple_eff*agentB_apple_time
agentB_have[1] = agentB_milk_eff*agentB_milk_time
#输出函数
def printf(time):
print("试验次数: ","%5d"%(time+1))
print("%13s"%"agentA","|","%16d"%agentA_apple_time,"|","%16d"%agentA_milk_time)
print("%13s"%"agentB","|","%16d"%agentB_apple_time,"|","%16d"%agentB_milk_time)
print("总效用为: ","%5d"%(agentA_eff_lst[time] + agentB_eff_lst[time]))
print()
print("%13s"%"","|","%9s"%"生产牛奶的时间","|","%9s"%"生产苹果的时间")
#首次实验
production()
exchange()
Get_Eff()
printf(0)
print()
#生产时间进行震荡
t1=random.randint(-3,3)
t2=random.randint(-3,3)
if(agentA_apple_time+t1<=24 and agentA_apple_time+t1>=0):
agentA_apple_time += t1
agentA_milk_time -= t1
if(agentB_apple_time+t1<=24 and agentB_apple_time+t1>=0):
agentB_apple_time += t2
agentB_milk_time -= t2
agentA_apple_time_lst.append(agentA_apple_time)
agentB_apple_time_lst.append(agentB_apple_time)
agentA_milk_time_lst.append(agentA_milk_time)
agentB_milk_time_lst.append(agentB_milk_time)
#第二次实验
production()
exchange()
Get_Eff()
printf(1)
print()
agentA_apple_time_lst.append(agentA_apple_time)
agentB_apple_time_lst.append(agentB_apple_time)
agentA_milk_time_lst.append(agentA_milk_time)
agentB_milk_time_lst.append(agentB_milk_time)
#进行循环实验
for i in range(2,ex_time):
#若效用降低,则回溯到前一次的生产时间,否则根据上次实验效用调整生产时间
if(agentA_eff_lst[i-1]+agentB_eff_lst[i-1]<agentA_eff_lst[i-2]+agentB_eff_lst[i-2]):
agentA_apple_time=agentA_apple_time_lst[i-2]
agentA_milk_time=agentA_milk_time_lst[i-2]
agentB_apple_time=agentB_apple_time_lst[i-2]
agentB_milk_time=agentB_milk_time_lst[i-2]
else:
t1=random.randint(-2,2)
t2=random.randint(-2,2)
if(agentA_apple_time+t1<=24 and agentA_apple_time+t1>=0):
agentA_apple_time += t1
agentA_milk_time -= t1
if(agentB_apple_time+t2<=24 and agentB_apple_time+t2>=0):
agentB_apple_time += t2
agentB_milk_time -= t2
agentA_apple_time_lst.append(agentA_apple_time)
agentB_apple_time_lst.append(agentB_apple_time)
agentA_milk_time_lst.append(agentA_milk_time)
agentB_milk_time_lst.append(agentB_milk_time)
production()
exchange()
Get_Eff()
printf(i)
多次测试获得概率:
import random
#改变量dp
dp=int(input("请输入改变量i:"))
#实验次数T
T=int(input("请输入试验次数T:"))
#手动计算得出最大值
max=int(input("请输入整体最大效用和:"))
#效率
agentA_apple_eff = int(input("agentA牛奶生产效率:"))
agentA_milk_eff = int(input("agentA苹果生产效率:"))
agentB_apple_eff = int(input("agentB牛奶生产效率:"))
agentB_milk_eff = int(input("agentB苹果生产效率:"))
def exchange():
if (agentA_have[0] > agentA_have[1]) :
if (agentB_have[0] < agentB_have[1]) :
exchange_num = min(agentA_have[0] - agentA_have[1],agentB_have[1] - agentB_have[0])
agentA_have[0] -= exchange_num//2
agentA_have[1] += exchange_num//2
agentB_have[0] += exchange_num//2
agentB_have[1] -= exchange_num//2
elif (agentA_have[0] < agentA_have[1]) :
if (agentB_have[0] > agentB_have[1]) :
exchange_num = min(agentA_have[1] - agentA_have[0],agentB_have[0] - agentB_have[1])
agentA_have[0] += exchange_num//2
agentA_have[1] -= exchange_num//2
agentB_have[0] -= exchange_num//2
agentB_have[1] += exchange_num//2
def Get_Eff():
#global agentA_eff
#global agentB_eff
agentA_eff = 3*min(agentA_have[0],agentA_have[1]) + abs(agentA_have[0]-agentA_have[1])
agentB_eff = 3*min(agentB_have[0],agentB_have[1]) + abs(agentB_have[0]-agentB_have[1])
agentA_eff_lst.append(agentA_eff)
agentB_eff_lst.append(agentB_eff)
def production():
agentA_have[0] = agentA_apple_eff*agentA_apple_time
agentA_have[1] = agentA_milk_eff*agentA_milk_time
agentB_have[0] = agentB_apple_eff*agentB_apple_time
agentB_have[1] = agentB_milk_eff*agentB_milk_time
wa=0
for j in range(1000):
agentA_apple_time = random.randint(0,24)
agentA_milk_time = 24-agentA_apple_time
agentB_apple_time = random.randint(0,24)
agentB_milk_time = 24-agentB_apple_time
agentA_eff_lst = []
agentB_eff_lst = []
agentA_apple_time_lst = []
agentB_apple_time_lst = []
agentA_milk_time_lst = []
agentB_milk_time_lst = []
agentA_have = [0,0]
agentB_have = [0,0]
#print(wa)
production()
exchange()
Get_Eff()
t1=random.randint(-dp,dp)
t2=random.randint(-dp,dp)
if(agentA_apple_time+t1<=24 and agentA_apple_time+t1>=0):
agentA_apple_time += t1
agentA_milk_time -= t1
if(agentB_apple_time+t1<=24 and agentB_apple_time+t1>=0):
agentB_apple_time += t2
agentB_milk_time -= t2
agentA_apple_time_lst.append(agentA_apple_time)
agentB_apple_time_lst.append(agentB_apple_time)
agentA_milk_time_lst.append(agentA_milk_time)
agentB_milk_time_lst.append(agentB_milk_time)
production()
exchange()
Get_Eff()
agentA_apple_time_lst.append(agentA_apple_time)
agentB_apple_time_lst.append(agentB_apple_time)
agentA_milk_time_lst.append(agentA_milk_time)
agentB_milk_time_lst.append(agentB_milk_time)
i=1
#进行循环实验
while(agentA_eff_lst[i] + agentB_eff_lst[i]<max):
i+=1
if i>T:
wa+=1
break
#若效用降低,则回溯到前一次的生产时间,否则根据上次实验效用调整生产时间
if(agentA_eff_lst[i-1]+agentB_eff_lst[i-1]<agentA_eff_lst[i-2]+agentB_eff_lst[i-2]):
agentA_apple_time=agentA_apple_time_lst[i-2]
agentA_milk_time=agentA_milk_time_lst[i-2]
agentB_apple_time=agentB_apple_time_lst[i-2]
agentB_milk_time=agentB_milk_time_lst[i-2]
else:
t1=random.randint(-dp,dp)
t2=random.randint(-dp,dp)
if(agentA_apple_time+t1<=24 and agentA_apple_time+t1>=0):
agentA_apple_time += t1
agentA_milk_time -= t1
if(agentB_apple_time+t2<=24 and agentB_apple_time+t2>=0):
agentB_apple_time += t2
agentB_milk_time -= t2
agentA_apple_time_lst.append(agentA_apple_time)
agentB_apple_time_lst.append(agentB_apple_time)
agentA_milk_time_lst.append(agentA_milk_time)
agentB_milk_time_lst.append(agentB_milk_time)
production()
exchange()
Get_Eff()
print("概率为:",wa/1000)