在使用Python中的Mesa库时,在多代理系统中是否有定义子代理的方法?

在使用Python中的Mesa库时,在多代理系统中是否有定义子代理的方法?,python,python-3.x,agent-based-modeling,mesa-abm,Python,Python 3.x,Agent Based Modeling,Mesa Abm,我正在尝试使用Mesa库用python编写一个多代理系统,虽然该库对于像我这样的初学者来说非常好(不像Spade这样的东西,我觉得太复杂了),但我不知道如何使用该库定义子代理 这是一个非常基本的代理系统的基本代码 from mesa import Agent from mesa import Model import random class Device(Agent): def __init__(self, unique_id, model, energy, threshold,

我正在尝试使用Mesa库用python编写一个多代理系统,虽然该库对于像我这样的初学者来说非常好(不像Spade这样的东西,我觉得太复杂了),但我不知道如何使用该库定义子代理

这是一个非常基本的代理系统的基本代码

from mesa import Agent
from mesa import Model
import random


class Device(Agent):

    def __init__(self, unique_id, model, energy, threshold, status):

        super().__init__(unique_id, model)
        self.energy = energy
        self.threshold = threshold
        self.status = 0

    def _cost(self, price):

        self.elec_cost = price*self.energy

    def run(self, price):

        print('price:', price)
        self._cost(price)

        if self.elec_cost > self.threshold:
            self.status = 0

        elif self.elec_cost <= self.threshold:
            self.status = 1

    def getStatus(self):
        if self.status == 1:
            return 'on'

        elif self.status == 0:
            return 'off'


class Fan(Device):

    def __init__(self, unique_id, model, energy=0.06, threshold=0.55, status=0):

        super().__init__(unique_id, model, energy, threshold, status)


class Light(Device):

    def __init__(self, unique_id, model, energy=0.06, threshold=0.55, status=0):

        super().__init__(unique_id, model, energy, threshold, status)


class HVAC(Device):
    def __init__(self, unique_id, model, energy=3, threshold=31, status=0):

        super().__init__(unique_id, model, energy, threshold, status)


class HomeModel(Model):

    def __init__(self):

        self.fan1 = Fan(1, self)
        self.fan2 = Fan(2, self)

        self.light1 = Light(3, self)
        self.light2 = Light(4, self)

    def run(self):
        self.price = get_Price()
        self._run(self.fan1)
        self._run(self.fan2)
        self._run(self.light1)
        self._run(self.light2)

    def _run(self, x):
        x.run(self.price)
        print(x.unique_id, 'is: ', x.getStatus())

def get_Price():
    priceSet = [8, 9, 7, 9, 7, 7, 6, 8, 9, 7, 7, 10, 13, 4, 7, 9, 7, 9, 10, 11, 14, 13]
    return random.choice(priceSet)

model = HomeModel()
model.run()
来自mesa进口代理
从mesa导入模型
随机输入
类设备(代理):
定义初始化(自身、唯一id、型号、能量、阈值、状态):
super().\uuuu init\uuuu(唯一的\u id,模型)
自我能量=能量
self.threshold=阈值
self.status=0
def_成本(自身、价格):
self.elec_成本=价格*self.energy
def运行(自身、价格):
打印('价格:',价格)
自身成本(价格)
如果self.elec_成本>self.threshold:
self.status=0

elif self.elec_cost我会添加一个包含设备的Home类。然后将主页添加到代理计划中

例如:

class Home(Agent): 

    def __init__(self, unique_id, model):
        super().__init__(unique_id, model)
        self.fan1 = Fan(1, self)
        self.fan2 = Fan(2, self)

        self.light1 = Light(3, self)
        self.light2 = Light(4, self)

class HomeModel(Model):

    def __init__(self):

         self.schedule = RandomActivation(self)

         for i in range(some_number_of_houses): 
              a = Home(i, self) #This will then add a house with the devices you want
                                # an object within an object 

这就是我最后做的。我使用了一个不同的库,因为它在代理之间有通信,但想法是一样的。谢谢你的帮助。