[英]Creating agents from a CSV file in mesa - python
I have a CSV file which is storing the ratings (0:lowest to 5:highest) given by consumers for several laptops of different producers.Each row shows the ratings given by one consumer regarding different features. 我有一个CSV文件,其中存储了消费者对不同生产商的几台笔记本电脑给出的评分(0:最低至5:最高)。每一行显示了一位消费者针对不同功能给出的评分。 An example of 2 consumers is shown below:
以下是2个使用者的示例:
Consumer Screen_13 Screen_14 Screen_15 Battery_7 Battery_10 Battery_11
1 0 3 3 2 5 5
2 1 4 3 1 3 5
I am using mesa
to create an agent_based model to evaluate purchases of different new laptops. 我正在使用
mesa
创建基于agent_的模型来评估不同新笔记本电脑的购买。 Assuming the number of consumers to be 100, the CSV fle has 100 rows and 6 columns for 2 features (screen size and battery. When the CSV is read row by row in a for loop
, I want each agent (consumer) can be assigned one row of the CSV (header should be also stored) and I can know who (which agent) is having which row. I did this, mainly following mesa
's tutorial to build the model and agent classes mesa tutorial 假设使用者数量为100,则CSV文件具有100行和6列,用于2种功能(屏幕尺寸和电池。当在
for loop
逐行读取CSV时,我希望可以分配每个座席(消费者)该CSV一行(头应该也存储),我可以知道是谁(哪个剂)是具有一行。我这样做,主要是以下mesa
的教程构建模型和代理类台面教程
To explain what I added to the code to make creating agents happen, each row of Rate
has one row of the CSV. 为了解释我添加到代码中以使创建代理发生的内容,
Rate
每一行都有CSV一行。 Now I want consumerAgent i
can be assigned Rate [i]
, but the way I did it using another argument 'ratingarray'
seems wrong, but I don't know how to correct it. 现在我想给
consumerAgent i
分配Rate [i]
,但是我使用另一个参数'ratingarray'
似乎是错误的,但我不知道如何纠正它。 The error tells me __init__() missing 1 required positional argument: 'ratingarray'
该错误告诉我
__init__() missing 1 required positional argument: 'ratingarray'
Looking forward to your comments, 期待您的评论,
Thank you very much, 非常感谢你,
import csv
import random
from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid
class ConsumerAgent(Agent):
def __init__(self, name, model, ratingarray):
super().__init__(name, model, ratingarray)
self.name = name
self.rate[i] = ratingarray
def step(self):
print("agent {} was activated".format(self.name))
# Whatever else the agent does when activated
class ProductEvalModel(Model):
def __init__(self, n_agents):
self.schedule = RandomActivation(self)
self.grid = MultiGrid(10, 10, torus=True)
for i in range(n_agents):
a = ConsumerAgent(i, self, rate[i])
self.schedule.add(a)
coords = (random.randrange(0, 10), random.randrange(0, 10))
self.grid.place_agent(a, coords)
def step(self):
self.schedule.step()
#self.dc.collect(self)
############################# Main #####################################
model = ProductEvalModel(100) #100 consumers
with open('RateVal.csv') as csvfile:
csvreader = csv.reader(csvfile,delimiter=',')
rate = []
for row in csvreader:
rateS = row
rate.append(rateS)
loopi = 1
while loopi < 101:
model.step()
loopi += 1
You are not passing in rate[i] attribute. 您没有传递rate [i]属性。
It should look something like this: 它看起来应该像这样:
import csv
import random
from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid
class ConsumerAgent(Agent):
def __init__(self, name, model, ratingarray):
super().__init__(name, model) #removed ratingarray is not part of the Agent
#superclass
self.name = name
self.rate[i] = ratingarray
def step(self):
print("agent {} was activated".format(self.name))
# Whatever else the agent does when activated
class ProductEvalModel(Model):
def __init__(self, n_agents):
self.schedule = RandomActivation(self)
self.grid = MultiGrid(10, 10, torus=True)
# you could also just pass it in as a parameter, but I usually make it an
#attribute of the model
self.ratingarray = [list of list with each row as a sublist \
(e.g.[[row1],[row2],[row3]])]
for i in range(n_agents):
a = ConsumerAgent(i, self, self.ratingarray[i]) #passes in row of data
#to be agent attribute
self.schedule.add(a)
coords = (random.randrange(0, 10), random.randrange(0, 10))
self.grid.place_agent(a, coords)
def step(self):
self.schedule.step()
#self.dc.collect(self)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.