Utopia 2, or Why the rich gets richer and the poor gets poorer

Some time back (more than 4 years ago) I experimented with Craig Reynold’s boids concept. I suppose everyone does boids some time or another. I called mine Utopia and wrote a simple simulation game around it, where two groups of opposing boids clashed with each other, try to eat each other up. Macabre, I know but it was pretty cool.

I was reading The Origin of Wealth by Eric Beinhocker recently. One of the chapters described Epstein and Axtell’s research in Sugarscape and it reminded me of Utopia. I thought why not to try reproduce it with some of what I did in Utopia but in Ruby? After some initial hiccups trying to find the best GUI toolkit to create Utopia2, I decided to use JRuby and Swing.

A bit of background on the subject matter. Joshua Epstein and Robert Axtell are researchers in the Brookings Institution, a public policy think-tank in US. The Sugarscape project was basically an attempt to use ideas from complexity theory and artificial life on to the social sciences. The goal is to see if behaviours we see in real societies can be modelled using just simple local rules, very much like the three basic rules used in modelling boids.

Sugarscape simulates the behavior of artificial people (agents) located on a landscape (50 x 50 grid) of a generalized resource (sugar, hence Sugarscape). Agents are born onto the Sugarscape with a vision, a metabolism, a speed, and other genetic attributes. Their movement is governed by a simple local rule: “look around as far as you can; find the spot with the most sugar; go there and eat the sugar.” Every time an agent moves, it burns sugar at an amount equal to its metabolic rate. Agents die if and when they burn up all their sugar.

So, at each turn the agents follow these basic rules:

  1. Look around itself, within the constraint of its scope of vision (north, south, east and west)
  2. Determine which direction has the most sugar
  3. Move to that square and if it has sugar, eat it
  4. When it eats some units of sugar, it gains that amount while the sugar on the square is decreased by the amount that is eaten
  5. Every turn the agent will consume a certain amount of sugar according to its metabolism
  6. If the agent runs out of sugar it will die. The agent also has a preset lifespan which is reduced every turn until it expires

Sugar is distributed unevenly in Sugarscape, some cells have higher concentration of sugar while others have lower and two sugar ‘mountains’ are formed at the top right and bottom left of the simulation. Sugar that is consumed is also grown back at the rate of one per unit of time, so the food never really runs out.

The simulation starts with 250 agents started at random places. The diagram below shows the output of the basic model, from Epstein and Axtell’s research.

Utopia2 follows a very similiar logic as with Sugarscape but instead of agents and sugar, I used the terms alf (Artificial Life Form) and food. There are also a number of differences:

  1. While Sugarscape is a 50 x 50 grid, with each agent occupying a cell, Utopia2 is a 600 x 600 canvas and each alf occupies a single point
  2. Instead of looking at the 4 directions only, each alf scans a radius of its vision scope to see if there is any food within its scope. If there is, it will move towards the nearest visible food, otherwise it will randomly wander to a direction. This differs from Sugarscape because the Sugarscape agent always goes to the direction where it is most likely to have food, whereas the Utopian alf has no idea where the most likely food is, beyond its scope of vision
  3. The alf also has a lifespan which amount is randomly assigned and is reduced every round. But more importantly if it runs out of sugar, it will not die immediately but it will increase its starvation rate. The alf’s starvation rate quickens the reduction of its lifespan and once its lifespan is 0 it will die
  4. Lastly, while the Sugarscape agent moves one cell per turn, the Utopian alf has different speeds which is also a randomly distributed endowment. This is actually why the alf is more life-like. Of course my alfs also have a cool-looking ‘tail’ :)

Besides this, in Utopia2 while food grows back, it doesn’t do so immediately but grows back at a different rate. The result is as shown in this YouTube clip.

Here are some still shots of the simulation and two charts that trace the the progress of the simulation.

The first chart shows the food level of each alf over the progression of the simulation. In t=0, the alfs’ food levels are evenly distributed, that is, some have more food than the others but overall, the gap is not large. As the simulation progresses, you’ll notice that the chart becomes a sort of power law graph, where there are some alfs with a lot of food, while most will eventually have either very little or none (dead).

The second chart shows the number of alfs compared with the amount of food (in ranges) it has. I took the food levels of all the alfs, and divided the difference between the most food with the least food with a 13 different ranges or food ‘bins’. Then I sorted the alfs accordingly into each bin. In t=0, the distribution, as expected is quite even, and it forms a sort of uneven bell curve. This is expected as the initial food levels of the alfs is a random distribution.

However, as the simulation progresses you’ll notice the chart starts to skew towards the left, with few very ‘rich’ alfs with a lot of food to the right, a long tail of ‘middle class’ alfs and a lot of alfs with little or no food (dead). In fact, in certain simulations, while the alfs start of with a random food level of between 0 – 100, the super-rich alfs can reach levels of more than 7,000!

You’re right if you notice that these two charts basically potray the same thing, and in fact it is the same results that Epstein and Axtell came up with from their Sugarscape model.

So what does Epstein and Axtell’s research show?

In 1895, Italian economist Vilfredo Pareto collected income data from a number of countries and created a distribution chart called the Pareto distribution. Rather than a bell-shaped curve, which it will be if it is a normal distribution, it was one with a few very rich people, a wide range of middle class people and a large number of poor people, what is sometimes also known as power law distribution. In fact you’d probably have heard of this one before – this is the 80-20 rule, where 20% of the people owns 80% of the wealth.

And the Sugarscape (and Utopia2) simulation model shows this.

The question is, why does this happen? Why does the rich get richer and the poor gets poorer? Two obivious answers to this question are:

  1. Because of their genetic predisposition (nature) i.e. because they were ‘born’ with better endowments – with more scope of vision, with larger appetites, with faster speed etc
  2. Because of their location (born with a silver spoon) i.e. because they were born at the right place at the right time

However if we dig deeper, both answers are not right, because in each case the distribution is random and therefore bell-curve distribution, if these are the reasons then the final distribution of wealth should also be bell-curve.

The real answer, apparently is described in complexity theory. The skewed Pareto distribution we see is an emergent property of the system, and not because of a single cause. Without going in depth, what it means is that from a simple set of rules rises a new complexity that cannot be easily derived from the existing set of rules.

If we apply it to the real world, the parallels can be that there is no single easy cause of why the rich gets richer and the poor gets poorer, not only because of oppressive explotation by the rich, or globalization, or protectionist policies or the laziness of the people or the color of the skin or whichever the cause of the day is.

If you want to try out Utopia2, I’m making it available here, under Apache 2 license. You’re free to take it up and do whatever pleases you, but I’d appreciate it if you reference me if you do use it. You’ll need JRuby, of course, and JFreeChart.

By the way, this model of the Sugarscape simulation that I have just described is just the basic model. The full work done by Epstein and Axtell is described in their 1996 book Growing Artificial Societies: social science from the bottom up from MIT Press. Utopia2’s implementation to date follows this basic model only, however it is really quite simple to program the other more complex models. Let me know if you use Utopia2 to do this, I’ll be interested in knowing how you do it.

Sending out emails from Seaside/Squeak

I think something that probably no-one has covered so far in any Seaside blogs is how to send SMTP emails through Seaside or Squeak. If you’re doing a public Seaside application you’ll probably end up having to send emails for various reasons like resetting of passwords or alerts to users etc, so this is something useful to have up your sleeves.

There are a number of ways to send an email in Squeak and Seaside and I know of 3 at the moment.

1. There is a utility class, which contains only class-level messages, called SeasidePlatformSupport, and it has a message called ‘deliverMailFrom: to: text: ‘ that sends a mail directly. However, you’ll need to set the SMTP server first, by using MailSender.

This is a code snippet you can cut and paste directly on to a workspace to evaluate(though obiviously replacing the appropriate email addresses):

MailSender setSmtpServer.
SeasidePlatformSupport deliverMailFrom: '<fromEmail>'
to: #('<toEmail>')
text: 'Subject: Hello from SeasidePlatformSupport!
Sending from SeasidePlatformSupport'.

Don’t worry that the setSmtpServer message doesn’t take in any parameters. What happens is that MailSender pops up a dialog box for you to enter the SMTP server. But you’ll need to enter an SMTP server that doesn’t need authentication because there isn’t a way to set the user name or password from this mechanism. Of course you can’t set the mail server from Seaside either (the dialog box is Morphic) but you can run the MailSender once from the Squeak environment and you’ll be good for the rest of the application. Notice the ‘to’ is actually an array of recipients.

2. The second way is to go directly to the MailComposition class that SeasidePlatformSupport is using (through MailSender). This is another piece of code snippet that you can directly evaluate from workspace:

MailSender setSmtpServer.
msg := (MailMessage new setField: 'to' to: '<toEmail>';
setField: 'from' to: '<fromEmail>';
setField: 'subject' to: 'Hello from MailMessage!';
body: 'Sending from MailMessage'; yourself).
(MailComposition new sendMailMessage: msg) submit.

Again you’ll need to set the MailSender SMTP server first. There isn’t any much difference in doing it this way, as far as I can see, but this is another way you can send emails.

3. Another way is directly go to the underlying guts of the other two mechanisms and use the SMTPClient class itself to send the email. SMTPClient subclasses TelnetProtocolClient and if you dig a bit deeper you’ll realise that it does nothing but to open up a socket session at the appropriate port and send in the SMTP commands as text.

username:= '<aUsername>'.
password:='<aPassword>'.
client := SMTPClient openOnHostNamed: '<yourSmtpServer>' port: <portNumber>.
client user: username; password: password; login; initiateSession.
client mailFrom: '<fromEmail>';
 	recipient: '<toEmail>';
 	data:
'Subject: Hello from SMTPClient!
From: <fromEmail>
To: <toEmail>
Sending from SMTPClient!'.
client quit.

This is actually the underlying code that sends the mail in the first two mechanisms though they are couched by different classes. Of course ultimately you can use TelnetProtocolClient itself to do the work but that’s a bit going down to the basics too much :)

Note that this last method of sending emails you can authenticate the SMTP server and also set the port number.

JavaOne 2007, San Francisco, Day 4 (final day)

So the final day has arrived and as in previous years, JavaOne ended on a rather quiet and subdued note. The general session on the last day was by James Gosling (for those uninitiated, the ‘Father of Java’) and his session was called ‘The Toy Show’. It featured a whirlwind of 12 demos aimed to display the wide range and variety of uses in the industry, from meat weighing machines to toy robots to terrain scanning helicopters. It was a pretty interesting session which highlighted the extended reach Java have in all aspects of software development today, though I dare say that this is really nothing compared to the reach C has in the market.

The only interesting technical session (at least in my opinion) that followed was the one on Java ME data binding by Sun Microsystems. I missed the earlier one where a new flashier UI using SVG was discussed but nonetheless, it was quite interesting how much further Java ME is going to go in the coming months and years.

I met up with Watt after that and he brought me across the Golden Gate Bridge to Sausalito for dinner where we had California styled sushi, which for the record tastes nothing like the Japanese food I have so often in Singapore.

I’ll be heading back to sunny Singapore tomorrow with a tired body but fresh new ideas and technologies. It was a good trip all in all.

sausheong-goldengatebridge.png

JavaOne 2007, San Francisco, Day 3

Today’s sessions were a mixed bunch. I started off with a rather droning session in the morning, coupled with a strong accent from the presenter (I think he’s from Russia or some Eastern European country) and the late night last night made me drowse off half the session on object-oriented databases in Java ME. I feel rather guilty on this because this is a rather useful utility that is open sourced under GPL.

The session that got me awake and interested was the one on JavaFX Script, previously known as F3 (Form Follows Function), the much talked about new technology from Sun. It really isn’t Java the programming language per se, it’s a totally different new programming language, as Chris Oliver, its inventor explained, but one running on the Java platform. Although it’s called a JavaFX Script, it’s not really a scripting language either. Confused? I think most people would be, considering that it just came out a couple of days ago and it’s inventor claims that it’s already a misnomer.

Anyway, the ultimate aim for JavaFX Script was to be compiled into bytecodes and not as it is today interpreted by a JavaFX interpreter. It is also supposed to be ultimately deployed on the browser through the Java Platform (today it’ll be the Java Plugin) althought right now it’ll run only on the desktop or through Java Web Start (there’s a cool calculator at Chris Oliver’s blog that uses this). The concepts are pretty interesting — it’s took the idea of a component based GUI toolkit (like Swing), peppered it heavily with influences from SVG and delivered it as a full programming language that is statically typed to allow a developer to create very compelling UIs.

So what does it have that others don’t already have? Frankly speaking I have no idea. If anything I thought Actionscript already does something like this in Flash and I’d imagine Silverlight would have too though I’m unfamiliar with either. From the session and the Q&A subsequently what I gathered is that there probably isn’t anything else other than making the developer more efficient and effective.

Another interesting session that I attended was on data mining. In fact I just got to know that there was a JSR-73 that was for data mining in Java. I went for it as I was mighty curious on data mining (after the several report related issues in office recently) and I was quite surprised at the complexity and sophistication in this domain. In fact many of the things which I have never thought was related to data mining, was related to data mining. For example one of the things I took for granted was ‘cross selling’, which is to say, if you buy beer, it could be a good idea to try to sell you chips as well (e.g. in put them together in a supermarket) as it has a very high chance of success. There is in fact a data mining method called Market Basket Analysis which provides algorithms to do this. Very cool.

I also attended a session on BD-J, that is, writing Java applications on the Blue-Ray disc. This is mostly revolving around writing the startup menus and other add-ons like games and such for DVD releases, though I can imagine other things can be done on it in the future once it’s properly released. I thought it was pretty cool as well though it’s way out of my domain (mostly it’s for studios and production houses — one of the speakers who was sharing experiences was from Disney).

After soaking up tech stuff for most of the day, I went for the After Dark Bash and it was quite a circus sideshow. The first thing that greeted me when I went in was a mini-concert by a group of ‘vertically-challenged’ entertainers. That is to say, they were a bunch of dwarf singers decked out like KISS (the heavy metal group) belting out heavy metal songs. There were various kinds of food and drinks (two drinks coupons per person) like burger, hotdogs, popcorn, beer, wine, soft drinks and the like and the whole hall was turned into a massive entertainment center with pinball machines, XBoxes, mini-inverse bungees and even a section for people to try out Segways.

But the main attraction was probably the Battlebots arena where there was an exhibition tournment where robots battle it out in very rough conditions trying to smash each other up to scrap metal. Very cool and I suppose very American as well. During an intermission, there was even a totally wierd ‘performance’ by Grindergirl, who came into the ring with a grinder and proceeded to create sparks by running the grinder over her metal-covered body very seductively. There was a collective groan when she moved the grinder over her crotch and created sparks that shot out seemingly from her nether regions.

Have to see to believe.

afterdark-javaone07.png

battlebots.png

grindergirl3.png

JavaOne 2007, San Francisco, Day 2

This will be a short post. I’m tired, it’s almost 3am and I have another full day tomorrow (not to mention the After-Dark Bash, which I look forward to, since I’ve not been to one in the previous years). Today’s session for me have been filled with JavaCard related technical talks and one on software as a service (SaaS) with an experimental Sun project called Project Caroline.

Briefly, I learnt a great deal more on JavaCard 3 today. I attended 3 sessions on JavaCard and 2 of them are about JavaCard 3. It’s true — JavaCard 3 will have a servlet engine based on Servlet 2.4 and will be multi-threaded but it will require 32-bit microprocessors in the chip. Gemalto even has a pretty neat AJAX contact management servlet app running as a demo. This is a first glimpse of the architecture, as presented during a session by Joe Wei from the JavaCard team:

javacard3.png

As you can see, it will have both ‘classic’ APDU protocol as well as the new ‘connected’ HTTP/S protocol stack. However there are lots of questions still unanswered.

I took my usual picture with Duke.

sausheong-duke.png

But the biggest surprise of the day was that I met Dennis. Yes, of all the people to bump into, it was him. He was here on a business trip and by chance he decided to drop into JavaOne as a pavillion visitor. But here he was. The first time I was in JavaOne was with Dennis 7 years ago and 7 years later here we are again, at the same place. Different, with the intervening years and both of us are much older and maybe wiser (?) and more experienced. We had dinner together at Mel’s Dine-In to reminisce on old times, of the heady days of past and the tumultuous days when the world crashed around us

sausheong-dennis.png

Life is sometimes a funny thing.

JavaOne 2007, San Francisco, Day 1

The first day of JavaOne proper began with a lesson in crowd control. I haven’t been in a conference of this kind of size for a long while, but the management of the crowds and generally the coordination of the conference attendees and speakers are right on target. Attendees were ushered along with brisk efficiency and had friendly (mostly) Moscone Center staff manning the area showing the way or giving directions with confidence and a quick nod or smile.

javaone07_day1_01.pngAfter a quick bite at the breakfast counter, I rushed to join the long snaking queue to go into the first keynote/general session. This year, as always, the general sessions was hosted by John Gage. However the first keynote was by Rich Green, Executive VP of Software in Sun Microsystems. As Rich Green started his session with flair and great finesse, he began to troop one partner after another on stage to give announcements on partnerships. Executives from companies such as Ericsson, Nasdaq, Sony, Yahoo! and even the United Nations(!) joined in the Java lovefest.

As I sat there listening to the fanfare, laser light-show and blasting rock music (Paul Harriman, the Ericsson executive called it a ‘Java rock concert’) I came to a realization that Java rightly owed its current pole position largely to Sun Microsystems. With 6 million developers, it is the currently most popular programming language and has been so for the past 3 years. Large companies put lots of money into Java and business people generally believe Java strongly at many levels, from the Java Card smart cards running in hundred of millions of SIM and EMV cards around the world, to hundreds of millions of Java ME enabled mobile phones and the countless Java EE server applications that run at the the backend of large enterprises. For another programming language or platform to overtake Java it would be a mamoth task without the backing of an organization like Sun.

But today Sun wanted to fight on a different level. It wanted to slug it out in the rich application client arena currently dominated by Adobe Flash, with a recent tentative entry by Microsoft in the form of its Silverlight offering and Adobe’s new Flex offering. The Web 2.0, of course, has also it’s current king of the hill technology, AJAX.

What is more interesting is more interesting to gadget people is perhaps the announcement of JavaFX Mobile, a JavaFX-based middleware application stack for mobile phones. Which was quite ironic since only the annoucement before, Yahoo came up to talk about Yahoo! Go, Yahoo’s platform for mobile applications. And of course, everybody’s favourite search engine, Google was rumoured to join the mobile application fray early next year.

The afternoon’s general session was equally interesting. NASA joined in the list of Java partners as they introduced and offered the NASA World Wind technology as a Java SDK, which provides the same global geo-mapping capabilities as Google Maps or the likes, but as a Java SDK.

Another point of interest for me is Glassfish version 3. I admit I pooh-poohed the earlier versions of Glassfish as a wannabe, following Apache Genronimo and other open source app servers (and having bad experience in previous versions of the J2SE-linked app servers) but the demo of the new version 3 impressed me. Someone had earlier on demoed how he packaged a Ruby on Rails application (Mephisto) as a war file to be run under Glassfish with JRuby. But in Glassfish version 3, Jerome Doche, the Glassfish lead showed how Mephisto, a PHP app and a war file was deployed side by side with no change to the original code!

The few technical sessions I managed to catch (the sessions were very annoyingly arranged such that sessions I was interested in were invertently running concurrently) ranged from eye-opening (Groovy) to humourous (Jason Hunter’s Web Algorithms), obivous (closures in Java) and vaguely cool (Java in Robotics).

javaone07_blackbox.pngGroovy excited me quite a bit as it was every bit the scripting language I wanted when I doing Jaccal (I can’t remember why I didn’t go for it), being very interestingly similiar to Ruby in its syntax and capabilities. It even has Groovy on Grails, and I suppose it’s flattering for Ruby on Rails to be copied in such an obivious manner. Closures in Java were maddening — while Neal Grafter from Google was very earnest in introducing the needs and gaps in Java for closures, it was evident to anyone else who knows any other programming language other than Java or C/C++, that closures are incredibly useful and elegant way of coding.

In between the sessions, what little time I had I wandered through the Java Pavilion and cruised around for the traditional JavaOne goodies. I got a couple of nice ones — a nifty little ball that flipped colour from yellow to red, and a retro-toy — a slinky. I also caught up with some cool exhibits, like the mobile datacenter in a container from Sun Microsystems (called, what else — the blackbox), a Java-controlled robotic helicopter and a Java-controlled robotic submersible, and interestingly got to know more about Java Card version 3.

javaone07_submersible.pngApparently in Java Card 3, the draft specifications to be released early next year, Java Card will include a built-in web server (you heard it right), which will serve AJAX-enabled html pages (and probably applications) from the smart card itself. This is from the Gemalto folks from the Sun booth so I probably wasn’t dreaming (or drunk — they had free flow of beer and wine by evening).

All in all, today was a very interesting and fruitful day, a head-ful of new technologies and interesting turn of events (I look now at JRuby with renewed interest and Java Card 3 is just plainly mind-boggling) and some neat toys in tow.

sausheong_javaone07.png

Can’t wait for tomorrow’s sessions.

A post from San Francisco

I’m in San Francisco now, for the JavaOne conference at Moscone Center and this is the third time I’ve been here. The first time was in 2000 with Dennis and his then girlfriend Soo Chen. The second time I came alone in 2003, like this time. Both times I had a busy side agenda and itinerary to meet up as many people as possible in the Valley. It was always rushed for meetings and missed conference sessions. This time round is more relaxed, I’m here just for the conference though I’m also meeting up with Watt this Friday since he has so kindly driven all the way from LA to meet up with me.

I spent the day roaming around the old places I’ve been to San Francisco the last two times. The last two times, my hotel was a bit far away from Moscone Center because of last minute arrangements; this time round Doreen arranged for me very much earlier and I had a nice hotel at Pickwick, which is just around the corner from Moscone. After collecting the conference passes and registering myself, I wandered around the area. Moscone Center is right at downtown San Francisco, near to Union Square and that was the first place I walked around.

sf07-union-square.png

Nothing much seem to have changed. The shops are almost exactly the same — Saks at Fifth Avenue, Macy’s Bloomingdale’s Nordstorm, Tiffany & Co, Macy’s, Niketown, Levis, Victoria’s Secret, Borders and so on.

I also took the famous cable car ride from Powell station up to Fisherman’s Wharf and walked around Pier 39, looking at the famous sea lions, peering over to Alcatraz and had the famous dungeness crab for dinner.

sf07-01.png

Nothing much has changed, which surprised me a bit though not a lot. Tomorrow is the conference proper. Let’s see how it goes. I hope it’ll be a good one this year.