Creating agents from a CSV file in mesa - python

535 Views Asked by At

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. An example of 2 consumers is shown below:

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. 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

To explain what I added to the code to make creating agents happen, each row of Rate has one row of the 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. The error tells me __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
1

There are 1 best solutions below

0
On

You are not passing in rate[i] attribute.

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)