Ruby meets System Dynamics!

By 0 No tags Permalink 0

Systems Dynamics = A way to model all sorts of systems as a set of feedback relationships.

Ruby = A fun, expressive programming language.

Put ‘em together and you get a flexible and expressive way to model feedback systems! I call it Rubynamics. It’s a small library that follows the ever popular “internal DSL” pattern, allowing this

growth_frac = 0.10
stock :account, 100, rate { account * growth_frac }

to yield a bunch of data that looks like this

 

As you see, the syntax to create a stock like the above  is simple:

   stock "stock-name", initial-value, rate-in, rate-out

The feedback is achieved by using the stock’s name within the rate-in equation. In the above example, { account * 0.10 } means that each successive value of account is 10% of the current value.

So far, I’ve been able to create the basic feedback patterns–positive feedback, negative feedback, s-shaped, and oscillating–using simple Ruby statements like the one above. A more interesting example is the rabbit population model from the Road Map series. These declarations,

will yield this behavior (yes, I know, the graphing is a little strange, but the point is clear)

I’m working on making the package more useful by adding better graphing and data output formatting. I’m hoping to get a stable version out before the end of the year. While I doubt there will be much of an audience for this library, I’ve enjoyed learning more about system dynamics modeling by wrapping a useful simulation engine in a simple interface. I think others with a mind to learn system dynamics by tinkering with simulation implementation will find this useful.

But why ever would anyone want to use Ruby for system dynamics modeling! I think there are a few good reasons:

  1. The library is all Ruby, and all of Ruby is available. The useful edge of this double-edged sword is that the modeling sandbox is really big. For example, if a model of stock performance relies on current prices, that data can be refreshed automatically during each simulation run.
  2. A consequence of 1 is that simulations could become operational. This is quite speculative on my part, but I imagine models that don’t simply run to completion and spit out a bunch of data. Instead, I wonder if models can be embedded within a larger system, providing information about model state which can then be used to make automated decisions.
  3. The source is (will be) free and open. All implementation details are available, so Rubynamics can be easily altered or extended. Not only might experienced modelers find this aspect useful, but I suspect that student modelers may learn more about the nature of feedback modeling by having access to the implementation logic. Nothing teaches like tinkering.

Of course the big downside is that you’d have to know Ruby to take full advantage of the library. But the model language itself is designed to be simple (there are five nouns right now) and intuitive, so even without broad knowledge of Ruby, the library might prove use a useful scratchpad for modelers.

Comments are closed.