Move agent based on probability/point attribute value and point distance, NetLogo - netlogo

I have the set of points implemented in netlogo and agents are moving from one point to another. Each point has a weight (number approximately between 0 and 9, its not a probability). What I want to made is a simple rule.
I want to give all points probability of visit by the value of weight.
So the next point which will be visited by agent should be calculated by the probability based on point weight and the closeness point (more close point - bigger probability), but that closeness isn't so much big factor as the point weight. For example, I would like to set in formula that closeness is twice lower factor then point weight.
I investigated rnd extension, but I am not sure how to append probabilities to points which I am having a lot (approximately around 250 points).

You're on the right track with the rnd extension. From that extension you need the weighted-one-of primitive and you just put the formula into the reporter block.
I think this is something like what you want. It's a complete model so you can run it and see what it does. The reporter block uses the weight and the distance in the probability. Since you want the probability to be larger for closer, then I have used the inverse of the distance, but you could simply subtract the distance from something like the maximum distance in the model. You will also need an appropriate scaling factor (replacing the 10 in my example) so that the weight is worth twice an average value of closeness.
extensions [rnd]
turtles-own [weight]
to testme
clear-all
create-turtles 10
[ setxy random-xcor random-ycor
set weight 1 + random 3
set size weight
set color blue
]
ask one-of turtles
[ set color red
let target rnd:weighted-one-of other turtles [ 2 * weight + 10 / distance myself ]
ask target [ set color yellow ]
]
end

Related

why are my results from behaviorspace (netlogo) so inconsistent?

I am somewhat new to Netlogo, and have been scratching my head over some of the results I get from behaviorspace. I have been playing with the wolf-sheep predation model, and changing their movement based on what color patch they are in. There is a single wolf and a single sheep, and what I want to measure is the number of time steps it takes for the wolf to eat the sheep. The patches are colored randomly, based on some proportion from 0-100, as below:
to color-patches
let total 100
let p-red slider1 / total
let p-green total - p-red
ask patches [
let x random-float 1.0
if x <= p-red + p-green [set pcolor green]
if x <= p-red [set pcolor red]
]
end
The issue I am having is when I set the movement of the wolf and the sheep to move independently of the patch color:
ask sheep [
set heading random 360
fd 1
]
ask wolves[
set heading random 360
fd 1
eat-sheep
]
My expectation is that the mean and standard error for the number of time steps until the wolf eats the sheep should be pretty similar regardless of how many red patches and how many green patches there are, since their movement is not affected by it. I ran it in behaviorspace, with 1000 iterations per 10% increase in proportion of red patches (from 0 - 100%). However, I keep getting results that look kind of like this:
enter image description here
Basically the means+se are all over the place. Every time I run it, they are distributed differently (but same grand mean). This is particularly odd since when I introduce any sort of patchcolor-specific behavior for either the wolf or the sheep, I get very clear patterns with less variation.
Any ideas what might be going on here? The only thing I could think of is that relative starting position of each is pretty important (but each is placed at random xy coords). I assumed that in behaviorspace for each iteration for a given set of parameters, it would run through all the code (thus generating a new random landscape and new random starting points for the wolf and the sheep for each of the 1000 runs per parameter combination). Does behaviorspace maybe take the first landscape and starting coordinates for each turtle and use them for each of the 1000 iterations per parameter combination?
Thanks!
Now I think you may not have a mistake but instead are just misinterpreting your graph. The Y axis on the graph you posted ranges only from 2000 to 2200; if you set the Y axis scale to 0 to 2500, the results from each experiment would look very similar to each other.
The difference in mean between your results (~2100) and my results (~3100) is probably just due to different world sizes. I presented standard deviation while you graphed standard error.
If you histogram the results, they seem to follow an exponential distribution.

Compute weighted mean of headings

I have each patch holding two lists: directions and magnitudes. Both lists contain values that are computed based on nearby turtles:
directions contains the headings from the patch to the various turtles (so basically it is just the turtle's heading rotated by 180°);
magnitudes contains a value that is directly proportional to the turtles' mass and inversely proportional to the distance between the patch and the turtles;
The items of the two lists are coupled in their order, i.e. the first item of directions and the first item of magnitudes are computed based on the same turtle, the two second items are computed based on another turtle and so on.
What I want to achieve is to come up with two single patch-own values, my-direction and my-magnitude, representing in some way the weighted average of directions, where the weights are magnitudes.
To put it another way, I am thinking of this in terms of vectors: turtles are exerting on the patch a force that can be represented as a vector, always pointing in the direction of the turtle and with a certain intensity (the magnitude). The resulting vector (represented by my-direction and my-magnitude) should be the resulting average of these forces.
I have seen this question. It does not address the issue of a weighted average; however it mentions the concept of circular mean. I've delved into it a bit, but I'm not sure how to apply it to my case and even if to apply it: does it still apply even with the formulation of the problem in terms of vectors?
I've seen this question/answer on SE Mathematics where it is said that the average vector can be found by averaging x- and y-coordinates of the initial vectors. In my case, ideally all the pairs of values in the two lists form a different vector with origin in the patch at issue, with heading found in directions and length found in magnitude. I suspect I can find the coordinates of each vector by multiplying its sine and cosine by its magnitude, however at this point I'd use some guidance as I might be overcomplicating things (either from the maths perspective or the NetLogo perspective).
This one below is a reduced version of the code that brings to the point where target-patches (not focusing on all patches, in order to make it quicker) have the two lists populated.
globals [
target-patches
]
turtles-own [
mass
reach
]
patches-own [
my-direction
my-magnitude
directions-list
magnitudes-list
]
to setup
clear-all
set target-patches n-of 10 patches
ask target-patches [
set directions-list (list)
set magnitudes-list (list)
set pcolor yellow + 3
]
create-turtles 10 [
move-to one-of patches with [(not any? turtles-here) AND (not member? self target-patches)]
set mass (random 11) + 5
set reach (mass * 0.8)
set size (mass / 8)
set shape "circle"
]
populate-lists
end
to populate-lists
ask turtles [
let relevant-targets (target-patches in-radius reach)
ask relevant-targets [
set directions-list lput (towards myself) (directions-list)
set magnitudes-list lput (magnitude-based-on-distance) (magnitudes-list)
]
]
end
to-report magnitude-based-on-distance
report [mass] of myself / (distance myself * 1.2)
end
Your initial instinct is right. You can do something like
LET my-dx mean (map direction magnitude [ theta scalar -> scalar * sin theta ])
(I may have that map and anonymous syntax wrong please edit)
And do the same for my-dy using cos (edit: or maybe negative cos?)
patch-at my-dx my-dy is one way of getting the patch.
You can also do (atan my-dx my-dy) to gets the new direction
And distancexy my-dx my-dy to get the magnitude
Don’t remember if patch can use distancexy or patch-at, but hopefully so. Otherwise you have to use a helper turtle, and do the math yourself.
I think I did a orbital mechanics toy model once that did something like this. It’s hiding on turtlezero.com.

Netlogo: How to plot degree of nodes vs. average "reward" of turtles with that degree?

I am building a model that tries to simulate a network-based market. In this model the turtles/nodes get a reward called points, which is a turtles-own variable.
I am now trying to plot a graph of the degree of the nodes against the average number of points that nodes with a given degree have. I have attempted to do this by creating a plot from the interface tab but I cannot manage to make this work.
Here are images of the windows of the plot settings.
Anybody know how can I make this work?
Also, I keep getting these "Runtime error: Can't find the maximum of an empty list" in all the plots/histograms I create. It is not a big deal at the moment as they seem to work fine, however if you know why these appear please let me know!
Thanks beforehand,
Carlos
For simplicity and to avoid overloading your plot setup, I like to use to-report procedures for things like this. As a quick example try this setup:
turtles-own [ points degree ]
to setup
ca
crt 50 [
set degree 5 + random 5
set points random 10
setxy random-xcor random-ycor
]
reset-ticks
end
Make a to-report each for a list of existing degrees, the average points of turtles that have each degree, and the maximum of those average point values:
to-report degrees-list
report sort remove-duplicates [degree] of turtles
end
to-report avg-points-list
let avg-list map [ i ->
mean [points] of turtles with [ degree = i ]
] degrees-list
report avg-list
end
to-report max-avg
report precision ( max avg-points-list + 1 ) 2
end
In this example, degrees-list reports [ 1 2 3 4 5 ], avg-points-list reports something like [6.5 3.9285714285714284 6 3.75 4.2], and max-avg reports something like 7.5- note that of course the exact values will vary since the setup is random.
Now, you can set up your plot window:
The actual plotting is handled by the foreach primitive in the plot pen, which uses plotxy to plot the point value in avg-points-list against the corresponding value in degrees-list. Should give a plot that looks something like:
Hope that's sort of what you're after!

how to calculate the distance of moving vehicles is how much?

I want to determine the distance the vehicle traveled for comparison with other values, I should use the command / function what to calculate.
for example in the picture, I want to use the function to determine the distance d1, after one-time drive, the distance will be the last .... d2 distance riding is dn
I'm not sure if I understand your question. Perhaps JenB's comment is better for you. But here's a different kind of answer:
A simple way for a turtle to keep track of how far it has traveled is shown in this small example program:
turtles-own [traveled]
to example
clear-all
create-turtles 1
ask turtles [
repeat 5 [
let delta random-float 1.0
fd delta
set traveled traveled + delta
]
print traveled
]
end
Basically, every time the turtle moves, you add the amount it moved to a turtle variable.
This assumes you are using forward to move the turtle. If you are moving the turtle using some other method like setxy or move-to, then you will need different code.

Netlogo: can I set the distance between turtles?

Netlogo: can I set the distance between turtles?
Hello,
I’m trying to create a model in which on each tick a turtle randomly chooses another turtle as a partner, and jumps to a specified distance of their partner (the distance that it’s given is based on a probability). It does not matter where it moves to, as long as the turtles are the specified distance apart.
I have tried to model this by creating a ‘jump-with-probabilities’ procedure, and defining distance the turtle jumps in the two ‘IID’ procedures:
to jump-with-probabilities ;; adds behaviours depending on how a random number compares with the odds.
ask turtles [
let random-fraction
random-float 1.0
if-else random-fraction <= 0.4
[ IID_10 ]
[ IID_50 ]
]
end
to IID_10
ifelse distance partner >= 10 ;; if the distance to their partner is larger than or equal to 10
[ jump (distance partner - 10) ] ;; TRUE - jump forward by the difference of distance partner & 10, so that the distance is now 10
[ jump (-1 * (10 - distance partner)) ] ;; FALSE - jump backward by the difference of distance partner & 10, so that the distance is now 10
end
to IID_50
ifelse distance partner >= 50 ;; if the distance to their partner is larger than or equal to 50
[ jump (distance partner - 50) ] ;; TRUE - jump forward by the difference of distance partner & 10, so that the distance is now 50
[ jump (-1 * (50 - distance partner)) ] ;; FALSE - jump backward by the difference of distance partner & 10, so that the distance is now 50
end
The problem with using this is that the distances between the turtles in the end are not the same as the distances that I specified. For example, Turtle 0 may jump towards Turtle 5 so that their distance is the specified 20. But, Turtle 5 will also jump towards its partner, which will change the distance between Turtle 0 and Turtle 5. I considered using ‘ask-concurrent’ instead of ask, but the problem remains, because I am telling the turtles to move a certain distance, rather than to move to a certain distance of their partner.
So my question is; is there a way that I can tell a turtle to be within a specified distance of another turtle? So that if the partner moves the turtle will move too to keep the distance at the specified length.
I thought it may be possible to use ‘move-to’ and add the specified distance somehow. Or alternatively, use ‘distance’ to set this between 2 turtles. It seems rather basic, but I have not been able to figure out how to do it!
Any help would be much appreciated!
There's possibly a better way, but I would do this by moving turtle B to where turtle A is (move-to turtleA), then giving it a random heading (set heading random 360) then moving it forward 10 (forward 10). You could also hide turtle B until you have finished moving it and then unhide it to make the visualisation neater. That sets up the relative position, then use Alan's suggestion of tie to hold the relative position.