Abhishek Bhatia
Abhishek Bhatia

Reputation: 9806

NetLogo Debugging

NetLogo being interactive makes debugging easy, but I yet to find any tools available for setting breakpoints and stepping through code. Please guide me if such exist. Or I can achieve the same with the current setup available.

Upvotes: 9

Views: 2731

Answers (7)

Matteo
Matteo

Reputation: 2926

NB: Note that this approach is not just a convoluted way to achieve the exact same benefit that using random-seed gives. Using random-seed is an ex-ante way to reproduce a run. However, for rare errors, it is impractical to manually change random-seed (maybe a few hundred times) until you hit by chance a run in which the error appears. This approach, instead, makes you able to reproduce the error after it occurred, potentially saving tons of time by letting you reproduce that rare run ex-post.

Feel free to download this blueprint from the Modelling Commons if anyone finds it useful and wants to save the time to set it up.



Not a NetLogo feature, but an expedient I've devised recently.

Some errors might occur only rarely (for example, every few hundred runs). If that is the case, even just reproducing the error can become time consuming.

In order to avoid this problem, the following arrangement can be used (note that the code blocks only contain very few lines of code, the vast majority is only comments).

enter image description here

globals [
 current-seed
]

to setup
  clear-all
  reset-ticks

  manage-randomness
end

to manage-randomness
; This procedure checks if the user wants to use a new seed given by the 'new-seed' primitive, or if
; they want to use a custom-defined seed. In the latter case, the custom-defined seed is retrieved
; from the 'custom-seed' global variable, which is set in the Interface in the input box. Such variable
; can be defined either manually by the user, or through the 'save-current-seed-as-custom' button (see
; the 'save-current-seed-as-custom' procedure below).
; In either case, this selection is mediated by the 'current-seed' global variable. This is because
; having a global variable storing the value that will be passed to the 'random-seed' primitive is the
; only way to [1] have such value displayed in the Interface, and [2] re-use such value in case the
; user wants to perform 'save-current-seed-as-custom'.

  ifelse (use-custom-seed?)
   [set current-seed custom-seed]
   [set current-seed new-seed]

  random-seed current-seed
end


to save-current-seed-as-custom
; This procedure lets the user store the seed that was used in the current run (and stored in the
; 'random-seed' global variable; see comment in 'manage-randomness') as 'custom-seed', which will
; allow such value to be re-used after turning on the 'use-custom-seed?' switch in the Interface.

  set custom-seed current-seed
end

This will make it possible to reproduce the same run where a rare error occurred, just by saving that run's seed as the custom seed and switching on the switch.

To make things even more useful, the same logic can be applied to ticks: to jump exactly to the same point where the rare error occurred (maybe thousands of ticks after the start of the run), it is possible to combine the previous arrangement about seeds and the following arrangement for ticks:

enter image description here

to go
; The first if-statement allows the user to bring the run to a custom-defined ticks value.
; The custom-defined ticks value is retrieved from the 'custom-ticks' global variable,
; which is set in the Interface in the input box. Such variable can be defined either
; manually by the user, or through the 'save-current-ticks-as-custom' button (see the
; 'save-current-ticks-as-custom' procedure above).

  if (use-custom-ticks?) AND (ticks = custom-ticks) [stop]

  ; Insert here the normal 'go' procedure.

  tick
end


to save-current-ticks-as-custom
; This procedure lets the user store the current 'ticks' value as 'custom-ticks'. This will allow
; the user, after switching on the 'use-custom-ticks?' switch, to bring the simulation to the
; exact same ticks as when the 'save-current-ticks-as-custom' button was used. If used in combination
; with the 'save-current-seed-as-custom' button and 'use-custom-seed?' switch, this allows the user
; to surely and quickly jump to the exact same situation as when a previous simulation was interrupted.

  set custom-ticks ticks
end

This will make it possible not only to quickly jump to where an otherwise rare error would occur, but also, if needed, to manually change the custom-ticks value to a few ticks earlier, in order to be able to observe how things build up before the error occurring. Something that, with rare errors, can otherwise become quite time-consuming.

Upvotes: 0

Joebevo
Joebevo

Reputation: 163

I found Netlogo somewhat difficult to debug until I discovered print statements.

I basically zone in on the module that is causing problems and I add print statements within critical blocks to inspect the state of the variables. I have find this to be an effective way to debug.

I do wish the documentation was more comprehensive, with more code examples. Perhaps some good Samaritan will take it up as a project.

Upvotes: 0

iusting
iusting

Reputation: 8373

Not existing currently. Still, you can use one of the alternatives from above or you might take a look at user-message (https://ccl.northwestern.edu/netlogo/docs/dictionary.html#user-message), which will pop up a dialog. This will also block the execution at that step, although not providing you with a jump-to-next-line mechanism, for me this solution proved to be the best.

Upvotes: 3

Daniel
Daniel

Reputation: 1448

I have used user-message and inspect together as the debugging tool for NetLogo. Here is a video demo on how to use them to identify the cause of an error.

  • the reason for using inspect is to examine all properties of an object when we are not sure where exactly went wrong
  • using user-message to print out some instruction, output some outcome and halt the program

Upvotes: 1

Arri Ferrari
Arri Ferrari

Reputation: 83

NetLogo is all about keeping the code in one spot. When I run a simulation in 2D or 3D, I usually have an idea what my whole system is going to produce at timepoint X. So when I'm testing, I usually color code my agents, the "turtles", around a variable I'm tracking (like number of protein signals etc.)

It can be as simple as making them RED when the variable your wondering about is over a threshold or BLUE when under. Or you can throw in another color, maybe GREEN, so that you track when the turtles of interest fall within the "optimal" range.

Upvotes: -1

ToonTalk
ToonTalk

Reputation: 468

Another possibility is to do the debugging in any modern browser if/when NetLogo Web produces source maps. This way one can set breakpoints in the NetLogo code and use Chrome or FireFox or IE11's developer tools on the NetLogo code.

Upvotes: 2

Marzy
Marzy

Reputation: 1894

I am not aware of such a tool if one exists. For debugging I use meaningful print statements. First I make a switch as a global parameter to set the debug mode on and off, then I add a statement to each method that prints which method updates which variable and in which order they were called (if debug mode is on).

I also use profiler extension which shows how many times each method was called and which one is the most or least time consuming one.

Upvotes: 5

Related Questions