Netlogo,如何显示刻度数

时间:2015-03-24 00:06:36

标签: counter netlogo

您好我正在编写使用刻度表示模拟环境中的时间的模拟。是否可以在监视器GUI中显示滴答计数器?此外,我试图输入代码,但它无法正常工作。

我的示例代码:

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

当代理单位值为零时,应显示精确值。例如,如果在{200}处agent = 0,它应该在监视器上显示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

3 个答案:

答案 0 :(得分:4)

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

ticks

这就是全部。

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

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

答案 1 :(得分:1)

  1. 您的语法不正确。但是,您可以在观察员程序(例如,您的go程序)中包含以下内容:if (count agents = 0) [show ticks]。这将显示在命令行上方的区域中。
  2. 然而,听起来您正在寻找user-message,而不是show。例如,if (count agents = 0) [user-message (word ticks)]http://ccl.northwestern.edu/netlogo/docs/dict/user-message.html

答案 2 :(得分:1)

如果您想更新每个滴答的监视器,但仅在有代理时,您需要创建一个单独的全局变量(在下面称为计数器)并更新它。 ticks内部变量将始终增加,因此监视它将始终增加。这是因为ticks变量是模拟时钟,它报告了时间的流逝。

globals [counter]

to setup
  ...
  set counter 0    ; note, not required as initialise to 0 but helps readability
  reset-ticks
end

to go
  ...
  if any? turtles [ set counter counter + 1 ]
  ...
  tick
end

然后创建一个只有counter(和0小数位等)的监视器