Search code examples
netlogo

How to deplete a global variable?


I need a little guidance on how to completely use a global variable.

I have the global variable num-tourists which is a number of how many tourists come to a place.

Then, I have my turtles get the maximum number of tourists (15), and they charge them 100,000. Each of my sellers can handle up to 15 tourists.

ask turtles with [turism = 1]
      [set earnings (earnings + ((15) * 100000))]
[set num-tourists num-tourists - 15]

However, I don't know how to tell the code that, if there is less than 15, it can take whatever is left, then multiply that by 100000


Solution

  • This is similar to the "Move Towards Target Example" in the NetLogo library. Functionally, you want to: count the number of remaining tourists. If that number is higher than 15, multiply 10000 X 15. Otherwise, multiply 10000 X (n remaining tourists). Here is one way:

    globals [num-tourists total-earnings]
    
    to setup
      ca
      set num-tourists 40
      set total-earnings 0
      print (word "There are " num-tourists " tourists")
      reset-ticks
    end
    
    to go
      ifelse num-tourists > 15 [
        set total-earnings total-earnings + (15 * 10000)
        set num-tourists num-tourists - 15
      ] [
        set total-earnings total-earnings + (num-tourists * 10000)
        set num-tourists num-tourists - num-tourists
        ; equivalently, set num-tourists 0
      ]
      print (word "There are " num-tourists " tourists, and the total earnings are " total-earnings)  
      tick
    end