I have an issue with load-data command. I manage to setup my model, shows up quite nicely. Then I need to load the data before I press "go". It does not work though and I have no idea why.
So I already defined my turtles as part of the setup and by the following code, I am trying to give the turtles properties from the csv document, but it says: "Expected a literal value. (line number 1841, character 5) error while turtle 1606 running file-read
called by procedure load-data
called by button "load-data" "
There are exactly 1841 respondents btw. I tried txt instead but no change. I ran out of ideas quite quickly.
Pretty please, does anyone have any idea what I might be doing wrong? I am quite new to this and pretty desperate since I am working completely on my own and my thesis advisor is not responding...
to load-data
file-open "Pokus2.csv"
while [not file-at-end?] [
ask turtles [
set n-turtles file-read
set behaviour file-read
set attitude file-read
set size 1.5]
ask turtles [setxy random-xcor random-ycor]
ask turtles
[if behaviour <= 0.33 [set pcolor red]
if behaviour = 0.34 and behaviour < 0.66 [set pcolor orange]
if behaviour >= 0.67 [set pcolor green]
]
]
file-close
show "file read"
end
Related
I'm new to NetLogo and I have a question that I'm sure is pretty basic. But, I'm not getting over the difficulty.
If anyone can help me overcome this difficulty, I would be very grateful.
I would like from the patch where the turtle is found to consider the 8 neighboring cells in search of the highest pveg value. If it has equally high values, choose 1 of these randomly. Upon finding the highest pveg value of the neighbors, the turtle went there.
I am using the command: max-one-of. I think it serves my purpose. But, I'm making some syntax error that shows the following error: MOVE-TO expected input to be an agent but got NOBODY instead.
Thanks in advance
extensions [ gis ]
globals [ veg ]
patches-own [pveg]
to setup
clear-all
reset-ticks
setup-patches
crt 1 [
ask neighbors [ set pcolor blue ]
set color black
]
end
to setup-patches
end
to go
ask turtles [neighboring]
end
to neighboring
let my-neighWith-pveg [ neighbors with [pveg > 0.2] ]of patch-here
ifelse neighWith-pveg = 0
[ ]
[ move-to max-one-of patches [my-neighWith-pveg] set pcolor red ;;ERROR HERE
]
end
The NetLogo dictionary says, max-one-of needs an agentset and a reporter as input:
max-one-of agentset [reporter]
In your code, you use two agentsets: turtles and my-neighWith-pveg
Since you want to chose from the neighbors (and not all turtles) with the hightes pveg, you can write:
max-one-of my-neighWith-pveg [pveg]
I want to have my turtles move back and forth between a central area and their starting location. I have set the central area (patch 0 0, and its neighbouring patches). I have set these turtles to begin from random locations on setup.
Now I need them to move to the central area and be able to remember and return to their respective starting positions. Here is my attempt, but one that is not working.
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
create-turtles 10
[ set shape "car"
set size 1
set color white
setxy random-xcor random-ycor
if (patches != patches with [pcolor = green])
[ set start-position (random-xcor random-ycor)] ;; line with error
]
to go
ask turtles
[ set heading target-patch move-to target-patch
set hold-time hold-time + 5
]
ask turtles
[ if hold-time >= 10
[ set heading start-position move-to start-position]
]
end
There are several problems with your code. I strongly suggest that you code in smaller pieces. That is, add some code and make sure it works before writing the next piece. Making sure it works is not just making it through without error messages, it needs to do what you expect it to do.
On your specific question. The line if (patches != patches with [pcolor = green]) is causing an error. First, patches is the set of all patches, not just a particular patch. So you are (sort of) asking whether the set of all patches is not equal to the set of patches that are green. Is that really what you intended? If so, it is easier to simply ask whether there is any patch that is not green:
if any? patches with [pcolor != green]
or to check whether they are all green and continue if not:
if not all? patches [pcolor = green]
However, since you are asking about moving back and forth to and from the central green patches, I think you really want to have the turtle check whether the patch they happen to be located on is green. This code looks at the patch where the turtle is located (patch-here) and checks whether the color (pcolor) is green:
if [pcolor] of patch-here = green [ ]
However, one of the tricks of NetLogo is that turtles can access the variables of the patch they are on directly. Note that a patch cannot access a turtle's variables because there may be multiple turtles on the patch so the patch doesn't know which turtle you want. But a turtle can only ever be on one patch at once. So you could write:
if pcolor = green [ ]
You also need to rethink this code:
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
This suggests to me that you have misunderstood something very fundamental to NetLogo programming. You need to think from the perspective of an individual agent. Looking at this code, you first do ask turtles, so that is going to run through all the turtles in random order. Let's call them A, then B, then C and so on.
What is each turtle going to do? Everything in the [ ]. So, A sets the value of the global variable named "target-patch" to patch 0 0. Then A asks that patch to turn green, have the 8 surrounding patches to turn green, and to set the variable "hold-time" to the value 5.
So far, so good. But then turtle B does exactly the same thing - it assigns "target-patch", turns it and its neighbors green etc. Then turtle C. If you have 100 turtles, this block of code will run 100 times and do exactly the same thing each time.
to setup-lava
ask n-of lava patches [set pcolor orange]
ask men [die]
end
When I ask the men to die when they stand on a lava patch Netlogo gives me and error saying 'expected a number here, rather than a list or block'.
You haven't told us what lava is, but the error message suggests it is not a number. If you look in the NetLogo Dictionary, you will see that n-of must be followed by a number and then the agentset to tell NetLogo how many of the agentset to select.
Choice 1: assumes that lava-patches is a variable with a number in it (eg a slider on your interface)
to setup-lava
ask n-of lava-patches patches
[ set pcolor orange
ask men-here [die]
]
end
Choice 2: lava? is an attribute of patches that flags whether it is lava and that you have (somewhere else in your code) set that flag for lava patches
to setup-lava
ask patches with [lava?]
[ set pcolor orange
ask men-here [die]
]
end
Neither of these are tested
upon compilation I receive an error message saying set is the wrong commander before patch size 10 what commander should I use instead and why?
globals[road?]
to setup
clear-all
ask patches [set pcolor green]
end
to go
if mouse-down?
[ ask patch mouse-xcor mouse-ycor [ edit-world ]
end
to edit-world
if EDIT_TOOL = "Road"
[set pcolor grey
set patch-size 10
]
end
The right command is set-patch-size, with the hyphen after set.
Patch-size can not be changed programatically but only in settings.
All patches have the same size.
What are you trying to do simulation wise with that line? Patch-size should not effect any outcomes.
Edit: I was wrong
Set-patch-size changes the size of all patches i.e. scales the view.
I am trying to make my turtle change the color of a patch when it comes into contact with it and have tried the following code:
to deesculateviolence
ask turtles [
if pcolor = red [set pcolor blue]
]
end
The code does not come up with any errors but when I play the model, the color of the patch does not change. I have tried similar codes from different models and still cannot get the patch to change color. If anyone knows where I am going wrong I'd really appreciate your help.
I think your code does the right thing :
to setup
clear-all
create-turtles 5 [
move-to patch random 20 random 20
]
ask n-of 25 patches [set pcolor red]
reset-ticks
end
to go
ask turtles [
rt random 10
fd 1
if pcolor = red [set pcolor blue]
]
tick
end
you can see the effect in following example better