你不能在龟背景中使用tick,因为tick只是观察者!!的NetLogo

时间:2017-11-28 18:23:03

标签: netlogo

如果我在if命令中添加go,我会收到错误消息

你不能在龟背景中使用tick,因为tick只是观察者

这是我的go命令。搜索吃回家和书房都在我的命令中定义。

能量也被定义为乌龟拥有的全局变量

to go

  if ticks = day-length  [set day day + 1 create-next-day]


  ask adults [search eat]
  if energy < 20000 [ask adults [go-home den]]

 tick 

end

如果我拿出这条线

if energy < 20000 [ask adults [go-home den]]

它完美运行,但我需要那条线或等效物。请帮忙

Commands
;;-------------------------------------------------------------;;
;;------------------- ADULTS COMMANDS--------------------------;;
;;-------------------------------------------------------------;;

;; Need to add a private variable (wolves own) for wolves [state] and then need to code 4 states 1. Den 2. Search 3. Eat 4. Return
;; need to code all 4 states
;; Need to correctly allocate energy and the state of decline

To den ;when wolf is full
  set energy  energy  - .04
end


to search ;when wolf is hungry
  set energy  energy  - .07
    fd v-wolf
   if random 600 = 1 ;; frequency of turn
  [ ifelse random 2 = 0 ;; 50:50 chance of left or right
    [ rt 15 ] ;; could add some variation to this with random-normal 45 5
    [ lt 15 ]] ;; so that it samples from a dist with mean 45 SD 5

  ;; check if it can see a prey/food item
  ;; here i think we probably pick one of several possible prey
  ;; that are detectable randomly using the one-of command.
  ;; We should probably select the nearest one instead ** The turtles are getting
  ;; caught between two prey species and dying because they cant choose which one **
  if any? prey in-radius smell [set heading towards one-of prey in-radius smell]
  if energy < 0 [die]

end


To eat ;to kill prey and eat it
  let kill one-of prey-here in-radius smell
  ;need to code in a variable for success too
  if kill != nobody
    [ask kill [ die ]
      set energy energy + 10000]
end



to go-home ;to head home after they've eaten and den until they need to feed again
 if energy > 30000 [set target-patch min-one-of (patches  with [pcolor = white]) [distance myself]]
  face target-patch
  fd v-wolf
  set energy  energy  - 1
end

2 个答案:

答案 0 :(得分:2)

如果if energy < 20000 [ask adults [go-home den]](如图所示)是一个海龟变量,

go将成为energy中的问题。这将使过程的上下文成为乌龟上下文,而不是观察者上下文。

修改

例如,如果energy是海龟变量,也许你的意思是

ask adults [if (energy < 20000) [go-home den]]

答案 1 :(得分:1)

首先,您需要逐渐构建代码。您的代码的多个部分无法正常工作且您无法理解。尝试添加尽可能少的数量,并确保在添加任何其他内容之前有效。目前您有三个不同的问题,代码的不同部分存在错误。

在艾伦回答的背景问题中,以这种方式思考:变量&#39;能量&#39;属于海龟。这意味着如果你有10只海龟,你就有10个名为“能量”的变量,每只海龟一只。你要检查哪一个是<20000?

你可能想要的是单独检查每只乌龟,如果它通过了测试,让乌龟去做所需的动作。所以它必须在ask turtles []内,并且从观察者变为乌龟上下文(模型实体正在做什么)。

to go
  if ticks = day-length
  [ set day day + 1
    create-next-day
  ]

  ask adults
  [ search
    eat
    if energy < 20000
    [ go-home
      den
    ]
  ]

  tick 
end

我还清理了你的格式。这不是必需的,NetLogo很乐意在任何地方处理空间。但是,随着代码变得越来越复杂,如果您遵循一些基本实践(1)每次调用一个过程,每个命令等在一个单独的行(2)括号[]和缩进上,您将更容易调试这样你就可以看到括号括起来的代码块。