我创建的模型非常慢。我是Netlogo的初学者,不确定我的代码效率低下还是系统配置问题。
我有一个35 x 35网格,我的大多数操作都是基于补丁的,即每个补丁必须找到最近和最远的乌龟以及它们的距离,并根据它进行额外的操作。此外,在基于if-else逻辑的每个tick中,不满足条件的补丁使用scale-color函数变为白色,而满足条件的补丁必须采用最接近的颜色龟
我可以发布代码但不想发送垃圾邮件,请提供建议。感谢。
以下代码似乎有问题。
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和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
此致 Yuvaraj
答案 0 :(得分:2)
第一个简单修复,如果您多次使用补丁集,请创建一次然后调用它。所以:
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
这些变化的改善程度主要取决于参与方的数量。请尝试一下,看看它是否解决了这个问题。