counter - Netlogo,如何显示刻度数

标签 counter netlogo

嗨,我正在编写模拟,它使用刻度来表示模拟环境中的时间。是否可以在监视器 GUI 中显示滴答计数器?我也尝试过输入代码,但它不起作用。

我的示例代码:

if [agents count = 0]
show "count ticks"

当代理单位值为零时,这应该准确显示刻度值。例如,如果 agent = 0 在刻度 200 处,它应该在监视器上显示 200,即使整个模拟运行在 500 个刻度上。

整个代码是

  patches-own[
  nest?
  nest-scent
  food
  food-source-number
  chemical
   ]

breed [agents agent] ;agent is ant
breed [antiagents a-antiagent] ;antiagent spider
antiagents-own[energy]
agents-own[venom]


;;;;;;;;;;;;;;;;;;;;;;;;;; Setup ;;;;;;;;;;;;;;;;;;;
to setup
  clear-all

set-default-shape agents "ant"
  create-agents initial-ants  ;; create the ants, then initialize their variables
  [
    set color orange
    set size 2  ;; easier to see
    set venom  (ant-venom-strength)

  ]

  set-default-shape antiagents "spider"
  create-antiagents initial-spiders  ;; create the spider, then initialize their variables
  [
    set color yellow
    set size 3  ;; easier to see
    set energy (spider-health)
    setxy random-xcor random-ycor
  ]
  setup-patch
  display-labels



  reset-ticks
 end


;;;;; nest food patch;;;
to setup-patch
   ask patches
  [ setup-nest
    setup-food
    recolor-patch ]
end

;;;;; setup nest
to setup-nest  ;; patch procedure
  ;; set nest? variable to true inside the nest, false elsewhere
  set nest? (distancexy 0 0) < 5
  ;; spread a nest-scent over the whole world -- stronger near the nest
  set nest-scent 200 - distancexy 0 0
end

to setup-food  ;; patch procedure
  ;; setup food source one on the right
  if (distancexy (0.6 * max-pxcor) 0) < 5
  [ set food-source-number 1 ]
  ;; setup food source two on the lower-left
  if (distancexy (-0.6 * max-pxcor) (-0.6 * max-pycor)) < 5
  [ set food-source-number 2 ]
  ;; setup food source three on the upper-left
  if (distancexy (-0.8 * max-pxcor) (0.8 * max-pycor)) < 5
  [ set food-source-number 3 ]
  ;; set "food" at sources to either 1 or 2, randomly
  if food-source-number > 0
  [ set food one-of [1 2] ]
end

to recolor-patch  ;; patch procedure
  ;; give color to nest and food sources
  ifelse nest?
  [ set pcolor violet ]
  [ ifelse food > 0
    [ if food-source-number = 1 [ set pcolor cyan ]
      if food-source-number = 2 [ set pcolor sky  ]
      if food-source-number = 3 [ set pcolor blue ]
      if food-source-number = 4 [ set pcolor brown ]  ]
    ;; scale color to show chemical concentration
    [ set pcolor scale-color green chemical 0.1 5 ] ]
end


  ;;;;;;;;;;;;;;;;;;;;;;;;;;; GO ;;;;;;;;;;;;;;;;;;;;;;


 to go  ;; forever button
   ;;;;;;;;;;;;Spider ;;;;;;;;
   ask antiagents [
     setup-spider-movemonet ; option for user to select spider movement 
     ;move-spider
     catch-ant
     spider-death
     ]

   ;;;;;;;;;; Ant ;;;;;;;;;;;;
  ask agents
  [ if who >= ticks [ stop ] ;; delay initial departure
    ifelse color = orange
    [ look-for-food  ]       ;; not carrying food? look for it
    [ return-to-nest ]       ;; carrying food? take it back to nest
    wiggle
    fd 1 ]
  diffuse chemical (diffusion-rate / 100)
  ask patches
  [ set chemical chemical * (100 - evaporation-rate) / 100  ;; slowly evaporate chemical
    recolor-patch ]

  tick
  display-labels
  ;;;;;;;;;;; Stop function ;;;;;;;;;;

  if count agents = 0 [stop]

  if count patches with [pcolor = blue] = 0 [stop] 




end

 ;;;;;;;;;;;;;;;;;;;;;;;;;; function in go ;;;;;;;;;;;;;;;

to look-for-food  ;; turtle procedure
  if food > 0
  [ set color green + 1     ;; pick up food
    set food food - 1        ;; and reduce the food source
    rt 180                   ;; and turn around
    stop ]
  ;; go in the direction where the chemical smell is strongest
  if (chemical >= 0.05) and (chemical < 2)
  [ uphill-chemical ]
end

;;;;;;;;;;; ant function ;;;;;;;;;;

 ;; sniff left and right, and go where the strongest smell is
to uphill-chemical  ;; turtle procedure
  let scent-ahead chemical-scent-at-angle   0
  let scent-right chemical-scent-at-angle  45
  let scent-left  chemical-scent-at-angle -45
  if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  [ ifelse scent-right > scent-left
    [ rt 45 ]
    [ lt 45 ] ]
end

to return-to-nest  
  ifelse nest?
  [ ;; drop food and head out again
    set color orange
    rt 180 ]
  [ set chemical chemical + 60  ;; drop some chemical
    uphill-nest-scent ]         ;; head toward the greatest value of nest-scent
end

to wiggle  ;; turtle procedure
  rt random 40
  lt random 40
  if not can-move? 1 [ rt 180 ]
end
to uphill-nest-scent  
  let scent-ahead nest-scent-at-angle   0
  let scent-right nest-scent-at-angle  45
  let scent-left  nest-scent-at-angle -45
  if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  [ ifelse scent-right > scent-left
    [ rt 45 ]
    [ lt 45 ] ]
end

to-report chemical-scent-at-angle [angle]
  let p patch-right-and-ahead angle 1
  if p = nobody [ report 0 ]
  report [chemical] of p
end

to-report nest-scent-at-angle [angle]
  let p patch-right-and-ahead angle 1
  if p = nobody [ report 0 ]
  report [nest-scent] of p
end




;;;;;;;;;;;;;;;;;;;;;;;;;;;; Spider-Function ;;;;;;;;;;;;;;;;;;;;;;
to move-spider
  rt random 360
  lt random 360
  fd 3
end

to spider-death  ;; turtle procedure

  if energy  <= 0 [ask antiagents-here  [die]]



end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to catch-ant  
  let prey one-of agents-here                    
  if prey != nobody                             
     [ ask prey [ die ]                         
      set energy (energy  - ant-venom-strength)] 
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



 ;;;;; GUI Function ;;;;
 to display-labels
  ask antiagents [ set label "" ]
  if show-spider-health [
    ask antiagents [ set label round energy ]
    ]
end

 ;;;;;;; Spider movement GUI ;;;;;;
 to setup-spider-movemonet
         if setup-spider-movement 
      [ 
        ask antiagents [ move-spider ]
      ]
 end

最佳答案

要制作一个显示滴答计数的监视器,要放入监视器的完整代码是:

ticks

仅此而已。

如果您希望监视器在没有代理时显示滴答计数,但否则为空白,您可以将此代码放入:

ifelse-value any? agents
  [ "" ]
  [ ticks ]

关于counter - Netlogo,如何显示刻度数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29222719/

相关文章:

Python 地向字典添加键

JavaScript 从 10 开始递减,然后在 0 时执行某些操作

php - 分别计算页面浏览量

javascript - 尝试让我的单击按钮始终显示结果,而不仅仅是单击时

lambda - 在 NetLogo 中运行具有可变数量参数的任务

NetLogo 6 和 rnd 扩展

计算文本文件中的字符使用情况? C

NetLogo:计算补丁集的地理中心

file - NetLogo:输出文件中的换行符??没那么简单。

netlogo - LogoLists 插入逗号,NetLogo -> Java -> NetLogo