Skip to main content

8.5) PV Installation Idea (cont'd)


If you run the model now, you will get an run-time error, probably saying that by running this procedure NetLogo can’t make a division by 0. This is not a syntax error as the syntax is correct, it means that for some turtles count neighbors with [any? turtles-here] in-radius sensing-radius reports a 0, or some turtles do not have any other houses within their sensing radius. To avoid this runtime error we use a little bit of defensive programming, and pack the if statement we just wrote into another if statement, allowing to run the second if statement (which caused the problem) only, if the turtle does actually have any neighbour patches in the sensing-radius with PV on it. This does not only avoid the runtime error but will make a huge impact on the speed of the program.

The complete determine-idea-to-install-PV procedure should therefore looks like:

to determine-idea-to-install-PV
   if random-float 1 < 0.001 [set PV-building-decision true]
   if any? neighbors with [PV] in-radius sensing-radius [
      if (count neighbors with [PV and any? turtles-here] in-radius sensing-radius /
          count neighbors with [any? turtles-here] in-radius sensing-radius) > neighbor-threshold
          [set PV-building-decision true]
   ]
end