首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mesa教程:代理商财富的曲线是确定性的,而不是随机的

Mesa教程:代理商财富的曲线是确定性的,而不是随机的
EN

Stack Overflow用户
提问于 2018-10-26 19:37:18
回答 1查看 219关注 0票数 0

谢谢你抽出时间来。(Python 3.7.0)我是python的初学者,正在做Mesa教程,因为我想为一项研究建立一个基于代理的模型。

我有以下问题:当我运行以下代码时,每次都会出现一个随机图,显示模型中10个代理的财富。代理人都从财富1开始,并随机地相互交易。然而,这个图总是一样的,只显示了一个值为10的堆栈!我认为agent_wealth的定义有误,但我直接从教程中获取了它。

代码语言:javascript
复制
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

下面是模型的定义

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2018-10-30 18:09:15

你能在这个类中发布你的Moneyagent类吗?代理应该随机交换金钱。请参阅下面的阶跃函数。

代码语言:javascript
复制
# 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

使用这个阶跃函数,你应该开始得到一个正偏斜分布或正态分布的正一半,如果代理可以变成负分布的话。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53007908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档