Netlogo - item expected input to be a string but got zero instead - csv

This is a follow-up question related to a previous post LinkI have data related to 16 laptop consumers' review ratings which are either satisfied (16 people) or dissatisfied (6 people). They are defined as turtles and they are distinguishable by asking if the boolean variable satisfied? or dissatisfied? is true.
The dataset is read as follows:
extensions [csv matrix array nw]
globals
[
rowcounter
csv
ii
Sc-headings Bat-headings Pr-headings income-headings average-headings;
Sc-set
Bat-set
Pr-set
prodcount ;num of producer agents
]
turtles-own [
turtle-Sc-list
turtle-Bat-list
turtle-Pr-list
turtle-income-list
turtle-average-list
review-set
satisfied?
dissatisfied?
LapUtl-set
ScPWU
BatPWU
PrPWU
]
to setup
clear-all
file-close-all
set rowcounter 1
proddata
readdataset
reset-ticks
end
breed [ producers producer ]
to go
Reviewrating
end
to intlz
set Sc-set []
set Bat-set []
set Pr-set []
end
Reading the dataset:
to readdataset
file-close-all ; close all open files
file-open "turtle_details.csv"
let headings csv:from-row file-read-line ;header is read
; Splitting headings of the csv file into 5 categories representing screen
; size data, battery charge data, Price data, income data, max age of an owner
set Sc-headings sublist headings 2 7
set Bat-headings sublist headings 7 12
set Pr-headings sublist headings 12 17
set income-headings sublist headings 17 18
set average-headings sublist headings 18 length headings
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set shape "person"
set size 2.5
ifelse rowcounter < 11
[
set color 125
set satisfied? true
set dissatisfied? false ;
]
;else
[
set color 65
set satisfied? false
set dissatisfied? true ;
]
setxy random-xcor random-ycor
; hide-turtle
set turtle-Sc-list sublist data 2 7
set turtle-Bat-list sublist data 7 12
set turtle-Pr-list sublist data 12 17
set turtle-income-list sublist data 17 18
set turtle-averageage-list sublist data 18 length data
]
set rowcounter rowcounter + 1
]
file-close-all
end
There are 3 producers who have some attribute levels for the screen, battery, price.
Sc Bat Pr
24 10 18000
18 6 22000
30 8 26000
to proddata
file-close-all ; close all open files
if not file-exists? "Prodinitattr.csv" [
user-message "No file 'Prodinitattr.csv' exists!"
stop
]
file-open "Prodinitattr.csv" ; open the file with the producers' initial attributes
let headings csv:from-row file-read-line
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-producers 1 [
hide-turtle
set producer? true ; this agent is a producer
set satisfied? false ; this agent is not a referrer ; REFERRERS
set dissatisfied? false ; this agent is not a pbuyer
set prodcount prodcount + 1
; set shape "house"
setxy random-xcor random-ycor
]
set Sc-set lput item 0 data Sc-set
set Bat-set lput item 1 data Bat-set
set Pr-set lput item 2 data Pr-set
]
file-close-all
end
The thing that should be extracted from the data set is the evaluation of consumers (reviews). Each consumer has a review-set which is at first an empty set ,[].Then it will keep thee values which corresponds to the three review values for each of the three producers.
to reviewrating
ask turtles [
set review-set []
]
ask turtles [
set ii 0
while [ii < 3 ][
set ScPWU turtle-Sc-rating item ii Sc-set
set BatPWU turtle-Bat-rating item ii Bat-set
set PrPWU turtle-Pr-rating item ii Pr-set
set LapUtl-set lput (ScPWU + BatPWU + PrPWU) LapUtl-set
set ii ii + 1
] ; while
];ask
end
to-report turtle-Sc-rating [Sc]
let pos position Sc Sc-headings
if is-number? position Sc Sc-headings
[
let turt-Sc-rate-value item pos turtle-Sc-list
report turt-Sc-rate-value
]
end
to-report turtle-Bat-rating [Bat]
let pos position Bat Bat-headings
if is-number? position Bat Bat-headings
[
let turt-Bat-rate-value item pos turtle-Bat-list
report turt-Bat-rate-value
]
;***************
end
to-report turtle-Pr-rating [Pr]
let pos position Pr Pr-headings
if is-number? position Pr Pr-headings
[
let turt-Pr-rate-value item pos turtle-Pr-list
report turt-Pr-rate-value
]
end
The problem is I cannot see consumers' LapUtl vector because of the error. I had reported another error previously here, but I changed where the "go" procedure was written, and now the error is marking this line :
let turt-Sc-rate-value **item** pos turtle-Sc-list
How can I resolve tihs?
Thank you,

I suspect you are not correctly reporting the error. I suspect the error is ERROR: ITEM expected this input to be a string or list, but got a number instead. Here is an example of a way to produce this error: item 0 0. If I am right, then you are running the code let turt-Sc-rate-value item pos turtle-Sc-list while turtle-Sc-list has a value of 0. In order to confirm this, replace this code with
ifelse (is-list? turtle-Sc-list)
[let turt-Sc-rate-value item pos turtle-Sc-list]
[error (word "turtle-Sc-list is not a list.")]
Now run your code. If it raises the error "turtle-Sc-list is not a list.", then you are ready to search for how you failed to initialize this variable correctly.

Related

Reading a specific line of a csv file based on "ID"

I have a NetLogo model and I would like to assign values to patches from a .csv file. The patches have a certain value that I want to use as an "ID" and if "item 0" in a line of that .csv file matches the ID, the rest of the values in the line would be assigned to the patches with that ID.
I made a workaround that creates invisible turtles for each line in the files and then assigning the values is no problem, but is there a way to do this directly using just the csv file?
EDIT: Here's what the workaround does, first opens a file and then creates "helper" turtles like this:
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set xcor 0
set ycor 0
set size 0
set color 0
set HPJid item 0 data
set A item 1 data
set B item 2 data
set iC item 3 data ] ]
Then I can just use:
ask patches [ let helper one-of turtles with [HPJid = HPJ of myself]
set D [A] of helper
set E [B] of helper
set F [C] of helper ]
Then all works, but I'd like a way to do this without the turtles.
Yes, having looked at your code, I think you can simply do:
while [ not file-at-end? ]
[ let data csv:from-row file-read-line
let in-ID item 0 data
ask one-of patches with [ID = in-ID]
[ set var1 item 1 data
set var2 item 2 data
]
]
or something like that anyway - with appropriate variable names of course

How to avoid creating an excess of agents when reading their characteristics from a CSV file

I have data related to 100 consumers as turtles who have rated laptops' features. The laptops have 2 kinds of features : size of the screen and battery life. Each has some levels. For example battery life has 5 hours, 12 hours, 24 hours, 30 hours. Data is stored in a csv file. For simplicity, here you see 2 consumers.
size12 size13.5 size14 size15 Battery5 Battery12 Battery24 Battery30
1 1 *2* 1 3 2 2 *4* 5
2 4 3 3 2 1 1 2 3
We access the data set to sum the rates of 2 levels of feature. For example for consumer 1 , what is:
The sum of rates of screen size of 13.5 + rate of battery life 24
Using the code below, this is achieved :
to CalculateSumRates
ca
reset-ticks
file-close-all
file-open "turtle_details.csv"
let headings csv:from-row file-read-line
set screen-headings sublist headings 0 4
set battery-headings sublist headings 4 length headings
let screen-to-evaluate 13.5
let battery-to-evaluate 24
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set turtle-screen-list sublist data 0 4
set turtle-battery-list sublist data 4 length data
set turtle-screen-eval turtle-screen-rating screen-to-evaluate
set turtle-bat-eval turtle-battery-rating battery-to-evaluate
set turtle-sum-eval turtle-screen-eval + turtle-bat-eval
]
]
file-close-all
end
to-report turtle-screen-rating [sc]
let pos position sc screen-headings
let turt-screen-rate-value item pos turtle-screen-list
report turt-screen-rate-value
end
to-report turtle-battery-rating [bc]
let pos position bc battery-headings
let turt-bat-rate-value item pos turtle-battery-list
report turt-bat-rate-value
end
Now I want to do something more. I need to consider a time interval. For example, in 20 years, how consumers change their ratings of some laptop features. To illustrate more, consumer 1 who has expressed her total ranking of size 13.5 and battery of 24, in year 2 (ticks = 2) got her laptop improved, so now we would like to know :
The sum of rates of screen size of 13.5 + rate of battery life **30**
I first created my go like this :
to setup
CalculateSumRates
end
to go
repeat 20 [
{ screen-to-evaluate changes and is no longer 13.5}
{ battery-to-evaluate also changes and is no longer 24}
; EDIT
set turtle-screen-eval turtle-screen-rating screen-to-evaluate
set turtle-bat-eval turtle-battery-rating battery-to-evaluate
set turtle-sum-eval turtle-screen-eval + turtle-bat-eval
; EDIT
tick
]
end
What is making trouble here is that, each time CalculateSumRates is called, it goes to this line :
create-turtles 1 [
So every year, 100 consumers are created from scratch while I need to monitor the behvavior of those 100 consumers at the beginning.
I then wrote 2 CalculateSumRates functions, called one in the set up. Renamed the function and put the other in the go. In order not to create an excess of consumers, I substituted create-turtles 1 [ with ask consumers [, hoping that now the csv is again read, but row by row is read when I say ask consumers, so I can find different values from the dataset. However, it is executing weirdly. I do not know how to modify that to avoid creating new consumers and losing the previous ones?
By adding the lines in the edit, I encounter an error telling me that I cannot use go in an observer context; go is turtle only!!
Thanks,
To give an example of what I meant in the comment above, check out this modified version of the setup that I suggested here.
extensions [ csv ]
globals [ screen-headings battery-headings ]
turtles-own [
turtle-screen-list
turtle-battery-list
turtle-screen-eval
turtle-bat-eval
turtle-sum-eval
turtle-row-number
;; New:
rating-each-year
]
to setup
ca
reset-ticks
file-close-all
file-open "turtle_details.csv"
let headings csv:from-row file-read-line
set screen-headings sublist headings 0 4
set battery-headings sublist headings 4 length headings
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set turtle-screen-list sublist data 0 4
set turtle-battery-list sublist data 4 length data
set rating-each-year []
]
]
file-close-all
ask turtles [
update-vals 12 5
set rating-each-year lput turtle-sum-eval rating-each-year
]
end
It's more or less the same, but there are some important changes like a new list called rating-each-year that is intended to let the turtles keep track of their rating each tick.
The reporters are mostly unchanged as well, except that update-vals is now a turtle-specific procedure so it must be called by ask turtles (or similar). Additionally, it takes two variables, one called screen? and one called battery?. You can then call the reporter by asking a turtle to: update-vals 12 24, and that turtle will then update its values for a screen size of 12 and a battery life of 24. I include all three reporters for completeness, but the other two have not changed from my answer to your other question:
to update-vals [ screen? battery? ]
set turtle-screen-eval turtle-screen-rating screen?
set turtle-bat-eval turtle-battery-rating battery?
set turtle-sum-eval turtle-screen-eval + turtle-bat-eval
end
to-report turtle-screen-rating [sc]
let pos position sc screen-headings
let turt-screen-rate-value item pos turtle-screen-list
report turt-screen-rate-value
end
to-report turtle-battery-rating [bc]
let pos position bc battery-headings
let turt-bat-rate-value item pos turtle-battery-list
report turt-bat-rate-value
end
So now, your turtles can at any time update their summed rating value according to the screen and battery combination that you have assigned them or that they have bought, however you are setting that up. Here is an example go procedure that every tick has them choose a random possible screen size and battery life to evaluate, then they add that summed rating value to their rating-each-year list. When 20 ticks go by, the procedure stops and the turtles show their lists in the command center (21 items long, since they include the value from setup as well).
to go
ifelse ticks < 20 [
ask turtles [
let screen-this-year one-of screen-headings
let battery-this-year one-of battery-headings
update-vals screen-this-year battery-this-year
set rating-each-year lput turtle-sum-eval rating-each-year
]
]
[
ask turtles [
show rating-each-year
]
stop
]
tick
end
In your model, you probably wouldn't have them randomly pick values of course- this was more to show what they're actually doing. I should also mention that "turtle_details.csv" is the same as the one I used for an example in the last question.

Using to-reports while creating agents from a csv file

My question is a bit long. I really appreciate it if you could please read it all and I'd be very grateful for any piece of advice.
I have data related to 2 consumers as turtles who have rated laptops' features. The laptops have 2 kinds of features : size of the screen and battery life. Each has some levels. For example battery life has 5 hours, 12 hours, 24 hours, 30 hours. Data is stored in a csv file.
12 13.5 14 15 5 12 24 30
1 1 2 1 3 2 2 4 5
2 4 3 1 2 1 1 2 3
I want to sum the rates of 2 levels of feature. For example for consumer 1 and 2, what is:
sum rate of screen size of 13.5 + rate of battery life 24
Since 13.5 and 24 may change later and I want to know for example the sum of rates of size of 14 and battery life of 5, I defined two functions using "to-reports" . Also since for example the value "12" is in the header row twice and represents both a size and a battery life, I made 2 subsets of the csv file one for screen size , the other for battery.
12 13.5 14 15
1 1 2 1 3
2 4 3 1 2
5 12 24 30
1 2 2 4 5
2 1 1 2 3
First in the main program, the csv file is read and a turtle is assigned to each row, expecting to have two consumers.
to setup
ca
reset-ticks
file-close-all
file-open "turtle_details.csv"
let headerrow csv:from-row file-read-line
set Sc 13.5 ; at the beginning I want the rate of this screen size
set Bat 24
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-consumers 1 [
set shape "person"
set k k + 1
print obtain-Sc (Sc) + obtain-Bat (Bat)
]
]
file-close-all
end
I assumed, first, row one is read and a consumer is generated. Now it goes to to-report to find obtain-Screen(13.5) which is 2, but I thought next time that obrain_Screen is called, again the csv opens and the cursor would still be at the beginning, but I want it to read the second row. And to extend it, I might need it to go further and further. To solve this, I defined a counter k that for example the first time this condition is checked : idx = 0 < k =1 so the first row is read. Then idx = idx + 1 , so nothing else is done.
to-report obtain-Screen[Sc]
file-close-all ; close all open files
file-open "turtle_detailsSc.csv"
let headings csv:from-row file-read-line
ifelse is-number? position Sc headings
[
while [idx < k ]
[ set fdata csv:from-row file-read-line
set idx idx + 1
]
report item position Sc headings fdata
]
[report 0.000000009]
Something similar for Bat. But it does not work and has errors. Any ideas how to improve to-reports?
Thanks
Edit
Considering the data set to be like this :
size12 size13.5 size14 size15 Battery5 Battery12 Battery24 Battery30
1 1 *2* 1 3 2 2 *4* 5
2 4 3 3 2 1 1 2 3
I can now access the data set and for each consumer find their evaluation of a laptop they have purchased. For example, consumer 1 has a laptop with size of 13.5 and battery life 24.
Consumer 1 evaluation of size 13.5 = 2
Consumer 1 evaluation of battery 24 = 4
Overall evaluation of laptop = 2 + 4 = 6
I have defined a procedure "Find Eval" that when I need to know the evaluation of different consumers, it enables me to access the dataset and find the values.
To explain the data in the table more, a consumer has a laptop so can evaluate it pretty well, but for other features like how he evaluates a laptop with the screen size of 15, he might have some exposure or just by hearing from others he has filled in the table.
I want to keep these 2 consumers and monitor their attitudes about laptop features during 20 years. In year 2, consumer 1 possibly updated his system, so now his battery life is 30. This time what I need to do is to access the dataset and calculate
Consumer 1 evaluation of size 13.5 = 2
plus
Consumer 1 evaluation of battery 30 = 5
Overall evaluation of laptop = 2 + 5 = 7
This time, I need to go to find the value for battery 30.
I think when I repeat my code for 20 years, create-consumer will be repeated each time that I want to work with the dataset, so instead of keeping 2 consumers, each year, some new consumers will be created and the previous ones will be totally replaced.
The question is how I can create consumers once, but can access any data point in the data set for many times?
Thanks a lot,
In response to your comment- got it, thought that might be the case. I will present an alternative approach that may or may not suit you, but it's probably how I would tackle this. The main difference is that I would store the lists for screen and battery ratings in a turtle variable, so you can easily access them after the fact without having to track counters. Given these variable declarations:
extensions [ csv ]
globals [ screen-headings battery-headings]
turtles-own [
turtle-screen-list
turtle-battery-list
turtle-screen-eval
turtle-bat-eval
turtle-sum-eval
turtle-row-number
]
I would split headings of the csv file to the screen-headings and battery-headings globals.
Then, I would iterate through the next lines of the csv, as you did below, and split the lines in a similar way but into the turtles-own variables turtle-screen-list and turtle-battery-list. That way, each turtle is aware of its own ratings for each screen and battery so you can modify your evaluated screen or battery as needed.
Set the screen and battery of interest with screen-to-evaluate and battery-to-evaluate (or use a Chooser on the interface), then use a reporter (similar to what you had set up) that checks the position of the battery and screen of interest to return the current turtle's rating for each- this will set turtle-screen-eval and turtle-bat-eval. Finally, sum those last two values.
to setup
ca
reset-ticks
file-close-all
file-open "turtle_details.csv"
let headings csv:from-row file-read-line
set screen-headings sublist headings 0 4
set battery-headings sublist headings 4 length headings
let screen-to-evaluate 13.5
let battery-to-evaluate 24
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set turtle-screen-list sublist data 0 4
set turtle-battery-list sublist data 4 length data
set turtle-screen-eval turtle-screen-rating screen-to-evaluate
set turtle-bat-eval turtle-battery-rating battery-to-evaluate
set turtle-sum-eval turtle-screen-eval + turtle-bat-eval
]
]
file-close-all
end
to-report turtle-screen-rating [sc]
let pos position sc screen-headings
let turt-screen-rate-value item pos turtle-screen-list
report turt-screen-rate-value
end
to-report turtle-battery-rating [bc]
let pos position bc battery-headings
let turt-bat-rate-value item pos turtle-battery-list
report turt-bat-rate-value
end
Of course, you can condense some of this as needed. I tested this setup with 4 rows (patterned after your example data) and it seemed to work ok. If you have a huge number of rows it might not work so well.
Edit:
If you end up replacing screen-to-evaluate and battery-to-evaluate with interface choosers (which I recommend- it allows you to quickly see the different values), you can update to the new values with something like:
to update-vals
ask turtles [
set turtle-screen-eval turtle-screen-rating scrn
set turtle-bat-eval turtle-battery-rating batr
set turtle-sum-eval turtle-screen-eval + turtle-bat-eval
]
end
where scrn and batr are the names of the choosers.
Or, if you want them to dynamically update, you can make an interface monitor that reports the following reporter :
to-report update
ask turtles [
set turtle-screen-eval turtle-screen-rating scrn
set turtle-bat-eval turtle-battery-rating batr
set turtle-sum-eval turtle-screen-eval + turtle-bat-eval
]
report "Dynamically updating."
end
With that one, as soon as you change the Chooser value, the turtles should immediately update their turtle-bat-eval, turtle-screen-eval, and turtle-sum-eval. Fun!
Edit 2
If your csv includes a column for row numbers like this:
row 12 13.5 14 15 5 12 24 30
1 1 2.0 1 3 2 2 4 5
2 4 3.0 1 2 1 1 2 3
I would recommend creating a turtle variable to store the row number used. I've now included one called turtle-row-number. Then, you would just have to include a line to update that variable from the first item in the list, which is the row number, and then modify your sublist values accordingly, to something like:
to setup-row-nums
ca
reset-ticks
file-close-all
file-open "turtle_details_2.csv"
let headings csv:from-row file-read-line
set screen-headings sublist headings 1 5
set battery-headings sublist headings 5 length headings
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set turtle-row-number first data
set turtle-screen-list sublist data 1 5
set turtle-battery-list sublist data 5 length data
]
]
update-vals
file-close-all
end
Where update-vals is as shown above.

creating agents using the CSV example in the models library in Netlogo

I have a question about the number of turtles created from this code :
to read-turtles-from-csv
file-close-all ; close all open files
if not file-exists? "turtles.csv" [
user-message "No file 'turtles.csv' exists! Try pressing WRITE-TURTLES-TO-CSV."
stop
]
file-open "turtles.csv" ; open the file with the turtle data
; We'll read all the data in a single loop
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
print "item column 4"
show item 4 data
]
]
file-close ; make sure to close the file
end
My turtles.csv file has only two rows, so I What is expected here is that create-turtles 1 is repeated for the number of rows and I have two agents for which 2 numbers in the 4th column get printed. Surprisingly though, 4 turtles are created! Why?
Thanks
I'm wondering if your turtles.csv is being read in as having more lines than it should? Try doing something like:
to read-file
file-close-all
file-open "turtles.csv"
while [not file-at-end?] [
print csv:from-row file-read-line
]
file-close-all
end
to see how Netlogo is reading the file. It seems like you're on the right track, otherwise- I just tested a similar file following your example and I got my two turtles as expected. Using a .csv file called "turtle_details.csv" that looks like:
color size heading
1 red 2 90
2 blue 4 180
I used this code to produce two turtles with the variables in the .csv:
extensions [ csv ]
to setup
ca
reset-ticks
file-close-all
file-open "turtle_details.csv"
;; To skip the header row in the while loop,
; read the header row here to move the cursor
; down to the next line.
let headings csv:from-row file-read-line
while [ not file-at-end? ] [
let data csv:from-row file-read-line
print data
create-turtles 1 [
set color read-from-string item 0 data
set size item 1 data
set heading item 2 data
]
]
file-close-all
end

How do I set the number of turtles in each patch using a .csv file full of data?

Using some 35x12 matrix full of data in a .csv file, is there an easy way to import this into Netlogo and set the number of turtles equal to the elements of the matrix?
one way to do it is :
first read the file into a list , then use location of each item and see if a patches pxcor AND pycor matches the location of the item in the list, then I set that number as plabel for double checking the number of created turtles:
extensions [csv]
globals [li]
to setup
clear-all
resize-world 0 34 0 11
set-patch-size 30
load-File
ask patches [
set plabel item (pycor) item (pxcor) li
sprout item (pycor) item (pxcor) li
]
end
to load-File
set li []
file-open "t.csv"
if file-at-end? [ stop file-close ] ;; protect against end of file
while [not file-at-end? ]
[
let _line (csv:from-row file-read-line ",")
set li lput _line li
]
end