如何将动态记忆分配给海龟,以了解其在 Netlogo 中的一组补丁/代理的运动?

问题描述 投票:0回答:1

我有两个品种的买家和卖家。 (1) 每个买家都会寻找最近的卖家并前往那里。 (2) 买家进行交易,然后寻找下一个最近的卖家并前往那里,依此类推。我尝试使用卖家的属性“访问过?”,买家访问过卖家吗? 设置代码:

breed [ buyers buyer ]
buyers-own [ target ]
patches-own [ visited? ]
to setup
  clear-all
  ask patches [ set visited? false ] 
  repeat 4 [
  ask one-of patches [   ;;used patches as sellers 
    set pcolor red  
    set visited? true 
  ]
]
  create-buyers 1 [
  set shape "person"  
  move-to one-of patches 
]

  reset-ticks
end

我已经尝试过问题的第一部分,像这样“去最近的卖家”,

to go-to-nearest-seller
  ask buyers [
  let nearest-seller min-one-of patches with [visited?] [distance myself]
  move-to nearest-seller
  ]
end

但是第(2)部分有点棘手。我已经这样做了; “拜访所有卖家”

to visit-all-seller

  while [any? patches with [visited?]] 
  [
    ask turtles [
    let nearest-seller min-one-of patches with [visited?] [distance myself]
    if nearest-seller!= nobody 
    [
      move-to nearest-seller
      ask nearest-seller [set visited? false] 
    ]
  ]
  ]
end

对于单个买家来说,效果很好。但当我带来多个买家时,这种模式就不起作用了。每个客户都将该属性设置为 true,因此其他人不会访问这些卖家。请指导我如何以不同的方式做到这一点。我应该使用动态列表或代理集吗?简单的代理集无法正常工作。

netlogo turtle-graphics patch agent-based-modeling vehicle-routing
1个回答
1
投票

在解决您的问题之前,我有一些一般性建议。

我发现在访问它们后将

visited?
从 true 更改为 false 非常令人困惑。这绝对应该是相反的,否则您或其他处理代码的人最终会感到困惑并犯错误。

除了

repeat 4 [ ask one-of patches [...] ]
,您还可以这样做
ask n-of 4 patches [...]
您的初始代码还存在要求同一个补丁更改颜色两次的风险,导致只有 3 个彩色补丁而不是 4 个。

现在解决你的问题:

你是对的,你不能再以同样的方式为多个买家做这件事了。一种选择是为每个买家创建一个单独的

patches-own
变量,但这很容易变得笨拙。 相反,我建议使用
turtles-own
变量来存储海龟仍想访问的所有补丁。我称之为
destinations
。这是一个补丁集,因此您可以将它与相同的基元一起使用,例如
min-one-of
。 这就留下了从该补丁集中删除补丁的问题,这实际上非常烦人。可以使用
min-n-of
max-n-of
n-of
other
等原语来对补丁集进行子集化。其中
other
是唯一一个仅从集合中删除一个补丁的补丁。如果我们想要使用
other
来定位补丁集,则必须由补丁调用它,但补丁本身无法自由访问海龟的目标变量。为了避免这个问题,我让海龟使用
let
创建本地补丁集。

let new-destinations destinations

补丁可以将自己从这个集合中删除,海龟最终可以使用这个集合来到达它的新目的地

ask nearest-seller [ set new-destinations other new-destinations]
set destinations new-destinations

总而言之:

breed [ buyers buyer ]
buyers-own [ target ]
turtles-own [ destinations ]

to setup
  clear-all
  
  ask n-of 4 patches [   ;;used patches as sellers 
    set pcolor red  
  ]
  
  create-buyers 3 [
    set shape "person"  
    move-to one-of patches 
    set destinations patches with [pcolor = red]
  ]
  
  reset-ticks
end

to go-to-nearest-seller
  ask buyers [
  let nearest-seller min-one-of destinations [distance myself]
  move-to nearest-seller
  ]
end

to visit-all-seller
  
  while [any? turtles with [any? destinations]] [
    
    ask turtles [
      let nearest-seller min-one-of destinations [distance myself]
      if nearest-seller != nobody 
      [
        move-to nearest-seller
        let new-destinations destinations
        ask nearest-seller [ set new-destinations other new-destinations]
        set destinations new-destinations
      ]
    ]
  ]
  
end
© www.soinside.com 2019 - 2024. All rights reserved.