Forgot your password?
typodupeerror
Image

Book Review: The Nature of Code 37

Posted by samzenpus
from the read-all-about-it dept.
eldavojohn writes "I kickstarted a project undertaken by Daniel Shiffman to write a book on what (at the time) seemed to be a very large knowledge space. What resulted is a good book (amazing by CC-BY-NC standards) available in both PDF and HTML versions. In addition to the book he maintains the source code for creating the book and of course the book examples. The Nature of Code starts off swimmingly but remains front heavy with a mere thirty five pages devoted to the final chapter on neural networks. This is an excellent book for Java and Processing developers that want to break into simulation and modeling of well, anything. It probably isn't a must-have title for very seasoned developers (unless you've never done simulation and modeling) but at zero cost why not?" Read below for the rest of eldavojohn's review.
The Nature of Code: Simulating Natural Systems with Processing
author Daniel Shiffman
pages 520
publisher The Nature of Code
rating 9/10
reviewer eldavojohn
ISBN 978-0985930806
summary A book concentrating on the simulation of natural elements through both basic and advanced programming concepts in Processing.
First off, I feel like defining the audience of this book is very important to avoid disappointment. This book is not for someone who's already developed games or modeled highway traffic or knows how to build their own physics engine. No, this book is geared at the people who are familiar with one language (preferably Java or Processing) and want to get a taste of all of the above. This book is possibly suitable for a someone new to the world of programming who is willing to put in the extra effort of coming up to speed on Processing in tandem with the text. After all, Processing is a comparatively forgiving language with a dead simple API to interact with the mouse and draw/animate objects.

I'd also like to address the "exercises" that are found throughout chapters and at the end of chapters in this book. They are excellent. I picked a couple and invested actual time in fleshing them out and I feel like Shiffman succeeded in inserting a wide range of difficulty. Leading along through each chapter, it is easy to successively complete each new exercise while the end of the chapters present stretch exercises. In addition to that, applicable chapters urge and provoke the reader to utilize newly learned concepts into what Shiffman calls "The Ecosystem Project." Where the user is basically defining an ecosystem and continually adding new animals, new movement patterns, new behaviors like predation and finally artificial intelligence.

Lastly this book can be found in many formats and I read the first half as HTML with animated diagrams. While the animated diagrams were awesome and added greatly to the text, I still found myself enjoying the dead tree book much more. I know I will soon be a dinosaur with shelves of needless weight that people will mock but I cannot make the jump to reading on a screen. The book's binding and paper quality is average as it appears to be from Amazon's CreateSpace. Diagrams that would animate are shown in the book as having progressively darkening shadows of the paths of objects and is fairly easy to envision movement. I did love the HTML version's moving examples though!

The introduction of this book brings up a few fundamental concepts on randomness like random walks and Perlin noise as well as a bit of statistics. For being labeled "Introduction" this chapter is fundamentally important and the aforementioned concepts are referred back to throughout the rest of the book. The book immediately dives into code snippets of a very simple nature that are easy to run and understand. Great detail and careful explanation are found throughout these opening chapters. The user is given informational boxes going further in depth to certain concepts. This was done really well in the first five or so chapters and was rare if even present in the final chapters.

The first chapter is devoted to vectors. It does an excellent job of explaining why they are so important as well as define and code mathematical concepts that affect vectors. A great aspect of this chapter is that the author fleshes out PVector functionality before your eyes to better understand Processing and object oriented programming. New (to beginners) ways of representing and implementing velocity and acceleration using vectors are explored at their most basic levels.

The second chapter moves naturally enough to forces on objects and begins to delve in basic physics formulas. Newton's Laws are modeled as well as friction, aerodynamics, fluid dynamics and gravity. Shiffman does a great job of keeping these unruly topics in easy to understand language while at the same time offering the scary looking formulas. He even goes so far as to insert an informational box imploring the reader to not be afraid of scary looking formulas by breaking down friction. I feel like one of the strengths of this book is showing how a complex looking formula can be deconstructed to easy English and then further implemented roughly in a model in Processing. While this modeling is by no means completely accurate or state of the art, it is a good introduction and would likely suffice for simple games and web design.

The third chapter brings angles into the mix by concentrating on oscillations. While it does a great job of talking about the important aspects of trigonometry, the text does really follow through with recalling these concepts. For instance, the mnemonic device SOHCAHTOA from geometry class is briefly explained and subsequently dropped. We use it in later chapters but it is used implicitly and may be difficult for people who are not intimately familiar with it to see the trigonometric reductions employed for simplified coding of the visualizations. Shiffman does an excellent job at starting with something that looks like a complex system, breaking it down to its component vectors and showing incremental changes to the code that iteratively improve on the visualization at hand. In doing so he gives an example of how a modeling programmer should think and work through known physical behavior to derive something that works visually in Processing.

Next up is particle systems. The reader is introduced to simpler ways of maintaining a set of particles as we start to focus on multiple particles with complex interactions. Shiffman opts to keep it simple and shies away from coding aspects like ArrayList versus LinkedList versus HashMap. Instead minimal space is spent on side ventures and the particle systems are surprisingly easy to get off the ground. The user is introduced to polymorphism, inheritance and more advanced class constructs so that the user can reduce the amount of code required to activate, handle and delete heterogenous groups of particles. For a beginning developer this chapter is great at walking them through these more advanced concepts and helping them see a direct benefit to the code.

So far, Introduction through Chapter Four of the book, everything has been great. Shiffman points out that there are a plethora of physics libraries out there in any imaginable language of any imaginable quality. And, consequently, it's unlikely you're going to forge forward with the aforementioned concepts and find yourself making the next engine for latest blockbuster space shooter game. As a result, Chapter Five is an overview of how to interact with physics libraries and use your Processing sketch as a facade that just queries said library for position. Box2D is the first library he tackles and with good cause — it's the same engine used by Angry Birds. And that's great because it is certainly empowering to know that if you can skin a simple game that adds a few game rules to physics, you can make a billion dollars. I learned a lot from this. I have never interacted with a physics library like this before and it was easy to produce fluid and impressive results. But it felt like glue code and it also felt like this text could be deprecated with a large update to Box2D (or it's Java and Processing equivalents). This really is a necessary and helpful chapter for this book but I felt sad that we had so quickly given up on rolling our own physics library. After Box2D, Shiffman presents VerletPhysics and provides a helpful resource for when you should use one over the other. Also, the terms for interacting with the libraries are slightly different but represent the same concept (side note: I wasn't a big fan of the convoluted names these two libraries used to designate objects and object types).

Chapter Six shows the reader how to emulate an autonomous agent by introducing "desired" vectors to each object. In this case it is a race car trying to reach a target. As the object moves, the desired vector is a updated. Examples of code are provided that show the object overshooting its target and Shiffman progresses on his path of slightly improving it by algorithmically adjusting the desired vector by introducing a slowing magnitude upon approach of the target. The actions of the object become more complicated as a flow field is suggested instead for behavior. The author explores path following and how to introduce a bit of wandering around straight line like an ant following a pheromone trail or a person walking along a wall. Simple examples of group behavior like even spacing in a crowded group or flocking in a sparse population within a large space. Lastly this chapter covers a very important aspect of code: performance. By now the reader has seen many examples where code can run slowly and this chapter's continual pairwise updating of all objects on the screen brings up Big O Notation. I wish Shiffman would spend more time on this or at least provide a separate box with more technical information on it like he did with other concepts.

The seventh chapter takes an interesting turn into cellular automata. While an interesting chapter and an interesting concept, it feels a bit disjoint from the rest of the text. While there is a way to tie it back into the long running ecosystem project. The most important aspect of cellular automata is that they are fun visualizations where as other concepts in programming that revolve around mutating state might not be as readily visible (like finite state machines or Markov models). This is the first chapter that feels a little rushed and more like a brief foray into a potentially deep field. The Game of Life is covered but only in its simplest aspects and I feel like this chapter could be better.

Chapter Eight dives into fractals. Again, like the last chapter, it is a bit short but I enjoyed this chapter. They are a great visual way to introduce newcomers to recursion and get them excited about it. On top of that, Shiffman shows how fractals appear in nature. Koch curves and Sierpinski triangles as fractal visualizations. Shiffman has a great informational box discussing the "monster" curve and tantalizes the reader with the paradox that an infinite recursion of the Koch curve results in an infinitely long line in a finite area of paper. This sort of stuff is what makes reading a book like this enjoyable and drives people to delve deeper into this concepts. I only wish the book had more of this. Also crucial to recursion in this chapter is a processing feature new to me: pushMatrix() and popMatrix(). As these are built out into trees, the author moves on to L-systems as devised by Aristid Lindenmayer. It's amazing how this simple grammar could result in a simulation of an algal growth.

The ninth chapter helps the user through a high level overview of genetic algorithms. I think one thing this book lacks is caution or warning about jumping into concepts or using concepts just because they sound cool. While genetic algorithms sound cool and futuristic, I have rarely found them to be at all useful on a professional level. Shiffman does a great job of explaining precisely how selection is determined by defining the constraints of the environment as well as the evaluation function. Unfortunately I find that these things are often hard to define and it's warnings like these that the text lacks. Nevertheless, there are a few good examples picked out for coding — unsurprisingly they use the laws of physics we just discussed and a number of computable variables for valuation. The best example is the rocket ship which is introduced after the standard monkeys trying to type the works of Shakespeare at a typewriter. Shiffman does a great job of explaining genetic algorithms and it's certainly a neat topic that's fun to think about but I'm not sure it's a good practical fundamental aspect of coding. It definitely works for the simulation side of coding so it should stay in the book but again it feels rushed with a lot of the simulation application left to the reader in the ecosystem project. I think that a much longer chapter that models predation — like wolves and rabbits — might work a lot better. You could even tie in a little bit of math and show situations where not enough mutations cause the hunter or prey to settle in on local maximums.

The tenth and final chapter briefly covers neural networks. Again, this chapter felt rushed and was missing a lot of the great explanations that were present in the first half of the book. The scant thirty five pages covers peceptrons, neural networks, training vehicles with them and even backpropagation of multilayered neural networks to hand more complex classification demands. In an effort to give this chapter some fun visualizations, the last thing Shiffman covers is the animation of the operation of a neural network. I'm intimately familiar with all these topics but the pace at which this chapter moves might be too much for a starting developer. I feel like there's a huge opportunity in this chapter to more thoroughly explain neural networks and to get readers more excited about classification systems in code.

All in all, the book was thoroughly enjoyable and I really enjoy that it is a creative commons work with both a github for the source code and the raw book. Although the latter chapters could use a lot of additional work this book is a great beginning tool for people who wish to start modeling nature in visualizations quickly and easily.

You can purchase a physical copy of The Nature of Code from amazon.com or you can name your price on a PDF directly. Slashdot welcomes readers' book reviews — to see your own review here, read the book review guidelines, then visit the submission page."

*

This discussion has been archived. No new comments can be posted.

Book Review: The Nature of Code

Comments Filter:
  • When I saw "book source", latex was expected.
    (As I am incompetent artist, I seek latex book sources to take something consistent for my projects)

    No, it is done in ASCIIDOC - yet another book making program.

    While TeX and LaTeX are old, they are still excellent tools for the job.
    What makes alternatives better? What can not be done properly by TeX?

    BR
    s52d

    • Re: (Score:3, Interesting)

      by dshiffman (2812203)
      ASCIIDOC was a somewhat arbitrary choice. The goal for this project was to be able to easily generate the book in a variety of formats. Since ASCIIDOC can parse into HTML rather easily, I was then able to generate a web version of the book, a PDF for print, and ultimately I hope to be able to generate ePub and Mobi as well. LateX is a fantastic tool as well.
  • ...do I have to read before I have any idea what the book is about? Crappiest book review ever. Is the book of similar caliber?

  • I really hate buying books about a topic and then it having exercises for me to figure stuff out I bought the book for. Not everybody is a student. At least Knuth gives the answers in his books.
    • The answers to the exercises are included in the github repo. As of now I probably only have 50% but I'm adding in the rest as fast as possible.
    • by turp182 (1020263)

      And I really like it. Problems really help in terms of practical learning (as opposed to foundational learning, which I consider reading to be) and retention, at least for me.

      I also use previous Top Code competition problems to hone my algorithm skills, but I don't compete.

Mommy, what happens to your files when you die?

Working...