我有一个节点和链接的网络。这个图
是对世界的捕捉。该图表示城市的街道。我导入了一个扩展名为 gis 的 shapefile。灰线是链接,黑点是节点,红点代表人。人们前往下一个节点。在街角,红点通过检查链接拥有的变量
popularity
来选择下一条街道。
链接品种有一个变量,
popularity
,我想将其值复制到下面的补丁中。
例如,如果我尝试像这样访问链接下的补丁,将会产生错误
ask links [show [(list pxcor pycor)] of patch-here]
另一种方法可以是从补丁中访问链接变量流行度,但我不知道该怎么做。
我想要这个的原因是因为我想在文件中写入流行度值矩阵,并且它在矩阵中的位置应该与链接在世界中的位置相对应。因此,链接下方的补丁将为我提供矩阵形式。我有一个程序,对于每个补丁,将补丁的值写入文件中。但是,我不知道如何将
popularity
值从链接传递到其下面的补丁。
有什么方法可以将链接拥有的变量复制到其补丁中吗?
问候
如果有人有更好的方法(或者可以简化我的代码),请随意。这是一个完整的工作示例。将其复制到空的 NetLogo 模型中并运行它以查看其工作情况。
setup
过程只是创建一些节点和具有适当测试值的链接,然后调用transfer-link-values过程,这就是我认为你想要的。然后,setup
过程将这些值放入补丁标签中以显示它们并查看结果。
transfer-link-values 过程的工作方式是在链接的一端创建一只乌龟,然后乌龟向链接的另一端移动,同时传输值。当它到达另一端时,乌龟就死了。
patches-own [patch-popularity]
links-own [link-popularity]
to setup
clear-all
create-turtles 10 [ setxy random-xcor random-ycor]
while [ any? turtles with [not any? my-links] ]
[ let to-pair turtles with [not any? my-links]
let thisNode one-of to-pair
ask thisNode
[ create-link-with one-of other to-pair
[ set link-popularity 5 + random 5 ]
]
]
transfer-link-values
ask patches [ if patch-popularity != 0 [set plabel patch-popularity ] ]
end
to transfer-link-values
ask links
[ let start-node one-of both-ends
let this-link self
let end-node nobody
ask start-node [ set end-node [other-end] of this-link ]
let transfer-value link-popularity
ask start-node
[ hatch 1
[ face end-node
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
while [ not member? end-node turtles-here ]
[ forward 1
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
]
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
die
]
]
]
end
参加聚会已经很晚了,但我认为这对于其他有类似问题的人很有用。
我创建了
patches-under-links
报告器,它返回链接交叉的所有补丁。它的工作原理是首先计算定义链接的线性方程,然后检查该线性方程与面片之间的任何边界相交的位置,最后将这些边界交叉点每一侧的面片组合成面片集。
我提供了一个小示例,我用它来计算每个补丁有多少个链接交叉
它还不能用于世界环绕。
patches-own [links-above]
to setup
ca
resize-world -20 20 -20 20
ask patches [set links-above 0]
crt 50 [set size 0.1 setxy random-xcor random-ycor]
ask turtles [create-links-with n-of 3 other turtles [set color red ]]
ask links [ask patches-under-link [set links-above links-above + 1]]
ask patches [set pcolor scale-color grey links-above 0 max [links-above] of patches ]
end
to-report patches-under-link
;A link-reporter
let lcors [list xcor ycor] of both-ends
let lxcor1 item 0 item 0 lcors
let lycor1 item 1 item 0 lcors
let lxcor2 item 0 item 1 lcors
let lycor2 item 1 item 1 lcors
let all-patches (patch-set)
ifelse lxcor1 = lxcor2 [ ;this specific case is separate since it would result in a 0 in the nominator for the slope calculation
set all-patches patches with [pxcor = round lxcor1 and pycor <= max list round lycor1 round lycor2 and pycor >= min list round lycor1 round lycor2]
][
let xdif lxcor2 - lxcor1
let ydif lycor2 - lycor1
let lslope ydif / xdif ; the slope of the linear equation: a
let lintercept lycor1 - lslope * lxcor1 ; the intercept of the linear equation: b
let fx [ x -> lslope * x + lintercept] ; linear equation of the link: y = ax + b
let fy [ y -> (y - lintercept) / lslope] ; linear equation of the link: x = (y - b) / a
let x-intercepts ifelse-value lxcor1 < lxcor2 [(range (round lxcor1 + 0.5) (round lxcor2 + 0.5))] [(range (round lxcor2 + 0.5) (round lxcor1 + 0.5) )]
let y-intercepts ifelse-value lycor1 < lycor2 [(range (round lycor1 + 0.5) (round lycor2 + 0.5))] [(range (round lycor2 + 0.5) (round lycor1 + 0.5) )]
let x-patches patch-set map [x ->
(patch-set patch x (runresult fx x) patch (x - 0.0000001) (runresult fx (x - 0.0000001)))
] x-intercepts
let y-patches patch-set map [y ->
(patch-set patch (runresult fy y) y patch (runresult fy (y - 0.0000001)) (y - 0.0000001))
] y-intercepts
set all-patches (patch-set x-patches y-patches)
]
report all-patches
end