홈>
Mesa 라이브러리를 사용하여 파이썬으로 멀티 에이전트 시스템을 작성하려고하는데 라이브러리가 나와 같은 초보자에게 훌륭하지만 (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()
내가 테스트하고 싶은 것은 단지 한 집의 모델 대신 이웃까지 확장 할 수 있는지 확인하고 싶기 때문에 각 집을 하나의 에이전트로 정의하려고합니다. 그 집에서 서브 에이전트로서 어플라이언스를 갖춘 완전한 모델. 각 하우스를 에이전트로 만들고 어플라이언스를 서브 에이전트로 정의하는 것에 대해 생각했지만 에이전트의 각 인스턴스는 자신이 속한 모델의 인스턴스를 인수로 사용하므로 하우스 에이전트 내에서 어플라이언스 에이전트를 정의하는 방법에 대해 다소 혼란스러워했습니다.
이 작업을 수행하는 다른 방법이있을 수도 있지만 파이썬을 처음 접하는 사람 (거의 3 개월간 사용하지 않음)이므로 다른 방법이 무엇인지 알 수 없습니다. 누군가가 여기에 나를 인도 할 수 있다면 매우 감사하게 될 것입니다.
- 답변 # 1
관련 질문
- python : StyleFrame row_index AttributeError : 'int'개체에 'value'속성이 없습니다.
- Python에서 명령 출력 구문 분석
- python : 파이썬에서 이웃 이웃을 찾는 가장 효율적인 방법
- python : guessesTaken 변수 및 for 문
- Python의 캡슐화 및 제한
- Python-바이트 배열을 이진 데이터로 변환 한 다음 비트 선택
- python : Celery와 asyncio를 결합하는 방법은 무엇입니까?
- Python 3의 힙 구현
- 까다로운 방식으로 목록 요소를 결합하는 Python
- python : 2 개의 CSV 파일을 가져오고, 다른 파일에있는 경우 값을 비교하고, 값이 있는지 여부를 나타내는 열이있는 최종 CSV를 생성하려면 어떻게해야합니까?
내 장치가있는 Home 클래스를 추가하겠습니다. 그런 다음 상담원 일정에 홈을 추가하십시오.
예 :