【问题标题】:move turtles and create a crowd to target移动海龟并创建一个目标人群
【发布时间】:2014-07-06 21:08:19
【问题描述】:
to setup
  ca
  reset-ticks
  ask patches [
    set inside? (abs pycor < 10 and abs pxcor < 10)       
    set exit? false
    ask patch 11 0 [ set pcolor lime set exit? true]
  ]

  repeat initial-population [ ; start condition turtles with any other turtles on neighbors 
    ask one-of patches with [
      inside? and (not any? other turtles-here) and (not any? turtles-on neighbors)] [
      sprout 1 [
        set color blue
        set size 1
      ]]]  
end

to go  
  tick
  define-neighbors-radius-2
  move  
end

to define-neighbors-radius-2
   ask turtles [     
    set neighbors-ahead2 patches at-points [[2 1] [2 0] [2 -1]] 
    set neighbors-for-y-up2 patches at-points [[2 0] [2 -1] [1 -2] [0 -2] [-1 -2]] with [inside?]
    set neighbors-for-y-down2 patches at-points [[-1 2] [0 2] [1 2] [2 1] [2 0]] with [inside?]  
  ]
end

to move  
;; my intent to move turtles to exit without their neighbors are occupied by other turtles,          ;;that is the 8 patches around turtles are empty until exit?  

ask turtles[
    ifelse inside? [
      if ycor = 0 [    ;strategy to turtles with in front exit
        ifelse exit? [
          set heading 90 
          fd .5
        ]
        [
          facexy 11 0
          if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-ahead2) [
            fd .5
          ]
        ]
      ]
      if ycor > 0 [     ; strategy to turtles occupied "bottom-side" of inside?
        facexy 11 0
        if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-for-y-up2) [
          fd .5
        ]
      ]
      if ycor < 0 [      ; strategy to turtles occupied "down-side" of inside?
        facexy 11 0
        if (not any? turtles-on neighbors) and (not any? turtles-on neighbors-for-y-down2) [
          fd .5
        ]
      ]
    ]
    [
      set heading 90 
      fd .5
    ]
  ]  
end

我尝试移动海龟退出,但不是所有海龟都移动,为什么? 此外,海龟必须在 ycor = 0 的情况下外出,即不允许倾斜方向,因为邻居将占据的补丁不在里面! 不能公开这个问题,因为“看起来我的帖子主要是代码”,所以谈谈我的生活: 说真的,我的意图是在出口前创建一个人群并设置一些规则来延迟海龟流出口,为此我需要邻居空着来显示代理之间的交互。 (也接受一些设置此交互的建议)但此时海龟到达出口! 谢谢

【问题讨论】:

  • "另外,海龟必须在 ycor = 0 的情况下出去,这是不允许的倾斜方向,因为邻居将占用的补丁不在里面" — 无法理解这句话
  • 抱歉英语不是我的素质。
  • 海龟从补丁 10 0 退出,补丁在相同的 pycor 退出,所以 ycor = 0,而不是像代码示例中那样从补丁 10 1 或 10 -1 退出。
  • 您可以编辑您的原始问题以使其更清晰。

标签: netlogo


【解决方案1】:

此代码的一个问题是您有三个ifs,您似乎每次只希望其中一个运行,但有可能触发不止一个。你有:

if ycor = 0 [ ...commands #1... ]
if ycor > 0 [ ...commands #2... ]
if ycor < 0 [ ...commands #3... ]

但是海龟的ycor 可能会被命令#1 或#2 改变。所以#1 和#2 都可能运行,或者#2 和#3 都可能运行,也许还有其他组合。我假设您打算让每只海龟每刻只移动一次,因此我建议您将其重写为:

ifelse ycor = 0
  [ ...commands #1... ]
  [ ifelse ycor > 0
      [ ...commands #2... ]
      [ ...commands #3... ] ]

此代码中应修复的其他内容:

  • reset-ticks 应该在 setup 的末尾,而不是靠近开头。它告诉 NetLogo 设置完成。
  • tick 应该在 go 的末尾,而不是靠近开头。它告诉 NetLogo 一个勾号已经完成。

我不知道我指出的这些问题中的任何一个是否真的导致了您所看到的意外行为。也许仅仅通过阅读代码,这个错误并不明显。在这种情况下,您有两种可能的行动方案:

1) 备份。扔掉这个损坏的代码,回到模型的最后一个版本,它只包含你已经验证可以正常工作的代码。然后再试一次,但这一次,不要一次添加这么多新代码。在继续进行下一个小改进之前,尝试对代码进行非常小的改进,并使其正常工作。等等。如果您在任何时候遇到困难,请到这里,展示您的代码,并提出一个具体的问题。这个问题应该比你当前的问题更容易回答。 “这里有大量代码无法运行,求助!”形式的问题很难回答。

2) 继续调查您的问题。你写道,“不是所有的乌龟都会动,为什么?”也许有人可以通过阅读您的代码来回答这个问题;我不能(除非我上面的第一个猜测是正确的)。为了弄清楚,我必须自己运行代码并用它做实验。我会做一些事情,比如尝试只用一只乌龟运行它,看看这种情况是否失败;在代码中添加print 语句,显示每个海龟的坐标和运动,这样我就可以尝试找出哪个海龟出了问题,以及具体在什么情况下;等等。这就像侦探工作,或者像在实验室里做化学实验。

【讨论】:

  • 我可以在 netlogo 中使用函数 arcsin 吗?怎么写?
  • 请打开一个新问题来问这个问题。
  • 这种问题我做不到。堆栈溢出规则'..我不知道
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多