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 - 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

Related

to turn turtle 90 degrees

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

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

Netlogo - Ordered Movement

I have the following error in Netlogo and I'm unsure why. I am trying to make the turtles move around in a ordered fashion but I keep getting an error when I change the d angle: "FACE expected input to be an agent but got NOBODY instead".
Any help would be appreciated.
globals [Angles]
to setup
clear-all
create-turtles amount [ setxy random-xcor random-ycor ]
ask turtles [
set Angles (random 360)
]
reset-ticks
end
to monitor
show count patches
show ticks
end
to go
if (all? patches [pcolor = yellow]) [stop]
ask turtles [
face min-one-of patches with [ pcolor = black ] [ distance myself ]
;; This line of code tells the turtle to head towards the nearest patch containing the colour of black.
set Angle d Angle * 1 - Angle
rightt Angle
forwardd 1
ifelse show-travel-line? [pen-down][pen-up]
set color red
if pcolor = black [
set pcolor yellow
]
]
tick
end
You can unveil the problem but running this test:
to test
ca
crt 1
let x -10E307 * 10
show x
ask turtle 0 [rt x]
inspect turtle 0
end
You will see that the heading is NaN because you gave it a turn of -Infinity. Now if you move the turtle, the xcor and ycor will become Nan.
To avoid this problem, you need to limit the values taken by angle. For example,
globals [turn angle]
to setup
clear-all
set turn random-float 1
create-turtles 10 [
setxy random-xcor random-ycor
set color red
pen-down
]
reset-ticks
end
to go
if (all? patches [pcolor = yellow]) [stop]
ask turtles [
part1
part2
if pcolor = black [
set pcolor yellow
]
]
tick
end
to part1
let _patches (patches with [ pcolor = black ])
face min-one-of _patches [ distance myself ]
end
to part2
set turn (4 * turn * (1 - turn))
set angle turn * 360
rt angle
fd 1
end

You can't use Migrate in a turtle context, because migrate is observer-only

I managed to build my small model, but i do get an error, but don't know why?
Ps: i'm a bloody beginner
The Error code is:
You can't use migrate in a turtle context, because migarte ia observer-only.
So what can I do?
Thank you for your answers.
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
There are 3 different primitives for creating new turtles in NetLogo:
create (observer), hatch (turtle) and sprout (patch).
Each one works only in a specific context.
This means you have to be aware of the context in which you are calling your procedure.
In your example, you are calling the create-turtles primitive inside of an ask turtles (specifically ask boats) context. Which means you are in a turtle context and not in an observer context and therefore create-turtles is not allowed to use.
To solve your problem, you just have to replace create-turtles by hatch (or hatch-<breed>, like hatch-fish if you want to spawn new agents in a specific breed).