运行 netlogo 列表中的过程堆栈

run procedure stack in a list in netlogo

我想制作一个模型,代理将他们的程序存储在一个列表中,并在执行过程中一个一个地展开这些程序 我找到了如何在有限状态机模型中传递一个过程,但在我的例子中它并没有像我预期的那样工作

; inspiration de "state machine exemple"
globals[

]
turtles-own[
  roles ; liste of all roles of out agent
  next-task
  task-stacked ;; liste of all task are stored and schedulled in that attribut
  myplots ;a plot agentset of my own plots
  myFamillyPlots ; a plot agentset of my famillie plts
  age
  
]

to setup
  clear-all
  create-turtles 10 [
    ;set task-stacked list
    setxy random-xcor random-ycor
    set roles list "people" "patriarche"
    if member? "people" roles [ 
      set age 50 + random 20
      set task-stacked list "death-prob" "createFamilly"
    ]
  ]
  reset-ticks
end


to go
  ask turtles[
   update-task-stacked
   run next-task
  ]

end

to update-task-stacked
  let string-next-task first task-stacked
  set next-task [ -> string-next-task ] 

end

to createFamilly
  
end

to death-prob ; turtle context
  set age age + 1
  if member? "people" roles [
   if 80  - (random age) <= 0 [
     die
    ] 
  ]
  
end

没用:

RUN expected input to be a string or anonymous command but got the 
anonymous reporter (anonymous reporter: [ -> string-next-task ]) instead.

核心问题在这里:

to update-task-stacked
  let string-next-task first task-stacked
  set next-task [ -> string-next-task ]
end

既然task-stacked是一个字符串列表,那么string-next-task就是一个字符串。但是随后我们将next-task设置为一个报告字符串的任务。因此,当执行 run next-task 时,它会给出您看到的错误 - run 将不适用于报告值的任务。你可能想要的是:

to update-task-stacked
  let string-next-task first task-stacked
  set next-task string-next-task
end

现在 next-task 是一个字符串,所以当您执行 run next-task 时,您是 运行 一个字符串,它将将该字符串编译为 NetLogo 代码并 运行 它.

但是,您可以通过一个小的更改完全避免使用字符串(运行通常不鼓励使用字符串,除非在极少数情况下):

      set task-stacked (list [ -> death-prob ] [ -> createFamilly ])

现在我们将任务列表设置为一个实际的任务值列表,可以直接用run运行,而不是需要编译的字符串值。这样做的好处是 NetLogo 可以在您编写代码时在编译时告诉您是否出现拼写错误或语法错误(如 dearth-prob)。使用字符串值时,您只会在 运行 时收到错误,这会使故障排除变得更加困难。