Action not happening every certain amount of ticks - netlogo

this code is for the reproduction of an animal. It should happen every 10 ticks, but it is happening instantly and i dont know why. They also have energy. Reproduction is set to be random to show females.
thanks in advance for the help.
turtles-own[ energia edad]
breed[perros perro]
to setup
resize-world 0 80 0 60
clear-all
ask patches[
set pcolor green]
create-perros 50
[
set size 3 ;; easier to see
set color yellow
setxy (41 + random 39) random 60
set heading random 360
set energia 100
set edad 1
]
reset-ticks
end
to go
ask perros [ set edad edad + 1]
ask perros [
morir-perros
moverse-perros
reproducirse
]
tick
end
to morir-perros
if edad > 15 [die]
if energia < 1 [die]
end
to moverse-perros
set heading random 360 fd 5
set energia energia - 5
end
to reproducirse
if ticks mod 10 = 0 [ if random 100 > 50 [set energia energia - 60
hatch 1 [ rt random-float 360 fd 2
set energia (energia + 60) / 3]]]
end

Right now you have tick to increment your time step at the end of your go procedure. In any model, ticks start at 0 by default, so your model is evaluating ticks mod 10:
observer> show 0 mod 10
observer: 0
Since that would satisfy the condition in reproducirse, the code is evaluated. You can handle this in a variety of ways. The simplest may be to modify your if statement in reproducirse- for example:
to reproducirse
if ticks > 0 and ticks mod 10 = 0 [ if random 100 > 50 [set energia energia - 60
hatch 1 [ rt random-float 360 fd 2
set energia (energia + 60) / 3]]]
end

Related

Netlogo - code adds a tick and then stops

With this model I need the code for the first year (tick = 0) to be different to the remaining 4. I've run the code below and the first tick runs ok, it then ticks and stops - none of the tick = 1 code seems to be running.
globals [num_agents difference year leader_test ]
breed [tasks task]
breed [managers manager ]
tasks-own [requirement leadership matched ]
managers-own [ability wealth matched requirement task_leader]
to setup
clear-all
set num_years 5
set tolerance 5
set num_agents 100
create-tasks num_agents [
set shape "box"
set leadership one-of [10 20 30 40 50 60 70 80 90 100]
ifelse who < 50 [setxy 0 who set color blue][setxy 45 (who - 50) set color blue]
set heading 90
set requirement who + 100
set matched 0
]
create-managers num_agents [
setxy random 30 + 10 random 50
set shape "person" set color green set heading 270
set ability (who - num_agents + 100)
set wealth 0 set matched 0
]
reset-ticks
end
to go
;;first year -different to remaining
ifelse ticks < 1 [
ask managers with [matched = 0]
[show ticks
move-to one-of tasks with [matched = 0]
fd -1
set requirement [requirement] of one-of tasks-on patch-ahead 1
set task_leader [leadership] of one-of tasks-on patch-ahead 1
set difference abs(requirement - ability)
set matched 1
set wealth (requirement)
show wealth
show task_leader
ask tasks-on patch-ahead 1 [set matched 1 set shape "arrow" set heading 0]
if difference > tolerance [set color red ask tasks-on patch-ahead 1 [set shape "circle" ] ]]
]
; years 2 - num_years
[
ask managers [
if ability > (requirement + tolerance) [
ask tasks-on patch-ahead 1 [set matched 0 set shape "box" ]
setxy random 30 + 10 random 50
set shape "person" set color green set heading 270 set matched 0
]
]
ask managers with [matched = 1]
[ set leader_test random 100
if ability < (requirement - tolerance) [
if leader_test <= task_leader
[;;leader should make correct decision and fire manager
ask tasks-on patch-ahead 1 [set matched 0 set shape "butterfly" ]
setxy random 30 + 10 random 50
set shape "person" set color green set heading 270 set matched 0]
]
]
]
ask managers with [matched = 0]
[move-to one-of tasks with [matched = 0]
fd -1
set matched 1]
ask managers with [matched = 1][
set requirement [requirement] of one-of tasks-on patch-ahead 1
set task_leader [leadership] of one-of tasks-on patch-ahead 1
set difference abs(requirement - ability)
set wealth (wealth + requirement)
ask tasks-on patch-ahead 1 [set matched 1 set shape "arrow" set heading 0]
if difference > tolerance [set color red ask tasks-on patch-ahead 1 [set shape "circle" ]
]
]
ifelse ticks > (num_years ) [
stop] [tick ]
I have had problems with ticks and stop before - there is obviously something I'm not getting.
Per LeirsW
It runs just fine for me. You are using a forever button right?

Hatch not changing the Breed Netlogo

im trying to change the breed of my turtles at hatch but its just giving the mothers breed.
if more code is need please let me know. Thanks!
So, the edit is a more complete code and can be copy pasted. The idea is that if males (machos) find a females (hembra) then they should reproduce, and the new hatch needs to be either male or female. However this is not happenign, theres only females, i chagned the variables in such way that they reproduce alot, meaning that i should see at least one male spawn, but it doesnt.
turtles-own[energia fecundidad vejez]
breed[machos macho]
breed[hembras hembra]
patches-own[alimento]
to setup
resize-world 0 90 0 60
clear-all
ask patches with [
pxcor <= 30 and
pxcor >= min-pxcor and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 65
set alimento random 100
]
set-default-shape turtles "cow"
create-machos 3
[ set size 3 ;; easier to see
set color gray
setxy random xcor random ycor
move-to one-of patches with [pcolor = 65]
set heading random 45 + 45
set energia random 500
set fecundidad 0
set vejez 0
]
set-default-shape turtles "cow"
create-hembras 3
[ set size 3 ;; easier to see
set color black
set xcor random 30
set ycor random 60
move-to one-of patches with [pcolor = 65]
set heading random 45 + 45
set energia random 500
set fecundidad 0
set vejez 0
]
reset-ticks
end
to go
ask machos [
if energia >= 60 [reproducirse]
set heading random 360
fd 1
set energia energia - 1
]
ask machos [set fecundidad fecundidad + 1]
ask machos [set vejez vejez + 1]
ask hembras [
if energia >= 50 [reproduccion_hembra]
set heading random 360
fd 1
set energia energia - 1
]
ask hembras [set fecundidad fecundidad + 1]
ask hembras [set vejez vejez + 1]
tick
end
to reproducirse
ask machos[if any? hembras with [fecundidad >= 6][ set heading towards one-of hembras with [fecundidad >= 6]]
]
end
to reproduccion_hembra
ask hembras [ if energia >= 50 [if any? machos in-radius 4 [ set energia energia - 60
hatch 2 [set breed one-of (list machos hembras) rt random-float 360 fd 15
set energia (100 + random 400)
set vejez 0]]]]
end

In Netlogo, why are the blue patches not overtaking the orange patches? why is it pulsing?

I´ve in through this in previous posts and i got a new problem. Im trying to abandoment a restoration processes. At the beginning orange and bue flow swiftly, but as soon as they interact non win and just change between them over and over. In the code, I stablished that if a patch is orange and it sees at least 3 blues it should change to blue, and i didnt stablish that if a blue patch is surrounded by orange it changes, however it happens. The idea is that at set up I have 3 color, and with some sliders (that would be in - if reforestado >= 3 [if random 100 <= SLIDER]) the patches changes and ultimetly blue (75) and a little bit of green (66) should win showing restauraton
This code can be copy and pasted and it should work, ignore the red patches that appear. Please set vertical limits to the world.
Why isnt the orange loosing agains the blue?
Thanks in advance.
patches-own
[bordear
abandono
reforestado
potrerizado
temperatura
humedad
dosel
]
breed [ potreros potrero ]
breed [ bordes borde ]
breed [ bosques bosque ]
to setup
resize-world 0 90 0 60
clear-all
ask patches with [
pxcor <= 30 and
pxcor >= min-pxcor and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 35
set temperatura 26.5
set dosel 1
set humedad 90.4
]
;Potrero
ask patches with [
pxcor <= 60 and
pxcor >= 30 and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 44
set temperatura 29
set dosel 84.3
set humedad 79.3;
]
;Borde
ask patches with [
pxcor <= 90 and
pxcor >= 60 and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 66
set temperatura 26.3
set dosel 85.2
set humedad 94 ;
]
;Bosque
;se establece la forma de la rana
; Se establecen las condiciones de las ranas, tamaño color y lugar de aparicion. La energia sera igual en todas las ranas en el set up.
create-potreros 50
[ set size 3 ;; easier to see
set color yellow
setxy random xcor random ycor
move-to one-of patches with [pcolor = 35]
set heading random 45 + 45
set energia 50
] ;; red = not carrying food
;Potrero
reset-ticks
end
to go
ask potreros [
if energia < 50 [descansar-potrero]
if energia >= 50 and energia <= 80 [move-potrero]
if energia > 80 [ if ticks mod 50 = 0 [reproducirse]]
set heading random 270
fd 1
set energia energia - 2
morir]
ask patches [ restauracion ]
ask patches [ deforestacion ]
ask patches [abandonacion]
ask patches [borderizacion]
if ticks >= 500 [stop]
tick
end
to restauracion
if pcolor = 44 or pcolor = 25 or pcolor = 35[
set reforestado count neighbors with [pcolor = 66 or pcolor = 75] ]
if reforestado >= 3 [if random 100 <= 100 [
set pcolor 75
set temperatura 24.4 + random 3
set humedad 91 + random 9
set dosel 82 + random 5 ]
]
end
to deforestacion
if pcolor = 44 or pcolor = 25 or pcolor = 75 or pcolor = 66 [
set potrerizado count neighbors with [pcolor = 35] ]
if potrerizado >= 3 [if random 100 <= 0 [
set pcolor 35
set temperatura 22.7 + random 5
set humedad 84.5 + random 10
set dosel 0 + random 10]
]
end
to abandonacion
if pcolor = 35 [
set abandono count neighbors with [pcolor = 44 or pcolor = 25] ]
if abandono >= 3 [if random 100 <= 50 [
set pcolor 25
set temperatura 24.6 + random 8
set humedad 68 + random 14
set dosel 79 + random 9]
]
end
to borderizacion
if pcolor = 66 [
set bordear count neighbors with [pcolor = 44] ]
if bordear >= 3 [if random 100 <= 50 [
set temperatura 24 + random 6
set humedad 80 + random 20
set dosel 80 + random 5]
]
end
to move-potrero
ask neighbors in-radius 2
[if temperatura >= 28.6 or temperatura <= 22.6 or dosel >= 5 or humedad <= 84
[ set pcolor red
;facexy random 30 random 60
;fd 5
;set energia energia - 10]]
;[ask potreros [descansar-potrero]]
]]
end
to descansar-potrero
ifelse pcolor = 35 [
set energia energia + 6]
[set energia energia + 1]
end
to reproducirse
if energia > 80 [ if random 100 > 60 [set energia energia - 70
hatch 1 [ rt random-float 360 fd 2
set energia energia / 3]]]
end
to morir
if energia <= 1 [die]
end ```
Right now, once your patches set abandono to some value in to abandonacion, they never reset it unless their pcolor is returned to 35. So, you have all patches evaluating the if abandono >=3... code. For a quick fix to see what I mean, change your to abandonacion chunk to:
to abandonacion
set abandono 0
if pcolor = 35 [
set abandono count neighbors with [pcolor = 44 or pcolor = 25] ]
if abandono >= 3 [if random 100 <= 50 [
set pcolor 25
set temperatura 24.6 + random 8
set humedad 68 + random 14
set dosel 79 + random 9]
]
end
The above simply resets the abandono to 0 right before evaluating the abandano count to ensure that all patches are starting "fresh." There are other ways to address this, but how you actually go about it may depend on if you are tracking the number of abandono patches every tick.

Netlogo: How to make a turtle check for variables in a radius

I want the turtles to check in a radius of 2 if the temperature of a patch is within the acceptable range that the turtle have, if it is then he would move there, if not then get out of the patch/dont go there. Any guidance will be appreciated and if any more info is needed please let me know.
Ive seen some questiones here but they seem to complex and i dont fully understand.
Thanks.
turtles-own [energia]
;La tierra tiene tres estados, borde abandono y bosque. Cada estado tiene sus valores de temperatura, humedad relativa y dosel
patches-own
[bordear
abandono
reforestado
potrerizado
temperatura
humedad
dosel
]
breed [ potreros potrero ]
to setup
clear-all
;Se segmenta el mundo en potrero, borde y bosque. Se ponen colores para diferenciarlos
ask patches with [
pxcor <= 30 and
pxcor >= min-pxcor and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 35
set temperatura 28
]
create-potreros 50
[ set size 3 ;; easier to see
set color yellow
setxy random xcor random ycor
move-to one-of patches with [pcolor = 35]
set energia 100
]
end
to go
ask potreros [
;if energia < 10 [descansar-potrero]
if energia >= 10 and energia <= 70 [move-potrero]]
end
to move-potrero
ask turtles in-radius 2 [if temperatura >= 27 + random 1 [
lt random-float 360
fd 1]
]
set energia energia - 5
end
´´´
This is most of the code, the turtle arent moving.
Here are the proper steps to debug:
-start with 1 turtle
-in the Gui, create watch/plots for the variables you are debugging. for example, plot the energy of turtle 0 to see where this is going wrong.
-once you have that bug figured out, have a look at the with keyword and the agentset documentation. instead of using the ask keyword, you can create an agentset of patches with sufficient temperature using with and choose one of them with one-of and move there.

NetLogo: Error in avoiding walls and closed gates

I have this code in which turtles are suppose to change direction when they encounter walls and closed gates.
It is okay at the beginning but then it will give this message.
OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.
error while turtle 259 running OF
called by procedure GO
called by Button 'Go'
I can send the whole model.
if state = 1 [fd speed
ifelse [pcolor] of patch-at-heading-and-distance 0 1 = black or [pcolor] of patch-at-heading-and-distance 0 1 = red
[ lt random-float 90]
[fd 1]
; ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
; [ fd 1 ]
if (pxcor >= 89 and pxcor <= 90) and (pycor > 5 and pycor < 10) [facexy (87 + random 3) 25 fd speed]
;if (pxcor >= -10 and pxcor <= 1) and (pycor >= 6 and pycor <= 23 ) [facexy ( 7 + random 3) (26 + random 3) fd speed]
if (pxcor >= 85 and pxcor <= 90) and (pycor = 26) [
let choice random 2
fd speed
if choice = 1 [leftbench]
if choice = 0 [facexy (87 + random 3) 76]
]
if (pxcor >= 83 and pxcor <= 92) and (pycor >= 75 and pycor <= 77) [rightbench fd speed]
if pcolor = brown and not any? other turtles-here
[ move-to patch-here
set seated? true
set pcolor orange
]
]
Does your model have wrapping at the world edges turned off in one or both directions?
If so, then your code has to handle the possibility that sometimes this patch:
patch-at-heading-and-distance 0 1
won't exist, if the turtle is near a world edge. If that happens, then patch-at-heading-at-distance will return nobody, and when you try and do:
[pcolor] of patch-at-heading-and-distance 0 1 = black
you get the error, because no pcolor can be retrieved from a nonexistent patch.
The typical way to code around this is to do something like:
let target patch-at-heading-and-distance 0 1
if is-patch? target and ...
Finally, I wonder whether you really mean patch-at-heading-and-distance 0 1. It's equivalent to patch-at 0 1, which refers to the patch north of the turtle; not sure why you'd want that. If you want to test the patch ahead of the turtle (the patch the turtle will land in if it uses fd), use patch-ahead.