【问题标题】:Initialize Netlogo world with two breeds; only one turtle per patch用两个品种初始化 Netlogo 世界;每个补丁只有一只乌龟
【发布时间】:2017-07-11 19:39:15
【问题描述】:

我正在尝试在 Netlogo 中建立一个世界,其中有两个品种,但每个补丁只有一只乌龟:

breed [supras supra]
breed [subs sub]

turtles-own [age]
subs-own [status]

to setup
  clear-all

  ;; Color the patches so they're easier to see
  ask patches [ set pcolor random-float 2 ]

  ;; num-turtles patches will sprout one turtle each
  ask n-of (num-turtles / 2) patches [
    if not any? turtles-on patch-set self [
      sprout-subs 1
    ]
  ]

  ask n-of (num-turtles / 2) patches [
    if not any? turtles-on patch-set self [
      sprout-supras 1
    ]

  ]

  ;; Set breed colors and own-variables
  ask subs [
    set color blue
    set shape "dot"
  ]

  ask supras [
    set color pink
    set shape "dot"
  ]

  reset-ticks
end

to go

  ask turtles [
    fd 1
  ]

  tick
end

这似乎可行,但我不能完全确定它在技术上是否正确。编写什么测试可以确保我在初始化时没有包含多个海龟的补丁?

【问题讨论】:

  • 我认为你的代码是可靠的——如果你想确认你可以通过在你的setup 程序的末尾添加类似if any? patches with [ count turtles-here > 1 ] [ setup ] 的东西来检查——如果有的话,这将重新运行你的设置程序带有两个以上turtles-here 的补丁。显然,如果您稍后在 setup 中包含更多海龟,那么该检查将不再有效。
  • 酷,谢谢!我尝试了您的建议,然后在我的设置方法结束时尝试了if any? patches with [ count turtles-here > 1 ] [ output-print "reset" ],只要我等待,它似乎就没有打印reset。对我来说已经足够了。
  • 正确。您还可以通过将> 1 更改为> 0 并观察setup 运行到无穷大来确认它是否有效,但您必须使用工具> 停止来停止它。

标签: netlogo


【解决方案1】:

我实际上会建议一种不同的方法;而不是随机选择一个品种的一些补丁和另一个品种的一些补丁并试图避免彼此,您可以选择最初发芽的全部补丁,然后将一半的海龟转换为另一个品种。

globals [num-turtles]
breed [supras supra]
breed [subs sub]

turtles-own [age]
subs-own [status]

to setup
  clear-all
  set num-turtles 99
  ask n-of num-turtles patches [sprout-subs 1] 
  ask n-of (num-turtles / 2) subs [set breed supras]
  <procedures to set colours etc>
end

【讨论】:

    【解决方案2】:

    尝试将您的代码精简为完整示例所需的内容。

    globals [num-turtles]
    breed [supras supra]
    breed [subs sub]
    
    turtles-own [age]
    subs-own [status]
    
    to setup
      clear-all
      set num-turtles 99
      ;; num-turtles patches will sprout one turtle each
      ask n-of (num-turtles / 2) patches [sprout-subs 1] 
      ask n-of (num-turtles / 2) patches with [not any? turtles-here] [
          sprout-supras 1
      ]
    end
    
    to test-setup
      if (int (num-turtles / 2) != count supras) [error "setup error: supras"]
      if (int (num-turtles / 2) != count subs) [error "setup error: subs"]
      if any? patches with [count turtles-here > 1] [error "setup error: patches"]
    end
    

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      相关资源
      最近更新 更多