NetLogo: Make turtles fear each other - netlogo

As a NetLogo learner (beginner) I am working on a small project. I was trying to write a small code were I would like to make the turtles fear each other. So walk away from each other. Can anybody please assist met with this? Thanks in advance

In the Flocking model in the NetLogo Models Library (under Sample Models -> Biology), the birds are attracted to each other (if they drift far apart) yet also repel each other (if they get too close). You might try just taking Flocking and moving the sliders, or tweaking the code, to get the behavior you're asking for?
You might also look at Heatbugs (also under Biology iirc), where some of the bugs seek company and others want to avoid it.
Finally, check out the Scatter model, under Social Science -- the turtles all move away from each other.

You can use layout-spring. Usually it is used in combination with links, but works also without them. Minimal example:
to setup
clear-all
create-turtles 20 [
setxy random-xcor random-ycor
]
reset-ticks
end
to go
layout-spring turtles no-links 0.2 5 1
tick
end

I have used
ask turtles [face one-of other turtles bk .1]
and
ask turtles [if any? other turtles in-radius 4 [face towards one-of other turtles in-radius 4 bk .1]]
or when I wanted turtles to scare other turtles
ask turtles [ask other turtles in-radius 2 [face myself bk .1]]

Related

Can't run Netlogo code - asking turtles to look around themselves and select the lowest patch variable

I am trying to get my turtles to look around themselves in netlogo and select a patch with the lowest slope variable in radius 2 - and if there isn't one to just select any patch.
I check my code, and it says everything is fine, but when I run it I keep getting this error: this code can't be run by a patch - error while patch X X running ifelse - called by procedure move - called by procedure go - called by button "go"
This is my move code:
to move
ask turtles [ask patches in-radius 2
[ifelse slope < 5
[fd 1]
[move-to one-of patches in-radius 2]
]
]
end
I have already tried downhill and downhill4 but my agents seemed to get stuck at the bottom of the slope and couldn't move anywhere.
Thank you for any help and advice!
Thank you - my code now works!
Because you are asking turtles to ask patches, the code inside the ask patches is run by the patch! A patch can’t use turtle variables, and doesn’t know that you mean to refer to the variables of the turtle that asked the patch.
This is what “of myself” is for. It lets an agent (self) talk to the agent that is telling it what to do. You’ll see stuff like “set heading [heading] of myself” But that’s not what you need here.
we could use an ask patches like you are doing here, but we really don’t want them to do anything, and it’s going to make the code much more complex looking. Anyway, We just want to find a patch that meets the turtle’s needs and move to it.
So instead. We can query the nearby patches using a WITH and store the set of patches found in a “patch set” variable.
If there are any, we can move to one of them.
Else, we can just move forward.
So
To move
Ask turtles
[
;; here the turtle tells patches to test the patch’s own slope
Let good-spots patches in-radius 2 with [ slope < 5 ]
;; are there some patches that pass the test?
If-else any? Good-spots
[ ;; Yes, pick one and go there
move-to one-of good-spots
]
[ ;; No, just move forward
Fd 1
]
]
End
Edit to add : Matteo’s answer correctly identifies the actual requirement, based on the question: move directly to the flattest patch, versus what the code above does, move to one of the flatter patches, if there is one.
Move-to min-one-of patches in-radius 2 [ slope ]
As you noted, This is similar but not identical to
Downhill slope
And neither of these may prevent turtles from getting stuck in a pit. You may need more code to detect pits and jump out of them.
The problem is not about variables but about commands: you are asking patches to run forward or to run move-to, while these are turtles' commands.
Now, for the purpose of the question, there is another issue: you said that you want
turtles to look around themselves in netlogo and select a patch with the lowest slope variable in radius 2 - and if there isn't one to just select any patch
However, even if we ignore the problem about asking patches to run forward or move-to, the structure of your code implies something very different. Your code seems to intend:
Turtles, check if there are any nearby patches with slope lower than a specific threshold;
If there are, move forward in whatever direction you are already facing;
If there are not, move to a random nearby patch.
The solution below is based on the assumption that what you want is what you said, and not what your code seems to imply.
The solution is very simple and only needs min-one-of (check it here):
to move
ask turtles [
move-to min-one-of patches in-radius 2 [slope]
]
end
As a demonstration, consider this full example:
patches-own [
slope
]
to setup
clear-all
ask patches [
set slope random 10 + 5
set pcolor scale-color green slope 5 14
]
create-turtles 10 [
setxy random-xcor random-ycor
set color yellow
]
end
to go
ask turtles [
move-to min-one-of patches in-radius 2 [slope]
]
end
You will see that turtles tend to go to darker patches, that are those with lower slope.

How to repeat the same setup on Netlogo?

I have a simulation in netlogo in which there is a setup for turtles all around the world.
The thing is when I create turtles, the go into random places.
How can I make them fix ?
note that I cannot specify xcor and yxor for every turtle as I have hundreds of them.
To setup-people
tick
set-default-shape people "person"
ask n-of 185 (patches with [pcolor = black]) [sprout-people 1]
ask people[ set color cyan ]
ask people [ set points 2 ]
reset-ticks
end
One way to do this is with the with-local-randomness command.
breed [ people person ]
people-own [ points ]
To setup-people
clear-all
set-default-shape people "person"
with-local-randomness [
random-seed 0
ask n-of 185 (patches with [pcolor = black]) [sprout-people 1]
]
ask people [ set color cyan ]
ask people [ set points 2 ]
reset-ticks
end
If you don't have a clear idea of what's happening here, I would strongly suggest reading the section on random numbers in the NetLogo programming guide.
The basic idea is that NetLogo will always use the same sequence of random numbers within the little block of local randomness, but it won't affect the rest of your model, so if you have other random behaviours, they will still vary from run to run.
That being said, how important is it that your people are always placed in the same location? Agent-based models usually have lots of random elements. If that makes you uncomfortable, it might be because you haven't fully taken stock of that yet. Just something to keep at the back of your mind as you move forward with your model design...
Note: I've replaced tick with clear-all at the top of your procedure, as I believe this is probably what you meant to write.

How to have each turtle share the same links as their link-neighbors

I've been trying to write a model where turtles create links with a certain number of other turtles, and for those turtles to create links with the link-neighbors of the turtle that linked to it. Right now, I can get turtles to create links with other turtles, but they don't share the same link-neighbors. I want to end up with little sub-groups of turtles of a certain group size. However, at the moment I can only tell turtles to create a certain number of links, but they don't end up in sub-groups because their link-neighbors don't necessarily share the same link-neighbors. I thought I could ask each turtle to ask their link-neighbors to create-link-with [link-neighbors] of myself. I think this would work, except I get an error saying a turtle can't link with itself. I've tried to change the code so it says to a turtle ask link-neighbors [create-link-with [link-neighbors] of myself with [who != self]], but this doesn't work either.
Here is some of my code:
;group size is 1 + (count link-neighbors)
;preferred-size is a slider, used to alter what group size I want turtles to be in
ask turtles
[if (preferred-size > group-size) and (any? other turtles in-radius 1 with [preferred-size > group-size])
[create-link-with one-of other turtles in-radius 1 with [preferred-size > group-size]
ask link-neighbors
[create-links-with [link-neighbors] of myself]
]
Also, is there a term like link-neighbors but referring to all the turtles on a string of connections?
Any help would be greatly appreciated!
You were very close! All you need is other:
create-links-with other [ link-neighbors ] of myself
You cannot compare who to self: who is a number and self is a turtle. And in the context of with [ who != self ]], they would always be variables of the same turtle. In any case, it's usually better to avoid dealing with who numbers anyway: there's almost always a better way to do things.
is there a term like link-neighbors but referring to all the turtles on a string of connections?
I'm not entirely sure that's what you mean, but maybe nw:turtles-on-path-to? Or perhaps you will find some other useful thing in the nw extension.

How to create a fixed number of turtles at a certain point?

This may seem like a very simple question to ask but at present I do not know how to create a controllable number of turtles. In addition to this I do not know how to set the coordinates where the turtles should spawn. Ideally I would like the turtles to spawn on a patch that the user will draw out in the U.I. so that the turtles can then move along the network the user will have drawn in the U.I. Any answers would be greatly appreciated.
Patches use the sprout command
Ask patches [
Sprout 5 [set color red ]
]
Asks all the patches to make 5 red turtles.
Netlogo has fantastic documentation
The netlogo dictionary in help is insanely useful.
Or just ask one
Ask patch 4 5 [sprout 1]
create-turtles takes in the number of turtles to create. It also allows you to pass in a block of code that will then be run by the created turtles. So you can do:
create-turtles 50 [
setxy 3.5 4.7
]
This will create 50 turtles at the coordinates (3.5, 4.7).

Maintaining two states of turtles netlogo

I am doing a small university project. In which i have to maintain 2 states of turtles.
1. Disperse
2. Explore
Disperse :
In dispersion, when at the start all the turtles (20 turtles) are at 0,0 they should disperse from each other. Every turtle has a radius of 2 patches around it, no other turtle should be in that radius. All turtles should go far until they all attain this radius. then other behavior will be called i.e. Explore.
Explore:
In explore , they have to explore the world and avoid different types of obstacles. When ever two turtles come close to each other above mentioned radius then state should be changed to disperse.
I have procedures for obstacle avoidance, and move-speed, and all other individual behaviors under Disperse and Explore. But i don't know how to join all this in one simulation.
It is unclear that you really need to maintain turtle state, since you will have to repeatedly check for other turtles in any case. But since you said you wanted that, you can use turtles-own. For example:
turtles-own [state]
to setup
ca
crt 20
end
to go
ask turtles [set-state]
ask turtles [move]
end
to set-state ;;turtle proc
ifelse (any? other turtles in-radius 2) [
set state "disperse"
] [
set state "explore"
]
end
to move ;;turtle proc
if (not member? state ["disperse" "explore"]) [
error "unknown state"
]
if (state = "disperse") [
disperse
]
if (state = "explore") [
explore
]
end
to disperse ;;turtle proc
move-to one-of patch-set [neighbors] of neighbors
end
to explore
move-to one-of neighbors
end
You might want to take a look at Moore machine and Automata, NetLogo works great with those.
A Moore machine can be seen as a set of 5 elements that interact with each other, in this particular example the start state(S0) would be Dispersing. In NetLogo you can use the word run that receives a string. You'd had to make a procedure that returns a string (say "explore") by checking the actual state of a turtle.
I made something like that a few months ago. We were trying to make a hunter-prey model for polar bears and seals (or wolves and sheeps) based on Moore Machines.
You can use the example of #Alan of course, I just skimmed through and I believe it was fine.
Here is my example based on Moore Machine. It's in spanish but the idea is the same.