我无法为我的品种设置不同的颜色

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

如果我在 NetLogo 上运行此代码几次,有时会得到一个绿色和两个红色,有时会得到三个红色,这是一个错误还是代码有问题?

breed [greens a-green]
breed [  reds a-red]
    
to setup 
        clear-all

        create-greens 1
        ask greens [ set color green setxy random-xcor random-ycor]

        create-reds   2
        ask reds   [ set color red   setxy random-xcor random-ycor]

        reset-ticks
end
colors parallel-processing simulation netlogo agent-based-modeling
1个回答
1
投票

您的代码似乎没有任何问题 - 我只是运行了一个稍微简化的版本 10000 次,除了 1 个绿色和 2 个红色之外,没有任何其他计数的情况。尝试这个稍微修改过的设置版本:

breed [greens a-green]
breed [reds a-red]

globals [ green-count red-count ]

to setup 
  create-greens 1 [
    set color green 
    setxy random-xcor random-ycor
  ]
  create-reds 2 [
    set color red 
    setxy random-xcor random-ycor
  ]
end

运行几次并跟踪每种颜色的计数:

to colors-check
  ca
  set green-count []
  set red-count []
  repeat 10000 [
    ask turtles [ die ]
    setup
    set green-count lput count turtles with [ color = green ] green-count
    set red-count lput count turtles with [ color = red ] red-count
  ]
  print word "Green counts recorded: " remove-duplicates green-count
  print word "Red counts recorded: " remove-duplicates red-count
  reset-ticks
end

我的输出是:

Green counts recorded: [1]
Red counts recorded: [2]

这告诉我,对于所有这 10000 次重复,颜色计数均符合预期(否则在删除重复项时您会看到其他数字)。也许在设置后运行了一些其他命令?

© www.soinside.com 2019 - 2024. All rights reserved.