我需要创建一些具有给定补丁大小的区域。地区总数为100个。
我从这些数据开始:
一个列表,其组件是具有相同大小的区域的数量(组件的总和必须为100),另一个列表的组件是这些区域的补丁大小。因此,如果L1 =(n1,n2,n3)和L2 =(s1,s2,s3),则意味着n1个区域的补丁大小为s1,依此类推。
所以我开始创建一个包含100个组件的列表,其中第一个n3组件的值等于s3,以下n2个组件的值为s2,以下n1个组件的值为s1。
to setup-world
let plot-distribution (list 9 20 25 15 11 5 5 5 4) ;sum = 100
let plot-patches (list 1 3 7 15 29 49 77 141 392)
let plot-list (list)
(foreach plot-distribution plot-patches [
let aux ?2
repeat ?1 [set plot-list (fput aux plot-list)]
])
...
end
然后我开始一个foreach循环,创建我需要的每个区域。我开始要求一个随机补丁(让我们说)绿色。然后,我将其“邻域”定义为具有黑色的邻居补丁的补丁集。如果邻域中的补丁数量大于我需要着色的补丁数量,那么我会对所有邻域进行着色;如果没有,我只是净颜色较少的补丁。之后,我将新的“邻居”重新定义为旧“邻居”的黑色片段,以继续处理循环。
to setup-world
...
foreach plot-list
[
let counter 1 ;;;;number of patches colored
let max_counter ? ;;;;number of patches of the region
let colorete one-of base-colors ;color
let neighborhood 0 ;;;;start a variable for the patch-set
;;;; Set one random patch with a random color and set a region of
;;;; its neighbours which have black color
ask one-of patches with [pcolor = black]
[
set pcolor colorete
set neighborhood (patch-set neighbors with [pcolor = black])
]
;;;; Start the loop to complete the region
while [counter < max_counter ] ;;;;while the amount of colored patches is smaller than the total size of the region
[
let patch-number (count neighborhood)
;;;; If the number of patches I have to color is greater than the number of patches of "neighbourhood", then color all the patches. Otherwise, ask a smaller group of patches in neighborhood, since it has more patches than those I need to color.
ifelse ( (max_counter - counter) > patch-number )
[ask neighborhood [
set pcolor colorete
set counter (counter + patch-number)
set neighborhood (patch-set (neighbors with [pcolor = black]))
]
][
ask n-of (max_counter - counter) neighborhood [set pcolor colorete]
set counter (max_counter)
]
]
]
...
end
然而,它无法完成循环;它卡住了,我不知道为什么。我尝试创建更简单(以某种方式,重复)的指令,只是为了更“清晰”,但它没用。
你能帮帮我吗?谢谢!