我有一个CSV文件,其中存储了消费者对不同生产商的几台笔记本电脑给出的评分(从0:最低到5:最高)。每一行显示了一位消费者针对不同功能给出的评分。下面显示了两个消费者的示例:
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
我正在使用mesa
创建一个基于agent_的模型来评估不同新笔记本电脑的购买。假设消费者数量为100,则CSV文件有100行和6列,用于2种功能(屏幕尺寸和电池。在for loop
中逐行读取CSV时,我希望每个代理商(消费者)可以分配给CSV的一行(标题也应该存储),并且我知道谁(哪个代理)拥有哪一行。我这样做的主要是根据mesa
的教程来建立模型和代理类mesa tutorial
为解释我添加到代码中以使创建代理发生的内容,Rate
的每一行都有一行CSV。现在我希望可以为consumerAgent i
分配Rate [i]
,但是我使用另一个参数'ratingarray'
的方式似乎是错误的,但是我不知道如何纠正它。错误告诉我__init__() missing 1 required positional argument: 'ratingarray'
期待您的评论,
非常感谢您
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
答案 0 :(得分:0)
您没有传递rate [i]属性。
它应该看起来像这样:
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)