How to set myheading equal to leader's heading in Netlogo - netlogo

what i'm trying to do is:
Pseudo code
to flock
check flockmates
if find any leader inside the flockmates
change myheading to leader's heading
else
follow flocking rule [separate, allign, cohesion]
end
Below is code that I use.
turtles-own
[ flockmates
nearest-neighbor
leader?
leader
]
to setup
__clear-all-and-reset-ticks
ask n-of population patches with [ pcolor = blue]
[sprout 1
[set color white
set size 0.6
set leader? false]
]
choose-leaders
end
to choose-leaders
ask n-of ((percent_of_leader / 100) * population ) turtles
[set leader? true
set color black
set size 0.6
set leader self
]
end
to go
ask turtles [flock]
end
to flock
find-flockmates
let nearby-leaders turtles with [leader? ]
ifelse any? nearby-leaders
[ set heading [heading] of nearby-leaders]
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[separate]
[ if any? flockmates
[align
cohere ]]]
end
to find-flockmates ;; turtle procedure
set flockmates other turtles in-cone vision vision-angle
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
however when I run the code, this error message pop-up can't set turtles variable HEADING to non-number [147]. and it point to this code [set heading [heading] of nearby-leaders]. what did i do wrong here? really appreciate if someone can help.

Because nearby-leaders in your code is a turtle-set, you should use one-of:
set heading [heading] of one-of nearby-leaders

Related

How to spawn turtles a certain amount of patches away from each other

I am trying to spawn turtles 5 patches away from each other but I'm not sure how, right now they all spawn on green patches (I don't want them to spawn on brown ones) and I'm not sure how exactly you control the distance between the spawning of turtles, thanks.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches [
set pcolor green
]
ask n-of 100 patches [
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown][sprout-humans 1 [set size 5
set color blue
set shape "person"]]
ask n-of 5 patches with [pcolor != brown][sprout-zombies 1 [set size 4
set color red
set shape "person"]]
end
Have you read this question: NetLogo Create turtle at regular distance from each other?
Anyway, I thought that showing you some working functions would be helpful, here I made two alternatives, sprout-distanced1 and sprout-distanced2, you can test them both by alternating which line is commented; I also added a slider called Min-Distance to control the turtles spacing.
sprout-distanced1 uses the keyword carefully with is basically a try-else block, it's there in case that the turtle doesn't find a patch distanced enough to move to, in which case rather than sending a warning the turtle will stay where it is and print its distance to the closest turtle.
sprout-distanced2 uses a while loop, in case that the turtle doesn't find a place to move to that is at least Min-Distance from another turtle it will reduce the minimum radius by a small amount until it can distance itself from other turtles, if it had to move to a patch where it is less than Min-Distance away from other turtles it will log the distance at the Command Center.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask n-of 100 patches
[
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown]
[
sprout-humans 1
[
set size 5
set color blue
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
ask n-of 5 patches with [pcolor != brown]
[
sprout-zombies 1
[
set size 4
set color red
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
end
to sprout-distanced1
carefully
[
; try to move at least Min-Distance away from other turtles
move-to one-of patches with [not any? other turtles in-radius Min-Distance]
]
[
; if can't move Min-Distance away from other turtles
; stay put and log the min distance to other turtle, just for reference
show distance min-one-of other turtles [distance myself]
setxy random-xcor random-ycor
sprout-distanced1
]
end
to sprout-distanced2
let min-dist Min-Distance
let moved? FALSE
while [not moved? and min-dist > 0]
[
; can distance it self somewhere?
ifelse any? patches with [not any? other turtles in-radius min-dist]
[
; if yes, go there
move-to one-of patches with [not any? other turtles in-radius min-dist]
set moved? TRUE
; if had to reduce the distancing radious log it
if moved? and min-dist < Min-Distance
[
show distance min-one-of other turtles [distance myself]
]
]
[
; no where to go, reduce the distancing radious
set min-dist min-dist - 0.1
]
]
end
Choose whichever suits better your model.

Netlogo, Moving Turtle along links

I am doing model where I want my agents to move along links between nodes. The idea is that all turtles move a constant distance after each tick. (Note I dont want them jump from one node to the another, also lets assume that its not possible to make the nodes at a constant distances, because I am trying to implement it on a gis model where of course the vertices are not at constant distances)
For this model there are two type of agents butterflies and collectors. Each of the agents placed on nodes which are linked to each other. The collectors find the closest butterfly to them using in-radius and then finds the path using nw: turtles-on-weight-path function. So for example lets suppose collector-02 has located butterfly-19. The path is [node-2 node-5 node-1 node-9]. I want the collector to travel to these nodes to get to the butterfly but instead of jumping to each node. He has to travel a distance of 0.01 netlogo units on the link from node-2 to node-5 and eventually node-9 where the butterfly is located.
The function follow-path is incomplete and it where i getting into trouble.
ANy ideas how to solve this problem. Thanks
extensions [nw]
globals [current-loc-global butterfly-location link-distance]
breed [nodes node]
breed [collectors collector]
breed [butterflys butterfly]
links-own [dist]
nodes-own [consist-butterfly linked-to]
collectors-own [ distance-from-location current-loc new-location targeted-
butterfly initial-node next-node current-node previous-node
node-butterfly-at commute-path route-length next-pos]
butterflys-own [ initial-node-collectors targeted? node-butterfly-on]
to setup
clear-all
;creating nodes
create-nodes 60[
set color blue
set shape "circle"
set consist-butterfly nobody
]
ask nodes [create-link-with one-of other nodes]
repeat 500[layout]
ask nodes [setxy 0.95 * xcor * 0.95 ycor]
create-butterflys number-of-butterflys[
set node-butterfly-on one-of nodes
set shape "butterfly"
move-to node-butterfly-on
set initial-node-collectors current-loc-global
set targeted? false
]
ask nodes [
set consist-butterfly min-one-of butterflys in-radius 0.0001 [distance
myself]
]
create-collectors number-of-collectors[
set current-loc one-of nodes with [consist-butterfly = nobody]
set color red
set targeted-butterfly nobody
move-to current-loc
]
ask collectors[
set current-loc-global current-loc
]
ask links [
let end-point-1 end1
let end-point-2 end2
set link-distance 0
ask end1[set linked-to end-point-2
set link-distance distance end-point-2
]
set dist link-distance
ask end2[
set linked-to end-point-1
]
]
reset-ticks
end
to layout
layout-spring nodes links 0.5 1 1
end
to go
find-agents
find-path
follow-path
end
to find-agents
ask collectors [
if targeted-butterfly = nobody[set targeted-butterfly min-one-of
butterflys in-radius 10 with [not targeted?] [distance myself]]
if targeted-butterfly != nobody [ask targeted-butterfly [set targeted?
true]]
]
ask collectors with [targeted-butterfly != nobody][
ask targeted-butterfly [set butterfly-location node-butterfly-on]
set node-butterfly-at butterfly-location
]
end
to find-path
ask collectors with [targeted-butterfly != nobody ][
set initial-node min-one-of nodes in-radius 0.0001 [distance myself]
let end-location node-butterfly-at
let path []
ask initial-node [set path nw:turtles-on-weighted-path-to end-location
dist]
set commute-path path
set route-length length commute-path
set current-node initial-node
set previous-node current-node
set next-node current-node
]
end
to follow-path
ask collectors with [targeted-butterfly != nobody][
set distance-from-location distance current-node
ifelse 0.09 < distance-from-location
[jump 0.09]
[move-to current-node
set current-node next-node
face next-node]
]
end
Managed to sort it out. Need to change the follow-path function as follows:
to follow-path
ask collectors with [targeted-butterfly != nobody][
set distance-from-location distance current-node
set next-pos ((position(current-node) commute-path) + 1)
if next-pos < route-length [set next-node item(next-pos) commute-path]
ifelse 0.09 < distance-from-location
[jump 0.09]
[move-to current-node
set current-node next-node
face next-node]
]
end

How to set heading of agentset from nest in Netlogo

What I'm planning to do is to ask a group of agentsets to flock from nest throughout world.
I'm using flocking codes and I have problem to identify heading for cohere part. Since all agents are moving from nest, how can I set their heading?
The error I got after editing my codes are :
No heading is defined from a point (0,0) to that same point.
error while turtle 1 running TOWARDS
called by procedure AVERAGE-HEADING-TOWARDS-FLOCKMATES
called by procedure COHERE
called by procedure FLOCK
Here is code for agentset:
to setup
clear-all
let number-of-groups 3
let turtles-per-group 4
create-turtles turtles-per-group * number-of-groups
[ set color red
set size 1.5
set pen-mode "down"
]
set groups [] ; empty list
repeat number-of-groups [
let new-group n-of turtles-per-group turtles with [
not is-agentset? my-group
]
ask new-group [ set my-group new-group ]
set groups lput new-group groups
]
reset-ticks
end
to go
ask turtles [flock]
end
Here is flocking part
to flock
find-flockmates
if any? flockmates
[
find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[ separate ]
[ align
cohere ] ]
end
to find-flockmates ;; turtle procedure
set flockmates my-group
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
;;; COHERE
to cohere
turn-towards average-heading-towards-flockmates max-cohere-turn
end
to-report average-heading-towards-flockmates
let x-component mean [sin (towards myself + 180)] of flockmates
let y-component mean [cos (towards myself + 180)] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
I have problems to change code for to-report average-heading-towards-flockmates since towards myself + 180)] of flockmates cannot be use here. Can anyone help me?

How to make simulation run faster in netlogo instead of using Slider bar near to view update

Is there any code to make simulation run faster in netlogo, instead of using slider bar near to setting? What my code need to do is to simulate the crowd behavior, it's work fine if the number of turtles around 100,however when I increase the number up to 300-800 turtles, simulation take very long to finish. Each tick also take very long to count from 0 to 1 and next until all turtles die. one thing that I suspect cause slow simulation is when ask turtles to evacuate. without evacuate rule, everything went smoothly even set a maximum number of turtles. is there other way to write evacuate rule, so that it can run faster? thanks.
to go
ask turtles [wander fd 0.01]
if emergency? = true [move]
if all? turtles [ pcolor = red ] ;stops simuation
[stop]
tick
end
to wander
[ do..something]
end
to move
set time-to-evacuate time-to-evacuate + 1
ask turtles [avoid-obstacles fd 0.1]
ask turtles [follow-leader fd 0.1]
ask turtles [flock fd 0.1]
ask turtles with [pcolor != red] [evacuate fd 0.1]
ask turtles with [pcolor = red][die]
end
to evacuate
ask turtles with [color = black ]
[let beings-seen patches in-cone 10 135 with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]]
ask turtles with [color = white ]
[let beings-seen patches in-cone 5 135 with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]]
end
to avoid-obstacles
[do something]
end
to follow-leader
[do something]
end
to flock
[do something]
end
In your move procedure you have:
ask turtles with [pcolor != red] [ evacuate ... ]
And then in evacuate you have:
ask turtles with [color = black] [ ... ]
evacuate is already being run by all of the non-red turtles, so you've got every non-red turtle asking every black turtle to do something at every time tick.
I don't think you intended that.
I have to guess a bit at your intent, but I think if in evacuate you replace the ask with an if:
if color = black [ ... ]
that's probably closer to what you meant.

How to avoid turtles revisiting the patch on which they were last time?

Turtles stay on patches for 60 ticks, and then move to another target patch. How to avoid turtles revisiting the patch on which they were last time? Thanks
Hi Seth and Frank,
Thank you very much for your reply. I am sorry I did not describe the questions in detail.
Turtles will not visit the patch that they were on the last tick, and will move to another nearest patch instead in next tick. The following codes mean they find the nearest patch, and move on to it.
What I would want to do is the turtle will find the nearest patch again in the next tick. They will move to other alternative that is nearest to themselves, if the nearest patch is still the same one that they were on the last tick. Thanks
let closest-leaf min-one-of (patches in-radius 1 with [pcolor = lime]) [distance myself]
face closest-leaf
fd distance closest-leaf
A good way is to have a turtles-own variable of patches visited that can be maintained (remember to initialize it to the empty list when you create the turtle).
turtles-own [ patches-visited ]
to setup
...
ask turtles [ set patches-visited [] ]
...
end
to move
let potential-targets filter [ not member? ? patches-visited ] target-patches
let target-patch one-of potential-targets
if target-patch != NOBODY [
set patches-visited fput target-patch patches-visited
; move to target patch
]
end
Add a get-target reporter.
to-report get-target ;;turtle proc
let %close patches in-radius 1 with [pcolor = lime and self != [patch-here] of myself]
ifelse (any? %close) [
report min-one-of %close [distance myself]
] [
report nobody
]
end
Now you can easily change your mind about how to pick a target. You can then move to the target (if one exists).
to move ;; turtle proc
let %target get-target
ifelse (%target = nobody) [
;handle this case
] [
face %target move-to %target
]
end