<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ed Baskerville &#187; EcoGillespie</title>
	<atom:link href="http://edbaskerville.com/category/ecogillespie/feed/" rel="self" type="application/rss+xml" />
	<link>http://edbaskerville.com</link>
	<description></description>
	<lastBuildDate>Mon, 06 Feb 2012 03:06:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Parameters re-thunk with annotations</title>
		<link>http://edbaskerville.com/2006/05/27/parameters-re-thunk-with-annotations/</link>
		<comments>http://edbaskerville.com/2006/05/27/parameters-re-thunk-with-annotations/#comments</comments>
		<pubDate>Sat, 27 May 2006 20:24:26 +0000</pubDate>
		<dc:creator>ed</dc:creator>
				<category><![CDATA[EcoGillespie]]></category>

		<guid isPermaLink="false">http://code.edbaskerville.com/2006/05/27/parameters-re-thunk-with-annotations/</guid>
		<description><![CDATA[I currently have code that automatically applies parameter values from a hash map to a model by reflectively looking up the appropriate setter methods. I mentioned that I&#8217;d rewrite this to use the reflection API to directly set fields. In &#8230; <a href="http://edbaskerville.com/2006/05/27/parameters-re-thunk-with-annotations/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I currently have code that automatically applies parameter values from a hash map to a model by reflectively looking up the appropriate setter methods.</p>
<p>I mentioned that I&#8217;d rewrite this to use the reflection API to directly set fields. In fact, it would be better to fully implement the Key-Value Coding hierarchy of Cocoa: first look for a setter method, and then directly set the field.</p>
<p>Directly setting the field, of course, breaks data hiding. This feature is meant for debuggers, not for applications. So, rather than just letting the standard parameter-setting capabilities set any field, there will be an annotation, <code>@Parameter</code>, to specify that a field should be accessible as a parameter to be set during a model’s <code>MyModel(Map&lt;String, Object&gt; parameters)</code> constructor.</p>
<p>There will be no <code>setParameter</code> method for use at runtime, because the only time parameters should be set is during object creation. Any class can choose to bypass this restriction by providing setter methods or declaring a parameter public, but</p>
<p>What about reading parameters at runtime? Just use the standard methods. A model will be able to access its own fields as usual. If the model wants other objects (such as its children) to have access, it should declare an accessor method.</p>
<p>I was hoping it would be possible to modify final fields using reflection in the constructor, but this is not the case (at least not with JDK 5.0—apparently it was possible in 1.3.0). That would let you do things like declaring parameters with <code>@Parameter public final int beta = 0.3;</code>, where the default value of a parameter is set in its declaration, AND it’s publicly accessible (for speed and convenience) without being modifiable, AND it gets automatically set in the constructor. I guess I can live with <code>@Parameter public int beta = 0.3;</code> (dangerous) or <code>@Parameter private int beta = 0.3;</code> + an accessor (safe).</p>
<p>In short, parameters will work like this:</p>
<ul>
<li>Parameter fields are declared with the <code>@Parameter</code> annotation.</li>
<li>By calling the <code>Model</code> parameter-setting constructor in a subclass constructor, all parameters present in the parameter map will get set automatically.</li>
<li>Normal access modifiers will apply at runtime.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://edbaskerville.com/2006/05/27/parameters-re-thunk-with-annotations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Events in EcoGillespie</title>
		<link>http://edbaskerville.com/2006/05/20/events-in-ecogillespie/</link>
		<comments>http://edbaskerville.com/2006/05/20/events-in-ecogillespie/#comments</comments>
		<pubDate>Sun, 21 May 2006 00:36:35 +0000</pubDate>
		<dc:creator>ed</dc:creator>
				<category><![CDATA[EcoGillespie]]></category>

		<guid isPermaLink="false">http://code.edbaskerville.com/2006/05/20/events-in-ecogillespie/</guid>
		<description><![CDATA[Today I figured out an interface for having agents (individuals (models)) specify events in EcoGillespie. Each possible event will be, surprise surprise, an object. I considered a few ways of structuring this object. The following were rejected for fairly obvious &#8230; <a href="http://edbaskerville.com/2006/05/20/events-in-ecogillespie/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today I figured out an interface for having agents (individuals (models)) specify events in EcoGillespie. Each possible event will be, surprise surprise, an object.</p>
<p>I considered a few ways of structuring this object. The following were rejected for fairly obvious reasons:</p>
<ul>
<li><strong>Specify events as <code>Method</code> objects</strong> Seemed like the right approach at first, because then you can just directly implement events as methods in the class. But it&#8217;s ultimately a bad idea. Doing indirect method invocation using Java reflection is way, way slower than direct method calls. Event methods are going to be called all the time. Furthermore, I&#8217;d have to wrap <code>Method</code> in an <code>Event</code> object anyway, since you might want to specify arguments to the methods.</li>
<li><strong>Use a string or int or enum</strong> The idea here is to just specify different events using some identifier that gets parsed out by a standard method implemented by the model. It&#8217;s basically indirect method calling, but faster, and uglier. Still indirect, though.</li>
</ul>
<p>The structure I adopted is to represent events using inner classes. Each event is an instance of a subclass of <code>Model.Event</code>, which the model implementor will define as, e.g., <code>MyModel.MyEvent</code>. The event includes a rate (for use in the Gillespie algorithm) and one required method, <code>perform()</code>, which actually performs the event. Because <code>Event</code> is an inner class of <code>Model</code>, it can access members and methods of <code>Model</code> directly. So with just a few extra curly braces and so on, you get all the advantages of passing <code>Method</code> objects without the slowdown.</p>
<p>A modeler could decide to have just one <code>Event</code> subclass, and add member data to store the specifics of the event. Or if there are a few distinct types of events, each of which has very different behavior, there could be separate subclasses for each type, with different implementations of <code>perform()</code>. It doesn&#8217;t matter—all the framework cares about is the presence of the rate value and the <code>perform()</code> method.</p>
<p>Pretty slick, I think.</p>
]]></content:encoded>
			<wfw:commentRss>http://edbaskerville.com/2006/05/20/events-in-ecogillespie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EcoGillespie + Eclipe + JUnit = Java Is Fun!</title>
		<link>http://edbaskerville.com/2006/05/17/ecogillespie-eclipe-junit-java-is-fun/</link>
		<comments>http://edbaskerville.com/2006/05/17/ecogillespie-eclipe-junit-java-is-fun/#comments</comments>
		<pubDate>Thu, 18 May 2006 00:23:57 +0000</pubDate>
		<dc:creator>ed</dc:creator>
				<category><![CDATA[EcoGillespie]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://code.edbaskerville.com/2006/05/17/ecogillespie-eclipe-junit-java-is-fun/</guid>
		<description><![CDATA[Why/Why Not Java I&#8217;ve had an on-again, off-again relationship with Java. It always appealed to me: it runs on every platform without modification (particularly if you&#8217;re not dealing with a GUI); since the invention of JIT compilers, it&#8217;s basically as &#8230; <a href="http://edbaskerville.com/2006/05/17/ecogillespie-eclipe-junit-java-is-fun/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h3>Why/Why Not Java</h3>
<p>I&#8217;ve had an on-again, off-again relationship with Java. It always appealed to me: it runs on every platform without modification (particularly if you&#8217;re not dealing with a GUI); since the invention of JIT compilers, it&#8217;s basically as fast as C/C++, and faster than Objective-C; and its syntax is like C++, only way less convoluted.</p>
<p>Still, a couple years ago, I probably wouldn&#8217;t have decided to do EcoGillespie in Java. Back then, Java had this weird hybrid between static and dynamic typing. Where this caused me trouble was with collections, primarily: you could put any kind of object you wanted into a collection (good general feature of collections!) but, because the Java compiler enforces type-checking strongly, when you pulled that object out of the collection, you had to manually cast it to the correct type. (The &#8220;correct&#8221; way to do something like that is to actually CHECK its type using the instanceof operator when you pull it out of the collection. But I&#8217;m talking about cases where you NEVER put anything other than one type of object into a particular collection.) In C++, there&#8217;s this abomination called templates, where the compiler automatically generates an entirely new class whenever you have a collection with a parameterized type.</p>
<p>C# added this nice thing called generics that&#8217;s the best of both worlds. Like Java collections, there&#8217;s just one class for all types of objects; like C++ template collections, instances are tied to parameterized types. The improvement on Java is that you have type parameterization, so you can specify at compile time what kind of objects are going into (and thus coming out of) your collections. But you do this without generating new classes for each parameterized collection type—rather, the parameters are set *per instance*.</p>
<p>The Java guys thought this was a good idea too, so it has made an appearance in the 1.5/5.0 release of Java (code-named Tiger, a name no doubt picked in order to cause confusion with Mac OS X 10.4).</p>
<p>There are bunch more features that make life easier: autoboxing (automatic conversion of primitive types like int and double to object types like Integer and Double), automatically iterating for loops (<code>for object : collection</code> rather than the full iterator rigamarole), and some others.</p>
<p>That was a long rant not really relevant to the meat of this post. The point is, I like Java way, way more now than I did. I don&#8217;t mind that generics work a little hackishly at the compiler level, for reasons of backward-compatibility: since I&#8217;m writing all new JDK 1.5/5.0/whatever the hell they&#8217;re calling it now code, I don&#8217;t have to worry.</p>
<h3>The Wonder of Eclipse</h3>
<p>I had heard about this free thing called Eclipse that&#8217;s all the rage these days, so I downloaded it and tried it out. At first I was confused—too many things going on! But after working with it for about a day, getting to know its way of thinking, I really like it. I&#8217;m particularly loving its refactoring features. Say I decide a method should be named something else, or have parameters in a different order: I just right-click, choose something from the Refactor submenu, tell it what I want, and not only does the method get fixed up, but every call to the method anywhere in the project gets fixed as well. Sure beats a manual find/replace, even if you&#8217;re comfortable with regular expressions (which I am ashamed to say are not second nature to me yet). Syntax coloring, completion, etc., etc., all nice.</p>
<p>One thing that annoys me: you can&#8217;t pop subviews of the window out into separate windows. You can create a new full environment window and do things that way, but I&#8217;d prefer being able to just drag a view out of the main window and have it magically turn into a single-function mini-window that logically behaves as a child of the main window. I like the one-window approach in general, but I like to be able to break things out across multiple monitors when I need to.</p>
<p>The only other annoyance is that completion doesn&#8217;t know the names to parameters (it calls them arg0, arg1, etc.)—they should integrate it with Javadoc and not just the class bytecode so this is smarter.</p>
<h3>JUnit</h3>
<p>I have also been hearing the joys of unit testing trumpeted for many years, but, being lazy and never working on mission-critical code, I never got around to using that methodology myself. Well, I read up on JUnit today and put in unit tests for the ParameterMap class in EcoGillespie, and, what can I say, it&#8217;s wonderful. For a modest increase in programming time, testing time will be greatly reduced, and test coverage will be way better. Furthermore, it just makes you write better code to write the tests first: your brain is forced to concentrate on the edge cases, as well as the usability of your classes, from the very beginning. Wunderbar.</p>
<p>Oh, and it integrates beautifully with Eclipse. And JUnit 4, which is built into Eclipse 3.2 (in development, but so far quite stable), uses a new JDK 1.5 feature called annotations to make defining test cases even easier.</p>
<p>Now that I&#8217;ve tried unit testing, I shall never go back! I&#8217;ll definitely add OCUnit tests to LilyPad.</p>
]]></content:encoded>
			<wfw:commentRss>http://edbaskerville.com/2006/05/17/ecogillespie-eclipe-junit-java-is-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallelizing EcoGillespie</title>
		<link>http://edbaskerville.com/2006/05/12/parallelizing-ecogillespie/</link>
		<comments>http://edbaskerville.com/2006/05/12/parallelizing-ecogillespie/#comments</comments>
		<pubDate>Sat, 13 May 2006 01:36:27 +0000</pubDate>
		<dc:creator>ed</dc:creator>
				<category><![CDATA[EcoGillespie]]></category>

		<guid isPermaLink="false">http://code.edbaskerville.com/2006/05/12/parallelizing-ecogillespie/</guid>
		<description><![CDATA[I haven’t even begun writing a single-thread version of the EcoGillespie, but the goal for this project is to insulate the modeler from the details of execution—including, hopefully, leaving open the possibility of multithreaded execution, and ultimately even execution on &#8230; <a href="http://edbaskerville.com/2006/05/12/parallelizing-ecogillespie/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I haven’t even begun writing a single-thread version of the EcoGillespie, but the goal for this project is to insulate the modeler from the details of execution—including, hopefully, leaving open the possibility of multithreaded execution, and ultimately even execution on a distributed-memory system (cluster).</p>
<p>The work-in-progress simulation algorithm goes something like this:</p>
<ol>
<li>
Initialize model: simply call the top-level model object’s constructor, with parameter settings, and let it do its thing.
</li>
<li>
Create a top-level simulation object that will manage sub-simulation objects, one for each processor.
</li>
<li>
Partition the model into n pieces, where n is the number of processors available to the simulation. In the case of, say, two processors and a top-level model object that contains two lower-level objects (say, metapopulations that in turn contain subnetworks), this is easy: assign one object to one processor. If there’s just one high-level network—say, one scale-free disease network—the graph will be partitioned using a mincut algorithm or something like that to ensure that connections between subgraphs is minimized.
</li>
<li>
Create a sub-simulation object for each of the pieces of the model. One thread, one simulation object. Also assign one random number generator per object.
</li>
<li>
Execute the Gillespie algorithm in parallel on each sub-simulation.
</li>
</ol>
<p>That last step is the tricky part, because the question arises: what happens when an event occurs on one thread that affects another thread? Next post&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://edbaskerville.com/2006/05/12/parallelizing-ecogillespie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reflection on the reflection API</title>
		<link>http://edbaskerville.com/2006/05/12/reflection-on-the-reflection-api/</link>
		<comments>http://edbaskerville.com/2006/05/12/reflection-on-the-reflection-api/#comments</comments>
		<pubDate>Sat, 13 May 2006 00:23:52 +0000</pubDate>
		<dc:creator>ed</dc:creator>
				<category><![CDATA[EcoGillespie]]></category>

		<guid isPermaLink="false">http://code.edbaskerville.com/2006/05/12/reflection-on-the-reflection-api/</guid>
		<description><![CDATA[Ha! It turns out I was doing too much work with my automatic parameter setting. Turns out the reflection API has methods for getting and setting field values directly. Very convenient for setting parameters, methinks. I will rework it using &#8230; <a href="http://edbaskerville.com/2006/05/12/reflection-on-the-reflection-api/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Ha! It turns out I was doing too much work with my automatic parameter setting. Turns out the reflection API has methods for getting and setting field values directly. Very convenient for setting parameters, methinks. I will rework it using those methods rather than mine.</p>
<p>These field-setting methods are still primitive compared to KVC in Cocoa. They’re intended for debuggers directly editing fields, not for dynamic application setting, so they don’t actually call setter/getter methods—they do it directly. That’s probably fine for parameter setting.</p>
]]></content:encoded>
			<wfw:commentRss>http://edbaskerville.com/2006/05/12/reflection-on-the-reflection-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WebSVN up</title>
		<link>http://edbaskerville.com/2006/05/09/websvn-up/</link>
		<comments>http://edbaskerville.com/2006/05/09/websvn-up/#comments</comments>
		<pubDate>Wed, 10 May 2006 06:08:50 +0000</pubDate>
		<dc:creator>ed</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[EcoGillespie]]></category>
		<category><![CDATA[LilyPad]]></category>

		<guid isPermaLink="false">http://code.edbaskerville.com/2006/05/09/websvn-up/</guid>
		<description><![CDATA[You can now browse the repositories for the two available projects (EcoGillespie and LilyPad) using WebSVN: EcoGillespie LilyPad]]></description>
			<content:encoded><![CDATA[<p>You can now browse the repositories for the two available projects (EcoGillespie and LilyPad) using WebSVN:</p>
<ul>
<li><a href="http://code.edbaskerville.com/websvn/listing.php?repname=EcoGillespie">EcoGillespie</a></li>
<li><a href="http://code.edbaskerville.com/websvn/listing.php?repname=LilyPad">LilyPad</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://edbaskerville.com/2006/05/09/websvn-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Today’s development notes</title>
		<link>http://edbaskerville.com/2006/05/09/today%e2%80%99s-development-notes/</link>
		<comments>http://edbaskerville.com/2006/05/09/today%e2%80%99s-development-notes/#comments</comments>
		<pubDate>Wed, 10 May 2006 05:47:21 +0000</pubDate>
		<dc:creator>ed</dc:creator>
				<category><![CDATA[EcoGillespie]]></category>

		<guid isPermaLink="false">http://code.edbaskerville.com/2006/05/09/today%e2%80%99s-development-notes/</guid>
		<description><![CDATA[I did two things with EcoGillespie development today: Renamed the Individual class Node, to reflect that it’s the superclass for anything that can be a node in a network. Wrote a temporary main() function in the DiseaseNetwork example that actually &#8230; <a href="http://edbaskerville.com/2006/05/09/today%e2%80%99s-development-notes/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I did two things with EcoGillespie development today:</p>
<ol>
<li>Renamed the <code>Individual</code> class <code>Node</code>, to reflect that it’s the superclass for anything that can be a node in a network.</li>
<li>Wrote a temporary <code>main()</code> function in the <code>DiseaseNetwork</code> example that actually instantiates an object.</li>
<li>That is, three things. I also actually tested the code that I had written so far, finding that there was a problem with the parameter-setting code (see below).</li>
</ol>
<p>One cool thing that’s in this code that I forgot to mention in the earlier post:</p>
<p><strong>Automatic parameter setting</strong> Cocoa has this fancy thing called key-value coding that lets you set properties on objects without a compile-time binding to a setter method. You just say <code>[object setValue:someValue forKey:@"someKey"]</code> and Cocoa automatically does the rest. This naturally extends to things like <code>[object takeValuesFromDictionary:dict]</code>, letting you set a whole bunch of values in one go.</p>
<p>Seems like a perfect match for setting parameters on model objects. Too bad Java doesn’t have it. If Java had support for categories like Objective-C, I could have just added a category to Object to provide this . This isn’t the case, so instead I added methods to the <code>ParameterMap</code> class to do this: <code>void applyAllParameters(Object)</code> and <code>void applyParameter(String, Object)</code>. They use the Java reflection API to look up the appropriate setter method and call it with the value set in the parameter map. This means that, as long as the model object inherits <code>Model</code>’s constructor implementation, parameters will get set automatically.</p>
<p>Finally, something I plan to start soon:</p>
<p><strong>Unit testing</strong> I’ve never been conscientious about doing this before. My first public API seems like a good place to start. I want this code to be very strong.</p>
]]></content:encoded>
			<wfw:commentRss>http://edbaskerville.com/2006/05/09/today%e2%80%99s-development-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementation notes</title>
		<link>http://edbaskerville.com/2006/05/09/implementation-notes/</link>
		<comments>http://edbaskerville.com/2006/05/09/implementation-notes/#comments</comments>
		<pubDate>Wed, 10 May 2006 01:55:00 +0000</pubDate>
		<dc:creator>ed</dc:creator>
				<category><![CDATA[EcoGillespie]]></category>

		<guid isPermaLink="false">http://code.edbaskerville.com/2006/05/09/implementation-notes/</guid>
		<description><![CDATA[The last post was heavy on the theory of the Gillespie algorithm and not so much on the details of the software. So here’s what I’ve come up with so far. Class hierarchy The root object for all objects created &#8230; <a href="http://edbaskerville.com/2006/05/09/implementation-notes/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The last post was heavy on the theory of the Gillespie algorithm and not so much on the details of the software. So here’s what I’ve come up with so far.</p>
<p><strong>Class hierarchy</strong> The root object for all objects created in EcoGillespie is <code>Model</code>. In a network model, <code>Node</code> is a subclass of <code>Model</code>. <code>Network</code>, in addition to containing a bunch of <code>Node</code> objects, is itself a subclass of <code>Node</code>, so <code>Network</code>s can be themselves linked together in a <code>Network</code>. In the disease model I’m implementing as a first example and to help guide the design of the system, the top-level model object is a <code>DiseaseNetwork</code>, and it contains a bunch of <code>DiseaseNode</code> objects.</p>
<p><img align="right" src="http://edbaskerville.com/wordpress/wp-content/uploads/2006/05/simple-class-diagram.png" alt="basic EcoGillespie class diagram" /></p>
<p><strong>Protecting the modeler from the simulation kernel</strong> A key design goal of this system is to ensure that all the things that exist in pretty much every model get taken care of in the simulation kernel and don’t have to be reinvented . Plus, there should be a simple set of conventions that ensure that the modeler doesn’t accidentally mess up this separation. A couple examples:</p>
<ul>
<li>The actual execution of the Gillespie algorithm will not be seen anywhere in a model’s code. Everything from calculating the next time to selecting the event to be executed will be handled by the simulation engine. A node will simply get called on to perform an event when appropriate.</li>
<li>Random number generation: the model will never create random number generators itself, but will simply call a standard method, <code>getRNG()</code>, to get the random number generator it should use. If I figure out a good way to parallelize this onto multiple threads, there will probably be a different RNG object per thread, and <code>getRNG()</code> will return the correct one. But the modeler shouldn’t have to know that.</li>
</ul>
<p><strong>Convenience methods</strong> There are a lot of things that are done in most models that aren’t necessarily obvious things to implement, because they’re so simple. But I will implement as many of these things that come up directly in the library, because any place you can shift complexity from the application to the framework is a good thing for code reliability. Super-simple example: say you want something to happen with 40% probability. The standard way is to get a random double between 0.0 and 1.0. If it’s less than 0.4, do it; if greater than 0.4, don’t. Because it’s so common, it makes sense to have a method in the RNG library: <code>boolean trueWithProbability(double probability)</code>.</p>
<p><strong>Use classes, not interfaces</strong> Swarm, Repast, and the Java libraries themselves make heavy use of interfaces (protocols in Objective-C) to define how objects interact with the system. This is all well and good from a theoretical standpoint, because it separates the interface completely from the implementation, and makes it possible for the same object, by implementing several different interfaces, to theoretically be both a random number generator <em>and</em> an agent. In practice, however, I find that, when building a framework, you build it with the idea of having users of the framework use a lot of functionality you’re providing—not just the interfaces, but the implementations too! By having an interface <em>and</em> an standard base class implementation, you’re just unnecessarily cluttering the library and confusing the user.</p>
<p>To see this more concretely, compare the complex forest of Java with the elegant simplicity of Cocoa. In my experience, the latter is far more intuitive and easy to work with, although that’s admittedly colored by the fact that I simply know it better.</p>
<p>If I do all this and find that (a) people actually use this software and (b) they provide good reasons for wanting interfaces, a refactoring can take place at that point. But I don’t anticipate it.</p>
]]></content:encoded>
			<wfw:commentRss>http://edbaskerville.com/2006/05/09/implementation-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EcoGillespie: preliminary notes &amp; code</title>
		<link>http://edbaskerville.com/2006/05/09/ecogillespie-preliminary-notes-code/</link>
		<comments>http://edbaskerville.com/2006/05/09/ecogillespie-preliminary-notes-code/#comments</comments>
		<pubDate>Tue, 09 May 2006 08:52:54 +0000</pubDate>
		<dc:creator>ed</dc:creator>
				<category><![CDATA[EcoGillespie]]></category>

		<guid isPermaLink="false">http://code.edbaskerville.com/2006/05/09/ecogillespie-preliminary-notes-code/</guid>
		<description><![CDATA[Last fall, I implemented a model for Mercedes Pascual that, inspired by some models by David Alonso, uses an exact, “event-based” stochastic simulation of disease spreading on a network rather than the usual (and epistemologically unsatisfying) discrete-time approximation. Rather than &#8230; <a href="http://edbaskerville.com/2006/05/09/ecogillespie-preliminary-notes-code/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Last fall, I implemented a model for Mercedes Pascual that, inspired by some models by David Alonso, uses an exact, “event-based” stochastic simulation of disease spreading on a network rather than the usual (and epistemologically unsatisfying) discrete-time approximation. Rather than representing time as a series of discrete time steps during each of which a whole bunch of things happen (how most models done in <a href="http://repast.sourceforge.net">Repast</a>, <a href="http://www.swarm.org/">Swarm</a>, etc. work), time is continuous (to the appropriate IEEE 754 approximation), and events occur akin to chemical reactions in the <a href="http://scholar.google.com/url?sa=U&#038;q=http://pubs.acs.org/cgi-bin/abstract.cgi/jpchax/1977/81/i25/f-pdf/f_j100540a008.pdf%3Fsessid%3D6006l3">Gillespie algorithm</a>.</p>
<p>To be clearer: before each event, you can think of the system as being comprised of a whole bunch of Poisson processes, each of which corresponds to one of the events that could be the next event to happen. As with failure rates or chemical events, you can draw from an exponential probability distribution to figure out how long it will be until the next event—the lambda parameter is simply the sum of the rates of all the “Poisson” processes (possible events). Then you can decide what the next event actually is by drawing from a discrete probability distribution, weighted with the individual rates for the individual processes. The tricky step here is that every event can change the</p>
<p>One important advantage of these models over discrete-time simulations is that they map perfectly onto differential equations. For example, say you’re starting with a basic SIR (Susceptible-Infected-Recovered) model of disease spreading. Assuming a well mixed system, you can set up a set of differential equations relating the rates of change of the three classes to the densities of the other three classes alone. If you want to relax the assumption of well-mixedness, start by setting up a Gillespie-type model with a fully connected network, so that any individual can be infected by any other individual, with rates proportional to the coefficients in the ODEs. Assuming proper scaling and all that, if you run the model a bunch of times and average the runs, the graph should look exactly like the ODE’s. So you can check your model against the ODE first, and then begin changing assumptions (like the network structure).</p>
<p>This is such a nice approach to building ecological models (or any other kind of agent-based model) that I thought I’d build a general software framework for others to use. The idea is to let the modeler write the software without dealing at all with the actual simulation algorithm, because that remains constant from model to model. All the modeler has to do is define two things: (1) the code for individuals to perform events; and (2) the relationships of individuals in a network structure.</p>
<p>When an event occurs, the affected individuals will have a chance to tell the framework that their list of possible events have changed. The framework then updates any necessary data structures, simulates the next event, and repeats. This makes it really easy to build models and know that you don’t have some subtle problem in the basic algorithm.</p>
<p>The framework approach also means that any improvements made to the core simulation algorithm will help any models implemented using the framework. If I figure out a way to parallelize the algorithm on two processors (or, hell, across a whole network), there’s no extra work for the modeler. If I implement a faster algorithm for drawing from discrete probability distributions, models will just get it for free.</p>
<p>The basic algorithm is as follows:</p>
<ol>
<li>Setup: create a discrete probability distribution based on the Poisson rates of all “reactions” (possible events) in the system.</li>
<li>Calculate the time until the next event by drawing from an exponential distribution with the sum of all the individual Poisson rates as the parameter.</li>
<li>Choose the event to occur by drawing the event from the distribution. <a href="http://theory.stanford.edu/~matias/papers/MVN03.dynamic_rv_gen.pdf">This algorithm</a> (Matias, Vitter, Ni) seems to be the state of the art in drawing from discrete probability distributions, but a modified version of <a href="http://scholar.google.com/url?sa=U&#038;q=http://www.cise.ufl.edu/~raj/vari12.ps">this</a> (Rajasekaran, Ross) that I came up with independently in the shower is all I’ve implemented so far.</li>
<li>Perform the event, updating the probability distribution for the next event in the process. The update process is an important part of how these algorithms are fast, of course.</li>
<li>Repeat from step 2.</li>
</ol>
<p>My framework, which I’m uncreatively calling EcoGillespie, is based on what I think is a pretty elegant object structure. At the top level of the simulation is a <code>Model</code> object, which, you guessed it, represents the model of the whole system. The <code>Model</code> could be just a single object with a set of events that can happen to it at any point in time. However, it could also be a <code>Network</code> object, which is a subclass of <code>Model</code> that contains a network of individuals, each of which manages its own set of events at any point in time. (Conceivably, one could—and maybe I will—write <code>Model</code> subclasses specifically for lattices, or for continuous spaces.) Of course, each of those could in turn be networks, so you can have an infinite hierarchy of <code>Model</code> objects. No matter how many levels you have, you’ll never have to touch the details of scheduling events and drawing from probability distributions: all you have to do is say what events can happen, and at what rate, and the simulation kernel will handle the rest.</p>
<p>That is, it will work that way when it’s done. The code doesn’t do much yet, but the basic structure is slightly apparent from what I’ve written so far. I’m hosting my development live on this site:</p>
<p><a href="http://code.edbaskerville.com/svn/ecogillespie/">http://code.edbaskerville.com/svn/ecogillespie/</a></p>
<p>I will track my progress on this blog.</p>
]]></content:encoded>
			<wfw:commentRss>http://edbaskerville.com/2006/05/09/ecogillespie-preliminary-notes-code/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

