unable to make non-stationary turtles change their direction if an obstacle a patch ahead - netlogo

If any one please give some time.
I have an area(say a colony) with boundary wall as black patches and at some point within the boundary there is one building with building wall as blue patches. People(breed) are normally moving inside boundary and the building as well. Also they are entering and going out of the boundary. Due to some reason (suppose rumor) and after certain condition (if more than 15 person hears the rumor) they starts moving randomly with any of the headings 0, 90, 180 and 270. So, the problem I am unable is, to apply check on the turtles(people) randomly moving to change their heading or turn back if they sense the boundary or wall a patch ahead.
I tried following ways but not working, they simple passes form these patches
1) Asked the turtles if heard-rumor? and times-heard > 1 [
if [pcolor] of patch-ahead 1 = blue [set heading [heading] of self - 180]
if [pcolor] of patch-ahead 1 = black [set heading [heading] of self - 180] ]
2) set patches with boundary-wall [set pcolor black] and building-wall [set pcolor blue] and then set patch variables boundary-wall? And building-wall? true on these patches. Further asked the turtles
if heard-rumor? and times-heard > 1 [
if boundary-wall? or building-wall? [ set heading [heading] of self - 180 ] ]
The procedure sequence is
to go
ask people [ ;breed
fd speed
spread-rumor
people-wander ]
end
So after spread-rumor function,
to people-wander
if heard-rumor? and times-heard > 1 and inside-boundary?
[
if people-heard-rumor > 10 [ set heading one-of (list 0 90 180 270) ] ];random 360
;people-heard-rumor is a count how many have received rumor
if heard-rumor? or fear-worst? [
; if [pcolor] of patch-ahead 1 = blue [set heading [heading] of self - 180]]
; if [pcolor] of patch-ahead 1 = black [set heading [heading] of self - 180]]
boundary-wall? or temple-wall? [set i? true set heading [heading] of self - 180 show 5] ]
end
I don’t know what wrong I am doing. But surely I am not using proper way. Any help is deeply thankful.

You start out with fd speed so your people will go right through the barriers on that command without ever testing for a barrier. Note that even if you were to test 1 patch ahead before doing that, if speed can be greater than 1, you could still go right through the barriers. Furthermore, in a corner a person might have a barrier both in front of it and behind it, so reversing course can also be a problem.
Btw, [heading] of self is the same as heading, and to turn around it is more natural to say rt 180.
EDIT (in response to comment):
Here is a simple example of moving step by step, checking along the way:
to fd-with-checks [#speed]
repeat #speed [
ifelse (isbarrier? patch-ahead 1) [
stop
] [
fd 1
]
]
end
to-report isbarrier? [#patch]
report pcolor = blue or pcolor = black
end

Related

How can I separate left- and right-vision field of a turtle in NetLogo?

I want to separate left- and right-vision fields of a turtle in NetLogo.
Left-vision field means angles are -179 to -1 degrees while right-vision field means angles are from 1 to 179 if an angle of front of a turtle is 0 degrees.
In NetLogo, patch-right-and-ahead and patch-left-and-ahead can separate left- and right-vision fields of a turtles, but those codes is only a angle incorporate a turtle.
And, in-cone can not separate the vision fields.
Is there any codes to program this in NetLogo?
You can save the turtle's current heading, turn it 90-degrees left and see what's on the left side of the original heading with
scan x 180
then turn it 90-degrees right from original heading and see what's on the right side of the original heading with
scan x 180
then turn it back to its original heading.
Here's code that illustrates this. Set the scan angle to something smaller than 180 to see more clearly what's happening. As you step through the go step it rotates and highlights what it is seeing.
globals [ scan-angle]
turtles-own [ actual-heading ]
to setup
clear-all
set scan-angle 180
create-turtles 1 [ set size 4 set heading 0 set actual-heading heading]
reset-ticks
end
to go
ask turtle 0 [ set heading (heading + 30) set actual-heading heading]
scansides scan-angle
tick
end
to scansides [ angle ]
ask patches [ set pcolor black]
ask turtle 0
[
;; scan left side
set heading (actual-heading - 90)
ask patches in-cone 10 angle [ set pcolor red]
;; scan right side
set heading (actual-heading + 90)
ask patches in-cone 10 angle [ set pcolor green]
set heading actual-heading
]
end

NetLogo.Trying to make turtle on a specific patch color die

So I've made the breeds player and zombie:
breed [zombies zombie]
breed [players player]
I would like to kill a zombie if it walks unto a black patch (pit).
to go
ask zombies
[
;set heading (heading + 45 - (random 90))
let closest-player min-one-of players[distance myself]
set heading towards closest-player
forward 1
;if xcor = pcolor black [Death] I have a lot to learn for netlogo syntax
;if ycor = pcolor black [Death] these lines are to give an Idea of what I'm trying to do.
]
end
to Death ;; turtle procedure
set shape "skull"
set color white
set heading 0
end
As described in the Variables section of the programming guide:
a turtle can read and set patch variables of the patch it is standing on.
In your case, this means that your zombies can directly check the pcolor of the patch that they're on:
if pcolor = black [ Death ]
This is the equivalent of the more verbose form using patch-here:
if [ pcolor ] of patch-here = black [ Death ]
You often don't need to use coordinates to identify a patch. NetLogo has plenty of reporters that can help you getting the patch you want. For example: patch-ahead, patch-at, patch-at-heading-and-distance, patch-here, patch-left-and-ahead and patch-right-and-ahead.
But in those cases where you do need to find a patch using coordinates, there is patch:
if [ pcolor ] of patch xcor ycor = black [ Death ]
But all of this is unnecessary in your case. Stick to the simple if pcolor = black [ Death ].

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.

Preventing a NetLogo turtle from hitting a wall

I'm trying to do something really simple, but for some reason I just can't get it to work.
My setup function creates a square wall from (-20, 20) to (20, 20), and generates a circular turtle of size 3 inside the wall. The square wall is simply made of patches colored blue.
Now I have a go function, which tells the turtle to rotate anywhere from -90 to 90 degrees, and moves it forward by 0.5 steps. It is not allowed to walk "into" the wall; when it hits the wall, it simply picks another direction to move in. The turtle cannot "sense" the wall until it actually walks into it.
The code I've been using is as follows:
ask turtle 0 [
let invalid true
let turn-degree (random(180) - 90)
rt turn-degree
let next-patch patch-ahead 1 ;; Declare next-patch before moving
while [invalid] [ ;; While invalid condition
ask next-patch [
;; Neighbors of the next patch are counted, because turtle is size 3
if ( count neighbors with [pcolor = blue] = 0 ) [
set invalid false
]
]
if (invalid) [
lt turn-degree ;; Turn the turtle back to the original direction
set turn-degree (random(180) - 90) ;; Randomize turn degree again
set next-patch patch-ahead 1 ;; Declare next-patch before moving again
]
]
;; Finally, jump 0.5 steps ahead in the chosen direction
jump 0.5
]
I'm sad to say that the above code doesn't work, because the turtle still somehow manages to overlap itself with the blue wall, which shouldn't happen. I suspect it is because of two reasons:
1) The 0.5 steps is screwing up the "patch-ahead" condition. However, I've tried with patch-ahead 0.5 to no effect.
2) The randomized turn degree is resulting in the blue wall being slightly more than 0.5 away from the turtle, but I have no workaround to this...
Any suggestions?
The problem is that when the turtle moves to the closer to the edge of a patch that is touching a patch that's touching the wall, the neighbors of the turtle's patch aren't part of the wall, but the turtle is still less than 1.5 away from the wall. Try this:
ask turtle 0 [
rt (random 180) - 90
fd .5
while [ any? patches in-radius 2 with [ pcolor = blue ] ] [
bk .5
rt (random 180) - 90
fd .5
]
]
I didn't exactly try Bryan's method, but what I had worked out for me just as well. I ended up using the following:
if (any? patches in-cone 3 60 with [pcolor = blue])
as my wall-detection condition. It worked well enough. :)

netlogo move turtle nearest patch

I want to move my turtle to the nearest red or green color patch in its vision.I tried this code but it doesnt move.What is wrong?
while [collectedDirt = 5]
[
ask turtle 0 [
let nearest-patch min-one-of (patches with [pcolor = red or pcolor = green] in-cone 15 20)[distancemyself]
face nearest-patch
fd distance nearest-patch
]
set collectedDirt collectedDirt + 1
search-dirt ;; research whether there is red patch in-cone because of new position
]
You might want to provide us with a little bit more context, but my guess would be that you need something like while [collectedDirt < 5] instead of while [collectedDirt = 5]. If this block is the only way your turtles can "collect dirt", the code probably never even gets executed...
Edit:
You also might want to add a condition in case there is no red/green patch in the cone of vision:
if is-patch? nearest-patch [
face nearest-patch
fd distance nearest-patch
set collectedDirt collectedDirt + 1
]