How do I ask an agent that isn't me or one of my neighbours? - netlogo

In Netlogo, how can I create an agentset for a turtle that contains all other turtles except them and their in-link-neighbors?
Thank you,
Thomas
This is so close to creating an agentset of turtles excluding neighbors, but doesn't quite work:
to setup
ca
create-turtles 10 [setxy random-xcor random-ycor]
ask turtles[create-link-to one-of other turtles]
end
to go
ask one-of turtles[
show in-link-neighbors
let poss turtles with [not member? self in-link-neighbors]
show poss
]
end
The above code came from: this previous answer

This will do the job, although it's not pretty.
to setup
ca
create-turtles 10 [setxy random-xcor random-ycor set color yellow set shape "circle"]
ask turtles[create-link-to one-of other turtles]
end
to go
ask one-of turtles[
set color green
ask in-link-neighbors [set color green]
ask one-of turtles with [color != green] [set shape "person"]
]
ask turtles [set color yellow]
end

The most straightforward way is:
turtles with [not link-neighbor? myself]
Here's an example showing this in action:
observer> crt 10 [ create-links-with other turtles ]
turtles> fd 10
observer> ask turtle 0 [ show link-neighbors ]
(turtle 0): (agentset, 9 turtles)
links> if random 2 = 0 [ die ]
observer> ask turtle 0 [ show link-neighbors ]
(turtle 0): (agentset, 7 turtles)
observer> ask turtle 0 [ show turtles with [not link-neighbor? myself]]
(turtle 0): (agentset, 3 turtles)
That's for undirected links. If your links are directed, substitute in-link-neighbor? or out-link-neighbor?, as appropriate.

Related

Spatial Autocorrelation in NetLogo

Is there any straight-forward way to adjust spatial autocorrelation for three different patch colors? I am trying to control both the number of red patches and how spatially autocorrelated (how close same colored patches are to each other). I can control the proportion of red patches, but don't know how to setup the autocorrelation.
Here is my code so far:
to setup-patches
resize-world 0 15 0 15
set-patch-size 30
ask patches [
set pcolor one-of [ green brown ]
]
ask patches [
let close-patches patches with [pcolor != red]
ask n-of ((proportion-red-plants * count patches) - count patches with [pcolor = red]) close-patches
[set pcolor red]
]
end
proportion-red-plants is a slider in the interface
If you know that AC of 0 means pick a patch that has no red neighbours, and that AC of 1 means pick a neighbour of any red patch, then all that is required is to choose AC=1 method with the given probability and the AC=0 method otherwise. This is what I meant by a design issue, you need to work out the steps required before trying to code those steps.
Here is an almost solution. I haven't bothered to do things like make sure the patches being turned red aren't already red so the counts will be incorrect.
to setup
clear-all
let prop-red 0.1
let AC 0
ask one-of patches [set pcolor red]
ask n-of (prop-red * count patches) patches
[ ifelse random-float 1 < AC
[ ask one-of patches with [ pcolor = red ]
[ ask one-of neighbors [ set pcolor red ]
]
]
[ let candidates patches with [not any? neighbors with [pcolor = red] ]
if any? candidates
[ ask one-of candidates [ set pcolor red ]
]
]
]
end
Thank you JenB again for helping. This is the code I ended up using which proved to get the job done (while keeping counts of red patches correct)
to setup-patches
resize-world 0 15 0 15
set-patch-size 30
ask patches [set pcolor one-of [green brown]]
let first-patch one-of patches
ask first-patch [set pcolor red]
repeat (proportion-red-plants * count patches - 1) [ask one-of patches [assign]]
end
to assign
ifelse random-float 1 < AC
[let candds patches with [any? (neighbors with [pcolor = red])]
ask one-of candds [set pcolor red]]
[ask one-of patches [set pcolor red]]
end

How to stop the ticks using a breed in netlogo?

I want to create a Democracy Model. I have created 4 breed for that. One for people who will vote, the rest 2 for parties. In my code, I wand to stop the model when any of the parties reach a total number of 100 votes. I can't figure it out. Please help me. Here's my code:
breed [people p]
breed [party1 p1]
breed [party2 p2]
breed [party3 p3]
party1-own [vote]
party2-own [vote]
party3-own [vote]
to setup
clear-all
setup-patches
setup-people
setup-parties
reset-ticks
end
to setup-patches
ask patches [
ifelse pxcor >= 4 and pycor >= 6
[set pcolor white]
[set pcolor brown]
]
end
to setup-people
set-default-shape people "person"
create-people number-of-people
ask people [setxy random-float -16 random-float -16]
end
to setup-parties
set-default-shape party1 "person"
set-default-shape party2 "person"
set-default-shape party3 "person"
create-party1 1
create-party2 1
create-party3 1
ask party1 [setxy 15 -1]
ask party2 [setxy 15 -3]
ask party3 [setxy 15 -5]
ask party1 [set color blue]
ask party2 [set color green]
ask party3 [set color yellow]
end
to go
start-voting
ask party1 [
if (vote) >= 100 [stop]
]
ask party2 [
if (vote) >= 100 [stop]
]
ask party3 [
if (vote) >= 100 [stop]
]
tick
end
to start-voting
let x random 3
ifelse x = 2
[ask party3 [set vote vote + 1]]
[
ifelse x = 1
[ask party2 [set vote vote + 1]]
[ask party1 [set vote vote + 1]]
]
ifelse show-votes?
[
ask party1 [set label vote]
ask party2 [set label vote]
ask party3 [set label vote]
]
[
ask party1 [set label ""]
ask party2 [set label ""]
ask party3 [set label ""]
]
end
It looks like the problem with your stop procedure is that a turtle can only stop its own role in a procedure. check out the procedure manual regarding buttons for a more thorough explanation, where it is stated:
In a turtle or patch forever button, the button won't stop until every turtle or patch stops -- a single turtle or patch doesn't have the power to stop the whole button
So it looks like what's happening is that your first group is stopping the procedure, but since that happens after voting happens, and the other turtles are not stopped, the votes will continue to be added and the procedure will continue to run. In this case, it's probably better to have a global-level stop condition, as below. Note that when the observer queries a turtle using of, the variable is returned in a list, so item 0 is needed below.
to go
start-voting
if ( item 0 [vote] of party1 > 100 ) or ( item 0 [vote] of party2 > 100 ) or ( item 0 [vote] of party3 > 100 ) [
stop
]
tick
end

how to create turtles in a specific area on the patch in Ntelogo

I am making and assignment. The assignment is to make a model of Democracy. I have made a parliament house with coordinates (4, 6). I have made 3 political parties. Now, I want to make the people who will vote. I make them randomly using random-xcor and random-ycor in setxy, but they some of them are made on the parliament house. How can I stop this from happening. I want the turtles to be made everywhere except in the parliament house. Here is the code. Please help me.
breed [people p]
breed [party1 p1]
breed [party2 p2]
breed [party3 p3]
party1-own [vote]
party2-own [vote]
party3-own [vote]
to setup
clear-all
setup-patches
setup-people
setup-parties
reset-ticks
end
to setup-patches
ask patches [
ifelse pxcor >= 4 and pycor >= 6
[set pcolor white]
[set pcolor brown]
]
end
to setup-people
set-default-shape people "person"
create-people 100
ask people [setxy random-xcor random-ycor]
end
to setup-parties
set-default-shape party1 "person"
set-default-shape party2 "person"
set-default-shape party3 "person"
create-party1 1
create-party2 1
create-party3 1
ask party1 [setxy 15 -1]
ask party2 [setxy 15 -3]
ask party3 [setxy 15 -5]
ask party1 [set color blue]
ask party2 [set color green]
ask party3 [set color yellow]
end
to setup-people
set-default-shape people "person"
ask n-of 100 (patches with [pcolor != white]) [sprout-people 1]
end

How to make turtles move in one color in Netlogo

I start developping with Netlogo and I face the problem that I want to make all Turtles moving in one way which has the Black Color. How can I do that ? I tried with patch-ahead but I didn't success.
Anyone have a solution ? I will be grateful.
to setup
clear-all
import-drawing "patch.png"
create-turtles 10 [set xcor -10 set ycor -13]
ask turtles [set color white]
ask turtles [set shape "bug"]
reset-ticks
end
to bouge
ask turtles[
fd 1
]
end
to go
bouge
ask turtles [if [pcolor] of patch-ahead 6 != black [set heading heading - 100] ]
end
enter image description here
You should use something like these:
set black-patches patches with [pcolor = black]
ask turtles [
face one-of black-patches
]
Bests, Ervin

Define home area-turtles?

I am very new to netlogo. I have searched every question here before I posted this.
I have the following code which sprouts a given number of horses:
ask n-of Number-horses patches with [grass? = "Yes"]
[sprout-horses 1 [set color 25 ]]
The person can change the number of horses using the slider but I would like each horse to have its own area/range/radius.
They can only move within this radius/area and they cannot meet each other.
From what I've read it's got something to do with the distance function?
You can find a similar problem here which has examples too :
Spacing agents in NetLogo based on territory size
There are several ways that you can assign a territory zone to each horse, but all methods that I know have two steps, first step is in order to make sure initial home area of horses are separated from each other , So we need to create horses only in patches which has a certain distance from another patch which has a horse on it,I did not follow your method that asked patches to sprout horses and instead I created them without asking patches.
I was not sure how you defined grass? Variable for each patch but I have assigned a number of patches with grass? = true and others false.
Second step is to set home-area property of each horse. If initially you moved them far away from each other they will have separate territories.
I have included a few examples here :
First to use in-radius for both steps:
Breed [Horses horse]
Horses-own [home-area]
patches-own [grass?]
globals [Zone-Radius]
to setup
clear-all
reset-ticks
set Zone-Radius 2
ask patches
[
ifelse pxcor mod 5 = 3
[ set Grass? true ]
[ set Grass? false ]
]
create-horses Number-horses
[ Move-to one-of patches with [Grass? and not any? other horses in-radius (Zone-Radius + 1)]
set home-area patches in-radius Zone-Radius
set color 25
]
end
to go
ask horses [
ifelse member? patch-ahead 1 home-area
[rt random 10 fd 1 ] ; move if next patch is in their zone
[rt random 180]
]
tick
end
In this example horses only move in the patches in their radius 2. But you can change that base on your model requirements.
In the second method you can use distance for the first step (finding empty patches with enough distance to current patch) and radius for second one (assigning home-area to each horse).
Move-to one-of patches with [Grass? and not any? other horses with [distance myself < (Zone-Radius + 1)]]
set home-area patches in-radius Zone-Radius
If you use higher distance for finding empty patches you will have completely seprated zones. Finally , you can use distance for both steps:
Move-to one-of patches with [Grass? and not any? other horses with [distance myself < (Zone-Radius + 1)]]
set home-area patches with [distance myself < Zone-Radius]
I just did it another way:
Breed [Horses horse]
Horses-own [home-area]
patches-own [ concession? forest? parks?]
globals [Zone-Radius]
to setup
clear-all
reset-ticks
set Zone-Radius 2
ask n-of 500 patches [ set concession? "No" ]
ask n-of 500 patches[ set forest? "Yes" ]
ask n-of 500 patches[ set parks? "Yes"]
let i 0
while [i < Number-horses]
[
ask one-of patches with [(concession? = "No" or forest? = "YES" or parks? = "YES" ) and (not any? horses in-radius (Zone-Radius + 2) )]
[
sprout-horses 1 [
set home-area patches with [distance myself < Zone-Radius]
let w who
ask home-area [set pcolor red]
set color 25 ]
]
set i (i + 1)
]
end
to go
ask horses [
ifelse member? patch-ahead 1 home-area [rt random 10 fd 1 ] [rt random 180]
]
tick
end
As you can see I used while and a condition to ask patches one by one, I might be mistaken but when I ask all the n-of Number-of-horses patches with [YourCondition][...] I get the wrong results and distance between horses is not effective, maybe they are created all at the same time and therefore upon creating a horse there was no horse nearby!? I am new to these concepts and might be wrong.
This is the code and view for the one which asks patches to create horses at once here :
Breed [Horses horse]
Horses-own [home-area]
patches-own [ concession? forest? parks?]
globals [Zone-Radius]
to setup
clear-all
reset-ticks
set Zone-Radius 2
ask n-of 500 patches [ set concession? "No" ]
ask n-of 500 patches[ set forest? "Yes" ]
ask n-of 500 patches[ set parks? "Yes"]
ask n-of number-horses patches with [(concession? = "No" or forest? = "YES" or parks? = "YES" ) and (not any? horses in-radius (Zone-Radius + 2) )]
[
sprout-horses 1 [
set home-area patches with [distance myself < Zone-Radius]
let w who
ask home-area [set pcolor red]
set color 25 ]
]
end
to go
ask horses [
ifelse member? patch-ahead 1 home-area [rt random 10 fd 1 ] [rt random 180]
]
tick
end