Link of the oldest agent dies after first tick - netlogo

I am modelling vaccinations at general practices. After patients made an appointment with the assistants, the doctor will call them up if they are available. After one tick, the oldest patient shows a link (appointment) with its doctor. But after the next tick, the link disappears. This is only the case for the oldest patient. Can someone help me out?
The code is as follows:
breed [patients patient]
breed [doctors doctor]
breed [assistants assistant]
undirected-link-breed [appointments appointment]
globals [
doctorslist
assistantslist
patientslist
patients-own [
age
ages
vulnerability
confidence ;in safety of vaccine
immune?
residence
appointment?
movementspeed
willing-to-vaccinate
my-assistant
my-doctor
omw?
homepatch
]
doctors-own [
vaccine-inventory
openingtime
vaccinations-on-planning-this-day
vaccinations-remaining-this-day
vaccinations-done-today
available?
]
assistants-own [
openingtime
occupied?
activityTime
my-doctor
]
calls-own [
duration
]
to setup
clear-all
setup-doctors
setup-assistants
setup-patients
setup-transporters
setup-packers
set central-vaccine-stock initial-central-vaccine-stock
initTime
ask patches [
ifelse pxcor = -16 and pycor = 0 [set pcolor orange][ set pcolor white]]
reset-ticks
end
to setup-doctors
create-doctors number-doctors [
let doctorlist (list doctor 0 doctor 1 doctor 2 doctor 3 doctor 4 doctor 5 doctor 6 doctor 7 doctor 8 doctor 9)
set doctorslist doctorlist
set shape "house"
if item 0 doctorlist != nobody [ ask item 0 doctorlist [setxy 2 0 set doctorlist remove-item 0 doctorlist]]
if item 0 doctorlist != nobody [ ask item 0 doctorlist [setxy 10 10 set doctorlist remove-item 0 doctorlist]]
if item 0 doctorlist != nobody [ ask item 0 doctorlist [setxy -10 -10 set doctorlist remove-item 0 doctorlist]]
if item 0 doctorlist != nobody [ ask item 0 doctorlist [setxy -5 5 set doctorlist remove-item 0 doctorlist]]
if item 0 doctorlist != nobody [ ask item 0 doctorlist [setxy 11 -7 set doctorlist remove-item 0 doctorlist]]
repeat (count doctors - 5) [if item 0 doctorlist != nobody [ ask item 0 doctorlist [setxy random-xcor random-ycor set doctorlist remove-item 0 doctorlist]]]
set color green
set vaccine-inventory 0
set openingtime true
set vaccinations-on-planning-this-day 0
set vaccinations-done-today 0
set available? true
]
end
end
to setup-patients
create-patients number-patients [
set shape "person"
setxy random-xcor random-ycor
let xy patch-here
set homepatch xy
set color black
set age 1 + random 99
set ages age
set immune? false
set appointment? false
set movementspeed 0
set omw? false
]
set patientslist sort-on [(- age)] patients
end
to setup-assistants
create-assistants (number-doctors * number-assistants-per-doctor)[
let assistantlist (list assistant count doctors assistant (count doctors + 1) assistant (count doctors + 2)
assistant (count doctors + 3) assistant (count doctors + 4)
assistant (count doctors + 5) assistant (count doctors + 6)
assistant (count doctors + 7) assistant (count doctors + 8)
assistant (count doctors + 9) assistant (count doctors + 10)
assistant (count doctors + 11) assistant (count doctors + 12)
assistant (count doctors + 13) assistant (count doctors + 14)
assistant (count doctors + 15) assistant (count doctors + 16)
assistant (count doctors + 17) assistant (count doctors + 18)
assistant (count doctors + 19) assistant (count doctors + 20))
let doctorlist doctorslist
set assistantslist assistantlist
set shape "person"
repeat (count doctors)[
if item 0 assistantlist != nobody [
repeat number-assistants-per-doctor [
ask item 0 assistantlist [ setxy [xcor] of item 0 doctorlist [ycor] of item 0 doctorlist
set assistantlist remove-item 0 assistantlist ]]
set doctorlist remove-item 0 doctorlist]]
set color red
set openingtime true
set calling-speed calling-speed
set occupied? false
set my-doctor one-of doctors-here
]
end
to go
repeat (count assistants + 1) [make-appointment]
go-to-doctor
order-vaccines
hang-up-phone
call-up-patients
vaccinate
;transport-vaccines
updateTime
tick
end
to make-appointment
ask max-one-of patients [ages] [
if appointment? = false [
let free-assistants assistants with [ occupied? = false]
set my-assistant min-one-of free-assistants [distance myself]
if my-assistant != nobody [set my-doctor [my-doctor] of my-assistant]
if my-assistant != nobody [
create-call-with my-assistant [hide-link]
set appointment? true
set color pink
set ages 0
]]]
ask assistants [
ifelse count my-calls = 1 [
set occupied? true][
set occupied? false]
]
end
to call-up-patients
let patientlist patientslist
ask doctors [
if CurHour = 0 and CurMinute = 0 [set vaccinations-on-planning-this-day 0
set vaccinations-done-today 0
ask appointments [die] ]
if (vaccinations-on-planning-this-day + vaccinations-done-today) < injection-capacity [ set available? true]
]
if length patientlist != 0 [ ask item 0 patientlist [
ifelse appointment? = true and [available?] of my-doctor = true
[
create-appointment-with my-doctor set patientslist remove-item 0 patientlist ][ask item 1 patientlist [
ifelse appointment? = true and [available?] of my-doctor = true
[
create-appointment-with my-doctor set patientslist remove-item 1 patientlist ][ask item 2 patientlist [
if appointment? = true and [available?] of my-doctor = true
[
create-appointment-with my-doctor set patientslist remove-item 2 patientlist ] ]]]]]]; hier zit een fout. Als ik set patientslist verander in set patientlist maakt alleen de oudste agent een afspraak.
;patientlist is niet hetzelfde als patientSlist. soms maken patienten een appointment met een andere doctor dan my-doctor. appointment van de oudste patient gaat aan uit.
ask doctors [if (count my-appointments + vaccinations-done-today = injection-capacity) [
set available? false]
set vaccinations-on-planning-this-day count my-appointments]
ask patients [if count my-appointments = 1 [
set omw? true]]
end
to go-to-doctor
ask patients [
if omw? = true [
if my-doctor != nobody [ face my-doctor let remaining-distance distance my-doctor
ifelse remaining-distance >= 1 [fd 1][fd remaining-distance ]]]]
end
I included the relevant parts of the code. There is not asked to the oldest patient to die its appointment, but it does. Does someone have a solution for this?
One more question: in the call-up patients procedure the command for removing the first item of the patientlist is "set patientSlist", while patientlist (without an S) is the local list of patientSlist. If i remove the S, the command does not work anymore. Does someone know how i can fix this?

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?

Netlogo. Update an history list when turtles birth and

I will be very grateful if someone could give me any kind of advice about a problem that I have in my code. I am working on a model with four kind of agents. Each type of agents uses a different strategy. These strategies are Prisoner Dilemma variations. Two of these strategies (tit for tat and unforgiving) need to record (in a list) who was the partner of interaction, and depending if that partner defected or not, will be the behave (cooperative or defect) of the agent in the next encounter with the same partner. Agents move randomly through the world. The problem come when agents die or birth according of their resources level. At this point these strategies that require to record who was the previous partner and how was his behave (cooperative or defect) do not work. This is the message that show up.
I am really appreciate any support!!!!.
number of initial turtles is 42, but two ticks later the number is 52
Below is a code that reproduces the error. In order to run it, it is necessary to create the sliders: n-tit-for-tat-com; n-unforgiving-com; inc-ecological-resources; energy-consume-companies:
globals
[
;;Number of companies with each strategy¨
num-tit-for-tat-com
num-unforgiving-com
;;number of interactions by each strategy
num-tit-for-tat-com-games
num-unforgiving-com-games
;; Total score of all companies playing each strategy
tit-for-tat-com-score
unforgiving-com-score
; Total companies en each ticks
num-companies
]
breed [companies company]
;;create companies variables
companies-own [
score-com
score-com-round
strategy-com
defect-now?-com
partner-defected?-com ;;action of the partner
partnered?-com ;;am I partnered?
partner-com ;;WHO of my partner (nobody if not partnered)
partner-history-com ;;a list containing information about past interactions with other turtles (indexed by WHO values)
earnings-tit-for-tat-Com
earnings-unforgiving-Com
earnings-com
tit-for-tat-com-score-round
unforgiving-com-score-round
]
patches-own
[
resources-amount ;; resources amount in each patches
resources-to ;; aux variable to count resources taken
]
;;;Setup Procedures;;;
to setup
clear-all
setup-patches
setup-companies ;;setup the turtles and distribute them randomly
reset-ticks
end
; Setup patches
;===============
to setup-patches
ask patches [set resources-amount random 4] ;; patches represent ecological system. Resources are distributed randomly. [0,1,2 or 3] 0 -> non resources; 3 -> plenty of resources
ask patches [
set pcolor (ifelse-value
resources-amount = 0 [49]
resources-amount = 1 [58]
resources-amount = 2 [67]
resources-amount = 3 [65]
[63] ;; this case never happen. Because 4 means integer number in range [0 to 3]
)]
end
to setup-companies
make-companies ;;create the appropriate number of turtles playing each strategy
setup-common-variables-com ;;sets the variables that all turtles share
end
;;create the appropriate number of turtles playing each strategy
to make-companies
create-companies n-tit-for-tat-com
[ set strategy-com "tit-for-tat-com"
set color 135
set earnings-tit-for-tat-Com random 4 + 1 ;;randomly initial resources in the range [ 1 - 4 ]. It is sum + 1 to enforce never inicial resource = 0
]
create-companies n-unforgiving-com
[ set strategy-com "unforgiving-com"
set color 4
set earnings-unforgiving-Com random 4 + 1 ;;randomly initial resources in the range [ 1 - 4 ]. It is sum + 1 to enforce never inicial resource = 0
]
end
; Setup common variables
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;set the variables that all turtles share
to setup-common-variables-com
ask companies [
set shape "pentagon"
set size 1.3
set tit-for-tat-com-score-round 0
set unforgiving-com-score-round 0
set score-com 0
set partnered?-com false
set partner-com nobody
setxy random-xcor random-ycor
]
end
;;;Runtime Procedures;;;
to go
ask companies [birth-die-com]
store-initial-companies-counts ;;record the number of turtles for each strategy
setup-history-lists-com
clear-last-round
ask companies [ partner-up-com ] ;;have turtles try to find a partner
let partnered-com companies with [ partnered?-com ]
ask partnered-com [ select-strategy-com ] ;;all partnered turtles select action
ask partnered-com [ play-a-round-com ]
adjust-patches
tick
end
;Birth-die-com ; According to the level of resources companies can die or birth
;======================
to birth-die-com
(ifelse
strategy-com = "tit-for-tat-com"
[
set earnings-com earnings-tit-for-tat-com
(if earnings-com > 2 * energy-consume-companies [
hatch 1 [set earnings-tit-for-tat-com random 2 + 1 ] ;;randomly initial resources in the range [ 1 - 2 ]. It is sum + 1 to enforce never inicial resource = 0
set earnings-com (earnings-com - energy-consume-companies )])
( if earnings-com < energy-consume-companies [ die ])
]
strategy-com = "unforgiving-com"
[
set earnings-com earnings-unforgiving-com
(if earnings-com > 2 * energy-consume-companies [
hatch 1 [set earnings-unforgiving-com random 2 + 1 ] ;;randomly initial resources in the range [ 1 - 2 ]. It is sum + 1 to enforce never inicial resource = 0
set earnings-com (earnings-com - energy-consume-companies )])
( if earnings-com < energy-consume-companies [ die ])
]
)
end
;;STORE INITIAL COMPANIES¨
;===========================
;;record the number of turtles at the begining of each tick
;;The number of turtles of each strategy is used when calculating average payoffs.
to store-initial-companies-counts
set num-tit-for-tat-com (count companies with [strategy-com = "tit-for-tat-com"])
set num-unforgiving-com (count companies with [strategy-com = "unforgiving-com"])
end
;Setup-History-Lists-com;; initialize PARTNER-HISTORY list in all turtles
;========================
to setup-history-lists-com
set num-companies (count companies)
let default-history-com [] ;;initialize the DEFAULT-HISTORY variable to be a list
;create a list with NUM-TURTLE elements for storing partner histories
repeat (count companies) [ set default-history-com (fput false default-history-com) ]
;give each turtle a copy of this list for tracking partner histories
ask companies [ set partner-history-com default-history-com ]
end
;Clear-last-round
;================
to clear-last-round
let partnered-com companies with [ partnered?-com ]
ask partnered-com [ release-partners-com ]
end
; release partner between companies and turn around
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to release-partners-com
set partnered?-com false
set partner-com nobody
rt 180
set label ""
end
;;have turtles try to find a partner
;;Since other turtles that have already executed partner-up may have
;;caused the turtle executing partner-up to be partnered,
;;a check is needed to make sure the calling turtle isn't partnered.
;;partner-up. Find partner
;===========================
to partner-up-com ;;turtle procedure
if (not partnered?-com) [ ;;make sure still not partnered
rt (random-float 90 - random-float 90) fd 1 ;;move around randomly
set partner-com one-of other (companies in-radius 1 ) with [ not partnered?-com ]
if partner-com != nobody [ ;;if successful grabbing a partner, partner up
set partnered?-com true
set heading 270 ;;face partner
ask partner-com [
set partnered?-com true
set partner-com myself
set heading 90
]
]
]
end
;Select stratey company
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;choose an action based upon the strategy being played
to select-strategy-com ;;turtle procedure
if strategy-com = "tit-for-tat-com" [ tit-for-tat-com ]
if strategy-com = "unforgiving-com" [ unforgiving-com ]
end
;Play a round-companies
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to play-a-round-com ;;turtle procedure
get-payoff-com ;;calculate the payoff for this round
update-history-com ;;store the results for next time
end
; get-payoff-com
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;calculate the payoff for this round and
;;display a label with that payoff.
;; Strategy-companies
;Turtles l l
;Action l C l D
;__________l________ l__________
; C l 3 l -3
;----------l---------l----------
; D l 5 l -2
to get-payoff-com
set partner-defected?-com [defect-now?-com] of partner-com
ifelse partner-defected?-com [
ifelse defect-now?-com
[ set score-com (score-com + 1 ) set label 1 set score-com-round 1 ]
[set score-com (score-com + 0) set label 0 set score-com-round 0]
]
[ifelse defect-now?-com
[set score-com (score-com + 5) set label 5 set score-com-round 5]
[set score-com (score-com + 3) set label 3 set score-com-round 3]
]
end
;Update-history
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;update PARTNER-HISTORY based upon the strategy being played
to update-history-com
if strategy-com = "tit-for-tat-com" [ tit-for-tat-com-history-update ]
if strategy-com = "unforgiving-com" [ unforgiving-com-history-update ]
end
;;;Strategies;;;
; Tit-for-tat-com
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to tit-for-tat-com
set num-tit-for-tat-com-games num-tit-for-tat-com-games + 1
set partner-defected?-com item ([who] of partner-com) partner-history-com
ifelse (partner-defected?-com)
[set defect-now?-com true]
[set defect-now?-com false]
ask companies in-radius 1 [
ask patches in-radius 1 [set resources-to (resources-amount * 1)]
ask patches in-radius 1 [set resources-amount (resources-amount - resources-to)]
set tit-for-tat-com-score-round ( tit-for-tat-com-score-round + score-com-round )
set earnings-tit-for-tat-com (earnings-tit-for-tat-Com + ((sum [ resources-to ] of patches in-radius 1)/ 2) + tit-for-tat-com-score-round - energy-consume-companies )
ask patches in-radius 1 [set resources-to 0 ]
set tit-for-tat-com-score-round 0
]
end
;Tit-for-tat-com-history-update¨
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to tit-for-tat-com-history-update
set partner-history-com
(replace-item ([who] of partner-com) partner-history-com partner-defected?-com)
end
;unforgiving-com ; Works similar to tit-for-tat, but once the another defect, always defect
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to unforgiving-com
set num-unforgiving-com-games num-unforgiving-com-games + 1
set partner-defected?-com item ([who] of partner-com) partner-history-com
ifelse (partner-defected?-com)
[set defect-now?-com true]
[set defect-now?-com false]
ask companies in-radius 1 [
ask patches in-radius 1 [set resources-to (resources-amount * 1)]
ask patches in-radius 1 [set resources-amount (resources-amount - resources-to)]
set unforgiving-com-score-round ( unforgiving-com-score-round + score-com-round )
set earnings-unforgiving-com (earnings-unforgiving-Com + ((sum [ resources-to ] of patches in-radius 1)/ 2) + unforgiving-com-score-round - energy-consume-companies)
ask patches in-radius 1 [set resources-to 0 ]
set unforgiving-com-score-round 0
]
end
;unforgiving-com-history-update
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to unforgiving-com-history-update
if partner-defected?-com [
set partner-history-com
(replace-item ([who] of partner-com) partner-history-com partner-defected?-com)
]
end
;; Adjusting Patches
;; Put limits
to adjust-patches
ask patches [ set resources-amount resources-amount + random-float Inc-Ecological-resources ] ;; Resources are renewable. They grow up again between [0-3). Use Slider
ask patches [ ;; Resources range [0-3)
set resources-amount (ifelse-value
resources-amount < 0 [0] ;;
resources-amount > 3 [3] ;;
[resources-amount]
)
]
;; Patches are colored according their level
ask patches [
set pcolor (ifelse-value
resources-amount < 0 [ black ]
resources-amount = 0 [ 38 ]
resources-amount > 0 and resources-amount <= 0.5 [ 49 ]
resources-amount > 0.5 and resources-amount <= 2 [ 67 ]
resources-amount > 2 and resources-amount <= 3 [ 65 ]
resources-amount > 3 and resources-amount <= 4 [ 63 ]
[ white ])
]
end

"You can't use TICK in a turtle context, because TICK is observer-only." - Don't know how to fix that error

I'm trying to create an own ecosystem and have already written about 100 lines of code. But I'm currently getting an annoying error message [Title] I cannot get rid of unless I delete the line with the «Tick» command.
That's how the Interface looks like at the moment
That's how the Interface looked like after the latest successful simulation
That's the defective Code:
turtles-own [saturation age gender]
breed [sheep a-sheep]
sheep-own [love-cooldown-sheep-f love-cooldown-sheep-m age%-sheep]
globals [tick-stopper]
patches-own [Next-to]
to setup
clear-all
setup-patches
setup-sheep
set tick-stopper 0
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-sheep
create-sheep number_of_sheep [set saturation max-saturation-sheep set gender random 2 set shape "sheep" set age 0]
if setup-sheep-random-age? [
ask turtles with [shape = "sheep"] [set age random life-exp.-sheep]
]
ask turtles with [shape = "sheep"] [
setxy random-xcor random-ycor
if gender = 0 [
set color 118
]
if gender = 1 [
set color 98
]
]
end
to go
if tick-stopper >= stop-at-tick and stop-at-tick? [
set tick-stopper 0
stop
]
move-sheep
sheep-eat-grass
make-love-sheep
check-death-sheep
regrow-grass
set tick-stopper tick-stopper + 1
tick
end
to move-sheep
ask turtles with [shape = "sheep"] [
right random 60
left random 60
forward 1
set saturation saturation - 1
set age age + (((random life-exp.-sheep) + 1) / ((life-exp.-sheep + 1)/ 2))
if gender = 0 and love-cooldown-sheep-f > 0 [
set love-cooldown-sheep-f love-cooldown-sheep-f - 1
set saturation (saturation - (saturation-loss-til-birth-sheep / contribution-period-sheep))
]
]
if gender = 1 [
if love-cooldown-sheep-m > 0 [
set love-cooldown-sheep-m love-cooldown-sheep-m - 1
]
]
end
to sheep-eat-grass
ask turtles with [shape = "sheep"] [
if pcolor = green and saturation < max-saturation-sheep [
set pcolor brown
set saturation saturation + saturation-from-grass
]
ifelse show-saturation?
[ set label saturation ]
[ set label "" ]
]
end
to make-love-sheep
ask turtles with [shape = "sheep"] [
if gender = 0 and love-cooldown-sheep-f = 1 [hatch 1 [set saturation birth-saturation-sheep set gender random 2 set age 0
if gender = 0 [set color 118]
if gender = 1 [set color 98]
if gender = 1 and love-cooldown-sheep-m = 0 and saturation > saturation-for-love-sheep and 1 = count turtles with [shape = "sheep" and gender = 0 and love-cooldown-sheep-f = 0 and saturation > saturation-for-birth-sheep] in-radius 1 [
set saturation saturation - saturation-loss-at-love-sheep
set love-cooldown-sheep-m cooldown-for-love-sheep
ask turtles with [shape = "sheep"] in-radius 1 [
if gender = 0 and saturation > saturation-for-birth-sheep and love-cooldown-sheep-f = 0 [set love-cooldown-sheep-f contribution-period-sheep]
]
]
]
]
]
end
to check-death-sheep
ask turtles with [shape = "sheep"] [
set age%-sheep (100 * (age / life-exp.-sheep))
if saturation <= 0 [die]
if age%-sheep < 22.5039287 [
if (10 ^(0.00984 *(age%-sheep ^ 2) - 0.25918 * age%-sheep + 2.922))/ life-exp.-sheep > random 10000 [die]
]
if age%-sheep > 156.24733 [
if 10000 / (life-exp.-sheep / 100) > random 10000 [die]
]
if age%-sheep > 22.5039287 and age%-sheep < 156.24733 [
if (10 ^(1.899801588 -(((16 / 75 * (age%-sheep ^ 3)) - (50 * (age%-sheep ^ 2))) / 131250)))/ life-exp.-sheep > (random 10000) [die]
]
]
end
to regrow-grass
ask patches with [pcolor = brown] [
set next-to count neighbors4 with [pcolor = green]
if (random 1000000 / 10000) <= (grass-growth% * count neighbors4 with [pcolor = green]) [set pcolor green]
]
end
Thanks for your help <3
This is my first project with NetLogo 6.0.4 and it isn't planned to start any other projects after this one.
You have a bracketing problem in your move-sheep procedure. You have a structure like this:
to move-sheep
ask turtles with [shape = "sheep"]
[ right random 60
...
if gender = 0 and love-cooldown-sheep-f > 0
[
]
]
if gender = 1
[ if love-cooldown-sheep-m > 0
[ set love-cooldown-sheep-m love-cooldown-sheep-m - 1
]
]
end
So you accidentally closed the ask turtles before dealing with the if gender = 1. As soon as you have if gender = 1 then it switches to turtle context.

My agents are producing too many children

I'm working on a model where there is sexual reproduction of offspring, so there are two agent types, males and females. I ask the agents to reproduce once they reach a certain age: 400 ticks and continue to do so every 400th tick.
Females should only produce one child provided there are males. The model works for the first few generations but then the population explodes. With a starting population of one female and one male the numbers proceed as follows: 2, 3, 7, 19, 575. I don't know why it suddenly increases from 19 to 575.
It looks like some of the female offspring reproduce immediately after birth despite having an age = 0 i.e. they're not following this command:
ask females [
if age > 0 and age mod 400 = 0 [
reproduce
]
Here's the full model:
turtles-own [age]
breed[males male]
breed[females female]
females-own [ mates max-mate-count mate-count availa-males mother father]
to setup
clear-all
crt 2 [
ifelse random 2 = 1 [set breed males] [set breed females]
]
ask females [set color grey
setxy random-xcor random-ycor
]
ask males [set color red
setxy random-xcor random-ycor
]
reset-ticks
end
to go
ask turtles [increment-age]
ask females [
if age > 0 and age mod 400 = 0 [
choose-mates
]
]
ask females [
if age > 0 and age mod 400 = 0 [
reproduce
]
]
tick
end
to increment-age
set age (1 + age)
end
to choose-mates
ask females [
set mates males in-radius 100 with [age >= 400]
]
end
to reproduce
ask females with [count mates > 0 ] [
hatch 1 [
set mother myself
set father one-of [mates] of mother
ifelse random 2 = 1 [set breed males
set color red
move-to one-of patches with [pcolor = black]
set age 0
]
[set breed females
set color grey
move-to one-of patches with [pcolor = black]
set mate-count 0
set age 0
]]]
end
Hope you can help!
Don't ask females in the reproduce proc. See below. I've made some other suggestions as well.
turtles-own [age]
breed[males male]
breed[females female]
females-own [ mates max-mate-count mate-count availa-males mother father]
to setup
clear-all
create-males 1 [init-male]
create-females 1 [init-female]
reset-ticks
end
to init
set age 0
move-to one-of patches with [pcolor = black]
ifelse (breed = males) [init-male][init-female]
end
to init-male
set color red
end
to init-female
set color gray
set mate-count 0
end
to-report fertile
report (age > 0 and age mod 400 = 0)
end
to go
ask turtles [increment-age]
let _fertile (females with [fertile])
ask _fertile [choose-mates]
ask _fertile [reproduce]
tick
end
to increment-age
set age (1 + age)
end
to choose-mates
;ask females [ ;DONT DO THIS!
set mates (males in-radius 100 with [age >= 400])
;]
end
to reproduce ;female proc
;ask females with [count mates > 0 ] [ ;DON'T DO THIS!!
if (count mates > 0) [ ;DO THIS INSTEAD
hatch 1 [
set mother myself
set father one-of [mates] of mother
set breed one-of (list males females)
init
]
]
end

How do I translate my code from Netlogo to Netlogo Web? ('TO or TO-REPORT expected' error)

I'd like to use Netlogo Web to open my Netlogo (Desktop version) model, but it does not seem to work. I get a 'TO or TO-REPORT expected' error when I try to upload my model. Not sure what I'm doing wrong. I've inserted the code below. It is supposed to model a cellular process, in which cells divide, filaments of cells branch, and cells change shape as they grow older (procedures 'divide', 'branch', 'transform'). Help please?
to setup
clear-all
setup-turtles
reset-ticks
ask turtles [ set size 1 ]
end
to setup-turtles
create-threads 1
ask turtles [
setxy random-xcor random-ycor
set shape "line"
set color 65
]
end
turtles-own [age]
to go
add-age
divide
branch
transform
tick
ask turtles [ set size 1 ]
end
breed [threads thread]
breed [cylinders cylinder]
breed [circles circle]
to add-age
ask turtles [
set age age + 1
ifelse show-age?
[ set label age ]
[ set label "" ]
]
end
to divide
ask turtles [
if breed = threads or breed = cylinders [
ifelse random 100 < 50
[if (not any? other turtles-on patch-ahead 1) and
(not any? other turtles-on patch-right-and-ahead 10 1) and
(not any? other turtles-on patch-left-and-ahead 10 1)
[hatch 1
[let turn-degree (random(20) - 10)
rt turn-degree
fd 1
set age age - 1
]
]
]
[if (not any? other turtles-on patch-right-and-ahead 180 1) and
(not any? other turtles-on patch-right-and-ahead 170 1) and
(not any? other turtles-on patch-right-and-ahead 190 1)
[hatch 1
[let turn-degree (170 + random(20))
rt turn-degree
fd 1
set age age - 1
]
]
]
]]
end
to branch
ask circles [
if random 1000 < 2[
ifelse random 100 < 50
[if not any? other turtles-on patch-right-and-ahead 90 1
[hatch-threads 1
[right 90
fd 1
set age 0
set shape "line"]
]
]
[if not any? other turtles-on patch-left-and-ahead 90 1
[hatch-threads 1
[left 90
fd 1
set age 0
set shape "line"]
]
]
]]
end
to transform
ask threads[
if (age > 50) and (random 100 < 50)[
set breed cylinders
set shape "cylinder1"
]
]
ask cylinders[
if (age > 100) and (random 100 < 50) [
set breed circles
set shape "circle"
]
]
end
NetLogo Web is stricter about order of declaration (and so will be future versions of NetLogo Desktop).
Just move your turtles-own and breed statements to the top of your code and you should be all right.