遍历 Netlogo 中的列表

Iterating through a list in Netlogo

在这里转动我的轮子!

所以,我想做的是:

  1. 对于一个叫做budget(n,0-19)的任意数,取前n
    node_ids 来自节点列表(海龟自己的变量名为 node_id)

  2. 查找匹配node_id

    对应的Who
  3. 请乌龟按谁号做某事,例如,[set adopt? = 真]

非常感谢任何帮助或建议!

您可以通过多种不同的方式浏览列表。 一种是使用“foreach”命令。 另一种方法是对索引号使用显式循环,从零开始。

这是两者的演示。 “设置”运行设置 “go”一旦找到项目并使它们变大并显示它们的标签 再“走”一下,又变小了。

globals [ NUMTURTLES BUDGET NODELISTSIZE nodelist ]
turtles-own [ adopt?  node_id]

to setup
  clear-all
  let rseed random 999999;
  print (word "random seed being used for this run is : " rseed );
  random-seed rseed;
  
  ;; globals or sliders, low values to see what's going on
  set BUDGET random  5;  ( 0 to 19 )
  set NUMTURTLES    30;
  set NODELISTSIZE  10; ;; something bigger than BUDGET!
  
  print (word "BUDGET = " BUDGET );
  
  
  create-turtles NUMTURTLES 
      [ setxy random-xcor random-ycor  
        set adopt? false 
        set node_id random 999999
       ]
  
  set nodelist [];
  ask N-of NODELISTSIZE turtles [ set nodelist lput node_id nodelist ]
  show nodelist;
  
  reset-ticks
end

to go
  if ( ticks = 0)
  [    ;; option #1: make a sublist and use the "foreach" command to walk down it
       let worklist sublist nodelist 0 BUDGET
       show worklist
       foreach worklist [ 
             a -> print (word "next item in worklist " a)
         ask one-of turtles with [ node_id = a ] [ set adopt? true set size 3 set label a]
          ]
  ]
  
  ;; undo the above a different way
  
  if ( ticks = 1 )  
  [   ;; option #2:  use effectively for i = 0 to BUDGET to process list item i
      ;; do NOT forget the "set i ( i + 1 )" or this will run forever! 
      let i 0
      repeat BUDGET [
      let b item i nodelist
      print (word "item " i " which is " b " will be undone");
      ask one-of turtles with [ node_id = b ] [ set adopt? false set size 1 ]
      set i ( i + 1 )
      ]
    
  ]
  tick
end