Functions of One Turtle Prevents another Breed of Turtle from Executing Function - netlogo

In my model, I have multiple breeds of turtle. One is a macrophage and another is a fibroblast. Each has specific variables and specific functions to carry out. When I comment out the fibroblast function, the macrophages are able to successfully carry out their function. However, when I add the fibroblast function to the model, neither its function nor the macrophages function is properly executed. Any advice on how to resolve this?
To clarify the components of this code, activation, phagocytosis-counter, and phagocytosis-time are variables of macrophages. Repair-counter and Repair-time are variables of fibroblasts and tissue-life and collagen are variables of the patch. Thank you in advance.
`
to Macrophage-function
if activation > 1 and [tissue-life] of patch-at 0 0 < Phagocytosis and phagocytosis-counter = 0 and [tissue-life] of patch-at 0 0 > 0
[ set phagocytosis-time 50
set phagocytosis-counter 1 ]
ifelse phagocytosis-time > 0
[ set phagocytosis-time phagocytosis-time - 1 ]
[ set phagocytosis-counter 0]
if phagocytosis-time = 1
[ask patch-at 0 0 [set tissue-life 0]]
to Fibroblast-function
fibroblast-movement
if ([tissue-life] of patch-at 0 0) < 5 and ([collagen] of patch-at 0 0 ) < 100 and repair-counter = 0
[set repair-time 100
set repair-counter 1 ]
ifelse repair-time > 0
[ set repair-time repair-time - 1 ]
[ set repair-counter 0]
if repair-time = 1
[ ask patch-at 0 0
[set collagen 25
]
]
`
I expected for the two functions to carry out independently, however, they are for some reason impairing the other.

In general, the way to diagnose perplexing problems is to create test output: create file output that reports the variables of each agent on each time step. You will always find mistakes that need to be corrected, and that will likely make your problem go away. (You will need to do this anyway to test your code before putting it to use.)
By the way: do not use "[tissue-life] of patch-at 0 0". You should know that turtles can use the variables of the patch they are on as if they were the turtle's own variables. Replace "[tissue-life] of patch-at 0 0" with "tissue-life". This is explained in the Variables section of NetLogo's Programming Guide.

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.

Difference between `is-agent?` and `is-turtle?`

I believe this must be an easy question, but I have not found the difference between is-agent? and is-turtle? primitives, and when I should use one instead of the other.
Turtles, patches, and links are all agents. So is-agent? will return true for any of those. But patches and links are obviously not turtles, so is-turtle? will return false for those.
breed [ ghouls ghoul ]
to test
clear-all
create-turtles 1
create-ghouls 1
ask turtle 0 [ create-link-to turtle 1 ]
show is-agent? turtle 0 ; true
show is-agent? ghoul 1 ; true
show is-agent? patch 0 0 ; true
show is-agent? link 0 1 ; true
show is-turtle? turtle 0 ; true
show is-turtle? ghoul 1 ; true
show is-turtle? patch 0 0 ; false
show is-turtle? link 0 1 ; false
show is-ghoul? turtle 0 ; false
show is-ghoul? turtle 1 ; true
end
So if you're trying to differentiate agents from things like numbers, strings, and true/false values, you'd use is-agent?. When you really, for-sure want to be dealing with turtles, use is-turtle?.

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 to set global variable as pxcor or pycor of a patch?

I want to save the location of the next patch to use as a global variable so I can refer back to it, like in position-shift so I can alter it in case it is near the edge of the world. However, the code I am using to execute whatever the patch does is in observer mode with set-position and position-shift incorporated in it.
An error shows that patch-at is in turtle/patch-only context while set-position in observer context.
(Not sure but would similar error come up for position-shift?)
to set-position
set nextXcor [pxcor] of patch-at 0 -5
set nextYcor [pycor] of patch-at 0 -5
position-shift
end
to position-shift
if nextYcor = -15
[set nextXcor [pxcor] of patch-at 3 30
set nextYcor [pycor] of patch-at 3 30]
if nextXcor = 15 and nextYcor = -15
[user-message "Space limit reached. Press Halt to continue." stop]
end
Q: How should I approach fixing the code for set-position and potentially position-shift so global variables nextXcor and nextYcor can save the location of the next patch to use?
It is generally preferable to work directly with patches when that is feasible.
globals [nextPatch]
to test
;first test
set nextPatch one-of patches
position-shift
;second test
set nextPatch (patch 0 -15)
position-shift
;third test
set nextPatch (patch 15 -15)
position-shift
end
to position-shift
if ([pycor] of nextPatch = -15) [
if ([pxcor] of nextPatch = 15) [
user-message "Space limit reached. Press 'OK' to continue or 'Halt' to stop."
]
set nextPatch (patch 3 10)
]
;remove the next line
print nextPatch
end