to turn turtle 90 degrees - netlogo

The orientation (heading) of turtles must be set randomly when wandering but must be limited to a range of 40 degrees (20 to the left and 20 to the right) per step taken, the only exception to this is when avoiding other agents, in this instance a maximum turn of 90 degrees is permitted.
I am new to netlogo. I want these movements with my turtles.
to move-turtles
ask turtles[
if who = ticks[
stop]
let agent-ahead one-of turtles-on patch-ahead 1
ifelse agent-ahead != nobody [
??????
][
rt random 20
lt random 20
]
fd 0.2
]

As stated in the comments you can just use rt and lt with random 45 to achieve what you want.
to setup
clear-all
reset-ticks
crt 5 [set color red]
end
to go
ask turtles[
if who = ticks[
stop]
fd 1
let agent-ahead one-of turtles-on patch-ahead 1
ifelse agent-ahead != nobody [
set color blue
ifelse random 2 = 1 [rt random 45] [lt random 45]
][set color red
rt random 20
lt random 20
]
fd 0.2
]
tick
end

Related

How to force turtles to move only in a half of the world?

I created a world divided in two parts with the command ask patches with [ pxcor < 0] [set pcolor blue] ask patches with [pxcor > 0] [set pcolor green] .
In one of this there're 100 turtles, who 5 are infected and the same in the other part.
My problem is to force the turtles to move only in their part of the world. So I want that turtles with pcolor = blue move randomly in the second and third quadrant (pxcor <0) and turtles with pcolor = green in the first and fourth quadrant (pxcor> 0). how do I do?
This is the code:
turtles-own
[
sick?
sick-time]
to setup
ca
ask patches with [ pxcor < 0 ] [set pcolor blue] ; we want divide the world in two parts: the blue one in the north of Italy
ask patches with [pxcor > 0 ] [set pcolor green]; the white one is the south of Italy
ask patches with [pxcor = 0 ] [set pcolor white ] ; represent the border
create-turtles 200 ; we create a population made up for 200 people
[ set size 1
set shape "person"
set sick-time 0
get-healthy]
ask n-of 100 turtles ; 100 of this one live in north of Italy
[setxy 12 random-ycor ]
ask n-of 100 turtles ; another 100 in the south
[setxy -12 random-ycor
]
ask n-of 5 turtles with [pcolor = blue] ; we want infect 5 people for each world
[get-sick ]
ask n-of 5 turtles with [pcolor = green]
[get-sick ]
reset-ticks
end
to get-healthy
set sick? false
set color white
end
to get-sick
set sick? true
set color yellow
set shape "circle"
set sick-time sick-time + 1
end
to go
ask turtles
[
move ]
tick
end
to move
rt random-float 360
fd 1
end
Your movement procedure looks like:
to move
right random-float 360
forward 1
end
If you want them to just stay where they are if moving would take them into the wrong half, then you can use patch-ahead to test the patch they'd be moving to. I think what you want is that they don't go to a different coloured patch. One way is:
to move
right random-float 360
if [pcolor] of patch-ahead 1 = pcolor [forward 1]
end
[pcolor] of patch-ahead 1 returns the colour of the patch that is one distance unit ahead, so where the turtle is trying to move to. pcolor is the colour of the patch that the turtle is currently standing on.

NetLogo: How to make a turtle recognise any shade of one color?

I'm using NetLogo for the first time and need to lay out a simple programme where i have one light source that diffuses light out beyond its source patch and one turtle that will avoid the light.
I can achieve this by using basic 'set pcolor yellow' and then use 'if patch-ahead [pcolor] = yellow [right 45][fd speed]' type command. However this doesn't give me diffused light.
By adapting the HeatBugs code, i can diffuse the color out past the source patch, however the roaming turtle no longer recognises the color as yellow, i think, as it is a scale-color. I tried setting the code to != black but this also doesn't work. I'm assuming it's because the patches are being recolored after each tick.
Is there a way to make the turtle recognise the patches of diffused color so as to avoid them? Or a simpler way to diffuse the light out. (i want a variable intensity so using neighbors and yellow -1 won't do it.)
Here's the code i have so far: (this is a condensed version as i have other things happening in the main body, so i apologise if it isn't clear)
globals [ color-by-unhappiness? ]
turtles-own[
speed
speed-limit
speed-min
ideal-temp ;; The temperature I want to be at
output-heat ;; How much heat I emit per time step
unhappiness ;; The magnitude of the difference between my ideal
;; temperature and the actual current temperature here
]
patches-own[
temp
]
to setup
clear-all
setup-turtles
;;creating diffused light
set color-by-unhappiness? false ;; button
ask n-of number-of-lights patches [
sprout 1 [
set color white
set shape "circle"
set ideal-temp min-ideal-temp + random (max-ideal-temp - min- ideal-temp) ;;these are all sliders
set output-heat min-output-heat + random (max-output-heat - min- output-heat) ;;these are all sliders
set unhappiness abs (ideal-temp - temp) ;;ideal-temp is a button
color-by-ideal-temp
set size 2
]
]
reset-ticks
end
to setup-turtles
create-fears number-of-fears [
set color violet
set shape "circle"
setxy random-xcor random-ycor
set speed 0.1 + random-float 0.9
set speed-limit 1
set speed-min 0.00
]
end
to go
ask turtles [
if speed > speed-limit [set speed speed-limit]
fd speed
ask fears[
if patch-ahead 1 = nobody [rt 135]
if patch-right-and-ahead 45 1 != nobody and [pcolor] of patch-right-and-ahead 45 1 != black[left 45]
if patch-left-and-ahead 45 1 != nobody and [pcolor] of patch-left-and-ahead 45 1 != black[right 45]
ifelse [pcolor] of patch-here = yellow [set speed speed-min][fd speed]
]
if not any? turtles [ stop ]
;; diffuse heat through world
diffuse temp diffusion-rate
ask patches [ set temp temp * (1 - evaporation-rate) ]
ask turtles [ set temp temp + output-heat ask bugs [bug-move patch-here]]
recolor-turtles
recolor-patches
tick
end
to recolor-patches
ask patches [ set pcolor scale-color yellow temp 0 150 ]
]
end
I can't use your code as-is; check out the MCVE guidelines for some tips on reducing your code to just the necessary parts.
Color in Netlogo can given as a string, but it's also just a range of numbers. If you look at Tools > Color Swatches, you will see that the range of "Yellow" colors corresponds roughly to 40 ~ 50. So if you want to, you can just have them evaluate patch color using a numerical range rather than the color name. So, using this unnecessarily complicated example setup:
patches-own [ light? temp]
to setup
ca
ask patches [
set light? false
]
ask n-of 5 patches [
set light? true
set temp 150
]
recolor-patches
crt 10 [
move-to one-of patches with [ not ( pcolor > 40 and pcolor < 49 ) ]
]
reset-ticks
end
to recolor-patches
ask n-of 3 patches with [ light? ] [
if temp < 20 [
set temp temp + random 20
]
]
repeat 5 [
diffuse temp 0.1
]
ask patches [
ifelse temp > 0.25 [
set temp temp - 0.005
] [
set temp 0
]
set pcolor scale-color yellow temp 0 15
]
end
You can ask your turtles to move and just avoid patches that fall in that numerical range:
to go
recolor-patches
ask turtles [
ifelse [pcolor] of patch-ahead 1 > 40 and [pcolor] of patch-ahead 1 < 49 [
let target min-one-of neighbors [pcolor]
if target != nobody [
face target
fd 1
]
] [
rt random 60 - 30
fd 1
]
]
tick
end
EDIT
As Seth Tisue pointed out, the shade-of? primitive can accomplish what the greater than / less than logical statement does:
to go
recolor-patches
ask turtles [
ifelse shade-of? ( [pcolor] of patch-ahead 1 ) yellow [
let target min-one-of neighbors [pcolor]
if target != nobody [
face target
fd 1
]
] [
rt random 60 - 30
fd 1
]
]
tick
end
However, this does require a slight modification to the recolor-patches procedure, as scale-color sets the base color to 40 (in the case of 'yellow'); just ask patches with that pcolor to set their color to black (0) so that movement works as expected here:
to recolor-patches
ask n-of 3 patches with [ light? ] [
if temp < 20 [
set temp temp + random 20
]
]
repeat 5 [
diffuse temp 0.1
]
ask patches [
ifelse temp > 0.25 [
set temp temp - 0.005
] [
set temp 0
]
set pcolor scale-color yellow temp 0 15
if pcolor = 40 [
set pcolor black
]
]
end

How to ask turtles to avoid a patch with specific color at patch-ahead 1 but turtles move forward by bigger number (not fd 1) in one step in NetLogo

I am trying to ask turtles with any heading (random 360) to avoid the patches with red color. But, I observed that if a turtle is asked to move " fd 1 + random-float 2.0" then sometime turtles turns (set heading heading - 180) when there is a red patch ahead and sometimes(even most of the times) do not turn. Also when I ask the turtles to move " fd 1 " or " fd 0.1 + random-float 0.9 " the code works all fine. Hopefully the reason behind is the number of patches I am asking the turtles to move in one step. What will be the next patch for the move "fd 0.1 + random-float 0.9" and how could I make this working with patch-ahead 1. My code and the interface is added.
to setup
clear-all
ask patches [set pcolor green ]
ask patches with [pycor = 3] [set pcolor red]
create-turtles 40
[
set color blue
set xcor random-pxcor
set ycor random-pycor
set heading random 360
set size 1
set speed 1 + random-float 2.0
]
end
to go
ask turtles [
fd speed
avoid-walls
]
end
to avoid-walls
if [pcolor] of patch-ahead 1 = red [set heading heading - 180]
end
Try using in-cone instead of patch-ahead
to avoid-walls
let front-patches patches in-cone 2 75
if pcolor of one-of front-patches = red [set heading heading - 180]
end

why does the hatchign stop after some time?

Morning everybody
After editing my model, by adding a random spawn rate:
to migrate
if random-float 100 < random-spawn-rate
[create-turtles 2 [rt random-float 360 fd 1]]
end
i get the problem that after one kind of turtles (the boats) die out, even their random hatching seems to die out?
i tried other ways, but nothing really works
Please help me
thank you
Full code:
breed [fish a-fish]
breed [boats boat]
boats-own [profit]
to setup
clear-all
ask patches [set pcolor blue]
set-default-shape fish "fish"
create-fish initial-number-fish
[
set color grey
set size 1.0
setxy random-xcor random-ycor
]
set-default-shape boats "boat"
create-boats initial-number-boats
[
set color black
set size 1.5
set profit random (1 * profit-per-fish)
setxy random-xcor random-ycor
]
reset-ticks
end
to go
if not any? turtles [stop]
ask fish
[
move
fish-reproduce
]
ask boats
[
move-boats
catch-fish
death
reproduce-boats
migrate
]
tick
end
to move
rt random 50
lt random 50
fd 1
end
to fish-reproduce
if random-float 100 < fish-growth
[hatch 1 [rt random-float 360 fd 1]]
end
to move-boats
rt random 50
lt random 50
fd 1
set profit profit - 0.1
end
to catch-fish
let prey one-of fish-here
if prey != nobody
[ask prey [die]
set profit profit + profit-per-fish]
end
to death
if profit < 0 [die]
end
to reproduce-boats
if profit > 1
[
set profit (profit / 2)
hatch 1 [rt random-float 360 fd 1]]
end
to migrate
if random-float 100 < random-spawn-rate
[create-turtles 2 [rt random-float 360 fd 1]]
end
If there are no boats to hatch new boats, then no more boats will be created. Your hatch is dependent on how many boats there are.
Essentially, for a boat:
if profit > 1
[
set profit (profit / 2)
hatch-boat 1 ...
]
You cut the profit in half for every boat spawned. Unless your profit grows or remains the same, your boats will eventually die out since no more will be hatching.

Development of turtles from one breed to another

I dont know much about netlogo so this may be a really simple solution but I have multiple breeds of turtles where each breed is a different age range (eggs, hatchlings, juveniles, hawksbills (adults)) and i need them to be able to go from one breed to the next after a certain amount of ticks (in my model one tick = one day). Is this possible to code for?
Here's my code so far
globals [island]
Breed [eggs egg]
Breed [hatchlings hatchling]
Breed [juveniles juvenile]
Breed [hawksbills hawksbill]
turtles-own [
age
z
]
to setup
clear-all
set-default-shape eggs "circle"
create-eggs 80
[ set color 49.5
set size 0.5
setxy random-xcor random-ycor ]
set-default-shape hatchlings "turtle"
create-hatchlings 70
[ set color 57
set size 1.5
setxy random-xcor random-ycor ]
set-default-shape juveniles "turtle"
create-juveniles 10
[ set color 55
set size 2
setxy random-xcor random-ycor ]
set-default-shape hawksbills "turtle"
create-hawksbills 9
[ set color 53
set size 3
setxy random-xcor random-ycor ]
clear-output
setup-environment
setup-birthdays
reset-ticks
end
to setup-environment
ask patches [set pcolor cyan]
ask patches with [abs(pxcor) < 5 and abs(pycor) < 5] [set pcolor yellow]
ask patches with [abs(pxcor) < 2 and abs(pycor) < 2] [set pcolor brown]
end
to setup-birthdays
end
to go
ask eggs
[ kill-eggs ]
ask hatchlings
[ move-hatchlings
kill-hatchlings]
ask juveniles
[ move-juveniles
kill-juveniles]
ask hawksbills
[ move-hawksbills
kill-hawksbills
reproduce-hawksbills ]
tick
if not any? turtles [stop]
end
to kill-eggs
set z random 100
if z > 50
[die]
end
to kill-hatchlings
set z random 100
if z > 10
[die]
end
to kill-juveniles
set z random 20
if z > 18
[die]
end
to kill-hawksbills
set z random 5
if z > 4
[die]
end
to move-hatchlings
rt random 360
fd 1
end
to move-juveniles
rt random 360
fd 1
end
to move-hawksbills
rt random 360
fd 1
end
to reproduce-hawksbills
if pcolor = yellow
[set z random 100
if z > 75
[hatch-eggs 5[
set color 49.5
set size 0.5]
]]
end
To turn a turtle into a hatchling, just do set breed hatchlings! So, your code will probably look something like:
if age > 10 [
set breed hatchlings
]