I am trying to assign each patch a particular value, which is based on its proximity to each of a certain group of turtles. Those values were going to be stored in a table. Is there any way to iterate over turtles within a patch iteration? This is part of my code:
ask patches [
let this_xcor pxcor
let this_ycor pycor
if pcolor != obstacle-color [
ask turtles with [patch-ahead Movement_Speed = obstacle-color][
set patchdist sqrt(((xcor - this_xcor)*(xcor - this_xcor) + (ycor - this_ycor)*(ycor - this_ycor)))
Related
When one variable of a patch reaches a certain value (e.g. age = 75 ), I redirect the patch to another procedure (called retirement) where I want to select all the patches that share the same value of the variable Farm_ID so the procedure is applied to all the patches.
I'm looking for a command which mean "the same as", I also considered to use the to-report and report procedure to report the pxcor and pycor of the original patch and then ask the other patches with the same ID_Farm as the patch pxcor pycor
But I feel like I'm missing something in the essence of using Netlogo (I'm a newbie).
to start-simulation
reset-timer
tick-advance 70
;; increase the farmer's age
ask patches with [seed = 1] [set age age + 1]
ask patches [
if age = 75 [ retirement ]
]
;; other stuffs
end
to retirement
ask patches with [ ID_Farm = ???]
;; procedure to change the variable ID_Farm depending on the closest patches with a different ID_Farm
end
You need myself (see here), and more specifically you need to use variable = [variable] of myself.
See a minimal and reproducible example below:
to setup
clear-all
ask patches [
ifelse (pxcor > 0)
[set pcolor lime]
[set pcolor orange]
]
end
to go
ask one-of patches [
type "I am the chosen patch. My color is " type (ifelse-value (pcolor = 25) ["orange"] (pcolor = 65) ["lime"] ["cyan"]) print ". I and all patches having my same color will become cyan."
operate-on-patches-with-my-same-color
]
end
to operate-on-patches-with-my-same-color
ask patches with [pcolor = [pcolor] of myself] [
set pcolor cyan
]
end
When you check the myself entry in the NetLogo Dictionary, note the difference between self and myself. At the beginning it may be confusing, but as you get more and more used with how NetLogo works it will become clear.
How can I build a syntax for an "ifelse" statement that has a turtle in a specified patch and has the turtle determine if the turtle's color is the specified color? The following is sample code, but it won't work. Incidentally, I would like to build it using "max-pxcor - 1".
ifelse count turtles-on patch (max-pxcor - 1) with [color = red] = 1[ ;The following code is omitted
(1) The patch X Y notation selects a specific patch but you have not provided a pycor. I assume you therefore want any patch with the correct value for pxcor. How many of those patches are coloured red? (2) If you are not selecting a patch with its identifier, then you are working with a patchset rather than a patch, even if there is only one patch in that set. If you need to get a patch from a patchset, you need one-of.
If you actually want to identify that patch so you can do something with it, then this approach separates the patches from the turtles so is reasonably readable:
let wanted-patch one-of patches with [pxcor = max-pxcor - 1 and color = red]
ifelse count turtles-on wanted-patch = 1
[
But you could also do:
ifelse any? patches with [pxcor = max-pxcor - 1 and
color = red and
count turtles-here = 1]
[
Of, if you genuinely want the patchset so the total number of turtles on all the right hand column red patches is 1 (so 1 on one patch and 0 on others), then:
ifelse count (turtles-on patches with [pxcor = max-pxcor - 1 and color = red]) = 1
[
For this last one, the () are optional, for readability.
I'm modeling a cityscape for looking at movement, where patches equate to buildings and have different influence values. I need to use a reporter function that calculates the value of each patch with something like:
(its own influence) plus (the influence of neighbors) divided by the
distance to a specific turtle (max-one-of distance myself)
The turtle will then move towards the patch with the highest influence value, but along a defined street.
I'm new to using netlogo and have gotten completely stuck.
I've included a portion of what I have so far, but I can't figure out how to write the reporter function that will compute each patches influence value (in-cone) so that the turtles can then move towards the best option.
to setup-influence-field
ask patches with [pcolor = green] [set influence commercial-influence]
ask patches with [pcolor = orange] [set influence production-influence]
ask patches with [pcolor = yellow] [set influence domestic-influence]
ask patches with [pcolor = pink] [set influence religious-influence]
ask patches with [pcolor = blue] [set influence public-influence]
end
to go
move-serapis
end
to move-serapis
ask serapis [set-procession-heading]
repeat 2 [ ask serapis [ fd .25 ] display ]
tick
end
;;;;; the reporter values are need for this part of the code so that the turtles (serapis) can move towards the patches with the highest influence value;;;;
to set-procession-heading
let direction patches in-cone 4 40 with [influence-field > 0]
if any? influence-field
[face max-one-of influence-field] ;;;; face towards the highest computed influence value
ifelse any? patches with [pcolor = black] in-cone 1 25
[process]
end
Any help would be greatly appreciated!
I don't think this is completely correct and I can't test it, but it should start you off and maybe someone here can fix it if you let us know the errors.
to set-procession-heading
let direction-targets patches in-cone 4 40 with [influence-field > 0]
if any? direction-targets
[ face max-one-of direction-targets [ influence-amount self ] ]
end
to-report influence-amount [ target-node ]
report ( [ influence-field ] + sum [ influence-field] of neighbors ) / distance target-node
end
What I have done is set up a separate procedure to report the results of your calculation. That procedure takes an argument (named target-node) because you need to be able to pass the identity of the turtle being influenced. Once you have that procedure, then you can simply pass the agent-set of potential directions to the calculation procedure and pick the one with the largest value.
I'm trying to create a simple simulation of agents moving randomly but avoiding certain obstacles. I want them to store the coordinates of the places they've been so they don't go there again. This is part of what I have so far:
to move-agent
let move random 3
if (move = 0) []
if (move = 1)[ left-turn ]
if (move = 2)[ right-turn ]
set xint int xcor ;;here i'm storing the coordinates as integers
set yint int ycor
set xylist (xint) (yint)
go-forward
end
to xy_list
set xy_list []
set xy_list fput 0 xy_list ;;populating new list with 0
end
However, it keeps giving me a "SET expected 2 inputs" error. Can anyone help me with this?
It looks like you are incorrectly using xy_list as both a variable and a turtle variable.
I don't see the need for the xy_list procedure - Keep it as a turtle variable. Make sure xy_list is in the turtles-own list:
turtles-own [xy_list]
initialize it to an empty list when you create a turtle. eg:
crt 1 [set xy_list []]
When a turtle moves, you could add their current position as an xcor, ycor list with:
set xy_list fput (list int xcor int ycor) xy_list
You will then need to check if that coordinate already exists in the list before moving there.
However, as you are using integer coordinates, it would be a lot easier to use a patch-set to keep track of a turtle's history. You could try this:
turtles-own [history]
to setup
ca
crt 3 [set history (patch-set patch-here) pd]
end
to go
ask turtles [
let candidates neighbors with [not member? self [history] of myself]
ifelse any? candidates
[move-to one-of candidates stamp
set history (patch-set history patch-here)]
[die]
]
end
I have limited programming experience (mechanical engineering student, so a bit of matlab and labview experience) and am very new to NetLogo, so I apologize in advance if this question is pretty basic or my code is of poor quality.
I need to have my turtles move to 1 of 2 possible neighboring patches based on a given probability function. The two patches that I need to input to the probability function are the two neighboring patches with the lowest nest-scent value. I have been able to pull the two lowest nest-scent values, but I cannot figure out how to actually figure out which patches those are, and how to put those coordinates into an ifelse statement to move the turtle to one of them based on the aformentioned probability function. I have the following code that is obviously not working:
to move
set farthest-patch sort-by < [nest-scent] of neighbors
let a1x pxcor of item 0 farthest-patch
let a1y pycor of item 0 farthest-patch
let a2x pxcor of item 1 farthest-patch
let a2y pycor of item 1 farthest-patch
let a1 item 0 farthest-patch
let a2 item 1 farthest-patch
let x (((a1 + a2) / 100 ) - 1)
let probability-move 0.5 * (1 + ((exp(x) - exp( - x)) / (exp(x) + exp( - x))))
ifelse random-float 1 < probability-move
[set to-move 1]
[set to-move 0]
let a1-probability (a1 / (a1 + a2))
ifelse random-float 1 < a1-probability
[set destination [a1x a1y]]
[set destination [a2x a2y]]
ifelse count turtles-here >= 20
[set full 1]
[set full 0]
if [a1x a21] = full
[set destination [a2x a2y]]
if [a2x a2y] = full
[set destination [a1x a1y]]
if [a2x a2y] and [a1x a1y] = full
[set to-move 0]
ifelse to-move = 1
[move-to destination]
[stop]
end
Basically what I have (tried) to do here is sort a farthest-patches list by increasing nest-scent, and I have pulled the two lowest nest-scent values in order to input those values into my probability functions (both for whether or not to move, and if they are to move which of the two patches to select). I am not sure how to properly pull the patch coordinates of the patches that the a1 and a2 values were taken from.
Thanks for any help,
Brad
okay, you are making life way more complicated than it needs to be. You can select the two patches (or turtles) with the smallest values of a variable with min-n-of. Look it up in the dictionary to get the details.
Having found the two candidates, the best option is to use the rnd extension for choosing the destination because it has a primitive for random selection by weight. Finally, since you are using a function of your variable as the weight (rather than the variable value itself), you need a way to construct that weight. The best option is to separate it out - you could also have a second variable with the weight value, but that just proliferates variables.
Here is a complete working model. Please copy the whole thing into a new instance of NetLogo and try and understand how it works, rather than just copy the relevant bits into your code because min-n-of, using agentsets and passing variables to procedures are important aspects of NetLogo that you need to know about. I have also set up colouring etc so you can see the choices it makes.
extensions [rnd]
patches-own [ nest-scent ]
to setup
clear-all
create-turtles 1 [ set color red ]
ask patches
[ set nest-scent random 100
set plabel nest-scent
]
reset-ticks
end
to go
ask one-of turtles [ move ]
tick
end
to move
set pcolor blue
let targets min-n-of 2 neighbors [ nest-scent ]
let destination rnd:weighted-one-of targets [ calc-weight nest-scent ]
move-to destination
end
to-report calc-weight [ XX ]
let weight 0.5 * (1 + ((exp(XX) - exp( - XX)) / (exp(XX) + exp( - XX))))
report weight
end