0

我有一个 CSV 文件,其中存储了消费者对不同生产商的几台笔记本电脑的评分(0:最低到 5:最高)。每行显示一位消费者对不同功能的评分。下面显示了 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

我正在使用mesa创建一个基于代理的模型来评估购买不同的新笔记本电脑。假设消费者数量为 100,CSV 文件有 100 行和 6 列,用于 2 个功能(屏幕大小和电池)。当在 a 中逐行读取 CSV 时for loop,我希望可以为每个代理(消费者)分配一行的CSV(标题也应该存储),我可以知道谁(哪个代理)有哪一行。我这样做了,主要是按照mesa'stutorial to build the model and agent classes mesa tutorial

为了解释我添加到代码中以使创建代理发生,每一行Rate都有一行 CSV。现在我想要consumerAgent ican be assigned 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
4

1 回答 1

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)
于 2018-10-29T17:24:18.397 回答