首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在NetLogo中循环?

如何在NetLogo中循环?
EN

Stack Overflow用户
提问于 2022-01-23 13:38:38
回答 1查看 446关注 0票数 0

我有以下问题。我需要循环通过代码。但是,它不起作用。

问题的上下文化:我有3个文件(在.asc中),表示涉及海龟3岁( 2岁、4岁和8岁)的数据。我希望如果用户将值1放在num-user中,它将只使用一个表示"2“的文件(L_2)进行模拟;如果用户将值2放在num-user中,他将对表示"2”"4“的文件(L_2和L_4)进行模拟,最后,如果用户将值3输入num-use,它将模拟表示"2”"4“"8”的文件(L_2、L_4和L_8)。问题是循环不工作,会产生各种错误。例如:扩展异常: ascii文件./L8.asc未找到或找不到列表2 4 8中的元素3,该元素仅为3或go运行了3次以上的模拟。

我无法将.ascii文件附加到这个问题中。但是,如果有人能够查看代码并识别错误,我将非常感激。我不能使用BehaviouSpace来解决这种情况,我需要在代码中使用这个循环。

提前感谢!

代码语言:javascript
复制
extensions [ gis ]

globals [ num turtle-ages num-ages files random-seeds  num-user repetitions ]

to setup
  ca
  
  set random-seeds 1
  random-seed random-seeds
  set num 0
  set turtle-ages [ "2" "4" "8" ]
  set num-ages item num turtle-ages
  setup-asc
  setup-turtles
  reset-ticks
end

to setup-2
  clear
  random-seed random-seeds
  set num-ages item num turtle-ages
  setup-asc
  setup-turtles
  reset-ticks
end

to setup-turtles
ask n-of 5 patches [ sprout 1 ]
end

to clear
  set files 0
  clear-ticks
  clear-turtles
end

to setup-asc
  let number1 num-ages
  set files gis:load-dataset ( word "./L_" number1 ".asc" ) ;; this loads a one raster file. There are 3 files in the folder with the names: (L_2.asc ; L_4.asc and L_8.asc
end

to go
  move
  tick
  let n count turtles
  if n = 0 or ticks = 10
  [
    set random-seeds random-seeds + 1
    set repetitions 1
    if random-seeds = repetitions + 1
    [
      set random-seeds 1

      set num num + 1
      
      ;; if the user puts the value 1 in num-user, it would only do the simulation with only one file (L_2) that would represent [ "2" ] 
      ;;; if the user puts the value 2 in num-user, he would do the simulation with the files (L_2 and L_4) that would represent [ "2" "4" ] 
      ;;;; and finally, if the user puts the value 3 in num-use, it would simulate the files (L_2 , L_4 and L_8) that would represent [ "2" "4" "8" ]
      set num-user 1 ;;
      if num = num-user [ stop ]
    ]
    setup-2
  ]
end

to move
  ask turtles [ 
    right random 360
    fd 1
    if ticks = 5 [ die ]
  ]
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-23 19:40:43

只要有可能,我建议把你的代码削减到一个MRE到a),确保这里的用户可以运行你的代码(例如,没有你的文件,这是不可行的)和b),看看用更简单的术语重新规划你的问题/目标是否有助于让事情正常运行--至少这才是对我有用的!

我认为,在这里,您可能会发现foreach作为一种循环所需模拟的方法很有用,而不是手动跟踪迭代次数。在本例中,假设num-user是接口上的一个数字输入小部件,下面的设置将确定要处理的年份:

代码语言:javascript
复制
globals [ ages-to-run ]
  
to base-setup
  ca
  ; Determine how many simulations to run, based on user input into
  ; 'num-user' numerical input widget on the interface
  ifelse num-user < 1 or num-user > 3 [
    print "Incorrect number of simulations indicated"
  ] [
    let possible-sims [ "2" "4" "8" ]
    set ages-to-run sublist possible-sims 0 num-user
  ]  
  reset-ticks
end

运行上述操作后,ages-to-run变量将包含["2"]["2" "4"]["2" "4" "8"]。接下来,您可以遍历所需的时间来运行您的模拟(在注释中有更多的细节):

代码语言:javascript
复制
to run-simulations 
  if ages-to-run = 0 [
    print "Simulation setup not complete"
  ]
  
  foreach ages-to-run [
    ; This is the loop proper where each "age" is iterated.
    ; All of your simulation calls (manual variable resetting,
    ; etc) should all go within this loop.
    current-age ->
    print word "Running simulations for age: " current-age
    let file-to-load ( word "./L_" current-age ".asc" )
    print file-to-load
    clear-turtles
    ask n-of 5 patches [ 
      sprout 1 [ 
        pd 
        set color runresult current-age + 55
      ] 
    ]
    repeat 20 [
      ask turtles [
        rt random 60 - 30
        fd 1
      ]
      tick
    ]
    print ( word "Simulations complete for age: " current-age "\n" )  
  ]
end

在输入到3中的num-user中运行上面的代码将为每个年龄运行模拟(不同的颜色表示不同的运行):

因此,为了正确地运行模拟,您的所有年龄代码都应该在上面指出的foreach循环内,并且要小心,就像您的问题一样,不要重置全局变量。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70822609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档