Farmer require to change value after discrete time/ ticks in NetLogo - time

I am writing a model where farmers update profits seasonally and cultivate crops accordingly. In total there will 630 ticks in a year's time period.There are two seasons. First season will take 252 ticks to complete a crop cycle. And other season will take next 378 ticks to complete a crop cycle. For both seasons farmer will calculate her profits. Initially after 252 ticks and then reset some of the variables to start for next season and calculate profits for next 378 ticks. This will go on simultaneously and run for the period of time let's say 10 years or more. Is there anyone who can help in writing the codes.
Below codes are given.
Problem: Ticks mod command will overcalculate or under calculate profits for every season. I want to calculate profits for season 1 from 0 to 252 ticks for season2 from 253 ticks to 630. and again for season1 from 631 to 882 and again for sesson2 till 1260 ticks. adn this will continue.
breed [farmers farmer]
farmers-own [water irrigation-turn]
to setup
clear-all
create-farmers 5
[ set label who
set size 2
]
ask farmer 0 [ setxy min-pxcor + 1 0 set irrigation-turn 0]
ask farmer 1 [ setxy min-pxcor + 1 2 set irrigation-turn 1]
ask farmer 2 [ setxy min-pxcor + 1 4 set irrigation-turn 2]
ask farmer 3 [ setxy min-pxcor + 3 0 set irrigation-turn 3]
ask farmer 4[ setxy min-pxcor + 3 2 set irrigation-turn 4]
ask farmers [ set label who
set size 2]
reset-ticks
end
to go
irrigate
update-profits
tick
end
to irrigate
ask turtles with [irrigation-turn = (ticks mod count farmers)]
[ set color red]
ask turtles with [ irrigation-turn != (ticks mod count farmers)]
[set color blue]
end
to update-profits
if ticks mod 630 = 252 [set-season1profit]
if ticks mod 630 = 378 [ set-season2profits]
end

Related

I can't filter my list of a patch-variable without an error 'expected a literal value'

I'm making a model of hoverflies in which adult hoverflies lay eggs in patches, which hatch after a number of days into larvae. These larvae will turn into pupae when their weight has grown from hatching weight to 28 mg, according to a type 2 functional response to how much they have eaten.
I set up the patches as follows:
set peggs n-values 4 [0] ;;number of eggs in a patch. every timestep a number of eggs is added and they hatch after 4 days.
set nl_per_age n-values 20 [0];; number of larvae per age.
set l_hatched n-values 20 [0.0001];; weight of hatched larvae
set l_weight_age n-values 20 [0];; weight of hatched larvae + their individual growth per age
set ppupae n-values 30 [0] ; number of pupae
set larvae 0 ;; larvae biomass
Now, in 'to time' (so this code happens every tick) I coded the following:
set nl_per_age fput item dtl peggs nl_per_age ;;dtl is 3, so the number of eggs in peggs item 3 are added to the number of larvae
set nl_per_age but-last nl_per_age; last item is deleted which means that if the larvae haven't grown to 28 mg in 20 days they die
set l_hatched fput ((item dtl peggs) * weight_hatch) l_hatched ;; same as nl_per_age but here the items are multiplied by the hatching weight (0.063)
set l_hatched but-last l_hatched
set l_weight_age map [ larvae-growth ->
(larvae-growth * 100 * e ^ (g * (aphids)*(1 - (larvae-growth / weight_max))))
] l_hatched ;; this follows a equation from the literature. But what it basically does it that it lets the larvae grow in weight according to the aphid density (prey) in their patch
So far so good, but here's where the trouble begins:
let larvae_max filter [weight -> weight = 28] [l_weight_age] ;;this filter function should filter out the larvae which weigh 28 mg. BUT: [l_weight_age] gives error 'expected a literal value'. However, when I make up a list like [24 6 21 53 28 28 23 28] this line does work and it counts 3 larvae.
let number_to_pup length larvae_max ;this counts the number of 28 mg larvae
set ppupae fput number_to_pup ppupae ; the number of larvae of 28 mg are added to ppupae
sprout item dta ppupae ; after 20 days (dta=20) the pupae are sprouted into new turtles.
Now the last block of code gives me an error saying 'expected a literal value' at [ l_weight_age]. But I don't understand what I'm doing wrong, can anyone help me?

Releasing two turtles per tick netlogo

I'm currently working on a model where I could simulated pedestrian movement while shopping. So, I have already figure some of things I need but I have been trying to figure out how to release 2 turtles per tick (like a pair) in a certain patch. Both turtles are release at the same time. My code is base from ant lines but it just releases all the num-of-pedestrians in one time and then the turtles start walking. the ticks also start after all turtles are release. I want the turtles to start "walking" as they enter.
Here is my code:
breed [ leadvisitors leadvisitor ]
breed [ visitors visitor ]
to setup
clear-all
setup-visitors
reset-ticks
end
to setup-visitors
create-leadvisitors num-of-pedestrians * 0.1 ;;create 10% of the total number of pedestrians
[
set demand-type "none"
set color black
set size 1
setxy 0 16
set heading 180
set pen-size 1
set destination one-of patches
set wait-time -1
set demand-lvl 0
]
create-visitors (num-of-pedestrians - (num-of-pedestrians * 0.1))
[ set demand-type 0
set size 1
setxy 0 16
set heading 180
set pen-size 1
set destination one-of patches
set wait-time -1
set demand-lvl 1
set attracted? false
] ]
end
to go
if turtles = 0 [ stop ]
ask turtles
[
set-demand-type
have-demand
]
if ticks > 100 [ stop ]
tick
display-labels
end
;;;;; visitor's internal state of demand ;;;;;
to set-demand-type
if demand-type = 0
[ set demand-type "food"
set color red
let target (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = red]
]
end
to have-demand
if demand-lvl = 0
[
ifelse wait-time = -1
[ stroll ]
[ set wait-time wait-time - 1
if wait-time = 0
[ stroll ]]
]
if demand-lvl = 1
[ ifelse wait-time = -1
[ stroll
evaluate ]
[ set wait-time wait-time - 1
if wait-time = 0
[ stroll
evaluate ]]
]
end
to stroll
if any? neighbors with [ pcolor = gray - 3 ]
[ die ]
ifelse any? neighbors with [ pcolor = gray or pcolor = orange or pcolor = blue or pcolor = red]
[ facexy exitpt-x exitpt-y ]
[ rt random-float visitor-view-angle lt random-float visitor-view-angle ]
fd walking-speed
end
to evaluate
if any? neighbors with [ pcolor = gray or pcolor = orange or pcolor = blue or pcolor = gray - 4 or pcolor = gray - 3 or pcolor = red + 2]
[ facexy exitpt-x exitpt-y
rt random-float visitor-view-angle lt random-float visitor-view-angle
fd walking-speed ]
let _mycolor color
if any? (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = _mycolor]
[ let new-target max-one-of ( patches with [pcolor = _mycolor] in-cone visitor-vision-depth visitor-view-angle) [patch-influence]
let dist-to-new-target min-one-of (patches with [pcolor = _mycolor] in-cone visitor-vision-depth visitor-view-angle) [distance myself]
face new-target
set heading towards new-target
fd walking-speed
set attracted? true
attracted-and-visiting
re-evaluate
]
end
to attracted-and-visiting
if pcolor = red + 2
[ set heading towards one-of patches with [pcolor = red + 2]
fd 0
set patch-popularity patch-popularity + 1
set wait-time avg-waiting-time
if count turtles-here > 0
[ set num-of-visitors num-of-visitors + 1 ]
set plabel num-of-visitors
]
end
to re-evaluate
let _mycolor color
if not any? (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = _mycolor]
[ set attracted? false ]
ifelse choose? [set heading towards one-of patches with [pcolor = _mycolor]] [facexy exitpt-x exitpt-y]
end
to-report choose?
report random 2 = 0
end
As a result of our back-and-forth comments, let me take a stab at what you are looking for. I assume that you want to create leadvisitors at the beginning of the run and then add two visitors per tick as the run progresses. If so, then your setup procedure would look something like
globals [num-visitors-created]
to setup
clear-all
set num-visitors-created 0
create-leadvisitors num-of-pedestrians * 0.1 [
set demand-type "none"
set color black
set size 1
setxy 0 16
set heading 180
set pen-size 1
set destination one-of patches
set wait-time -1
set demand-lvl 0
set num-visitors-created num-visitors-created + 1
]
reset-ticks
end
creating your leadvisitors, but no visitors. Note that the global variable num-visitors-created keeps track of the number of leadvisitors and visitors created, being incremented by one each time a leadvisitor or visitor is created.
In your go procedure, you would then create two visitors each tick until the total number of visitors and leadvisitors created reaches num-of-pedestrians. (If there is room for only one new visitor, would you want just one to be created, or do they need to be created in pairs? I'm assuming the latter.) Since you don't want the death of a visitor to open up space for a new one, we test the number created, not the number still alive.
to go
if turtles = 0 [ stop ]
; create a new pair of visitors if there is room.
if num-visitors-created <= (num-of-pedestrians - 2)
create-visitors 2 [
set demand-type 0
set size 1
set heading 180
set pen-size 1
setxy 0 16
set destination one-of patches
set wait-time -1
set demand-lvl 1
set attracted? false
set num-visitors-created num-visitors-created + 1
]
ask turtles
[
set-demand-type
have-demand
]
if ticks > 100 [ stop ]
tick
display-labels
end
(Since it appears that leadvisitors may die as well, do you want to keep a certain minimum number or proportion of leadvisitors? If so, you should open up a new question.)
The code above will create the pair of new visitors on patch 0 16 at each tick. If, however, you want to have the new visitors created on a different patch, say one of the red ones, you could in your go procedure have that patch sprout the new visitors.
ask one-of patches with [pcolor = red] [
sprout-visitors 2 [
set demand-type 0
set size 1
set heading 180
set pen-size 1
set destination one-of patches
set wait-time -1
set demand-lvl 1
set attracted? false
set num-visitors-created num-visitors-created + 1
]
]
Note that here the xy coordinates of the new visitors are not set, so they start on the patch that sprouted them.

how do I place my turtles within a square of 5 x 5 patches and 10 x 10 patches?

I am trying to randomly place my turtles within a square of 5 by 5 patches, I have 2 questions as below:
Is below code correct?
setxy (50 + random 5) (60 + random 5)
How do I make a 10 x 10 patch square?
Your code would place the turtles running it in a 5 X 5 square centered on patches with the bottom-left corner on patch 50 60.
If you want it to be 10 x 10
setxy (50 + random 10) (60 + random 10)
if you want them not to be have to centered on patches use random-float
thus. The patch center coordinates are integers.
setxy (50 + random-float 5) (60 + random-float 5)
If your world is not big enough they will wrap around.

setting variable in netlogo ifelse function and making turtle to wait in the patch

I have the following in netlogo
ask m-depts [ ; this check the values of turtles against some set numbers
ifelse
(m- k >= 5) and (m-t >= 3) and (m-c >= 3 ) and (m-s >= 4) and (m-b >= 3) and (m-d >= 0) [
move-to one-of patches with [pcolor = yellow]
] [
ifelse
(m-k >= 5) and (m-t >= 5) and (m-c >= 5 ) and (m-s >= 5) and (m-b >= 3) and (m-d >= 1) [
move-to one-of patches with [pcolor = green]
] [
action-m-depts
] ]]]
]
1st I want to add some conditions (set ……… ) if the turtle will move to yellow and also some other condition if it will move to green eg
If pcolor = yellow [
set m-k m-k + 0.5
set m-t m-t + 1
] ;
If pcolor = green [
set m-k m-k + 0.8
set m-t m-t + 2
] ; etc otherwise do action-m-depts (defined elsewhere)
2nd For the next move (ticks) I want to fix the turtle to wait at the patch eg (for yellow patch, wait for 5 years (5 ticks) and for green patch wait for 2 year (2 ticks). How should I incorporate the two issues in this model?
The code you have for the first part looks good to me. Have you tried it? I say go for it!
The second part is very similar to netlogo: how to make turtles stop for a set number of ticks then continue and Making turtles wait x number of ticks — check out those answers.

Scheduling random subset of agents to run task in NetLogo

I have been developing a procedure for 'human' agents to move south into a
forest from hours 0600 to 1200 and then leaving the forest from 1200 to 1800.
Currently all of the human agents start walking in at 0600. This creates a wave
effect, but instead I'd like a continuous stream of humans. One option I
thought of was to have a random subset of them begin moving at 0600, another
subset begin at 0700, etc, until 1200 when all of them turn around and go back
out. Alternatively, it may be nice to generate a new set of 'humans' at each of
the morning times (i.e., 0600,0700,0800,0900,1000,1100) to move in, instead of
selecting a random subset. I've been stuck on this for awhile and any help
would be much appreciated. I have the code below.
--Neil
to setup
ca
clear-all-plots
clear-output
set typeAgro 1 ;this is Agricultural land outside forest
set typeTrop 2 ;this is tropical forest
ask patches
[ set habitat typeAgro ]
ask patches with [pycor <= 300] ;all patches south of y-coord 300 is tropical
forest
[ set habitat typeTrop]
create-humans number-humans ;on a slider from 100 to 200
[ setxy random-xcor 310 ; start humans in agricultural land just north of
tropical forest
if any? turtles-on patch-here
[ setxy random-xcor 310 ]
set morning 6
set midday 12
set afternoon 18
set midnight 24
set step-size 50
set color white
set size 10
set shape "person" ]
reset-ticks
end
to go
tick
ask humans [move-humans] ; humans moving into the forest
end
to move-humans
let hour ticks mod midnight ; sets the ticks on 24 hour day
if morning <= hour and hour < midday [ ; from 0600 to 1200 people move into
forest
set heading (random-normal 180 30)
fd random-normal step-size 4
]
if midday <= hour and ycor < 310 [ ; from 1200 to 1800 people move out of the
forest
set heading (random-normal 360 30)
fd random-normal step-size 1
]
end

Resources