Raj
Raj

Reputation: 1071

Dynamic charting in Netlogo

In my model, the number of turtle is dynamic based on the value defined by the user using a slider. The slider can take values between 2 and 10. Each turtle has its own set of co-ordinates and characteristics, hence i used the following code to create them.

create-parties 1 
[set color red set label-color red set label who + 1 set size 3 setxy party1-left-right party1-lib-con ]
create-parties 1 
[set color green set label-color red set label who + 1 set size 3 setxy party2-left-right party2-lib-con ]
if Num-of-parties >= 3
  [ create-parties 1 
[set color blue set label-color red set label who + 1 set size 3 setxy party3-left-right party3-lib-con ] ]

I have repeated the above till Num-of-parties =10.

In one of the modules i have created a condition where if a certain value of the turtle reaches 0 it will die.

In the later part of the model i have used set-current-plot to create a chart using the below code:

set-current-plot "Voter Support"
  set-current-plot-pen "Party1"
  plot 100 * [my-size] of turtle 0 / sum[votes-with-benefit] of patches
  set-current-plot-pen "Party2"
  plot 100 * [my-size] of turtle 1 / sum[votes-with-benefit] of patches
  if Num-of-parties >= 3 [ set-current-plot-pen "Party3"
  plot 100 * [my-size] of turtle 2 / sum[votes-with-benefit] of patches ]

so on and so forth for all ten possible turtles.

The problem is if the user has defined 5 turtles and turtle 3 dies at tick 10, then chart portion of the code is throwing an error since there is no turtle 3 but num-of-turtles slider defined by the user has a value of 5.

Please advise on how to solve this. Thanks, appreciate the help.

Regards

Upvotes: 2

Views: 444

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

When writing model code, you should try to apply the DRY principle: Don't Repeat Yourself. Creating each turtle separately and then trying to do something with each of them by addressing them separately as turtle 0, turtle 1, etc. will lead to all sorts of problems. What you're experiencing with plotting is only the tip of the iceberg.

Fortunately, NetLogo gives you all the facilities needed to deal with a "dynamic" number of turtles. ask is the primitive you will use most often for this, but there are plenty of other primitives that deal with whole agentsets. You can read more about agentsets in the programming guide.

In the case of plotting, you can ask each of your parties to create a "temporary plot pen". We'll use the who number to give a unique name to each of these pens. (That's one of the very few legitimate uses of the who number in NetLogo.)

Put this code in the "Plot setup commands" field of your plot:

ask parties [
  create-temporary-plot-pen (word "Party" (who + 1))
  set-plot-pen-color color ; set the pen to the color of the party
]

(Note that you won't need the plot pens that you previously defined anymore: you can just delete them. New plot pens will be dynamically created every time you set up your plot.)

To do the actual plotting, we can use very similar code. Put this code in the "Plot update commands" field of your plot:

ask parties [
  set-current-plot-pen (word "Party" (who + 1))
  plot 100 * my-size / sum [ votes-with-benefit ] of patches
]

Upvotes: 5

Related Questions