感谢您抽出宝贵的时间。(Python 3.7.0) 我是 python 的初学者,正在做 Mesa 教程,因为我想为研究创建一个基于代理的模型。
我有以下问题:当我运行以下代码时,每次都会出现一个随机图,显示模型中 10 个代理的财富。代理人都从财富 1 开始,并相互随机交易(=给予财富)。但是,情节总是一样的,只是显示一叠值 10!我认为 agent_wealth 的定义有问题,但我直接从教程中得到了它。
from mesa_tutorial import * #import all definitions from mesa_tutorial
import matplotlib.pyplot as plt
model = MoneyModel(10)
for i in range(10):
model.step()
agent_wealth = [a.wealth for a in model.schedule.agents]
plt.hist(agent_wealth)
plt.show()
导致以下情节: 堆栈 10 的非随机情节
这是模型的定义
class MoneyModel(Model): # define MoneyModel as a Subclass of Model
'''A model with some number (N) of agents'''
def __init__(self, N):
#Create N agents
self.num_agents = N
self.schedule = RandomActivation(self) #Executes the step of all agents, one at a time, in random order.
for i in range(self.num_agents): #loop with a range of N = number of agents
a = MoneyAgent(i, self) # no idea what happens here, a = agent?
self.schedule.add(a) #adds a to the schedule of the model
def step(self):
'''Advance the model by one step'''
self.schedule.step()