September 16, 2011

Note: this is just a quick overview of the SFX UNM CS590 course. More in depth later!

Just finished a nifty experimental educational stunt at the Santa Fe Complex (SFX): taking the UNM CS590 course taught by Ed Angel .. but given in Santa Fe at SFX to a group of folks interested in a wide variety of topics, from GPU programming to game programming to high performance parallel computation to ambient computing combining projectors and cameras to do computing in your spatial environment .. your whiteboards, rooms and domes.

Ed had just finished a major rewrite of his textbook to be Shader based. Shaders are programs running in your computer’s GPU chip with very fast matrix manipulations of huge number of vertices. Not only were shaders a new addition to CS590, but OpenGL itself had morphed considerably .. including not only the desktop C/C++ version, but also GLES, the Embedded Systems (phones and tablets, mainly) but the HTML5 WebGL variant. Within the class we had all versions being used.

I chose to use WebGL, mainly using Google Chrome, WebKit (Safari’s developer build), and Firefox .. all of which have most of HTML5 implemented. The documentation was pretty raw but fortunately the Learning WebGL site is extraordinarily useful.

We had three fascinating homework assignments, plus a final project if taking the course for graduate school credits:

October 29, 2006

Last article showed how we could use Processing with Eclipse and Java 1.5. Heady with success, I decided to try to convert the near-trivial RandBoxes Processing demo to Jython.

Why?? Well, our group uses Blender quite a bit, and Blender uses Python as its scripting language. More generally, Python is quite popular within our scientific community.

Jython, a Python implementation using the JVM, has been surprisingly successful, having most of what we like about CPython. And its Java integration made it a natural for Processing. This presentation is a good overview.

Java

Here’s the Java 1.4 code we’re starting with, the Processing .java file created when creating the applet above. A few notes:

The two methods setup() and draw() are required by PApplet.

Class Box is an inner class, as is usual for Processing, because:

Several utilities are in PApplet like color(), random() and all drawing methods. Use of inner classes lets you “inherit” these.

Notes:

My first try did not use the 4 “global” variables: winWidth, winHeight, .. but it was pointed out to me that its more pythonic to use globals like this, and avoids overuse of “self”.

Similarly, to avoid way too many uses of “self.random()” and so on, I migrated to using Python utilities for max, min, random.uniform, and so on. You can tell I was beset with way, way too many uses of self.foo()!

I tried using inner-classes within Jython, but apparently that doesn’t work, even with __future__ nested_scope. The ASPN Cookbook discusses solutions, but I decided simply to pass in the PApplet instance to the Box constructor. Not sure what the most pythonic solution would be, but being explicit doesn’t hurt.

I chose to use JFrame for putting my PApplet subclass in an application even though PApplet has a main() that I could call. (PApplet, like many Java Applets, has a main for running as an application). I did this for three reasons: to learn Jython’s Java integration, to use the Python “if __name__” cliche, and to avoid having to use jythonc to create a .class file everytime I made a change. The latter was a subtle point caused by the main() in PApplet actually running your class file.

The getField() utility was needed to disambiguate frameRate, a variable, and frameRate(), a method, both in PApplet. This was a gift from Jeff Emanuel on the jython-users list

I did succeed in building an Applet, see below. This used jythonc to build a true Java .class file, and has facilities for including the Jython core libraries as well your Jython source.

The .py file cannot be RandBoxes.py due to a jythonc convention which creates a main() for the outermost Jython code. This main throws an exception, which does not match the standard Java main() signature. Because PApplet has its own standard Java main(), jython flags an error and stops. And, yes, you can bet this took a long time to fine!

Thoughts

Jython is for real: It interoperates well with Java, implements a recent enough version of the Python language, has a good support forum and documentation.

This wasn’t easy, but was made possible due to the Jython community and website, and the Jython books (I used Jython for Java Programmers)

There is friction between Jython and Java, and is likely to remain there. This will require Jython users trying to integrate with Java to smooth the way somehow. Reflection, like in getField(), can help. Also, Proxy classes are a very reasonable approach. I used this initially, and then had the Jython code subclass PAppletProxy rather than PApplet :

Python: well, there’s good and bad. Its a well crafted language, but I certainly made all the blunders newbies make: I got my indentation wrong, found myself using self.foo() way, way too much, and wished for standard lexical scoping. But boy is the code clean.

Performance: Java’s RandBoxes runs at about 40fps, while the Jython version runs at about 6-7fps, around 7 times slower, give or take. This is really unfair in a way, I’m such a Jython newbie, and this is definitely a difficult task. And for rapid prototyping, that sort of decrease may easily be acceptable. I think if it were more like 2-3 times slower, that would be fine. Any suggestions welcome!

Processing is more than a graphics library, it has a delightful IDE which makes Java much easier, especially for the Java novice. The IDE has great tools such as a web-page builder with the project as an applet. It also can build an application version for Mac, Windows and Linux.

One difficulty we faced however, was that we wanted to commit to fairly complicated models that would benefit from Java 1.5 (Processing uses an earlier version) and an IDE familiar to Java programmers. We also are interested in interfacing Java to other languages such as Groovy, Python/Jython, Ruby/JRuby, and even JavaScript/Rhino.

So I decided to experiment with Processing, using Eclipse and Java 1.5. To do this, I made two models: RandBoxes, which is really simple .. just showing 1000 boxes and randomly jiggling them each step. The second model, RoadGrid, is more complicated: it draws a rectangular road grid and has “cars” (rectangles) moving along the roads. At each intersection, the cars randomly chose a new road segment.

By a very fortunate coincidence, the Processing team had just done a fairly major cleanup, making their system work nicely in Eclipse. So I built a new Eclipse project, including the Processing core.jar file and the Processing JavaDocs, and using the Java 1.5 SDK.

The Processing IDE puts its code fragments into .pde (Processing Development Environment) files which the web-page builder includes in the applets above. Here’s the RandBoxes .pde file. Note that there is no Class defined, nor imports. The Processing IDE handles that for you by generating a java file automatically. Here’s the RandBoxes.java file before and after formatting by Eclipse. Note how the IDE “wraps” the .pde file in a class and adds a Main(), as per the Java norm. It also does other preprocessing like changing the “color” type to a Java “int”.

Well, imaging my surprise when after dumping the above file into Eclipse, although it had many warnings, it none the less compiled and ran, both as an applet and as an application! Here’s what Eclipse looked like:

The next step was to upgrade the .java file to be 1.5 savy: removing unnecessary imports, migrating to generics, using the for/in loop and so on. This resulted in the RandBoxes1.java file.

Another nifty 1.5-ism was using Generic methods. Their syntax is a bit odd, but it sure is great not having to use casts all over the place! Here’s an example taken from the second demo, RoadGrid. This old-style method:

So Processing with Eclipse is working like a charm, and lets us use the latest Java version, use the JavaDocs easily, and even browse the Processing code. And with a little hand tweaking of Processing’s IDE-generated web page, we can even use the new Java 1.5 applet.

One downside: many browsers are not yet using 1.5 so will not be able to view the newer version. But a solution is RetroWeaver which converts a 1.5 class back to its 1.4 format, thus potentially letting us use Eclipse, Java 1.5, Processing .. yet deploying for 1.4 browsers.

We’ll follow up shortly with some other experiments with Processing with other languages .. stay tuned!

August 19, 2005

Last year, Nick Thompson, Professor of Psychology and Ethology of Clark University, dropped by Santa Fe on Sabbatical. Nick brought with him a puzzle relating to the Prisoner’s Dilemma.

The core idea of the Prisoner’s Dilemma is a game where two folks choose to cooperate or not (defect). The scoring is such that if you cooperate and your opponent defects, you (the “sucker”) get quite a low score. If you both cooperate, however, you get a reasonable score but not as high as a defector gets against a cooperator. This classic in game theory poses the paradox that your best move is to defect, even though if you both defect, you both get a very poor score. Grim.

Robert Axelrod was puzzled by this, and suggested that if the game were repeated (iterated), the best outcome would could vary according to the strategy of the players. This resulted in his now famous computer tournament pairing strategies against one another. The sweet result was that a firm but friendly strategy, Tit for Tat, which cooperated initially, then simply repeated the opponent’s previous move from then on, was the overall winner.

Nick, looking at this, thought that it’d be nuts to stay in a series of Prisoner’s Dilemma games if the opponent was clearly no fun to work with. Thus he introduced the idea of leaving the tournament. MOTH, My way Or The Highway, builds a new suite of strategies which have the additional capability of leaving an unpleasant encounter. This creates a pool of strategies without partners, so they are re-paired randomly, and the tournament continues.

The result is that Nick, along with several of our local Friam group here in Santa Fe, worked on a NetLogo model exploring this new approach. This model expanded upon and validated earlier work done by Nick and his colleagues David Joyce and John Kennison. This resulted in a paper delivered to the Lake Arrowhead Conference on Human Complex Systems.

The NetLogo Model we created is viewable by you, gentle reader. The explanation provided with the model presents considerable detail about the ideas behind the Moth strategy.

Not surprisingly, a quite successful strategy is a variation of Tit for Tat: play Tit for Tat and on the first defection by your opponent, leave the tournament to be paired with a (hopefully!) more cooperative player!

November 23, 2004

A friend of mine, Michelle Aden, recently asked me to write about some aspect of Open Source for a conference on that topic. It occurred to me that research had recently enjoyed becoming part of the Open Source community, so I looked into writing about that.

October 10, 2004

While updating my ancent website (started in the 1995’s .. so very html 1.0), I decided to look into the current state of the art of web publishing.

Imagine my surprise when I found that the cruddy table based mess the .com sites used to achieve their unnatural, pixel perfect goals had been replaced by a very clean separation of content and structure (xhtml) and the looks and style of the page (css).

I put together several examples for a talk to our local technical team. Here’s the talk’s “slides” .. pages of reference material and demos.

Note: The talk included considerable CSS magic by making simple changes in the style sheet using CSSEdit and having immediate response in the preview page. So some of that is missing for folks just looking at these pages.

The Basics

Before we start: Some of The Heros

These folks, and many others, brought order back from the chaos of the early, table-centric (read “Gawd Awful”) mess our poor web wandered into. Notice how design played a strong role in achieving this revolution. Techies — be humbled.

October 7, 2004

Recent Web Findings

This is a writeup on recent findings I’ve made in the web & internet world as I explored it to upgrade my web site and to begin thinking about development issues we’ve been discussing at WedTech and other venues like Ars Publica, RedfishGroup, and other opportunities.

First and foremost is a huge change in the core web world itself. Html has been regularized into being xhtml, a subset of xml. (Xhtml is simply html with ballanced tags and other xml regularities. We’ll use “html” from now on but mean xhtml) In addition, the MVC style (Model, View, Controller) has become a core value, promoting an extremely clean separation of content from layout/looks.

The xml direction has prompted several new capabilities such as news feeds and added web semantics. Robert’s recent posting on his web site’s use of feeds was quite interesting. Google, Yahoo and other sites have dynamic interfaces letting you use the xml world to programmatically query their huge repositories.

While becoming immersed in all this, I tried some new tools.

One was Textpattern, a content management system (CMS) which makes it quite easy to manage and update a web site. It provides easy access to html/css, and makes it easy to incrementally modify both the content and looks of the site. It provides a browser based “dashboard” for doing all this in a very simple way. It manages fragments of content (articles) and html (forms), integrating them into pages for you.

Textpattern is based on PHP and MySql, two interesting core web technologies. PHP is a server based dynamic page generation technology, while MySql is a freely available, well supported SQL server. HostGo, our popular hosting service, provides both. And there is an excellent phpMyAdmin web application for managing and viewing your MySql database.

Templating is a widespread technology for separating the php programming from the html content. It works by custom defined html tags. For example, the tag <txp:page_title /> will generate a reasonable page title for the current page based on the section/category/article contents of the page. It invokes this php function:

All extensions to Textpattern are done via these tags. Thus your article and form fragments have no php programming, keeping programming separate from content and form. It is possible to use Textpattern with absolutely no knowledge of PHP and MySql.

Textpattern is installed on your server (hostgo for many of us) and the site is directly managed from there. You need not have a local “mirror” of the site on your computer. Because html/css is used, the actual content you add to the site is trivial html with none of the complex table layout, and embedded font, color, etc styling. This leads to using a “MarkDown” style of data entry. By markdown I mean any of several simple text-to-html schemes. Wiki’s use a simple scheme of * bold *, _ italic _, CamelCase or [http://foo/bar.html] links, and so on. Several blogs use this text-to-html. Textpattern comes with Textile, a similar markdown. There are others as well.

There are several very interesting Java/XML CMS systems too. Carl’s presentation on XML and XSLT was quite an eye-opener. Combined with Ant, the Java/Apache build tool, an entire site can be maintained from a command-line interface! Very impressive and it has the advantage of being closer to the core xml used to have sites communicate with each other.

I view the Java/XML – PHP/MySql split as a positive thing, and something that brought about several Cathedral and the Bazaar discussions. Some sites combine both technologies. The http/webservices protocols provide clean separation.

Another tool I’ve begun using is jEdit, another interesting open source project. It has become a favorite among folks wanting an editor that runs on Windows, Mac, and Linux systems and which is easily modified via plugins and mode scripts. I think the most interesting thing for us is its ability to manage all its plugins directly within jEdit via a GUI that directly interacts with the plugin website without having to download and install. This surprising design, including the network directly in the application, makes sense for us as we consider complexity applications.

Because html/css usage promotes very simple html, I find jEdit handles content creation, when I’m not using Textile directly, quite adequately, with the bonus of being a good xml editor (for Ant and most modern configuration scripts). I use a separate css editor to very dynamically experiment with page style. So with Textpattern, I generated a simple page, cut/paste it to a local html file, and started styling with a css editor. Extremely simple and understandable, and very rapid design cycles.

During all these explorations, I found that the web communities have grown considerably in sophistication. First and foremost were the Forum sites with easy to use “bulletin board” style posts. The Textpattern forum (and WordPress, a blog system I also was exploring) are very active and useful. Because Textpattern is meant for more than just blogging, it is a bit more complex to install and use. The community was/is a huge help .. I visit daily and am starting to contribute help and plugins.

This community approach was used brilliantly by several open source projects. One example is MySql’s on-line manual. It allows readers to leave comments on each page! This has lead to very rapid discovery of problems and interesting usages. PHP uses the same style. Some projects use this as a way to build the manual itself: the core developer(s) create an outline for a manual in an open Wiki, and the community fills it in. This was used by the single Textpattern developer to document user-supplied plugins. All this documentation is very dynamic, often to pointers to the discussions which lead particular decisions.

I find myself making choices of which software to start using based on their community. A vibrant community often makes for a successful project. So when looking for Java based systems recently, I found myself guided considerably by the community activities around the project.

These experiences led to suggesting that we have an explicit “portal” section part in the Ars Publica grant proposal.

My most recent plunge into a new system is Eclipse, the open source IDE based on a plugin architecture which prompts considerable community activity. What captured me was it superb integration into the Mac desktop and its ease of configuration. No IDE is easy to use, but getting rid of barriers makes it much simpler.

My interest in Eclipse was not just as an IDE. Roger and others had found that it hoped to be used as an application framework. It is called the Rich Client Platform (RCP). So I’m currently going through a couple of RCP tutorials to see if it could be used as an integration platform for agent based modeling.