问题
As a toy, it works well but obviously, when It scales up it bogs down. How can I do this system without asking turtles to ask the other turtles? the code is thus.
to go
ask turtles
[
ask other turtles [
set heading towards myself
let D distance myself
let C .1 / D - 1 / (D ^ 2)
if C > 1 [set C 1]
fd C
]
]
tick
end
I should know how to do this but the brain is not working. I will race y'all to the answer and post my own if I get it first.
回答1:
You're doing essentially an N-body simulation. The main difference is that you don't have velocity in your system. Unfortunately, there is no simple way to simulate this system without asking every turtle to ask every other turtle. There are a number of more complicated algorithms however.
The Barnes-Hut divides the space up into a tree of embedded regions and calculates the center of mass of each region. Then, when a turtle is determining where to go, it looks at the regions instead of the individual turtles. If you're familiar with O-notation, it runs in O(n log(n))
. It should be possible to modify Barnes-Hut to work with your equation.
I don't believe particle mesh methods could be used in your model, but, to be honest, I don't fully understand how they work.
I notice that the denominator in your equation (.1 / D - 1 / (D ^ 2)
which is .1 / (D - 1 * (D ^ 2)
) is cubic in D
, which means it will drop off pretty fast. So you could limit the turtles by radius: ask other turtles in-radius 5 ...
. in-radius
runs in time proportional to the number of patches in the radius (plus the number of turtles in the radius), so if that number is significantly smaller than the total number of turtles, this can result in a decent speedup.
回答2:
Were you thinking of this?
turtles-own [m]
to setup
ca
crt 1000 [
setxy random-xcor random-ycor
set shape "dot"
set color white
set m random-float 5
set size 2 * (m / pi) ^ .5
]
end
to go
ask turtles
[
set heading weighted-mean-heading other turtles
let mv sum [m / (distance myself ^ 2) / [m] of myself ] of other turtles
if mv > .1 [set mv 1]
if mv < -.1 [ set mv -1]
bk mv
]
ask turtles
[
ask other turtles in-radius (size / 4) [ask myself [set m m + [m] of myself] die ]
set size 2 * (m / pi) ^ .5
]
end
to-report weighted-mean-heading [turts]
let mean-x sum [sin towards myself * m / (distance myself ^ 2)] of turts / sum [m / distance myself ^ 2] of turts / m
let mean-y sum [cos towards myself * m / (distance myself ^ 2)] of turts / sum [m / distance myself ^ 2] of turts / m
report atan mean-x mean-y
end
来源:https://stackoverflow.com/questions/34749916/need-help-on-speeding-up-a-particle-system-or-how-to-ask-turtles-not-to-ask-oth