首页 > 解决方案 > 当我的海龟品种开始更快地移动并限制海龟可以长到多大时,设置能量下降

问题描述

目前,鲨鱼和鱼的体型和速度继续增长,我试图做的是让鱼和鲨鱼的能量下降更多,因为它们继续移动得更快,正如前向能量超过 7 所证明的那样,我也试图设置鲨鱼和鱼可以达到的大小限制

To go
  update-plots
  tick
   ask sharks[
    set Btimer (Btimer - 1)
    forward Energy / 7
    right random 80
    left random 80
      ask fishes[die]
      set Energy (Energy + 7) ;this essentially controls speed making it so it will go faster if enough energy is collected
       set size size + 0.1 ; this makes it so the sharks will continue growing and I'm not sure how to set a limit to the size they can grow to be
    ]
    set Energy (Energy - 1)
    if (Energy <= 0)[                                    
      die
    ]
    if any? sharks-on patch-ahead 0 and Btimer <= 0[  
      ask sharks-on patch-ahead 0 [
        set Btimer (Btimer + 400 - random 200)
        set Energy (Energy - 50)
      ]
       hatch 10 [                                         
      ]
    ]
  ]
end

标签: netlogornetlogo

解决方案


基本问题似乎是您在每个滴答声中都提高了速度,但没有任何代码来阻止增长。这是一种方法——你只需测试一下鲨鱼是否完全长大:

to go
  update-plots
  tick
  ask sharks
  [ set Btimer (Btimer - 1)
    forward Energy / 7
    right random 80
    left random 80
    ask fishes [ die ]
    if energy <= max-energy      ; here is where you set the condition
    [ set Energy (Energy + 7) ; also increases speed
      set size size + 0.1
    ]
  ]
  ask sharks                       ; There must be an ask here, otherwise error message
  [ set Energy (Energy - 1)
    if (Energy <= 0) [ die ]
    if any? sharks-on patch-ahead 0 and Btimer <= 0
    [ ask sharks-on patch-ahead 0
      [ set Btimer (Btimer + 400 - random 200)
        set Energy (Energy - 50)
      ]
      hatch 10 [ ]
    ]
  ]
end

但这主要是一个设计问题——你试图通过停止增长来代表什么?是不是鲨鱼已经成年了,在这种情况下,像我插入的条件是正确的方法。

还是您预计增长会因其他原因自然停止?例如,您可能认为鲨鱼会因为食物耗尽而停止生长。然而,无论有没有食物,都会发生增长。如果您希望仅在鲨鱼所在的地方有食物(鱼)时才会发生生长,那么您可以执行以下操作:

to go
  ....
  ask sharks with [any? fishes-here]  ; restrict to sharks with food
  [ set Btimer (Btimer - 1)
    forward Energy / 7
    right random 80
    left random 80
    ask fishes-here [ die ]          ; since they are shark food
    set Energy (Energy + 7)          ; max not required, grows if food available
    set size size + 0.1
  ]
  ....
end

只是一般性评论,基于代理的模型很难调试,因为代理、环境和行为之间的复杂交互意味着并不总是清楚代码的哪一部分导致了问题,而且逻辑可能很困难。处理这个问题的最好方法是编写更小的代码——只需添加最小的更改并在编写其他任何内容之前使其工作。例如,您编写代码的方式,所有的鱼都会立即死亡。那是一个不同的问题。如果您一次只写一篇文章,那么您只会遇到一个问题,并且您知道是什么原因造成的。


推荐阅读