如何跨 netlogo 列表中的项目实现数值公式

How to implement a numerical formula across the items in a netlogo list

我必须使用 Lists 在 netlogo 中做一些操作。虽然我可以用它们完成简单的任务,但我还不够熟练,无法编写我当前的需求。

我有一个场景,其中海龟有称为 Current-AgePrevious-Age 的变量。而龟不遇某某threshold.

可生可死

我想为每个补丁实现以下公式。

Best-list = (-1/Previous-Age) * (Distance between Patch & Turtle) for all the turtles

Best = Min [ Best-list]

我知道所涉及的步骤,但未能成功编码。以下是步骤:

  1. 创建一个包含所有当前存活的海龟的列表
  2. 创建第二个列表,其中包含 Previous-Age
  3. 创建第三个列表,其中包含单个补丁和每只活海龟之间的距离
  4. 然后使用 Best-List 公式的输出为列表中的所有海龟创建另一个列表
  5. 最后在列表中找到Min值,并将具有最小值的海龟的name/who#存储在一个名为Best-Turtle
  6. 的单独变量中

这是我试过但没有用的代码。

set turtle-list (list turtles)
set turtle-age-list n-values length(turtle-list) [0]
set turtle-patch-dist-list n-values length(turtle-list) [0]
set best-list n-values length(turtle-list) [0]

ask patches[
foreach turtle-list(
set turtle-age-list replace-item ?1 turtle-age-list Previous-Age of turtles with [turtle= ?1]
)
]

我无法继续下一步,因为上面的代码本身不正确。

在此先感谢您对代码的帮助。

此致

首先,列表可能不是执行此操作的最简单方法。但是,如果您出于某种原因必须使用列表,我认为您所要求的是可能的。我不太确定你的意思是最好的——你是想让每个补丁评估哪只乌龟是该补丁的最佳乌龟,并将该变量存储在全局列表中?我假设这就是你的意思,但如果我误解了,我认为你可以根据你的需要调整我在这里所做的。

首先,传递给 foreach 的任何列表的长度必须相同。所以,既然你打算这样做 per-patch,请确保每个补丁都调用列表创建过程,而不仅仅是检查列表。接下来,查看 n-values 的字典 - 报告者的语法意味着你需要使用你试图接收的报告者 - 使用 n-values length(turtle-list) [0] 只会给你一个长度相同的零列表作为海龟的数量。

所以每个补丁都需要创建这些列表 - 确保您为列表变量定义 patches-own,或者只使用 let 在过程中定义列表。您将需要一个有序海龟列表、它们以前的年龄以及从调用该过程的补丁到每只海龟的距离。接下来,您可以创建一个列表,根据您的公式生成一个值。然后,您可以使用 position 原语在您的 formula-generated 列表中找到最小值的位置,并使用它来索引海龟 该值。

它可能看起来像

to numerical

  set best-turtle []

  ask patches [
    let turtle-list (sort turtles)  ;;; list of sorted turtles
    let turtle-prev-age-list n-values length(turtle-list) [ [i] -> [pre_age] of turtle i ] ;;; list of previous ages of turtles, in same order as above
    let turtle-patch-dist n-values length(turtle-list) [ [i] -> distance turtle i ] ;;; list of distance from this patch to each turtle, in same order
    set best-list n-values length(turtle-list) [ [i] -> ( ( -1 / ( item i turtle-prev-age-list ) ) * ( item i turtle-patch-dist ) ) ]  ;;; list of calculated values for each turtle
    let best-position position (min best-list) best-list  ;;; gets the index of minimum value
    set best-turtle lput item best-position turtle-list best-turtle ;;; adds the best turtle for this patch to the global list of best turtles
  ]

end

以上过程假设你的海龟有一个 pre_age 变量,补丁有一个 best-list 变量,每个补丁的列表 'best turtle' 保存在全局变量 best-turtle。从那里,您可以使用 foreach 要求列表中的海龟做某事。请注意,如果乌龟的前一年龄为 0,则会出现除以零的错误。

turtles-own [age previous-age]

to-report evalfrom [_patch]
  report  (- distance _patch) / previous-age
end

to test
  ca
  crt 25 [
    set age (10 + random 75)
    set previous-age age - random 10
  ]
  print min-one-of turtles [evalfrom (patch 0 0)]
end