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

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?.

Related

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

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.

Changing the return of true/false with each click though the condition says otherwise in NETLOGO

We tried coding to get true/false based on an upper and lower limit. We tried to get "true" if the base moving is 2-5 and everything <2 and >5 should return "False". Instead we are getting true/false with each click when the base moving is changing to fraction. With 1st click we got TRUE for one turtle and base movement that has base movement 3 and that is correct as we set more than 2 should be TRUE. With the next click the base movement changed to 3.5 and we got FALSE that is not correct as we set 2-5 should be TRUE. Here is our code. We couldn't figure out the problem.
to setup
ca
file-close-all
resize-world 0 250 0 250
set-patch-size 2.5
load-shapefile
load-map
get-lor
get-at
get-sqr
create-farmers
patch-with-sqr
ask turtles [ set adopted? false ]
reset-ticks
end
to go
ask turtles
[ set age age + 1
get-probability
check-moving-prob
if adopted? = true
[ let target one-of patches with [ commune? = true ]
move-to target
]
]
tick
if ticks > 5 [stop]
end
to check-moving-prob
if base-moving > 5 [ set base-moving 5 ] ; setting boundaries of probability
if base-moving < 0 [ set base-moving 0 ]
ifelse base-moving > 2
[ set adopted? adopted? = true ]
[ set adopted? adopted? = false ]
ifelse adopted? = true
[ set color orange ]
[ set color black ]
end
You have written this (assuming I formatted your code correctly):
ifelse base-moving > 2
[ set adopted? adopted? = true ]
[ set adopted? adopted? = false ]
In NetLogo, the equals sign is a logical test, not an assignment operator. So just like you use set color orange to set the colour to orange, you should set the boolean variable to true or false with:
ifelse base-moving > 2
[ set adopted? true ]
[ set adopted? false ]
What you are doing in your code is checking whether it is true and then assigning the outcome of that test to the variable, as if you had written:
let test-result adopted? = true
set adopted? test-result

SEIS Disease Model Help in NetLogo - infected individuals do not become susceptible again?

I'm developing a simple NetLogo disease model that has 4 compartments:
S - susceptible individuals
E - Exposed individuals
I - Infected individuals
S - Recovered individuals that become susceptible again (i.e. there is no immunity).
My simulation starts off with 1 individual who is initially infected with the rest being susceptible.
This is the code I have so far:
turtles-own [
disease?
latent?
susceptible?
latent-period-time
infectious-period-time
]
to setup
clear-all
create-turtles num-agents [ setxy random-xcor random-ycor
set shape "wolf"
set size 2
become-susceptible
]
ask n-of infected-agents turtles [become-infected]
reset-ticks
end
to go
move
spread
tick
end
to move
ask turtles [
right random 50
left random 50
fd 1 ]
end
to spread
ask turtles [
ifelse disease? [] [
if any? other turtles-here with [ disease? ]
[ become-latent
set latent-period-time 0 ]
]]
ask turtles [
if latent-period-time = latent-period ;latent-period is a slider variable set to 30
[
become-infected
set infectious-period-time 0]
]
ask turtles [
if infectious-period-time = infectious-period ;infectious-period is a slider variable set to 100
[
become-susceptible]
]
ask turtles [
if latent?
[ set latent-period-time latent-period-time + 1 ]
if disease?
[set infectious-period-time infectious-period-time + 1] ]
end
to become-susceptible
set disease? false
set latent? false
set susceptible? true
set color orange
end
to become-latent
set latent? true
set disease? false
set susceptible? false
set color gray
end
to become-infected
set latent? false
set disease? true
set susceptible? false
set color blue
end
For some reason only the initial infected individual seems to go back to the susceptible pool, while any other newly infected individuals do not go back to the susceptible pool. The initially infected individual is also unable to get infected again after going back to the susceptible pool even though it encounters infected individuals.
I'm not sure how to fix this problem.
Thanks!
Your problem is that you never reset the value of latent-period-time and infectious-period-time back to 0. There are two ways to fix this:
Put the set to 0 into the same code that changes all the state flags and colours
Scrap the tracking and incrementing entirely and use a variable that records when the turtle gets to the state - assume it's called 'state-start-time' then you would simply have set state-start-time ticks and then do a subtraction for your test of duration.

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.

Netlogo I want to convert turtles-own variable to global's variable, and run them as "counter" of turtles-own variable without any error

I want to convert turtles-own's variable (nb-of-turtles, nb-dead) to global's variable (number-of-turtles, number-dead) in order to compile with BehaviorSpace. nb-of-turtles is a decrement-counter (In the beginning of the model, I use this as increment counter. The counter counts the number of turtle in the road. This counter does not count as cumulative value. Therefore I puts "set nb-of-turtles nb-of-turtles - 1"). nb-dead are increment- counter (This counter needs to count as cumulative value of number of dead turtles totally). However, these counters do not count well. When the turtle at the end of the road dies, increment nb-dead by one. Similarly, when the turtle at the end of the road dies, it decrements nb-of-turtles by one. In my model, when the turtle dies at the end of the road, I use the flag (onend). The following is sample code. Please give me advice.(Some codes have already been consulted and discussed, then solved in the following link.the link Thank you very much.
globals [ number-of-turtles number-dead A ]
turtles-own [ onend? nb-of-turtles nb-dead ]
let numle count [turtles-at 0 0] of patch min-pxcor 0
if numle = 0 [
create-car
set number-of-turtles number-of-turtles + 1
]
to create-car
crt 1 [
set onend? FALSE
]
end
ask turtles with [onend?]
[ if gamma-A = 0 [
die
set nb-dead nb-dead + 1 ;;This does not count.
set nb-of-turtles nb-of-turtles - 1 ;;This does not count well.
]
]
ask (turtles-on patch max-pxcor 0) with [not onend?]
[
set number-of-turtles nb-of-turtles
set number-dead nb-dead
set gamma-A precision (random-gamma (α) (β))0
set speed 0
set color red
set onend? TRUE
]
tick
end
You may be mixing up the use of global and turtles-own variables. It doesn't really make sense in this context to use a turtles-own variable as a counter, since every new turtle created will have its "nb-dead" or "nb-of-turtles" variable start at 0. It's better in this case to have the turtles access the global counter directly when they die, for example. Additionally, you can just use count turtles to get the current number of turtles- no need to have the turtles manually add to that value. For an example, please see below:
globals [ number-of-turtles number-dead gamma-A start-patch end-patch]
turtles-own [ onend? speed ]
to setup
ca
reset-ticks
set start-patch patch min-pxcor 0
set end-patch patch max-pxcor 0
set gamma-A random-gamma 10 1
end
to create-car
ask start-patch [
sprout 1 [
set heading 90
set shape "car"
set color green
set onend? false
set speed 1
]
]
end
to go
;; If start patch has no turtles, it has a 10% chance
; of spawning a new turtle.
if [ count turtles-here ] of start-patch = 0 and random 100 < 10 [
create-car
set number-of-turtles number-of-turtles + 1
]
;; Ask any turtles not on the end patch to move fd
; at their speed, if they get to the end-patch
; set their speed to 0
ask turtles with [ not onend? ] [
fd speed
if patch-here = end-patch [
set speed 0
set color red
set onend? true
]
]
;; Decrease the GLOBAL variable of gamma-A by 0.1
set gamma-A gamma-A - 0.1
;; Ask turtles that are at the end,
; if gamma-A is less or equal to 0,
; increase the number-dead variable by one
; and then die
ask turtles with [onend?]
[
if gamma-A <= 0 [
set number-dead number-dead + 1
die
]
]
;; Use count to set the number-of-turtles
set number-of-turtles count turtles
;; If gamma-A has dropped to 0 or below,
; reset it to its new higher value
if gamma-A <= 0 [
set gamma-A random-gamma 10 1
]
tick
end