NetLogo in-radius - netlogo

i have this code and it's not clear to me what is it doing:
patches-own [ field ]
let a max-one-of patches in-radius b [field]
ifelse ([field] of a > 0.1) and ([field] of a < 0.5)
[
;; do something
]
[
;; do something else
]
Thanks,
Marco

This is apparently code to be run by a turtle or patch, it isn't apparent which.
patches in-radius b is an agentset of the circle of patches, of radius b, around the calling agent. max-one-of ... [field] finds the patch in that agentset that has the largest value for field. That patch is then stored in the new local variable a. (A better name than a might have been winner or peak or best-patch.)
[field] of a is then that maximum value of field, the same one that max-one-of found. The ifelse checks to see if that value is in a certain range or not, and does something different, depending.
Does the code inside the ifelse make any further use of a? If it does, cool. If it doesn't, well, the code could be more easily and simply written as:
let m max [field] of patches in-radius b
ifelse m > 0.1 and m < 0.5
[
;; do something
]
[
;; do something else
]
perhaps seeing it in this form will help making the meaning clear.

Related

Netlogo - Iterating through patch list and writing each output to a row in a list

I have a list of cells destination_cells, and am trying to loop through each cell and count the number of turtles and the average weight of the turtles in each. For each cell, I want to write the cell's ID (which I am assuming is just destination_cells as coordinates) and the two variables to a list cell_list. I think my confusion stems from not understanding how to use anonymous reporters in foreach. If I replace 'next cell' with destination_cells in the foreach block, I get, for example, the same total_turtle_in_destination_cells value for every single cell in destination_cells. The same occurs with using next_cells in the block. Where am I making a mistake?
set destination_cells [self] of patches with [ any? turtles-here]
foreach destination_cells [next_cell -> ask next_cell
[
; Counts number of turtles in each cell
set total_turtle_in_destination count turtles-on destination_cells
; Finds average weight of turtles in each cell
set avg_weight_turtle_in_destination mean [mass] of turtles-on destination_cells
]
set cell_list lput (csv:to-row (list
destination_cells
total_turtle_in_destination
avg_weight_turtle_in_destination
)) cell_events_list
]
Here is an alternative that:
Makes a reporter to get the info desired (the patch info as patch x and y, the total turtles on a patch, the mean weight of present turtles) from whatever patch that calls the reporter
Updates the info list directly by querying the destination_cells
turtles-own [ weight ]
globals [ destination_cells cell_info_list]
to setup
ca
resize-world 0 5 0 5
set-patch-size 30
reset-ticks
crt 20 [
set weight random 10 + 1
move-to one-of patches
]
end
to-report report-self-info
let n_turtles_here count turtles-here
let mean_weight mean [weight] of turtles-here
report (list self n_turtles_here mean_weight)
end
to create-info-list
set destination_cells patches with [ any? turtles-here]
set cell_info_list [report-self-info] of destination_cells
print cell_info_list
end
In response to your question "how can the patch call the reporter? I tried ask destination_cell [report-self-info], but a command is expected":
When you make a to-report procedure with the structure like the above that uses self, it's somewhat like a turtles-own or patches-own variable. In this case, you can call it just as you would have a turtle variable like color or xcor or a patch variable like pcolor or pxcor- if you run something like:
ask one-of patches [ print report-self-info ]
You should get something like
[(patch 5 2) 2 7.5]
This is why the set cell_info_list [report-self-info] of destination_cells above works- because you could do something like set cell_info_list [pcolor] of destination_cells in a similar way.

Is there a way to create a modification of Breed-On that includes specific characteristics?

I want my forager turtles to follow a path laid down by leader turtles, and this path consists of a turtle breed called trail-markers. The forager turtles also lay down trail-markers (to reinforce the trail), so I would like them to check for leader trail-markers on some patch/patches. I know Netlogo includes breeds-on to check for the presence of members of a breed, but I need the foragers to check for blue leader trail-markers. Is there a way to do this?
Here is what I had in mind:
if count (trail-markers with [color = blue]) on patch-ahead 1 > 0 [fd 1]
But I got, error:nothing named on has been defined, which makes sense, since the Netlogo primitive is breeds-on, so I modified it to:
if any? (trail-markers with [color = blue])-on patch-ahead 1 [fd 1]
However, I simply received: error:nothing named -on has been defined instead.
See the code below. I think you just want to use trail-markers-on, but you can simplify by using any? a bit. And you might want to have the custom reporter so you can just check for blue-markers-on if you find that simpler.
breed [ trail-markers trail-marker ]
to example
; I think this is what you want
if count ((trail-markers-on patch-ahead 1) with [color = blue]) > 0 [ forward 1 ]
; this is slightly simpler, using `any?` instead of `count ... > 0`
if any? (trail-markers-on patch-ahead 1) with [color = blue] [ forward 1 ]
; you could also write a custom reporter if you'll need to get this info a lot
if any? blue-markers-on patch-ahead 1 [ forward 1 ]
end
to-report blue-markers-on [p]
report (trail-markers-on p) with [ color = blue ]
end

Netlogo: How to select a turtle with the lowest ID for each stagnant turtle in each specified patch in the world?

I have a model that keeps turtles in several patches each. I would like to select a turtle with the lowest ID for each stagnant turtle in each specified patch in the world.
For example, the answer is as follows. At patch coordinates (1, 0), five turtles stayed. And the ID of the turtle with the smallest ID was ID = 5 within the patch coordinates (1, 0).
The following is a sample program. But this program is not intended. Is there something good syntax instead of the syntax "min-one-of turtles [who]"? I want your advice. Thank you.
ask (turtles-on patch 1 0) [
ask min-one-of turtles [who] [
set flag-1 TRUE
]
I'm not sure what you mean by 'program is not intended'. Your problem is that you ask each of the turtles on patch 1 0 to identify the minimum who over all turtles. What you want is:
let targets (turtles-on patch 1 0)
[ ask min-one-of targets [who]
[ set flag-1 TRUE
]
]
If the only thing you are going to do with the turtles on that patch is to select the lowest who, you don't need to set up the agentset explicitly. Instead:
ask min-one-of (turtles-on patch 1 0) [who]
[ set flag-1 TRUE
]

NetLogo: select neighboring patches to a patch-set

I'm modeling home range selection in NetLogo. I'd like a turtle to identify the subset of patches that are neighbors to its current home range. The turtle should then identify which neighboring patch is best (defined for now as the patch with highest food benefit) and move to that patch to claim it. It continues these steps (identify available patches neighboring the home range, select best option, add to homerange, repeat) until it has enough food within the home range.
Part of the code is as follows, but I get the error that "FACE expected input to be an agent but got NOBODY instead." I think something is off with the reporter or my use of it (I'm new to reporters; I'm trying to make the code modular to minimize procedure length).
Any idea where I'm going wrong? Thanks!
patches-own [
owner
benefit
used?
]
to-report edge-patches-of [my-homerange] ;; reporter to find patches along edge of homerange
report my-homerange with [
any? neighbors with [
owner = [owner] of myself
]
with [used? = false] ;; (only want neighboring patches not already owned)
]
end
to pick-homerange
ifelse food < food-needed ;; (turtle builds homerange until it has enough food)
[ let my-homerange patches with [owner = myself]
ask edge-patches-of my-homerange [set pcolor red ] ;; (this to just see the options)
;; now that we know the options, pick the best:
let available-destinations edge-patches-of my-homerange
set destination max-one-of available-destinations [([benefit] of patches)]
face destination
forward 1
if patch-here = destination
[ add-destination ]
]
[stop]
end
You have not included enough code or description to be sure, but it seems that your logic is overcomplicated and contains conceptual errors. Try this as a starting point. Note in particular the test for a nonempty set of candidate edge patches.
globals [food-needed]
patches-own [owner capacity]
turtles-own [homerange]
to setup ;make sure all turtles have an initial homerange
ca
ask patches [set owner nobody]
crt 10 [
set-initial-ownership
;do the following just once, not over and over
set homerange (patches with [owner = myself])
]
;etc
end
to-report benefit [_t] ;patch proc
;patch reports benefit to turtle _t
report 1 ;or whatever
end
to-report food ;turtle proc
report sum [capacity] of homerange
end
to pick-homerange ;turtle proc
;; (turtle builds homerange until it has enough food)
if (food >= food-needed) [stop]
let _candidates edge-patches
if any? _candidates [ ;you almost certainly need to add this
let _destination max-one-of _candidates [benefit myself]
face _destination forward 1
if (patch-here = _destination) [augment-homerange _destination]
]
end
to augment-homerange [_p] ;turtle proc
ask _p [set owner myself]
;just add to the homerange as needed; don't keep recreating it
set homerange (patch-set homerange _p)
end
to-report edge-patches ;turtle proc
;; (only want neighboring patches not already owned)
report (patch-set [neighbors] of homerange) with [owner = nobody]
]
end

how to sprout around a turtle in netlogo?

I have been looking to spread around turtles and I don't get it.
The idea is that I have an insect population (a type of turtle) and this insect population check around if there's a nesting patch available. If there's one and there's no other insect population I would like this patch to generate a new insect population. So far I have come with this idea :
ask insect-populations
[
ask patches in-radius 2
[
if lay? = 1
[
if not any? insect-populations [ask self [sprout-insect-populations 1]]
]
]
]
Thanks in advance for any tip
ask insect-populations
[
ask patches in-radius 2 with [lay? = 1 and not any? insect-populations-here]
[sprout-insect-populations 1]
]
should be what you want if I understand your intent correctly.
The trick is in the [with]. It takes a true/false block. So any agent for which the boolean statement inside the square brackets is included in the set.