[英]Importing agents and their attributes from CSV in mesa
My data is in .csv format and each row of data represents each agent while each column represents a certain attribute. 我的数据为.csv格式,数据的每一行代表每个代理,而每一列则代表某个属性。
My question is how to assign agents and their attributes from a csv file in Mesa? 我的问题是如何从Mesa中的csv文件分配代理及其属性?
Could anyone help me with how to import them in Mesa please? 有人可以帮我将它们导入Mesa吗?
Thanks. 谢谢。
To import a .csv and turn them into attributes you want to know how you are reading in the file and then pass that into the agent class as you are creating it. 要导入.csv并将其转换为属性,您想知道如何阅读文件,然后在创建文件时将其传递给代理类。
For example: 例如:
you have 'people.csv': 您有“ people.csv”:
agent, height, weight
bill, 72 in, 190lbs
anne, 70 in, 170lbs
you read in as a list of lists: 您以列表列表形式阅读:
import csv
people = []
with open('people.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
pass #skips the column headers
line_count += 1
else:
people.append(row)
# This will look like [[ 'bill', '72 in', '190lbs'], ['anne', '70 in',\
#'170lbs']]
you pass in a row into your agent instantiation, this would typically occur in the creation of your agent schedule in the model module: 您将一行传递到您的代理实例中,通常会在模型模块中创建代理时间表时发生:
for i in range(number_of_agents):
a = myAgent(i, self, row[i])
self.schedule.add(a)
you assign the row variables to agent attributes: 您将行变量分配给代理属性:
class myAgent(Agent):
def __init__(self, unique_id, model, row):
super().__init__(unique_id, model)
self.name = row[0] # e.g bill
self.height = row[1] # e.g. 72 in
self.weight = row[2] # e.g. 190 lbs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.