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

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:

Related

calculate the total move distance of one turtle

I am new to Netlogo and learning a model is about animals moving around to eat grass based on the grazing model from NetLogo. The moving behavior is based on the biomass's richness, so it is not just impacting at right angles.
Given the conditions, I don't know how to add a monitor in my model that calculates the total distance a turtle moved by traveling each pixel.
Move specifically, I wonder if there is a way to calculate the total length of the pen-marked lines. (Show in the picture)
enter image description here
The basic setting of move is
to move-turtles
uphill-biomass
forward 1
set distance-traveled (distance-traveled + 1)
if not can-move? 1 [ rt random 150 ]
end
to uphill-biomass
let biomass-ahead biomass-scent-at-angle 0
let biomass-right biomass-scent-at-angle 35
let biomass-left biomass-scent-at-angle -35
if (biomass-right = biomass-ahead) and (biomass-left = biomass-ahead) [ wiggle ]
if (biomass-right > biomass-ahead) or (biomass-left > biomass-ahead)
[ ifelse biomass-right > biomass-left
[ rt 35 ]
[ lt 35 ] ]
end
Thank you very much for any helps!
You need to use some turtle variables with the turtles-own primitive and do the distance calculation by hand.
The easiest way to do such a calculation is to save the coordinates between a turtle's previous coordinates with turtle variables and then use the distancexy primitive (link for dictionary entry: https://ccl.northwestern.edu/netlogo/docs/dictionary.html#distancexy)
Here's a simple implementation:
turtles-own [last-x last-y distance-traveled]
move-turtle
uphill-biomass
set last-x xcor
set last-y ycor
forward 1
set distance-traveled (distance-traveled + (distancexy last-x last-y))
if not can-move? 1 [ rt random 150 ]
end
I hope this answers your question.

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

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.

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

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

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. :)