i have this code to crate a turtle of a certain brees (nodi)
create-nodi 1 [
set hidden? true
set x 0
set y 0
set larg lato
set altez lato
set livello n
set media mean [valore] of patches with [ nel-quadrato? x Y larg ]
]
i need to set the variable media as the mean of the variable valore (a patch variable) of the patches that respond positively to the returner nel-quadrato?( a boolean returner ).
this returner should have 3 argumentes (x, y, larg) of the turtle i'm trying to create.
I cannot find a way to express tha the 3 arguments belog to the turtle that i'm creating and not to the patches; in fact i have this error:
you cant use x in patch contex because is turtle only
You could use myself here to refer to the asking turtle:
patches with [nel-quadrato? [x] of myself
[y] of myself
[larg] of myself]
If this seems clunky, another way to do the same thing would be to use let:
let my-x x
let my-y y
let my-larg larg
patches with [nel-quadrato? my-x my-y my-larg]
I also like JenB's suggestion to consider refactoring.
Related
I want to change the mean, lambda, of a poisson process every time ticks advance by one.
Let's say lambda is a predetermined list of the following numbers: [1 2 3 4 5].
If I define lambda in this way, is it possible for NetLogo to loop through each position of the list based on the time step? E.g., at the first tick, NetLogo will set lambda to equal to the first item of the list, such that lambda = 1, when tick = 2, lambda = 2, etc.
Here is an example of what I'm trying to do:
to recruit-spores
ask turtles with [color = red] [
hatch-spores random-poisson lambda ; lambda is what I want to change at every tick
[set shape "dot"
set size 0.01
set color orange
setxy random-xcor random-ycor ]
]
end
Is there a way to change the value of lambda by looping through a list in NetLogo? Either through NetLogo's built-in functionality, or through an extension? Is there a way to do this using the R extension?
Straightforward to do it entirely in NetLogo if you want. The reproducible code below shows how you can use first, lput and but-first to take (in this example, print) the first value of the list and then append it to the end of the list:
globals [
lambda-values
]
to setup
set lambda-values [1 2 3 4 5]
end
to go
print first lambda-values
set lambda-values lput (first lambda-values) lambda-values
set lambda-values but-first lambda-values
tick
end
To put this in your example, it would be:
to recruit-spores
let current-lambda first lambda-values
set lambda-values lput current-lambda lambda-values
set lambda-values but-first lambda-values
ask turtles with [color = red] [
hatch-spores random-poisson current-lambda [
set shape "dot"
set size 0.01
set color orange
setxy random-xcor random-ycor
]
]
end
Alternatively, you can use remainder or mod, in combination with the current value of ticks, to find out the position of the item that needs to be extracted from the list (you can extract it using item). This solution might be in some way less preferable than the previous one, as it is ticks-dependant (as opposed to the previous one).
Another approach: implement a counter variable that is incremented each time that you use a value from lambda-values and that indicates the position of the next item you want to extract, and that is reset to 0 when it reaches the end of the list. This latest solution might be the least preferable because it requires you to create a global variable for the counter.
I'm trying to get turtles (foragers) to move based on a reporter of mean nest-scent, which should report the mean of nest-scent spread throughout the world, from a high of 200 at the nest to 120 at the opposite corners of the world. However, at the nest, the error below is triggered, and I don't understand why. Isn't a mean nest-scent a list of numbers? How do I correct this error?
Can't find the mean of a list with no numbers: [].
error while forager 7 running MEAN
called by procedure MEAN-NEST-SCENT-IN-CONE
to-report mean-nest-scent-in-cone [cone-distance angle angle-width ] ; ant procedure - reports the mean amount of nest-scent in cone
rt angle
let p patches in-cone cone-distance angle-width
;ask p [ show chemical ]
if p = nobody [report 0]
lt angle
report (mean [nest-scent] of p)
end
`````````````````````````````````
You set up 'p' as a patchset but you are testing whether it is 'nobody'. The primitive for agentsets is any?. Try replacing:
if p = nobody [report 0]
lt angle
report (mean [nest-scent] of p)
with
ifelse any? p
[ lt angle
report mean [nest-scent] of p
]
[ report 0 ]
I'm trying to help a student with a NetLogo homework. The gist of the homework is to load images, save the patch values to properties c1 c2 for each patch, set an alpha value 0.0 <= alpha <=1.0 and then set the patches to a linear combination of c1 and c2 under alpha.
This ought to be simple: all I want to do is code (this is pseudocode, I know this doesn't actually work) like:
to combine
ask patches [ set pcolor (c1 * alpha) + (c2 * alpha) ]
end
the problem being that c1,c2 are vectors, alpha is a scalar, and I can't figure out how to do vector multiply and vector addition in NetLogo.
Here's the code to set up a test case:
patches-own [ c1 c2]
to setup
ask patches [ set pcolor one-of [ red green blue] ]
end
to load-image
import-pcolors image
end
to set-one
ask patches [ set c1 pcolor ]
end
to set-two
ask patches [ set c2 pcolor ]
end
to return-c1
ask patches [ set pcolor c1 ]
end
I don't know what your various vectors (or lists really in NetLogo) and other numbers look like. But if the heart of the problem is that you want to multiply a 'vector' and scalar, here is a function that does so:
to-report mult-vec-num [ #vec #num ]
report map [ ii -> ii * #num] #vec
end
The map primitive is essentially an implicit foreach, with the arbitrarily named ii being used as an iterator through the provided list (named #vec) and multiplied by the provided scalar (named #num). Example use case:
show mult-vec-num [ 1 2 3 ] 5
UPDATE:
To add two lists together, again use the map primitive:
let l1 [1 2 3]
let l2 [4 5 6]
show (map + l1 l2)
I setup two different breeds with the breed command:
breed [breeds1 breed1]
breed [breeds2 breed2]
and in go, I ask to a random turtle to execute an action command, like this:
to go
ask one-of turtles [
action
]
end
where action is defined as
to action
ifelse (breed = breeds1)
[
set q random-float 1
set c q
set potential_1 (1 + d) * (1 - c)^(d)
]
[
set c random-float 1
set potential_2 (1 + (1 / d))*(1 - c)^(1 / d)
]
end
For breeds1, q has value in [0,1] and c takes its value.
For breeds2, q has value equal to 0 and c takes random values in [0,1].
Both breeds have d=3 (fixed value).
c,q, and both potentials are global variables.
What I need to do is to plot the two potentials/distributions.
I used plotxy to plot the distributions in the plot code box:
[![enter image description here][1]][1]
What I would like are the following two distributions in the same plot. Plot 1 shows the distribution for breeds1, plot 2 for breeds2.
If I consider potential_1 and potential_2 as turtles-own (the first one for breeds1, the second one for breeds2) I receive the message that I can't use potential_1 in an observer context, because potential_1 s turtle-only. Same for potential_2.
If I consider q and c as turtles-own as following:
breeds1-own
[ q
c
potential_1
]
breeds2-own
[
q
c
potential_2
]
I receive the following error:
You can't use c in an observer context, because c is turtle-only
So my question is: how could I plot the two distributions?
I hope you can help me.
Okay, I can't answer your question using your code as I can't work out the logic of the ask one-of. So, what I have done instead is created a complete model that does the plotting in the hope that will help you work out what's wrong with your code.
Try this:
globals [d]
turtles-own
[ potential
group
c
]
to setup
clear-all
set d 3
create-turtles 100
[ set group one-of ["type1" "type2"]
action
]
reset-ticks
end
to action
ifelse (group = "type1")
[ let q random-float 1
set c q
set potential (1 + d) * (1 - c)^(d)
]
[ set c random-float 1
set potential (1 + (1 / d))*(1 - c)^(1 / d)
]
end
Then you will need the following as the pens in your plot. You will also need to change the plot settings for each pen to 'point' rather than 'line'.
ask turtles with [group = "type1"] [plotxy c potential]
ask turtles with [group = "type2"] [plotxy c potential]
What I have done is create 100 turtles in two groups, with the values of the variables 'c' and 'potential' calculated for each group using your code. But it's all done in a single pass - each turtle does its calculation and then control moves to the next turtle as they are created. Then I have each pen in the plot just plot the values from one group.
The error message you were getting "You can't use c in an observer context, because c is turtle-only" means that you tried to use the variable c without letting NetLogo know which turtle's value of 'c' you wanted to use.
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