Set Turtle to Specific Grid Location - NetLogo - arrays

I have seen similar questions but haven't found what I needed yet...
Was wondering if anyone knew how to set specific locations for turtles at NetLogo Setup?
Currently tried:
to setup
create-turtles 5
set turtles at-points [[-12 20 ] [-11 19] [-12 18 ] [-18 18 ] [-11 17]]
but they're all showing up at [0 0]
Thanks!

This can be done in many ways:
You can use setxy in the create-turtles command block. In most models you will see this is used in combination with random-xcor and random-ycor to give each turtle a random location, but you can also use it in any other way.
You can use move-to to ask a specific turtle or group of turtles to go to a specific agent (e.g. to a specific patch).
You can use sprout to directly ask a specific patch or group of patches to create turtles there.
In any case, I suggest you take a look at the NetLogo Programming Guide and the NetLogo Dictionary

Related

Netlogo: How to iterate over agentset and set variable speed

I am attempting to iterate over a set of turtles and assign each of them a different, random speed. When I attempt to use:foreach turtles [ ... ] I get an error message stating "cannot iterate over agentset". I know I can use ask for setting all turtles the same, but I want to have turtles moving at different speeds from one another.
ask can do this job just fine:
ask turtles [
set speed random 10
]
this will give each turtle its own different, random speed.

Netlogo while loop only one time

I have a small problem in Netlogo, which I began to work on only a few days ago, I try to make a maze with two adventurers, and I make them go from two different starting locations, to one final location. All the beginning of my code works fine, to draw my maze, but when I want to make the adventurers go, only one of them goes in the right direction and find the exit, and the second doesn't even go in the asked direction (East).
I think the problem is in my GO procedure, but I can't reach to find a solution...
Here is my code, I work on Netlogo 5.2
to createaventurier
create-aventuriers pointsdepart
[set shape "person"
set color pink
set size 1
move-to one-of patches with [pcolor = green]
ask patch-here
[set pcolor blue]
set beta ycor
]
show count aventuriers
end
Here the program does what it's supposed to do.
to go
set i 0
createaventurier
while [i < pointsdepart]
[show count aventuriers
ask one-of aventuriers
[set heading 90
execute]
set i i + 1
]
show count pas
end
And it's here that the program return that there are no adventurers (no agents or agentsets) while the observer returns me that there are two of them (when I want two adventurers). I Breed-ed them at the beginning of the code, and I used a lot of while loops in other procedures, which worked perfectly.
I'm not at ease with the software, I'm just looking for a simple explanation, (I'm not so good in english too).
If you need some other parts of my program I can post it, but I don't think they'll be needed. If you need more informations I can also post it, but I hope I have been clear enough.
I thank you in advance.
Here is a simplified version of your code. I have changed adventurers into turtles so I didn't need breeds and hard-coded the number 2 for your variable pointsdepart. It works fine, in the sense that there are always 2 turtles.
to setup
clear-all
ask n-of 20 patches [set pcolor green]
reset-ticks
end
to make-agents
create-turtles 2
[ set shape "person"
set color pink
set size 1
move-to one-of patches with [pcolor = green]
ask patch-here [ set pcolor blue]
]
show count turtles
end
to go
let i 0
make-agents
while [ i < 2 ]
[ show count turtles
ask one-of turtles
[ set heading 90
forward 1
]
set i i + 1
]
show count turtles
end
This suggests that the problem is in your execute function (which I replaced with forward 1).
Running my code will demonstrate a logical problem. You are looping through (twice in this example) and running ask one-of in each loop. one-of selects a random turtle, so you might get them each to run your execute code once, or you might have the same turtle chosen each time. It is very likely you want code that looks more like this:
to go
make-agents ; note - should really be in setup, not go
ask turtles
[ set heading 90
forward 1
]
show count turtles
end
Also, you would generally have a tick command at the end of the go procedure to advance the clock and then the go procedure is run again so the turtles continue to move etc. This is why I have commented that the call to create the adventurers should really be in the setup procedure, otherwise another 2 adventurers will be created each time the clock advances.
The setup procedure is for everything that needs to be in place for the beginning of the simulation (eg creating adventurers, setting up your maze, giving initial resources to adventurers). The go procedure is for the actual process that is being simulated (eg moving, obtaining resources from the environment, depleting energy).

How to randomly generate turtles in netlogo that do not overlap/touch

I am building a parking model and am having trouble when I set up my model. Users are allowed to select the number of parked cars and I currently have the program randomly generating these turtles on rounded random x-coordinates (ensuring all turtles are on only one space/patch). The problem is multiple turtles can be located on the same patch. Is there a way to create these turtles so that they do not lay on the same patch, while randomizing the x-coordinate? If not, I was envisioning a do-until type of loop to keep moving turtles forward until the patch the turtle was on had no other turtles on it.
If the loop is the clear solution, I have never used a loop in this program, and would really benefit from seeing an example, but if that is too much to ask I'm sure I could find one posted online somewhere.
to setup-turtles
ask n-of 10 patches with [pycor = -1] [
sprout 1
]
print max [count turtles-here] of patches
ask turtles with [who > 0]
[set color blue
set shape "car"
set heading 90
set xcor round random-xcor
set ycor -1 ;; this ycor indicates that it is in the parking lane
set pcolor red
]
Another solution approach would be to use n-of, so e.g. to make 20 turtles,
ask n-of 20 patches with [pycor = 0] [
sprout 1
]
So for example if we test it with:
to test
clear-all
ask n-of 20 patches with [pycor = 0] [
sprout 1
]
print max [count turtles-here] of patches
end
the result printed is always 1.
Your best bet would be to use the turtles-here primitive. Check out the following which will only add turtles to patches on pycor 1 if there's no other turtles on the patch:
let new-patch one-of patches with [ (pycor = 1) and not any? turtles-here ]
if new-patch != nobody [
ask new-patch [ sprout 1 ]
]
See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles-here

NetLogo - How to see the number of patches that contain turtles

This seems like it should be very simple and have a primitive command for, however I cannot seem to find one in the netlogo dictionary.
I just want to report back a number, which is the number of patches which have one or more turtles on.
I thought about using the "turtles-here" command, but that returns an agentset rather than something I could use such as a boolean or value.
Anyone know a way of doing this?
Simply:
count patches with [ any? turtles-here ]

NetLogo dynamic Manhattan distance

I am trying to implement some form of AI into my Net Logo game.
I am planning on calculating the Manhattan distance from a zombie turtle to a human turtle.
So far I have managed to calculate the Manhattan distance from the two and draw the path and also move the zombie agent along the calculated path towards the human turtle.
The problem I am facing now is that the human agents location is going to be controlled by the user. The Manhattan distance uses a while loop and doesn't break out of the loop until the it has reached the human agent. I would like the zombie agent to move one step towards the human agent and then let the human agent move.
Code so far Net Logo Game
I am not sure what you are trying to do in your plot-manhattan-distance procedure. It seems like a complicated approach to something that should be relatively simple, but maybe I misunderstood your purpose. Here is how I would approach the whole problem:
globals [
zombie
human
]
to setup
clear-all
ask n-of 2 patches [ sprout 1 ]
set human turtle 0
ask human [ set shape "person" ]
set zombie turtle 1
ask zombie [ pen-down ]
end
to go
ask human [ flee zombie ]
ask zombie [ pursue human ]
end
to pursue [ target ]
face target
set heading first sort-by [abs (?1 - heading) < abs (?2 - heading)] [0 90 180 270]
fd 1
end
to flee [ pursuer ]
face pursuer
rt 180
fd 0.5
end
The meat is in the pursue procedure. Since the zombie cannot predict where the human is going to go, it is just trying to move in its general direction. It starts by facing the human directly (with the handy NetLogo face primitive). But since it is presumably only allowed to move in one of the four cardinal directions, it must choose the one that is the most desirable, i.e.: the one that is the least different from its current (ideal) heading. This is what the sort-by [abs (?1 - heading) < abs (?2 - heading)] [0 90 180 270] expression does: it sorts the list of directions by comparing their absolute difference with the current heading. The first item of this sorted list will be the one with the smallest difference, and thus, the one that the zombie needs to use.
In the current implementation, the human just tries to move away from the zombie, but you could easily replace that with player control code.

Resources