Netlogo 模型非常慢并且在每个 tick 之间有延迟
Netlogo model is extremely slow and has a delay between each tick
我创建的模型非常慢。我是 Netlogo 的初学者,不确定是我的代码效率低下还是我的系统配置有问题。
我有一个 35 x 35 的网格,我的大部分操作都是基于补丁的,即每个补丁都必须找到最近和最远的海龟以及它们的距离,并基于此执行其他操作。此外,在基于 if-else 逻辑的每个 tick 中,不满足条件的补丁使用 scale-color 函数通过渐变变为白色,而满足条件的补丁必须采用最近的海龟的颜色。
我可以post代码但不想垃圾邮件,请指教。谢谢
下面的一段代码似乎有问题。
to update-support
ask patches [
set closest-party min-one-of parties [distance myself]
set closest-party-dist [distance myself] of closest-party
set farthest-party max-one-of parties [distance myself]
set farthest-party-dist [distance myself] of farthest-party
set f 0
set h 0
set f ( -1 / ( [my-old-size] of closest-party / sum[my-old-size] of parties )) * (closest-party-dist ^ 2)
set h ( [my-old-size] of farthest-party / sum[my-old-size] of parties ) * (farthest-party-dist ^ 2)
set b-c (f + h)
ifelse (b-c <= threshold)
[ set votes-with-benefit 0 set pcolor scale-color white citizen-share 0 max-citizen-share ]
[ set votes-with-benefit citizens set pcolor scale-color ([color] of closest-party) citizen-share 0 max-citizen-share]
]
ask parties [ set my-size sum [votes-with-benefit] of patches with [closest-party = myself]
;set my-benefit mean[b] of patches with [closest-party = myself]
set my-benefit-chen mean[b-c] of patches with [closest-party = myself]
]
set largest-party max-one-of parties [my-size]
end
参与方的数量是动态的 - 可以在 2 到 10 之间。更新支持模块在设置和运行中都被调用。下面是代码:
to setup
clear-all
setup-citizens
setup-parties
update-support
setup-plot
reset-ticks
end
to go
ask parties [ adapt set my-old-size my-size ]
update-support
plot-voter-support
plot-voter-turnout
tick
end
问候
尤瓦拉吉
第一个简单的修复,如果你多次使用补丁集,创建一次然后调用它。所以:
ask parties [ set my-size sum [votes-with-benefit] of patches with [closest-party = myself]
set my-benefit-chen mean[b-c] of patches with [closest-party = myself]
]
变成(并且更容易阅读,如果您稍后更改条件,也能确保一致性)
ask parties
[ let my-patches patches with [closest-party = myself]
set my-size sum [votes-with-benefit] of my-patches
set my-benefit-chen mean[b-c] of my-patches
]
以类似的方式,你在代码中有两次sum[my-old-size] of parties
(计算f和h),你实际上一遍又一遍地计算它,因为你要求每个补丁运行这段代码.你应该计算一次然后使用它:
to update-support
let old-total sum [my-old-size] of parties
ask patches
[ ...
set f ( -1 / ( [my-old-size] of closest-party / old-total )) * (closest-party-dist ^ 2)
set h ( [my-old-size] of farthest-party / old-total ) * (farthest-party-dist ^ 2)
set b-c (f + h)
...
]
...
end
这些变化能带来多大的改善主要取决于参与方的数量。请尝试一下,看看是否解决了问题。
我创建的模型非常慢。我是 Netlogo 的初学者,不确定是我的代码效率低下还是我的系统配置有问题。
我有一个 35 x 35 的网格,我的大部分操作都是基于补丁的,即每个补丁都必须找到最近和最远的海龟以及它们的距离,并基于此执行其他操作。此外,在基于 if-else 逻辑的每个 tick 中,不满足条件的补丁使用 scale-color 函数通过渐变变为白色,而满足条件的补丁必须采用最近的海龟的颜色。
我可以post代码但不想垃圾邮件,请指教。谢谢
下面的一段代码似乎有问题。
to update-support
ask patches [
set closest-party min-one-of parties [distance myself]
set closest-party-dist [distance myself] of closest-party
set farthest-party max-one-of parties [distance myself]
set farthest-party-dist [distance myself] of farthest-party
set f 0
set h 0
set f ( -1 / ( [my-old-size] of closest-party / sum[my-old-size] of parties )) * (closest-party-dist ^ 2)
set h ( [my-old-size] of farthest-party / sum[my-old-size] of parties ) * (farthest-party-dist ^ 2)
set b-c (f + h)
ifelse (b-c <= threshold)
[ set votes-with-benefit 0 set pcolor scale-color white citizen-share 0 max-citizen-share ]
[ set votes-with-benefit citizens set pcolor scale-color ([color] of closest-party) citizen-share 0 max-citizen-share]
]
ask parties [ set my-size sum [votes-with-benefit] of patches with [closest-party = myself]
;set my-benefit mean[b] of patches with [closest-party = myself]
set my-benefit-chen mean[b-c] of patches with [closest-party = myself]
]
set largest-party max-one-of parties [my-size]
end
参与方的数量是动态的 - 可以在 2 到 10 之间。更新支持模块在设置和运行中都被调用。下面是代码:
to setup
clear-all
setup-citizens
setup-parties
update-support
setup-plot
reset-ticks
end
to go
ask parties [ adapt set my-old-size my-size ]
update-support
plot-voter-support
plot-voter-turnout
tick
end
问候 尤瓦拉吉
第一个简单的修复,如果你多次使用补丁集,创建一次然后调用它。所以:
ask parties [ set my-size sum [votes-with-benefit] of patches with [closest-party = myself]
set my-benefit-chen mean[b-c] of patches with [closest-party = myself]
]
变成(并且更容易阅读,如果您稍后更改条件,也能确保一致性)
ask parties
[ let my-patches patches with [closest-party = myself]
set my-size sum [votes-with-benefit] of my-patches
set my-benefit-chen mean[b-c] of my-patches
]
以类似的方式,你在代码中有两次sum[my-old-size] of parties
(计算f和h),你实际上一遍又一遍地计算它,因为你要求每个补丁运行这段代码.你应该计算一次然后使用它:
to update-support
let old-total sum [my-old-size] of parties
ask patches
[ ...
set f ( -1 / ( [my-old-size] of closest-party / old-total )) * (closest-party-dist ^ 2)
set h ( [my-old-size] of farthest-party / old-total ) * (farthest-party-dist ^ 2)
set b-c (f + h)
...
]
...
end
这些变化能带来多大的改善主要取决于参与方的数量。请尝试一下,看看是否解决了问题。