5

Hi I am writing simulation which uses ticks to represent time in simulation environment . Is it possible to display tick counter in monitor GUI? Also I have tried to input the code but it is not working.

My sample code:

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

This should show tick exactly value when agent unit value is zero. For example if agent = 0 at tick 200 it should display 200 on monitor even the whole simulation run on 500 ticks.

whole code is

  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
Prit Tuntisak
  • 51
  • 1
  • 3

3 Answers3

4

To make a monitor that displays the tick count, the complete code to put in the monitor is:

ticks

That's all.

If you want the monitor to show the tick count when there are no agents, but be blank otherwise, you would put this code in:

ifelse-value any? agents
  [ "" ]
  [ ticks ]
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
  • I have try your code still it not working due to no closing parenthesis for this open parenthesis can you please help I have no idea what to do now – Prit Tuntisak Mar 24 '15 at 23:24
  • 1
    The code in my answer is correct; I have tried it. Check your typing. If you still don't see the problem, you may need to open a new question and include the exact code you are using and the exact error message you are getting. – Seth Tisue Mar 25 '15 at 01:02
  • Prit - Seth's code is correct, but if you are getting an error about parentheses, then one of your OTHER pairs of parentheses is mismatched so a parenthesis in this bit of code is being paired with your code elsewhere. Please either post the whole section of code (from `to procedure-name` to `end`) or comment out these lines in your code and see where the error moves to. – JenB Mar 25 '15 at 21:38
  • @SethTisue , I have work on your code now it is work . It is because of netlogo version it not work on 5.0R-3 version but it is working on 5.1 version thank you however there is still new problems is tick still running due to main simulation is rung I want just exactly tick value which all agents die for example whole simulation end at 500 ticks, where all agents die at 200 tick , interface should display ticks value at 200 and static, how ever in your code value which it show is 500 not 200 because tick value still keep running until simulation is done thank you – Prit Tuntisak Mar 26 '15 at 16:20
  • @JenB thank you for your help now it working in different netlogo version , however tick value still keep running as the whole simulation is keep running can you please help on this issue thank you I also have post you the whole code of my project on original topic – Prit Tuntisak Mar 26 '15 at 16:42
1
  1. Your syntax is incorrect. You could however include in an observer procedure (e.g., your go procedure) the following: if (count agents = 0) [show ticks]. This would display in the area above the command line.
  2. However it sounds like you are looking for user-message, rather than show. E.g., if (count agents = 0) [user-message (word ticks)]. http://ccl.northwestern.edu/netlogo/docs/dict/user-message.html
Alan
  • 9,410
  • 15
  • 20
  • if (count agents = 0) [user-message (word ticks)] is not working due to expected reporter please help – Prit Tuntisak Mar 24 '15 at 23:25
  • @PritTuntisak If you append that code to your schedule (`go`), it will not raise an error (unless your forgot to `reset-ticks` or you have no agentset named `agents`). Post the entire error message if this comment does not help you figure it out. – Alan Mar 25 '15 at 12:29
1

If you want to update the monitor each tick but only when there are agents, you need to create a separate global variable (called counter below) and update that instead. The ticks internal variable will always increase so monitoring it will always increment. This is because the ticks variable is the simulation clock, it reports the passage of time.

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

Then create a monitor with just counter (and 0 decimal places etc)

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
JenB
  • 17,620
  • 2
  • 17
  • 45
  • @Seth Tisue , I have run your code however counter value still keep running after all anti-agent dies , it should just display exactly counter value when anti-agent die in simulation , however your code still running untill the end of simulation – Prit Tuntisak Apr 07 '15 at 17:34
  • @Prit No, counter only increases when there are live agents (that's what `if any? agents` checks). Check that you have that line correct. – JenB Apr 07 '15 at 17:53