当前位置: 首页>>代码示例>>Python>>正文


Python DataCollector.add_table_row方法代码示例

本文整理汇总了Python中mesa.datacollection.DataCollector.add_table_row方法的典型用法代码示例。如果您正苦于以下问题:Python DataCollector.add_table_row方法的具体用法?Python DataCollector.add_table_row怎么用?Python DataCollector.add_table_row使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mesa.datacollection.DataCollector的用法示例。


在下文中一共展示了DataCollector.add_table_row方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: CrisisWorld

# 需要导入模块: from mesa.datacollection import DataCollector [as 别名]
# 或者: from mesa.datacollection.DataCollector import add_table_row [as 别名]
class CrisisWorld(MetaModel):
    '''
    Basic MetaModel for running multiple iterations with a fixed population.

    By default, creates agents with random military strength, assets and
    bloc membership. Fixed agent parameters (e.g. learning rates) 
    can be passed as a dictionary. To generate more complex behaviors, override
    the make_agents method.

    By default, the log is a list of run qualities. Override assess_run method
    as needed to change that.
    '''
    model_class = CrisisModel
    agent_class = CrisisAgent
    agent_count = 10
    agent_args = {}

    def __init__(self, agent_class, agent_count, agent_args={}, seed=None):
        '''
        Instantiate a new CrisisWorld model.

        Args:
            agent_class: Class to instantiate the agents 
            agent_count: How many agents to instantiate with.
            agent_args: Dictionary of arguments to pass to all agents.
            seed: Random seed to launch the model with.
        '''
        self.agent_class = agent_class
        self.agent_count = agent_count
        self.agent_args = agent_args
        super().__init__(self.model_class, agents_per_model=2, seed=seed)
        
        # Instantiate data collector
        self.dc = DataCollector(tables={
                    "Interactions": 
                        ["Step", "A", "B", "Outcome", "SPE", "quality"],
                    "Agents": ["Name", "Assets", "Capability", "Bloc"] })

        for agent in self.agents:
            row = {"Name": agent.name, 
                   "Assets": agent.assets,
                   "Capability": agent.mil_strength,
                   "Bloc": agent.bloc}
            self.dc.add_table_row("Agents", row)

    def make_agents(self):
        '''
        Create self.agent_count agents.
        '''
        self.agents = []
        for i in range(self.agent_count):
            m = random.randrange(1, 100) # Mil strength
            a = random.randrange(1,100) # Assets
            b = random.randrange(2) # Bloc
            agent_args = dict(learning_rate=0.1,
                              discount_factor=0.9,
                              assets=a,
                              mil_strength=m,
                              bloc=b,
                              name=i)
            for arg, val in self.agent_args.items():
                agent_args[arg] = val
            a = self.agent_class(**agent_args)
            self.agents.append(a)

    def step(self):
        '''
        Pair up all agents at random and have them interact.
        '''
        random.shuffle(self.agents)
        for agent in self.agents:
            alters = [a for a in self.agents if a is not agent]
            random.shuffle(alters)
            for alter in alters:
                model = self.model_class([agent, alter])
                model.run()
                self.assess_run(model)
        self.steps += 1

    def assess_run(self, model):
        '''
        Log the model outcome and equilibrium outcome, and compute similarity.
        '''
        spe_model = model.find_equilibrium()
        a, b = model.agents
        q = model.log.tversky_index(spe_model.log)
        row = {"Step": self.steps, "A": a.name, "B": b.name, 
               "Outcome": model.current_node, "SPE": spe_model.current_node,
               "quality": q}
        self.dc.add_table_row("Interactions", row)
开发者ID:acrooks2,项目名称:Agents-In-Conflict,代码行数:92,代码来源:small_crisis.py


注:本文中的mesa.datacollection.DataCollector.add_table_row方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。