NetLogo If Command - netlogo

I created an if statement and if this statement is true, I want two commands to be executed, one is in regards to a patch and the other is in regards to the turtles. If this condition is true, I want the patch a turtle is on to decrease a variable of the patch and I also want a variable of the turtle to increase. How would I link the two commands to the same if statement?
Example:
if ([patch-variable] of patch-at 0 0) > 0.2
[ask patch-at 0 0
[set patch-variable patch-variable - 0.01]
[ask turtle
[set turtle-variable turtle-variable + 1 ]]]
Thanks in advance!

You don't have to put the command into separated square brackets. If the condition is true, Netlogo will execute all commands within the next pair of square brackets.
if patch-variable > 0.2
[
set patch-variable patch-variable - 0.01
set turtle-variable turtle-variable + 1
]

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

How do I check for collisions between any two turtles?

Netlogo is honestly very weird for me, but I've been trying to make Space Invaders for a project. I need to be able to check the distance between multiple turtles of the same breed in order to run a collision check. How do I do this?
to shoot
ask turtle 0 [
hatch 1
[
set shape "dot"
set size 2
set color red
set ctrl "projectile"
set xcor [xcor] of turtle 0
set ycor [ycor] of turtle 0 + 2
repeat 40
[
ifelse ycor < 15 and distancexy [xcor] of turtle 1 [ycor] of turtle 1 > 0.5
[
set ycor ycor + 1
]
[
ifelse distancexy [xcor] of turtle 1 [ycor] of turtle 1 < 0.5
[
ask turtle 1 [die]die][die]
]
wait .01
]
]
]
end
So you want something like a 'hero' turtle to shoot all the turtles within some distance. I have modified your code a little but the basic idea is sound. You didn't really have a step for identifying the targets. I also simplified the movement by using face and used a while because there is no guarantee it will take 40 steps to get to the target.
to shoot
let shooter turtle 0 ; or however the shooter is selected
ask shooter
[ let targets other turtles with [distance myself < 0.5 ; finds the targets
if any? targets
[ let this-target one-of targets
face this-target ; so shooter is facing the target so trail better
; do the shooting
[ hatch 1
[ set shape "dot"
set size 2
set color red
set ctrl "projectile"
let step-distance 0.02
while distance this-target > step-distance
[ face this-target ; you don't need to worry about coordinates
forward step-distance
wait .01
]
die ; once close enough, projectile dies
]
ask this-target [ die ] ; and kills the target
]
]
]
end
This code is completely untested and needs another loop to get all the targets. Usually you don't use loops much in NetLogo, but an ask would change this into the perspective of the target rather than the shooter.

Netlogo: Can Netlogo set up an infinite number of turtles for only one specific patch?

Can Netlogo set up an infinite number of turtles for only one specific patch? And the patch is road setting. This link is the image of that specific patch. https://i.stack.imgur.com/DdBF0.jpg And the following is the sample code. However this is not compleated.
turtles-at 0 0 of patch min-pxcor 0 ; this is not compleated
Not exactly sure what you're asking, but there is no limit to the number of turtles that can be on a patch (save the limit imposed by your computer's memory).
Also, the code you're probably looking for is something like:
turtles-on patch 0 0
for the left patch and
turtles-on patch 1 0
for the right patch.
As per Bryan's answer, there is no theoretical restriction on the number of turtles in a single patch, although your computer will have a limit- the more turtles in your model (on any patch) the more memory your model will use. So the short answer is, as far as I know, there is no way to just say to Netlogo, "Spawn infinite turtles on this patch."
If, however, by infinite you really just want enough turtles that you won't run out of them for specific interactions, you could probably get by either by just spawning a large number on that patch or by just sprouting more as needed (my preference).
For the first option, you can have a bunch of turtles on the same patch:
to setup
ca
reset-ticks
ask patch 0 0 [
sprout 10000
]
ask patch 0 0 [
print count turtles-here
]
end
Alternatively, if your turtles on the patch get used up or become unavailable in some way, just have more sprout as needed to keep your numbers high enough for what you're trying to do. Here's an example where red turtles walk to a patch with "infinite" (1000) blue turtles, link to one of the blue turtles, and take them away. However, at the end of each tick, the "infinite" patch checks if there are fewer than 1000 turtles-here. If there are, it spawns enough turtles to bring that count back up to 1000. Try this code in a new file:
to setup
ca
reset-ticks
infinite-sprout
source-sprout
end
to go
ask turtles with [ color = red ] [
fd 0.5
if any? ( turtles-on patch-ahead 1 ) with [ color = blue ] [
create-link-with one-of turtles-on patch-ahead 1 [
tie
]
set color green
]
]
ask turtles with [color = green] [
move-to patch-right-and-ahead 90 1
if pycor = max-pycor [
ask link-neighbors [
die
]
die
]
]
infinite-sprout
source-sprout
tick
end
to source-sprout
ask patch max-pxcor 0 [
if not any? turtles-here and random 3 = 1 [
sprout 1 [
set shape "arrow"
set color red
set heading 270
]
]
]
end
to infinite-sprout
ask patch 0 0 [
if count turtles-here < 1000 [
sprout ( 1000 - count turtles-here) [
set shape "circle"
set color blue
]
]
]
end
Then set up your interface like this:
If you run that model for a while, you will see that at the end of every tick, the count turtles of patch 0 0 is brought back up to 1000, effectively giving you an infinite source of turtles that you can "use up." Does that accomplish what you need?

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.

Trying to get factions to arrange in segments? NetLogo code advice

I've made a model that aranges factions (turtles in different colours) in a circle.
At the moment they arrange randomly, was wondering if someone could help me arrange them so, for example, red occupies the first 90 degrees, blue the next 90 degrees, etc (on setup).
Here's my code...
ask patch 0 0
[ ask patches in-radius ( max-pxcor * .9) with [ random-float 100 < density ]
[ sprout 1
[ set breed cons
set shape "circle"
set faction random factions
set heading random 360
set size 1
]
]
]
.. guessing I will have to do 360 / fractions, but not sure how to phrase it, if someone could help me out that'd be great. Thanks!
The NetLogo primitive that's the closest to what you want to do is in-cone, which reports the set of turtles that are in the "cone of vision" of another turtle. But your "pie slices" should just be relative to patch 0 0, not to another turtle! No problem: just make a temporary turtle at patch 0 0, use it to get turtles in-cone with the appropriate angle, and kill your temporary turtle.
The following procedure can be used "as is" with your code (just call it from your setup procedure after creating your turtles exactly as you were doing before):
to assign-factions
let angle 360 / factions
foreach n-values factions [?] [
ask patch 0 0 [
sprout 1 [
set heading ? * angle
ask turtles in-cone max-pxcor angle [ set faction ? + 1 ]
die
]
]
]
end
The code is pretty straightforward, except for maybe the more obscure n-values. You could replace it with a while loop if you prefer, but it's really just counting from 0 to factions.
Here is what you'd get with 5 factions: