Netlogo code problems, day/night cycle not working - netlogo

I'm trying to crate a complex environment, where three types of trees co-exits and you can modify the numbers of each type using a slider. What I'm having problems implementing is a "day/night" cycle. When it is night the colors should turn darker, but with the code I have the colors get darker and never return to a brighter color. I used as a base the Algae model.
Here is my code:
to setup
clear-all
setup-world
reset-ticks
end
to setup-world
ask n-of synchronic-tree-density patches [
set pcolor blue
]
ask n-of asynchronious-tree-density patches [
set pcolor yellow
]
ask n-of tree-patches patches [
set pcolor green
]
recolor-world true
end
to recolor-world
ask patches [
if pcolor = blue [
ifelse setting-up? or daytime? [
set pcolor blue
] [
set pcolor blue - 3
]
]
if pcolor = yellow [
ifelse setting-up? or daytime? [
set pcolor yellow
] [
set pcolor yellow - 3
]
]
if pcolor = green [
ifelse setting-up? or daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
]
end
to go
recolor-world false
tick-advance 1
end
to-report daytime?
report ticks mod 24 < day-length
end

Luke's answer addresses your issue. But you might additionally want to simplify the code somewhat to only assess the state of daytime? once each tick. For example:
to recolor-world
if-else daytime? [
ask patches with [ tree-type = 1 ] [ set pcolor blue ]
ask patches with [ tree-type = 2 ] [ set pcolor yellow ]
ask patches with [ tree-type = 3 ] [ set pcolor green]
] [
ask patches with [ tree-type = 1 ] [ set pcolor blue - 3 ]
ask patches with [ tree-type = 2 ] [ set pcolor yellow - 3 ]
ask patches with [ tree-type = 3 ] [ set pcolor green - 3 ]
]
end

welcome to Stack Overflow. Please have a look at the MCVE guidelines for some asking tips. Ideally your question code is pared down to just what is necessary for other users to run your program- the goal being that they can just copy your code as is. Right now, I can't run your program without a fair bit of modification- I'm not sure if my solution will apply to your setup. You are more likely to get useful answers if you simplify your code.
That said, I'm pretty sure your issue comes from that fact that all your if statements in your recolor-world procedure. Consider this one:
if pcolor = green [
ifelse daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
So on the first go-around, you do have some green patches since they were set that way in the setup procedure. However, once daytime? becomes false, those patches run the command set pcolor green - 3 and so they no longer evaluate if pcolor = green as true- they will never run that code block again. I think the easiest fix is to use a grouping variable other than color for filtering:
patches-own [ tree-type ]
to setup
clear-all
reset-ticks
setup-world
end
to setup-world
ask n-of 50 patches [
set pcolor blue
set tree-type 1
]
ask n-of 50 patches [
set pcolor yellow
set tree-type 2
]
ask n-of 50 patches [
set pcolor green
set tree-type 3
]
end
to recolor-world
ask patches with [ tree-type = 1 ] [
ifelse daytime? [
set pcolor blue
] [
set pcolor blue - 3
]
]
ask patches with [ tree-type = 2 ] [
ifelse daytime? [
set pcolor yellow
] [
set pcolor yellow - 3
]
]
ask patches with [ tree-type = 3 ] [
ifelse daytime? [
set pcolor green
] [
set pcolor green - 3
]
]
end
to go
recolor-world
tick
end
to-report daytime?
report ticks mod 24 < 12
end
Edit
See #JenB's further improvement in her answer for a more efficient implementation.

Related

Is there a way to create impassable barriers in NetLogo?

I am attempting to code a path-finding behavior wherein agents will locate an optimal patch in the environment and navigate their way around fences to reach said patch. I've created a patch variable 'f', which is set to 1 to indicate fences and 0 for any other patch.
I want to make these fences impassable (i.e. I want them to be patches the agents will not use for movement), but agents still seem to be able to travel on them to some extent and in some cases are even able to fully cross them.
Here is a picture of an agent crossing a barrier I don't want it to cross
Relevant decision-making code for the agents is as follows:
{let moveset patches in-radius 30 with [f = 0 and n > 0]
let target max-one-of moveset [n]
ifelse patch-here != target
[
set heading towards target
]
[]
let chance random-float 10
if chance >= 5 [let pick -145]
if chance < 5 [let pick 145]
ask patches in-radius 1
[if f = 1
[ask myself
[set heading towards min-one-of patches [distance myself] + 180 - random 10 + random 10 ]
]
]
fd 1}
For clarity, 'n' is simply a variable to denote the patch I want my agent to locate and venture to.
Is anyone aware of a simple way in NetLogo to exclude certain patches as viable zones for movement in the decision making process (i.e. hard barriers)?
If you haven't yet, have a look at the "Look Ahead" example in the Models Library- it's a simple demonstration of using patch color to control turtle movement. Some code based on that model is below. With this setup:
breed [ seekers seeker ]
breed [ goals goal ]
patches-own [ steps-from-goal ]
to setup
ca
ask patches [
set steps-from-goal 999
]
ask patches with [ pxcor mod 10 = 0 ] [
set pcolor red
]
ask patches with [ pycor mod 10 = 0 ] [
set pcolor black
]
ask one-of patches with [ pcolor = black ] [
sprout-seekers 1 [
set color blue
pd
]
]
ask one-of patches with [ pcolor = black ] [
sprout-goals 1 [
set color white
set shape "circle"
]
]
reset-ticks
end
You can have the seekers breed wander around the black squares until they share a patch with a goal turtle:
to random-wander
ask seekers [
if any? goals-here [
stop
]
rt random 61 - 30
ifelse can-move? 1 and [pcolor] of patch-ahead 1 = black [
fd 1
] [
rt one-of [ 90 -90 ]
]
]
tick
end
However, note that turtles can still 'jump' corners of patches using this method, because they are able to assess the patch-ahead 1 at any angle- so, a patch one space ahead of a turtle may be assessed across the corner of another patch. The turtle should never actually land on the forbidden patch, but you may notice that their path can cross those blocked patches.
Edit:
See simplified code that "traps" a turtle in a square cage:
to setup
ca
crt 1 [
setxy 5 5
set heading 180
repeat 4 [
repeat 10 [
ask patch-here [ set pcolor red ]
fd 1
]
rt 90
]
die
]
crt 1 [ pd ]
reset-ticks
end
to go
ask turtles [
rt random 61 - 30
ifelse can-move? 1 and [pcolor] of patch-ahead 1 = black [
fd 1
] [
rt one-of [ 90 -90 ]
]
]
tick
end
After 1100 ticks:
After 13300 ticks:

How to use to in radius command for turtles to seek food and how to regenerate food over time in Netlogo?

I am trying to do the following in NetLogo:
Get turtles (Elephants) to seek food
Ask plants to reproduce slowly over time on each side, one side before the other
Keep turtles (Elephants) to stay within world boundary
Keep turtles (Elephants) upright
What I would basically like to do is have our turtles (elephants) eat food on one side and seek food to cross to the other side. They will die if they get hit by a car. We want them to cross back and forth between sides so that they all die over time. We have tried to use the seek food primitive but is does not work for our simulation. We have also had the turtles stay within the world using the bounce primitive but with this current code they tend to move everywhere once again. As for the food regeneration, we have tried to use the hatch function but that also does not work.
Your help is very much appreciated.
Here is our code for the simulation:
breed [ elephants elephant ]
breed [ cars car ]
breed [ plants plant ]
turtles-own [
speed
speed-limit
speed-min
]
to setup
clear-all
setup-patches
setup-elephants
setup-cars
setup-plants
reset-ticks
end
to setup-patches
ask patches [
ifelse (pycor > -2) and (pycor < 2)
[ set pcolor black ]
[ set pcolor green ]
]
end
to setup-elephants
ask n-of number-of-elephants (patches with [ pycor < -4 ])
[ sprout-elephants 1
[ set shape "elephant"
set color 4
set size 4
]
]
end
to setup-cars
ask n-of number-of-cars (patches with [ pcolor = black ])
[ sprout-cars 1
[ set shape "car"
set color 105
set size 2
set heading 90
]
]
end
to setup-plants
ask n-of number-of-plants (patches with [ pcolor = green ])
[ sprout-plants 1
[ set shape "plant"
set color 62
set size 1
]
]
end
to go
ask elephants [
bounce forward 1
]
ask cars [
set xcor random-xcor
set heading 90
forward 1
move-elephants
move-cars
eat-plants
kill-elephants
]
end
to bounce
if abs pxcor = max-pxcor
[ set heading ( - heading ) ]
if abs pycor = max-pycor
[ set heading ( 180 - heading ) ]
end
to move-elephants
ask elephants [
right random 360
forward 1
]
end
to move-cars
set speed 0.1
set speed-limit 0.1
end
to eat-plants
ask elephants
[ let prey one-of plants-here
if prey != nobody [ask prey [die]]
]
end
to kill-elephants
ask cars
[ let prey one-of elephants-here
if prey != nobody [ask prey [die]]
]
end
There are several problems with this code so I am going to try and get rid of the more obvious logical issues and see if that allows you to focus on a specific question. Note that you should really be building your code more gradually - add one behaviour (eg move elephants, move cars, eat food or whatever) and make sure it works before adding the next behaviour.
Your go procedure doesn't have a tick for time passage
Your go procedure has each car randomly move all the elephants, so they are moving multiple times
Your car speeds and speed limits are being set to the same value each tick and never changed
You have nested ask cars [ ask elephants [ <do stuff> ] ] for eating plants and killing elephants, which will make these happen many times each tick
Fixing just those problems, gets this (note that I replaced slider inputs with numbers so you will have to change them back). This should fix the things you mentioned in your comments. You will have to ask a specific question about whatever else it is you are trying to fix.
breed [ elephants elephant ]
breed [ cars car ]
breed [ plants plant ]
turtles-own
[ speed
speed-limit
speed-min
]
to setup
clear-all
setup-patches
setup-elephants
setup-cars
setup-plants
reset-ticks
end
to go
ask elephants
[ bounce
forward 1
]
ask cars [ forward 1 ]
move-elephants
eat-plants
kill-elephants
tick
end
to bounce
if abs pxcor = max-pxcor
[ set heading ( - heading ) ]
if abs pycor = max-pycor
[ set heading ( 180 - heading ) ]
end
to move-elephants
ask elephants
[ right random 360
forward 1
]
end
to eat-plants
ask elephants
[ let prey one-of plants-here
if prey != nobody [ask prey [die]]
]
end
to kill-elephants
ask cars
[ let prey one-of elephants-here
if prey != nobody [ask prey [die]]
]
end
to setup-patches
ask patches [
ifelse (pycor > -2) and (pycor < 2)
[ set pcolor black ]
[ set pcolor green ]
]
end
to setup-elephants
ask n-of 20 (patches with [ pycor < -4 ])
[ sprout-elephants 1
[ set shape "wolf"
set color 4
set size 4
]
]
end
to setup-cars
ask n-of 20 (patches with [ pcolor = black ])
[ sprout-cars 1
[ set shape "car"
set color 105
set size 2
set heading 90
set speed 0.1
set speed-limit 0.1
]
]
end
to setup-plants
ask n-of 50 (patches with [ pcolor = green ])
[ sprout-plants 1
[ set shape "plant"
set color 62
set size 1
]
]
end

NETLOGO: Count patches outside of certain radius

I want to count patches of a specific color starting at a patch radius end and outward. There is the in-radius command, but it considers the space between a patch center and its radius, but I want this space to be excluded and start counting where the radius ends. Any help is appreciated.
Javier
How about a reporter that uses member? to return only a patch-set consisting only of patches that are not in-radius?
to setup
ca
ask n-of 10 patches [ set pcolor white ]
crt 1 [
setxy random-pxcor random-pycor
set color red
set size 2
]
reset-ticks
end
to go
ask turtles [
ask patches in-radius 5 with [ pcolor = black ] [ set pcolor black + 2 ]
print count ( patches-outside-radius 8 ) with [ pcolor = white ]
]
end
to-report patches-outside-radius [ radius ]
let inrad patches in-radius radius
report patches with [ not member? self inrad ]
end

Initial proportion of three different patches with only two sliders

i am trying to create a predation model in netlogo with foxes and rabbits. foxes eat rabbits and waste (left from people) und rabbits eat grass in a special pleasure ground.
the initial proportion of patches fixed in the setup should be: 65% green patches (grass), 35 % brown patches (eaten grass), 10 % margenta patches (waste from people). they can be distributed randomly.
at the moment it looks like that :
globals [grass waste]
breed [rabbits rabbit]
breed [foxes fox]
turtles-own [energy]
patches-own [countdown]
to setup
clear-all
ask patches [ set pcolor green ]
if grass? [
ask patches [
set pcolor one-of [green brown]
if-else pcolor = green
[ set countdown grass-regrowth-time ]
[ set countdown random grass-regrowth-time ]
]
]
ask patches [ set pcolor magenta ]
if waste? [
ask patches [
set pcolor one-of [ green brown magenta ]
if-else pcolor = [ green brown ]
[ set countdown waste-regeneration-time ]
[ set countdown random waste-regeneration-time ]
]
]
set-default-shape rabbits "rabbit"
create-rabbits initial-number-rabbits
[
set color white
set size 2.2
set label-color gray + 1
set energy random ( 2 * rabbit-gain-from-food)
setxy random-xcor random-ycor
]
set-default-shape foxes "fox"
create-foxes initial-number-foxes
[
set color red
set label-color gray + 1
set size 2.2
set energy random ( fox-gain-from-food )
setxy random-xcor random-ycor
]
display-labels
set grass count patches with [pcolor = green]
set waste count patches with [pcolor = magenta]
reset-ticks
end

NetLogo, How can I make the agent move is a specific area and then colour the area while it moving without stucking?

the problem
In this code I try to make the agent to move in the yellow area which is = 47, and to colour the patches to be "red", but the problem is that the agent stuck in the red coloured area.
I want the agent to move and to colour until all the patches become red, it is like cleaning.
Please guys, I need help please....
;;leftcheck and rightcheck has been declared in globals
if (behaviour = "RCleaner")
[
ifelse any? patches in-cone 2 10 with [pcolor = 47]
[ fd random-float 0.03 ]
[
rt 2
ifelse any? patches in-cone 2 10 with [pcolor = 47]
[ set rightcheck true ] [ set rightcheck false ]
lt 2
ifelse any? patches in-cone 2 10 with [pcolor = 47]
[ set leftcheck true ] [ set leftcheck false ]
rt 2
if leftcheck [ lt 1 ]
if rightcheck [ rt 1 ]
fd 0.01
]
set heading heading + random 1 - random 1
ask patch-here [ set pcolor red]
]