delaye
delaye

Reputation: 1399

run procedure stack in a list in netlogo

I would like to make a model where the agents store their procedures in a list and unfold these procedures one by one during the go I found how to pass a procedure in the finite state machine model, but in my case it doesn't work as I expect

; 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

It doesn't work:

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

Upvotes: 1

Views: 94

Answers (1)

Jasper
Jasper

Reputation: 2780

The core issue is here:

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

Since task-stacked is a list of strings, string-next-task is then a string. But then we set next-task to a task that reports a string. So when doing run next-task, it gives the error you see - run will not work with a task that reports a value. What you probably want is this:

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

Now next-task is a string, so when you do run next-task you are running a string, which will compile that string as NetLogo code and run it.

However, you can avoid strings entirely with one more small change (running strings is generally discouraged except in rare situations):

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

Now we set the task list to an actual list of task values that can be run directly with run, instead of string values that need to be compiled. The benefit here is that NetLogo can tell you at compile time as you write your code if you make a typo or syntax error (like dearth-prob). When using string values, you only get an error at runtime which can make it much harder to troubleshoot.

Upvotes: 2

Related Questions