【问题标题】:Move turtle to a patch with some neighbors of same type of turtle and stay there将海龟移动到具有相同类型海龟的一些邻居的补丁并留在那里
【发布时间】:2020-03-02 11:31:00
【问题描述】:

我试图将一只乌龟移动到一块有 2 只与它的邻居具有相同类型(例如收入)的乌龟的地方并留在那里。我做了以下代码

 to set-move
let target []

 ask turtles with [income = "low"]
 [ let potential-target1 patches with [value < buymiddle and any? turtles-here = false]
   set target potential-target1 with [length remove-duplicates [any? turtles-here with [income = "low"]] of neighbors = 2]
   set target min-one-of potential-target1 [value]
    if target != nobody and any? turtles-on neighbors 
    [ move-to target ask patch-here [set empty false]]]

但它似乎不起作用。一些海龟在选择了一个补丁后仍然会四处走动。有些海龟不会选择一个有两个邻居的补丁。 如何指定具有某些海龟组的两个邻居的补丁?

breed [agens agen]

patches-own [value
empty]
turtles-own [income
            myHouses
            ]
to setup
ca


 ;;Check inputs
 let total-prob prob-low + prob-mid + prob-high
 if (total-prob != 100 )
 [
     print (word "Adoption types must sum to 100, but instead sum to " total-prob)
     stop
 ]

   ask patches [set value random-normal 10 3]

   ask patches [ifelse (value < 8)[ set pcolor green  ]
 [ifelse (value < 11)[ set pcolor blue]
[if value < 15[ set pcolor gray]]]]



end

to go
 ask patches [
 if random 100 < 3 [sprout-agens 1 [set color red
     set shape "default"
     set size 1
     set-income
     set-move]]]

 end

 to set-move
let target []

 ask turtles with [income = "low"]
 [ let potential-target1 patches with [value < buymiddle and any? turtles-here = false]
   set target potential-target1 with [length remove-duplicates [any? turtles-here with [income = "low"]] of neighbors = 2]
   set target min-one-of potential-target1 [value]
    if target != nobody and any? turtles-on neighbors 
    [ move-to target ask patch-here [set empty false]]]


ask turtles with [income = "middle"]
 [ let potential-target2 patches with [(value > buymiddle and value < buyhigh) and any? turtles-here = false]
   let target2 potential-target2 with [length remove-duplicates [any? turtles-here with [income = "middle"]] of neighbors = 2]
   set target2 min-one-of potential-target2 [value]
    if target2 != nobody and any? turtles-on neighbors 
    [ move-to target2 ask patch-here [set empty false]]]


ask turtles with [income = "high"]
 [ let potential-target3 patches with [(value > buyhigh) and any? turtles-here = false]
   let target3 potential-target3 with [length remove-duplicates [any? turtles-here with [income = "high"]] of neighbors = 2]
   set target3 min-one-of potential-target3 [value]
    if target3 != nobody and any? turtles-on neighbors 
    [ move-to target ask patch-here [set empty false]]]



end

to set-income
 let kind-prob (random 100)
 let cumulative-prob prob-low
 ifelse (kind-prob < cumulative-prob)[set income "low" set color red]
 [set cumulative-prob cumulative-prob + prob-mid
 ifelse (kind-prob < cumulative-prob)[set income "middle" set color pink ]
    [set cumulative-prob cumulative-prob + prob-high
      if income < cumulative-prob [set income "high" set color blue]
]]
end

【问题讨论】:

    标签: netlogo move neighbours


    【解决方案1】:

    让我们看看您的第一个代码段的ask 块中的第一行。

    let potential-target1 patches with [value < buymiddle and any? turtles-here = false]
    

    相同
    let potential-target1 patches with [value < buymiddle and not any? turtles-here]
    

    这样你的potential-target1 补丁集就没有海龟了。这将使后续行无关紧要。但是可以说我们做了那条线

    let potential-target1 patches with [value < buymiddle and any? turtles-here]
    

    在下一行,

    set target potential-target1 with [length remove-duplicates [any? turtles-here with [income = "low"]] of neighbors = 2]
    

    [any? turtles-here with [income = "low"]] of neighbors 产生一个包含八个真/假值的列表,如果相邻的补丁有任何带有income = low 的海龟,则为真,否则为假。然后减少该列表中的重复项,并最终得到一个 [true](如果全部为真)、一个 [false](如果全部为假)或一个真假 [true false][false true]如果有些是真的有些是假的。然后,您查看该简化列表中的条目数,并将其与 2 进行比较。当至少一个邻居有这样的海龟而至少有一个没有时,就会发生这种情况。我怀疑这不是你想要的。如果你想要两个相邻的补丁至少有一个 income = low 的乌龟,那么就像

    count neighbors with [any? turtles-here with [income = low]] = 2
    

    应该这样做。另一方面,如果您希望邻居正好有两只乌龟income = low,那么您会想要

    neighbors with [count turtles-here with [income = low] = 2]
    

    我不清楚你在追求哪一个。

    看到下面的问题后,我推测 Dudi 正在寻找第一个解释。如果是这样,那么在所有候选者中找到具有最低value 的补丁将是(就像他们在原始代码中所做的那样)

    let potential-targets patches with [count neighbors with [any? turtles-here with [income = low]] = 2]
    let target min-one-of potential-targets [value]
    

    【讨论】:

    • 谢谢。你的解释真的很有帮助。然而,除了邻居条件之外,潜在补丁也应该在其他潜在补丁中具有最低值。因此我认为我需要使用“列表”来做到这一点。所以列出可能的补丁,它的邻居至少有 2 个补丁具有相同类型的代理。之后,从列表中选择一个值最小的补丁。但是,我仍然不明白该怎么做。
    • 我在回答中添加了一些内容,以(希望)解决您的问题。
    • 谢谢查尔斯。我根据您的解释修改了一些代码。真的很有帮助。
    • 我还有一个问题stackoverflow.com/q/60540292/4915309。如果你有什么想法,请。
    猜你喜欢
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多