Reputation: 31
I have a question. I have start using NetLogo recently and I have to finish an assignment for my study with NetLogo, however I have a problem. I want to use data from an Excel file in a model. The dataset contains two rows of numbers each row representing a different variable, say a and b, and I want to assign to each turtle a set of those two variables, so that each turtle has a value for variable a and b. However I have no idea how to do this. I’ve succeeded in loading the dataset into the model by converting it to a txt file. The rows in the .txt file are divided by a tab. This is de code I used for loading the dataset in the model:
globals [ turtle-data ]
turtles-own [ awareness income housingtype adopt ]
to setup
clear-all
reset-ticks
create-turtles 11557
ask turtles [
set color white
]
ask turtles [
setxy random-xcor random-ycor
]
load-turtle-data
assign-turtle-data
end
to load-turtle-data
ifelse ( file-exists? "input-data.txt" ) [
set turtle-data []
file-open "input-data.txt"
while [ not file-at-end? ][
set turtle-data sentence turtle-data (list (list file-read file-read))
]
user-message "File loading complete!"
file-close
]
[
user-message "There is no input-data.txt file in current directory!"
]
end
to assign-turtle-data
assign-income
assign-housingtype
end
to assign-income
foreach turtle-data [
ask turtles [ set income item 0 ? ]
;link to turtle-data
]
end
to assign-housingtype
foreach turtle-data [
ask turtles [ set housingtype item 2 ? ]
;link to turtle-data
]
end
How can I link the values in the dataset to the right variables of the turtle? Alternative solutions for my problem are also welcome.
Upvotes: 3
Views: 742
Reputation: 12580
Nice job figuring out the data import part. I've actually been working on an extension to do exactly that, but it looks like you don't even need it! Now, for your question:
Rather than creating a bunch of turtles (I assume there's one per data row), I would create the turtles one-by-one as you iterate through the data:
to setup-turtles
foreach turtle-data [
crt 1 [
set income item 0 ?
set housingtype item 1 ?
]
]
end
This simplifies the problem of assigning the data to individual turtles without having to deal with indices or who
numbers (which other solutions require). It also makes it so that the number of turtles will automatically adjust if you add or remove data.
Upvotes: 3