I need to find the minimum distance between patches in front of my agent to a certain patch (goal), in order to select the patch that would create the most optimal (shortest) path. The primitive distance only requires one argument so I can't use it as is for this function.
The distance primitive only requires one argument, yes, but it is a "patch or turtle primitive": it must be run in the context of a particular agent by "asking" it for its distance to another, so you can think of the context in which it run as another argument.
If you want to know the distance between patch 0 0 and patch 1 1, you can write:
ask patch 0 0 [ show distance patch 1 1 ]
or, likely more useful:
[ distance patch 1 1 ] of patch 0 0
Related
I would like to change the minimum coordinate unit of the patch space, which is set to exist only one turtle per patch space, to less than one, and monitor it. We want to set the minimum spatial unit to the same tick size n as in tick-advance (e.g. n=0.1). Here is a piece of code, but it doesn't work. Can anyone help me? Thanks in advance.
globals [n] ; n<=1 i.e. n=0.1
; omitted
ask turtles [forward n]
tick-advance n
I find myself in the same boat as Matteo- I don't know that what you're looking to do is possible / makes sense in the context of NetLogo. The coordinates of patches are fixed (ie, one patch is one unit) but arbitrary (1 in Netlogo can mean 1 m or 1 km, depending on the model). In other words, a patch's coordinates are discrete, while turtles can move around in continuous space. So you can, of course, have a turtle wander around in step sizes of 1/10:
globals [n]
to setup
ca
set n 0.1
crt 10
reset-ticks
end
to go
ask turtles [
forward n
]
tick-advance n
end
After one run of go above, you could conceivably have a turtle at coordinates [xcor = 0.1, ycor = 0.1], although it would still be on patch 0 0 since pxcor values are integers.
It seems that what you are actually needing to do is not coming across as needed- can you edit your question to provide a little more detail / context? Perhaps knowing the why behind your need to model in this way will help askers get you pointed in the right direction. I personally am curious about:
Why you are using tick-advance instead of just tick
How you have implemented your one-turtle-per-patch restrictions- in other words, can you show a Minimal Reproducible Example? That may prompt other ways to approach what you're after.
Here is an example world with time tied to ticks:
globals [ seconds ]
to setup
ca
set seconds 0
resize-world 0 50 0 50
ask patches with [
floor (pxcor / 10) mod 2 + floor (pycor / 10) mod 2 = 1
] [
set pcolor white
]
crt 10
reset-ticks
end
to go
ask turtles [
fd 1
]
set seconds precision (seconds + 0.1) 2
if seconds mod 1 = 0 [print ( word "It has been " seconds " seconds.")]
tick
end
Dear NetLogo Community,
I am aiming to set a variable's value to a number between -1 and 1. I have tried the following code but in vain.
to xyz
[
set probability-of-wom compute-wom [-1 1]
if probability-wom > 0 [...]
]
end
to-report compute-wom -1 1
report -1 + random-float (1 - -1)
end
probablity-wom is a global variable in this case.
Appreciate your support in advance.
Thank you.
Regards,
Shreesha
Shreesha
Let's say you want to generate a random number between lower and upper. Then your compute-wom would be
to-report compute-wom [lower upper]
report upper - random-float (upper - lower)
end
In your case, you would
set probability-of-wom compute-wom -1 1
But a couple of comments. First, what you are generating here is a random number between two limits (as your title suggests), so calling it a probability could be misleading to anyone reading your code. Probabilities will normally be in the range from zero to one. If you really are just looking to do something with a 50% probability, you can simply say
if random-float 1 >= 0.5 [...]
Second, reporters should generally take variable arguments if they are to have arguments at all. Note that since you hard code -1 and 1 in the body of your to-report compute-wom reporter, passing them as arguments is redundant, and possibly misleading to anyone reading your code.
I want to add the values of a variable ( let's say transportation cost) for specific turtles which are in a group. The transportation cost depends on the distance between a specific patch and the location assigned to the turtle. As the value of the said cost is different for each turtle, I want to sum the total cost for turtle which are in a group. To clarify, let's suppose there are 7 turtles in total and only 4 are in a group.
The values of transportation cost of each turtle is assigned as tcost.
to calculate-ttcost
set ttcost 0
let cnt 0
ask turtles [
if in-group? [
set ttcost (tcost + tcost)
set cnt cnt + 1
]
]
end
With the correction of one typo, the code you have should work, assuming that tcost is declared as a turtles-own variable with that turtle's transportation cost (or a reporter that gives the transportation cost for the turtle that calls it), and assuming that those turtles that are in the group in which you are interested have their turtles-own variable in-group? set to true. The typo is in the line
set ttcost (tcost + tcost)
which should be
set ttcost (ttcost + tcost)
But there is a more straight-forward coding that will accomplish the same task.
let cnt count turtles with [in-group?]
let ttcost sum [tcost] of turtles with [in-group?]
with limits the set of turtles to those for which in-group is true. of creates a list of the values of tcost for each of those turtles, which can then be summed up.
In NetLogo: suppose the model has
a turtle (0) of breed A with undirected links with 3 turtles (1, 2 and 3) of breed B;
the turtle 0 has an attribute named "number-of-links" that equals 3.
Now, let one of the 3 neighbors of 0 dies..
How can I program turtle 0 to change its number-of-links automatically to 2?
If all you want is a way of keeping track of the number links, use count my-links instead of a custom variable.
In general, the least bug prone way of having a value update when the number of links changes is to compute that value when you need it. For number of links, this is simply count my-links. For more complicated things, wrap them in a reporter:
to-report energy-of-neighbors
report sum [ energy ] of link-neighbors
end
If this doesn't work for whatever reason (agents need to react to a link disappearing or you're seeing a serious, measurable performance hit from calculating on the fly), you'll have to make the updates yourself when the number of links change. The best way to do this is to encapsulate the behavior in a command:
to update-on-link-change [ link-being-removed ] ;; turtle procedure
; update stuff
end
and then encapsulate the things that can cause the number of links to change (such as turtle death) in commands as well:
to linked-agent-death ;; turtle procedure
ask links [
ask other-end [ update-on-link-change myself ]
]
die
end
I am trying to find if there is a turtle on patch-ahead n
whose speed - acceleration is <= 0. The code I came up with is:
if any? turtles on patch-ahead n with [speed <= (speed - acceleration)]
but this gives an error that:
patch-ahead expects a number, instead got agent set.
How do I remedy this?
n is a number variable. I want to access the turtle's 'speed', which is a user defined turtle-own variable, at the nth patch from the calling turtle. The command 'with' doesn't work here. Please suggest an alternative to access the speed of the turtle at, say, the 3rd patch from the calling turtle.
If you look at the patch-ahead documentation you will notice that it does require one argument: a number representing the distance to look ahead. You are using a patch 'n' instead of a number.
As per you comment, I think maybe you want turtles-on, and use parenthesis to make it clearer, as such:
if any? ((turtles-on patch-ahead n) with [speed <= (speed - aceleration)])
In the above I am assuming that n is a number: the distance you want to look ahead.