NetLogo: Issue with Chooser - netlogo

I'm trying to do a disease simulation where you can choose which disease to simulate. To achieve this, I've added a chooser to the Interface, which is named "disease", with options like smallpox, measles and mumps. It is also accompanied by the following code in the setup:
if disease = smallpox
[
set infection-rate 90
set infectivity 6
set lethality 30
set recovery recovery-rate
set immunity (1 - (1 / infectivity)) * 100
]
if disease = measles
[
set infection-rate 90
set infectivity 15
set lethality 1
set recovery recovery-rate
set immunity (1 - (1 / infectivity)) * 100
]
if disease = mumps
[
set infection-rate 80
set infectivity 6
set lethality 1
set recovery recovery-rate
set immunity (1 - (1 / infectivity)) * 100
]
And the following:
ask turtles with [sick? != true and immune? != true]
[if any? (turtles-on patch-ahead 1) with [sick? = true]
[if (random 100) < infection-rate
[
set sick? true
]
]
]
In the Interface, I've also set up a tracker to track the "infection-rate". However, when I run the program, the infection-rate sits at zero; furthermore, the turtles aren't infecting others. (This isn't an issue, as I've tried my "infection" code with manually set values and it worked.)
Is there something wrong with how I'm coding the "ifs", or rather how I'm trying to work with the variables? Thanks in advance for the help.

Related

NetLogo: Issues when restricting number of users charging at a certain patch

I am working on a project to model the impact of charging electric cars on the grid and modeling/simulating the driving and charging habits of the car users. I'm getting an issue in my code that unable to resolve yet.
Each location has a limited number of charging ports. For example, WORK has a total of 2 TERMINALS, so only 2 adopters can charge there simultaneously (first-come-first-serve basis). What I want to do is when 2 adopters arrive at WORK, they start charging (if required, i.e. "charging-status" = true). Any additional adopters wait until a port is available there. The adopters who finish charging should vacate the charging port for those in the wait-list, even if they don't leave.
Here's part of my effort (code) that I did:
to go
...
charge-car ; sets the charging-status based on state-of-charge.
ask adopters
[
if charging? and not marked?
[
ifelse remaining-ports != 0
[
set remaining-ports max list (remaining-ports - 1) 0
set marked? true
]
[set occupied? true]
]
if marked? and not charging?
[
set remaining-ports min list (remaining-ports + 1) terminals
set marked? false
set occupied? false
]
]
ask adopters with [charging? and marked?]
[
set color green
let battery0 battery
let charging-speed0 charging-speed
let battery1 max list 0 ( battery + charging-speed0 )
set battery min list battery1 battery-capacity
let charged min list ( battery - battery0 ) charging-speed0
set charge-demand charge-demand + charged
set soc battery / battery-capacity
set range-left battery / discharge-patch
]
tick
end
Now, the issue is this: there are multiple location on the map with charging ports. This code gives different results at some locations, even though it is the same algorithm for all locations and agents. For example, if both ports are occupied at certain locations, the "occupied?" will be true for some locations and not all of the ones with all ports engaged. I mean to say, this is showing quite a random response.
Can anyone please help me with this? Is there another way to do what I want to do? Also, please let me know if you need more info to understand my situation.
Thank you!
Edit:
This is my code for to go
to go
...
ask adopters
[
if patch-here = current-loc ; choose next target only when reached at a destination (current location)
[
choose-target
set nearest-station min-one-of patches with [location = "charging-station"][distance myself]
] ; choose target based on start time and current location
; go to target only when NOT at the arbitrary target location
if target != [0 0]
[
let dist-to-targ distance-between current-loc target
let dist-to-station distance-between current-loc nearest-station
ifelse dist-to-targ > range-left and dist-to-station < range-left
[go-to-station nearest-station]
[go-to-target]
]
if charging = "Charge Car Now"
[charge-car]
...
]
where, charge-car is
to charge-car
if patch-here = current-loc and charging-point
[
ifelse soc < 1
[
if charge-power = 1
[
set charging-speed 1 / 12
set charging-status true
]
if charge-power = 2
[
set charging-speed 6.6 / 12
set charging-status true
]
]
[
set charging-status false
set color blue
]
]
end
and go-to-target is
to go-to-target
ifelse patch-here != target
[
; move towards destination and use fuel
face target
; set marked? false
set color blue
ifelse distance target <= speed
[set speed1 0.3 * distance target] ; decrease speed as target gets nearer
[set speed1 speed]
forward speed1
set moving? true
set charging-status false
if marked?
[
set rem-term min list (rem-term + 1) terminals
type patch-here type "Updated ports" print rem-term
set marked? false
set occupied? false
]
]
[
move-to target
if target != [0 0]
[set dist-trav distance-between current-loc target]
set current-loc target
set moving? false
set dwell dwell-acq day-ind time-ind position [location] of target places ; calculate dwell time based on arrival time at target
ifelse dwell < 0
[
set dwell 288 - (ticks mod 288) ; spend rest of the time till 24:00 at that location
set dwell-flag 1
]
[set dwell-flag 0]
if current-loc = target
[
set arrival-time (ticks mod 288)
set start-time (dwell + arrival-time) mod 288
set target [0 0]
set battery battery - (discharge-patch * dist-trav) ; discharge based on distance traveled per tick
set soc battery / battery-capacity
set range-left battery / discharge-patch
if battery < 0
[set battery 0]
if soc < 0
[set soc 0]
]
]
end
where, rem-term is same as remaining-ports and charging-status is same as charging?.
I tried adding the same code in the go-to-target function, since charging-status changes there first, but that didn't show any change in the results I'm getting.
I can't see anything obviously wrong with your code. This sort of thing usually happens because you have multiple ask turtles blocks, and you work out the intention in the first block but don't do the behaviour until the second block. In your case, I can see you updating the ports count in the first block, so that doesn't directly apply.
However, I wonder if you're doing something similar with your if statements, that turtles are going through different blocks than you expect and the relevant code is missing from the extract that you pulled out. The easiest way to diagnose this type of problem is with print statements. See below for one possibility.
ask adopters
[ if charging? and not marked?
[ ifelse remaining-ports > 0
[ type patch-here print remaining-ports
set remaining-ports remaining-ports - 1
set marked? true
type patch-here type "Updated ports" print remaining-ports
]
[ set occupied? true ]
]
if marked? and not charging?
[ set remaining-ports min list (remaining-ports + 1) terminals
set marked? false
set occupied? false
]
]
Note that I also changed your code for testing/updating number of remaining ports for clarity.
On your question about lists, there is no problem adding a turtle to a list (eg set queue lput self queue) but if you want more detail than that, please ask a separate question. I strongly recommend that you do not make any attempt to introduce queues for your ports until you have the existing code working properly.

Counting turtles after a chunk of code has executed

In my model currently, I have a monitor on the Interface that counts the total number of turtles (deer in my case) every tick. Is it possible to have another monitor that counts the number of deer after a certain line of code is executed? For example, here's a snippet of code:
to catch-fawns-source
let fawn-hunting-rate (fawn-harvest-rate-source)
if any? fawns-on source-patches
[ ask fawns-on source-patches [
if random-float 1.0001 < (fawn-hunting-rate)
[ set harvest (harvest + 1)
set fawn-harvest (fawn-harvest + 1)
set source-harvest (source-harvest + 1)
die ] ]
]
end
In this case, this is where I have fawns being harvested. This code is the same for my male and female adult and juvenile deer. So is there a way I could track my population specifically after that snippet of code (and the other identical ones for the juveniles and adults) is executed?
I'm not sure if I would need any sort of Netlogo extension (if there are any applicable to this) or if I could somehow add in another global variable and lines of code that accomplishes this task.
Thanks for all of your help as always!
I think you can get away with another global variable that you just update however often you want. For a very simple example, consider this setup:
globals [ most-recent-pop-check ]
to setup
ca
crt 10
set most-recent-pop-check count turtles
reset-ticks
end
Here, most-recent-pop-check will only be updated when needed, then you can just set a monitor to display that variable. In this example, the value will only change every 25 ticks- see comments for more detail:
to go
ask turtles [
; Turtles may die
if random-float 1 < 0.25 [
die
]
; Throw in some density-dependence to control population size
if random-float 1 < ( 0.5 * ( 1 - ( count turtles / 500 ) ) ) [
hatch random 2 + 1
]
]
; If the ticks are not 0, and if the remainder after dividing
; the ticks by 0 is 0, update the most-recent-pop-check
; variable to reflect the current turtle population.
if ticks != 0 and ticks mod 25 = 0 [
set most-recent-pop-check count turtles
]
tick
end
Of course, in your case rather than having the update occur based on the number of ticks, just have it occur whenever you run the code chunk you're after.

Seed dispersal around a tree provokes an infinite loop?

I've just started a project in which I intend to simulate (among other things) the seed dispersal around trees (named zsps). Neighboring patches can already be settled or unable for seeding due to low fertility.
I wrote code that runs (at the begining of simulation) but that made the code enter into an infinite loop (I think) because some trees attempt to spread seeds but cannot find any available place.
Does anyone have a solution to escape from this trap (in case the number of places is < the number of seeds or in case there are no places at all)?
Here is the procedure I wrote :
to check-seed-dispersal
ask zsps[
let nb-seeds 0
ifelse (age > 4) [set NFemFlowers 100]
[set NFemFlowers 0]
let temp-color color
if Tot_pol > 0
[set nb-seeds round ((NFemFlowers / Tot_pol) * 6 ) ]
if (nb-seeds > 0 )[
ask patches in-radius 5 with [fertility > 8 and not any? zsps-here]
[sprout-zsps nb-seeds
[ifelse (temp-color = yellow)
[set color gray set age 0 ]
[set color red set age 0 ]
]
]
]
]
end

how can i find the exact tick in netlogo in which agents take an action?

I have agents that their devices may face failure randomly and based on the probability of failure. (working-devices? false)
when the device does not work, I should calculate the summary of waiting-time till now and then update the status of the device (working-devices? true).
My problem is in update part.
let assume the waiting-time = 1 and we are in tick = 10 that working-device? become false, and we do our model for 365 days(ticks).
In updating, I should let my status became true if tick > 11 ( 10 + 1 ).
11 means (the-tick + waiting time).
My questions is how can I understand in which tick the status became false?
and what is the best way to write my update procedure?
;; waiting-time is slider (for example ) 1
breed [ customers customer]
....
customers-own [device-working? real-waiting-time? ... ]
to setup
....
ask customers [
set real-waiting-time 0
set device-working? true
.....
]
end
to go
if ticks < 365 [
ask customers [if (device-working? = true)
[ impose update]
]
to impose
if random 100 > 95
[set device-working? false
set real-waiting-time real-waiting-time + waiting-time ]
end
to update
;; let the-tick when devices of customers faces failure
;; if tick > the-tick + waiting-time-slider and device-working? = false
;; [set device-working? true]
end
What you need to store is add another attribute for each customer that stores the tick at which it failed or when it is fixed. I have taken the latter approach and called that attribute end-waiting-time (NOT tested).
to impose
if random 100 > 95
[set device-working? false
set real-waiting-time real-waiting-time + waiting-time
set end-waiting-time ticks + waiting-time] ; this is the new line
end
to update
if device-working? = false tick and ticks = end-waiting-time
[ set device-working? true ]
end

Report inner loop error

Code:
to-report check-wall
hatch 1[
set color grey
set size ([size] of one-of walls) / 2
while [distance myself < (2 * ([size] of myself)) and (pycor + ([size] of one-of walls) / 2) < max-pycor ]
[
fd ([size] of one-of walls) / 2
if any? walls in-radius size
[
report true
]
]
die
]
report false
Error:
REPORT must be immediately inside a TO-REPORT. error while inboxturtle
260 running REPORT called by procedure CHECK-WALL
Please ignore the functionality of the code. I didn't elaborate it because I don't think it would be required.
Note: I wish that when inner if is true it stop the method and return. Thus, creating a variable and setting it true in that if and then returning at the end is not a likely option.
This an unfortunate limitation of the NetLogo language, that report can't be used inside of ask, hatch, and similar primitives.
But in this case, even if the limitation weren't present, the code still wouldn't be right, because you want to kill the turtle on the way out of the procedure, and just using report doesn't accomplish that.
You can fix it as follows:
to-report check-wall
let result false
hatch 1 [
...
while [not result and ...] [
...
if ... [ set result true ]
]
die
]
report result
end