Maintaining two states of turtles netlogo - 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.

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.

netlogo tie-mode "fixed" fails to maintain link-length in graphs with medium-level degree

I am trying to create a network that moves through the environment as a "static" unit, i.e. nothing in the simulation changes except the position and orientation of the whole, the position and orientation of the individual turtles relative to one another are fixed by their links. Turtles are connected via undirected links, which are tied and set to tie-mode "fixed".
The problem is that in certain situations the links fail to remain fixed and link-lengths begin to change. Initially I noticed that, where the average network degree is relatively low or the network is a complete graph, the tie primitive works. However, when links were created to produce a graph that is moderately connected the link-lengths between the turtles begins to change. Upon further experimentation I can create a network with the same number of links and turtles but with different configurations i.e. the network structure is different, that sometimes maintain the positions and link-lengths but in other situations fail to do so.
How do I get the network to move as a unit no matter how connected the network is or what the configuration of the network is? See example code below, I have added code at the end where you can run multiple configurations of a network with 6 turtles and 6 links to see the issue for yourself, try running at least a half dozen iterations. Thanks!
this produces a network that moves as a unit
to setup
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
reset-ticks
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
This produces a network whose links are still tied and set to tie-mode "fixed", but change their link-lengths. The more links that are asked to die, the more the link-lengths change.
to setup
clear-all
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
ask one-of links [die]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
Here is additional code showing a specific instance of link-length change. Please input the seed 659269695 when prompted by the button "use-seed-from-user". Apologies if the code is clunky, first time using random-seed. "Print-lengths" button is to confirm that lengths change.
;USE seed: 659269695
to use-new-seed
let my-seed new-seed ;; generate a new seed
output-print word "Generated seed: " my-seed ;; print it out
random-seed my-seed ;; use the new seed
reset-ticks
end
;; Use a seed entered by the user
to use-seed-from-user
loop [
let my-seed user-input "Enter a random seed (an integer):"
carefully [ set my-seed read-from-string my-seed ] [ ]
ifelse is-number? my-seed and round my-seed = my-seed [
random-seed my-seed ;; use the new seed
output-print word "User-entered seed: " my-seed ;; print it out
reset-ticks
stop
] [
user-message "Please enter an integer."
]
]
end
to setup
clear-all
create-turtles 6
ask turtles [
fd 5
set shape "circle"
set size 1
set color yellow
if count links < 7 [ask one-of turtles [create-link-with one-of other turtles
[tie]]]]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
to print-lengths
print sort-by < [precision link-length 2] of links
end
I slightly revised your code so that the go procedure includes breaking a link. I also got rid of the explicit setting of tie-mode since that is done by setting the link to tie and added a tick so I could plot. So the code looks like this:
to setup
clear-all
create-turtles 10 [fd 2]
ask turtles [create-links-with other turtles [tie] ]
reset-ticks
end
to go
ask one-of links [die]
ask turtles [lt 1 fd 1]
tick
end
As far as I can see, the turtles move as a unit until it fragments with the loss of links.
I added a monitor for mean [link-length] of links, which is what I think you are asking about and also a plot of the same calculation. Yes, it is true that the average link length changes, but remember that the links are not all the same length. If a longer one dies, then the average length will reduce, and if a shorter one dies then the average will increase. The plot wanders a little, but it is basically flat until fragmentation.

How do I make turtles avoid patches in NETLOGO?

My agents are boats moving on water, surrounding and at some places within that water are bits of land which need to be impossible to pass. I'm struggling to conceptualize how to tell an agent this information in netlogo.
I've assigned
patches-own
[DEPTH
PASSABLE?
]
with
ask patches with [DEPTH > 0] [set PASSABLE? FALSE]
How do I tell a turtle to not cross over or occupy a patch with PASSABLE? = FALSE while engaging in an otherwise random walk search for
patches in-radius VISION with [DEPTH = 10]
?
sorry for the lack of a reproducible example, but this is a more conceptual question than anything. I will rough out a simple example model if need be.
When your agent is about to take a step forward, you can have them check if they can, then make them pick a new destination if they are going onto dry land
You can do this with Patch-Ahead or In-Cone if you want. Use that to set the destination.
Somthing like:
to walk
"pick destination"
ifelse destination = water [fd 1] [walk]
end
To pick what the possible destination is, you use what the turtle's current heading is like this:
to pick-destination
let destination patch-ahead 1
end

NetLogo: Make turtles fear each other

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]]

Assign a cost to a link

I have a landscape where each patch contains a cost value.
I placed a turtle within each patch according to the following code :
to create-turtles
ask neighbors [ sprout 1 [
set shape "dot"
set size 0.5 ] ]
end
Then, I built a link between each turtle according to the following code :
to create-link-turtles
ask turtles [ create-links-with turtles-on neighbors ]
end
As each patch contains a cost value, I would like to assign a cost value to links between turtles.
For example,
If the link intersects two patches (patches 1 and 2) that have two different costs, the link would be equal to cost in patch 1 + cost in patch 2.
If the link intersects two patches (patches 1 and 2) that have the same cost, the link would be equal to cost in patch 1.
How can I assign a cost value to links between turtles in this way ?
After this, I would like to apply the dijkstra' s algorithm.
Thank you for your help.
Have a good day
Assuming that:
your patches have a cost variable
your links have a link-cost variable
turtles are always connected to turtles on neighboring patches (like in the code you posted)
You can simply :
ask links [ set link-cost sum [ cost ] of both-ends ]
This will just add the costs of the two patches under the turtles at both ends of the link. (If you had links traversing more than two patches, this approach would not work and things would get much more complicated.)
For calculating distances afterwards, I'd suggest you take a look at the NW extension. Its weighted-distance-to primitive uses Dijktra's algorithm internally.