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
Related
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
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.
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.
I want turtles to read and adopt data from csv file. I have written the following code: the problem is even-though the data gets loaded, i'm unable to make the individual turtles take on each of the income values. Any assistance to this effect would be appreciated
extensions [csv]
breed [households household]
households-own [income]
globals [income-data]
to setup
load-income-data
setup-households
end
to load-income-data
set income-data []
file-open "income.csv"
while [ not file-at-end? ]
[ set income-data sentence income-data ( file-read-line)
]
user-message "income data loading complete!"
file-close
end
to setup-households
create-households 700
ask one-of households
[ setxy random-xcor random-ycor
set income income-data
]
end
Have a look at the File Input Example in the NetLogo Model Library (Code Examples). You need to use a foreach to loop through the imported values / agents.
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