NetLogo - calculate the difference of a variable of neighbouring agents - netlogo

I would like to have each agents ask their neighbours of their value of a turtle-owned variable and set them according to the differences the have.
I know how to do this for distances:
if (any? other turtles-here)
[
ask neighbors [ ;; ask 8 neighbors / neighbors4 for 4
;if (max-one-of turtles [distance myself]) <= 3
;[set opinion opinion - .1] ; no change in opinion
;if (distancexy point1-pxcor point1-pycor) > 20 and (distancexy point1-pxcor point1-pycor) <= 50
;[set point1-location "middle"]
;if (distancexy point1-pxcor point1-pycor) > 50
;[set point1-location "far"]
]
however I struggle with implementing it for an exchange of values. How do I achieve this?
This is my MWE.
Note that the code part in question is in pseudo-code.
breed [ turtles ]
turtles-own [ variable ]
to setup
clear-all
create-turtles 100
[
set variable random-float 10
]
reset-ticks
end
to communicate
if (any? other turtles-here)
[
ask neighbors [
pseudo-code: if difference of your variable and my variable is bigger then 3, than do nothing
if differences less then 3, calculate the higher variable minus 0.1 and the lower variable plus 0.1
if difference less then 2, calculate the higher variable minus 0.3 and the lower plus 0.3
if difference less then 1, calculate the arithmetical mean
]
]
end
to go
ask turtles [
rt random 360
fd 1
communicate
]
tick
end

Your pseudo-code leaves a lot of room for interpretation, so this might not be exactly what you want, but I think it can get you started:
to communicate
ask turtles-on neighbors [
let both-turtles (turtle-set self myself)
let difference abs (variable - [ variable ] of myself)
if difference < 1 [ ask both-turtles [ set variable mean [ variable ] of both-turtles ] ]
if difference < 2 [ bring-closer both-turtles 0.3 ]
if difference < 3 [ bring-closer both-turtles 0.1 ]
]
end
to bring-closer [ both-turtles delta ]
ask min-one-of both-turtles [ variable ] [ set variable variable + delta ]
ask max-one-of both-turtles [ variable ] [ set variable variable - delta ]
end
There is a lot going on here, but nothing overly complicated. I think the main concepts you will have to understand here are: self and myself, the idea of storing an agentset in a local variable, and the idea of writing a procedure that takes arguments. You can read about all of this in the programming guide and look up the relevant primitives in the dictionnary.

Related

How to identify neighbors with some conditions

I'm trying to set up a toy model of connected turtles. Every turtle has a random belief [0,1] and I want to implement some conditions:
If it's a turtle with belief > .5 the turtle doesn't do anything.
If it's a turtle with belief < .5 and one of their immediate neighbour has a belief > .5, their new belief (of the turtle with belief < .5) would be belief + .1.
If the turtle with belief < .5 doesn't have an immediate neighbour with belief > .5, the turtle doesn't do anything.
The basic setup:
turtles-own [belief]
to setup
ca
reset-ticks
crt 5 [
set shape "person"
layout-circle turtles 5
set belief random-float 1
]
ask turtles [ create-links-with min-n-of 2 other turtles [distance myself] ]
end
This creates a 5 turtles cycle network. Now they have to check the beliefs of their (two) closest neighbors. I tried this:
to go
ask turtles with [one-of link-neighbors with [belief > .5]] [
set belief belief + .1
]
end
So the basic idea is that every turtle with a connected neighbour with belief > .5 has to add .1 to its own belief. I have tried several variations of the ask turtles with line, but it always shows me a runtime error of this sort: WITH expected a true/false value from (turtle 0), but got (turtle 2) instead. Any ideas? Thank you in advance.
one-of returns an agent. It is not suitable in this case for evaluating a condition with with- the condition is incomplete, since the line ask turtles with [one-of link-neighbors with [belief > .5]] translates (roughly) to "ask turtles with the condition turtle x to do something".
I think what you're after is something that uses count to count the number of believing turtles or any? to evaluate if an agentset exists:
to go-1
ask turtles with [ (count link-neighbors with [belief > 0.5]) > 0 ] [
set belief belief + 0.1
]
end
to go-2
ask turtles with [ any? link-neighbors with [ belief > 0.5 ] ] [
set belief belief + 0.1
]
end

NetLogo - turtle to go to the closest concentration of turtles

I would like a turtle to go to the closest patches with most turtles if a threshold of a given variable is met for 5 ticks.
My code is:
to move
let count-tick 5
if var >= 9.5 [
set count-tick count-tick - 1
if count-tick = 0 [
ask turtle [
let nearest-group min-one-of (patches with [sum turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
ask turtle [ ;; once there do the following
set shape "star"
set color red
]
]
]
]
end
The issue I have is that a) I am unsure how to say the patch with >= 3 turtles closest to you at the given range of 3 (attempted code above) and b) how to say once there, change your shape.
Revised to keep a permanent variable to track whether the variable is high enough 5 times in a row.
turtles-own
[ count-tick
]
; wherever you create the turtles, you need to `set count-tick 5`
to move
ifelse var >= 9.5
[ set count-tick count-tick - 1 ]
[ set count-tick 5 ]
if count-tick = 0
[ let nearest-group min-one-of (patches with [count turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
set shape "star"
set color red
]
end
First, you are already within an ask turtles code block from the procedure calling this move procedure. So you don't need the additional ask turtles. Look up ask in the NetLogo Dictionary, it iterates through the turtles, running all the code for each turtle in turn.
Second, you need count turtles rather than sum turtles as sum is to add up values.
Note that there is no error checking in this, you may have problems if there are no patches within radius of 3 that have at least 3 turtles.

Calculating turtle mortality based on distance from origin in netlogo

I am writing a procedure (Pass-Away-Space) to calculate mortality of turtles moving from the origin (start-patch) through out the world. Each turtle calculates its own mortality based on its distance from the origin (start-patch). The code I am attempting to implement for this procedure is as follows:
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * [distance start-patch] of turtles)
if chances >= 1 [die
set dead-count dead-count + 1
]
]
end
The error I am getting is expected input to be a number but got the list. I am not sure what the issue is and I was wondering if anyone could point out and rectify the problem with the code.
The problem here is your of turtles. Since an ask procedure affects one turtle at a time, each turtle in your procedure above is evaluating the [distance start-patch] of all turtles instead of just its own distance to the start patch. To clarify, check out the following setup:
globals [ start-patch ]
to setup
ca
reset-ticks
crt 10 [
setxy random 30 - 15 random 30 - 15
]
set start-patch patch 0 0
end
to incorrect-example
ask turtles [
print ([ distance start-patch ] of turtles)
]
end
to correct-example
ask turtles [
print distance start-patch
]
end
Compare the print output of the incorrect-example and the correct-example procedures, and you'll see that when you use [distance start-patch] of turtles you get the list of distances of all turtles. When you ask turtles to evaluate a turtles-own variable (including color, size, etc) each turtle will automatically access its own version of that variable- there's no need to specify which turtle. So, your pass-away-space might look something more like below (untested):
to Pass-Away-Space
ask turtles [
let chances 1 - exp( -1 * mortality * (distance start-patch) )
if chances >= 1 [
die
set dead-count dead-count + 1
]
]
end

move turtles and create a crowd to target

to setup
ca
reset-ticks
ask patches [
set inside? (abs pycor < 10 and abs pxcor < 10)
set exit? false
ask patch 11 0 [ set pcolor lime set exit? true]
]
repeat initial-population [ ; start condition turtles with any other turtles on neighbors
ask one-of patches with [
inside? and (not any? other turtles-here) and (not any? turtles-on neighbors)] [
sprout 1 [
set color blue
set size 1
]]]
end
to go
tick
define-neighbors-radius-2
move
end
to define-neighbors-radius-2
ask turtles [
set neighbors-ahead2 patches at-points [[2 1] [2 0] [2 -1]]
set neighbors-for-y-up2 patches at-points [[2 0] [2 -1] [1 -2] [0 -2] [-1 -2]] with [inside?]
set neighbors-for-y-down2 patches at-points [[-1 2] [0 2] [1 2] [2 1] [2 0]] with [inside?]
]
end
to move
;; my intent to move turtles to exit without their neighbors are occupied by other turtles, ;;that is the 8 patches around turtles are empty until exit?
ask turtles[
ifelse inside? [
if ycor = 0 [ ;strategy to turtles with in front exit
ifelse exit? [
set heading 90
fd .5
]
[
facexy 11 0
if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-ahead2) [
fd .5
]
]
]
if ycor > 0 [ ; strategy to turtles occupied "bottom-side" of inside?
facexy 11 0
if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-for-y-up2) [
fd .5
]
]
if ycor < 0 [ ; strategy to turtles occupied "down-side" of inside?
facexy 11 0
if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-for-y-down2) [
fd .5
]
]
]
[
set heading 90
fd .5
]
]
end
I try to move turtles to exit but not all turtles move, why?
also, turtles must go out with ycor = 0, that is obliques direction don't allow because neighbors will occupied patches aren't inside!
Can't public this question because "looks like my post is mostly code", so talk about of my life:
seriously my intent is to create a crowd in front of exit and set some rules to delay the turtles flow exit, for this I need the neighbors empty to show interaction between agents.
(also accept some suggest to set this interaction) but at the moment turtles reach exit!
thanks
One problem with this code is that have you three ifs where it appears you only want one of them to run each time, but it's possible for more than one of them to trigger. You have:
if ycor = 0 [ ...commands #1... ]
if ycor > 0 [ ...commands #2... ]
if ycor < 0 [ ...commands #3... ]
but the turtle's ycor might be changed by commands #1 or #2. So it's possible that both #1 and #2 might run, or both #2 and #3, and perhaps other combinations as well. I assume you intended that each turtle should only move once per tick, so I recommend you rewrite this as:
ifelse ycor = 0
[ ...commands #1... ]
[ ifelse ycor > 0
[ ...commands #2... ]
[ ...commands #3... ] ]
Something else that should be fixed in this code:
reset-ticks should go at the end of setup, not near the start. It tells NetLogo setup is finished.
tick should go at the end of go, not near the start. It tells NetLogo a tick has finished.
I don't know if either of these problems I've pointed out are actually causing the unintended behavior you're seeing. Perhaps the bug isn't obvious just from reading over the code. In that case, you have two possible courses of action ahead of you:
1) Back up. Throw away this broken code and go back to the last version of your model that contained only code that have you have verified to work correctly. Then try again, but this time, don't add so much new code all at once. Attempt to make a very small improvement to the code, and get that working, before moving on to the next small improvement. And so on. If at any point you get stuck, come here, show your code, and ask a specific question about it. That question should be much easier to answer than your current question. Questions of the form “Here is a big mass of code that doesn't work, help!" are very difficult to answer.
2) Press on by investigating your questions yourself. You write, “not all turtles move, why?” Perhaps someone can answer this just by reading your code; I can't (unless my first guess above is correct). In order to figure it out, I would have to run the code myself and do experiments with it. I'd do things like try running it with just a single turtle and see if that case fails; add print statements to the code, showing each the turtle's coordinates and motions, so I could try and figure out which turtle is going wrong, and exactly under what conditions; and so forth. It's like detective work, or like doing chemistry experiments in the lab.

NetLogo check if turtles are on same coordinates

I have two turtle breeds who populate each sides of the window and then only move round in there own side.
The problem I am having is that I want to constantly check to see if one singular instance of a turtle from each breed are both on the same y coordinate. And if this returns true i want both of those turtles to stop, but for all other turtles from each breed to carry on moving. I know you can identify a turtle by there unique ID but i don't know how to use this and how to use the correct syntax.
The best way to describe this in pseudo code would be
ask turtles [
if breed1 turtle ycor = breed2 turtle ycor
[ stop breed1 turtle and breed2 turtle ] ]
UPDATE
Tried getting the code to work but still nothing happening. Not sure if it is the way the procedure is wrote or the number I have chosen for the threshold.
to move-turtles
ask turtles [
if not any? turtles with [ breed != [ breed ] of myself and abs (ycor - [ycor] of myself) < 1 ]
[
ask redteam with [pcolor = green - 3] [
right random 360
forward 1
]
ask redteam with [pcolor != green - 3] [
back 1
]
ask blueteam with [pcolor = green - 2] [
right random 360
forward 1
]
ask blueteam with [pcolor != green - 2] [
back 1
]]
]
end
Note that "same coordinate" is actually somewhat ambiguous. If one turtles ycor is 5.0000001 and another's is 5.0000000, are they at the same coordinate? Because of this, you should check to see if their coordinates are within a certain amount of each other.
Also, the best way to stop moving is to simply not move. So, here is a possible go procedure that would do what you want:
to go
ask turtles [
if not any? turtles with [ breed != [ breed ] of myself and abs (ycor - [ ycor ] of myself) < threshold ] [
move ;; replace with your move procedure or code
]
]
end
Here, each turtle checks to see if there are any turtles of a different breed who's ycor is within threshold of their own ycor. If there are not, then it moves. Otherwise, it does nothing.
The myself stuff is the most confusing part here, so I recommend reading the docs.