谢谢你抽出时间来。(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()结果如下所示:non-random plot with stack 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()发布于 2018-10-30 18:09:15
你能在这个类中发布你的Moneyagent类吗?代理应该随机交换金钱。请参阅下面的阶跃函数。
# model.py
class MoneyAgent(Agent):
""" An agent with fixed initial wealth."""
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
self.wealth = 1
def step(self):
if self.wealth == 0:
return
other_agent = random.choice(self.model.schedule.agents)
other_agent.wealth += 1
self.wealth -= 1使用这个阶跃函数,你应该开始得到一个正偏斜分布或正态分布的正一半,如果代理可以变成负分布的话。
https://stackoverflow.com/questions/53007908
复制相似问题