useless supercollider class no.1

today i wrote a helpfile for an old class i had laying around. it simulates old telephone dtmf signals. pretty silly must say and i forgot why i created it in the first place. but i bet someone can find some strange use for it. Jim?

RedDTMF is part of redSys. install it by typing...

//and then recompile
{RedDTMF.dial("12AA34", 4)}.play;

work with mark: genetics

i also spent time at UoW learning about genetic algorithms and genetic programming. mainly from john h holland's books and karl sims' papers. i found it all very interesting and inspiring and again i got great help and input from rob saunders.

one of our ideas was to construct synthesis networks from parts of our agents' genomes i.e. to have the phenomes be actual synths that would synthesise sound in realtime. the first problem to tackle was a really hard one. how to translate the genome - in form of an array of floats - into a valid supercollider synth definition?
of course there are millions of ways to do this translation. i came up with the RedGAPhenome class which works with only binary operators, control and audio unit generators. unfortunately there can be no effects or modifier units. on the other hand the class is fairy flexible and it can deal with genomes of any length (>=4). one can customise which operators and generators to use and specify ranges for their arguments. you can also opt for topology of the synthesis network (more nested or more flat).
there is no randomness involved in the translation, so each gene should produce the exact same synthdef. of course generators involving noise, chaos and such might make the output sound slightly different each time but the synthesis network should be the same.
this class produces a fantastic range of weird synths with odd synthesis techniques, and it is useful just as a synth creation machine on its own. here are some generated synths... n_noises, n_fmsynths, and corresponding 5sec audio excerpts are attached below.

then, after the struggle with the phenome translation, the code for the actual genetic algorithms was easy to write. the genome and its fitness are kept in instances of a class called RedGAGenome, and the cross breeding and mutation are performed by the class RedGA. there are a couple of different breeding methods but i found the multi-point crossover one to give the generally best results. all the above classes and their respective helpfiles and examples are available here. and there are many more automatically generated synths in the attached krazysynths+gui.scd example below.

i also made a couple of fun example applications stemming from this. one is a six voice sequencer where you can breed synths, patterns and envelopes. it is attached as 'growing soundsBreedPatternEnv.scd' below. (note that the timing is a bit shaky. i really should rewrite it to run on the TempoClock instead of the AppClock.)

ref articles:

Frankensteinean Methods for Evolutionary Music Composition, Todd and Werner
Sounds Unheard of – Evolutionary algorithms as creative tools for the contemporary composer, Palle Dahlstedt
Evolutionary Design by Computers, Peter J. Bentley
Artificial Evolution for Computer Graphics, Karl Sims
Evolving Sonic Ecosystems, Jon McCormack

ref books:

John H. Holland - Hidden Order: How Adaptation Builds Complexity
Melanie Mitchell - An introduction to Genetic Algorithms
Richard Dawkins - The Blind Watchmaker





update 101128: growing_soundsBreedPatternEnv.scd file updated, also see this post.
update 171229: converted some rtf files to scd and made the gui run on latest sc (qt)

work with mark: istreet - online

taking the intelligent street project further, mark d'inverno wanted me to try to get it online. the idea was to let people surf to a webpage, send commands to a running istreet system and hopefully collaborate with other online users to compose music. just like in the original sms-version of the piece, everybody could make changes to the same music and the result would be streamed back to all the online users.

the first prototype was easy to get up and running. we decided to use processing and write a java applet for the web user-interface and then stream the audio back with shoutcast. the users would listen to the music through itunes, winamp or some similar program. this of course introduced quite a delay before they could actually hear their changes to the music. but that was not all too bad as we had designed the original istreet in a way that latency was part of the user experience :-)
(the commands sent there via sms took approx 30 sec to reach us from the vodafone server and that could not be sped up. the users were given no instant control over the music - rather they could nudge it in some direction with their commands.)
so our internet radio/shoutcast solution worked just fine and we had it up and running for a short while from mark's house in london.

that was a total homebrewn solution of course. we wanted it to handle a lot of visitors, deal with some hopefully high traffic, be more stable, permanent and not run on an adsl connection.
so at UoW we got access to a os x server cluster and i started to plan how to install supercollider, a webserver and istreet on that. little did i know about servers, network and security and i had to learn ssh and emacs to get somewhere. rob saunders helped me a lot here.

then there were some major obstacles. first of all the cluster didn't have any window manager installed - not even x11. i spent many days getting supercollider and stefan kersten's emacs interface for sclang called scel to compile.
we also had some minor issues with starting the webserver and punching hole in the university firewall etc. but the major problem turned out to be to get the audio streaming going. i didn't have root access and wasn't allowed to install jack on the cluster. to stream i needed a shoutcast client and some way to get audio to that from supercollider. i did find os x programs that could have worked but none would run windowless on the console. so stuck.

the only solution was to write my own streaming mechanism. the resulting supercollider class for segmenting audio into mp3s is here. a java gateway handled the communication between sc and the java applet that would stitch these files back together. (the java gateway program also distributed all the other network data like the chat, checking online users, pending/playing commands etc. it used NetUtil by sciss).

unfortunately i never got the streaming thing to run smoothly. nasty hickups in the sound made it impossible to listen to. the hickups were probably partly due to my crappy coding but i think the main error was in the ESS library for processing. either ESS (releases 1 and 2) can't do asynchronous loading or java is just too slow to load mp3s without dropping audio playback. very annoying.
after that defeat i also spent time with flash and did a little player there that could load and play back mp3s smoothly. with the help from my flash expert friend abe we also could talk to the flash thing from my java applet via javascript. but time ran out and this would have been a too complicated system anyway.

so the istreet never made it online. but again i learned a lot about networks, unix, java and some tools got developed in the process. RedGUI - a set of user-interface classes for processing, ISRecord and ISGateway for sc, and the

screenshot of the java applet running istreet online...

work with mark: istreet - recording mp3s for streaming

mark d'inverno wanted to see the intelligent street installation gain new live online. so for streaming the sound from istreet over internet, i wrote a class for supercollider called ISRecord. it basically records sound to disk in small mp3 segments. so any sound supercollider produces will be spliced into many short mp3 files that could later be sent as small packages over the internet.
the technique is to continuously save the sound into one of two buffers. when one buffer is filled, the recording swaps and continues in the other. the buffer that just got filled is saved to disk and conversion to mp3 is started. this swap-and-write-to-disk cycle should have no problems keeping up realtime recording. but as the mp3 conversion takes a little bit of extra time - depending on quality and segmentation size etc, there is a callback message from the mp3 converter that evaluates a user defined segAction function when the mp3 conversion is finished. thereby one can notify other programs when the mp3 file is ready to be used.
there is also a cycle parameter that controls how many mp3 segments to save before starting to overwrite earlier ones. this is needed to not totally flood the harddrive with mp3s.

the actual mp3 conversion is done using lame and cnmat's sendOSC is also needed for the lame-to-sc communication.
attached is the recorder class plus a helpfile.

Package icon ISrecord060920.zip5.81 KB

work with mark: istreet - osx

at UoW i also rewrote an old installation called the intelligent street. mark d'inverno and i had worked together on this one earlier and we now wanted it to run on a modern computer (os x). we also wanted to redesign it a bit and make it into a standalone application.

the original intelligent street was a transnational sound installation were users could compose music using mobile phones and sms. that in turn was an extended version of a yet older installation called 'the street' by john eacott. this totally reworked 'intelligent' version was premiered in nov'03 and realised as a joined effort between the ambigence group (j.eacott, m.d'inverno, h.lörstad, f.rougier, f.olofsson) and the sonic studio at the interactive institute in piteå (way up in northern sweden).

for the new os x version we dropped sms as the only user interface and also removed the direct video+sound links between uk-se that was part of the old setup. except for that the plan was to move it straight over to supercollider server (sc3) and rather spend time on polishing the overall sound and music.

i roughly estimated it would take just a few days to do the actual port. most of the old code was written in supercollider version 2 (mac os 9) and the generative music parts were done using sc2's patterns and crucial libraries. so that code, i thought, would be pretty much forward compatible with our targeted supercollider 3. but sigh - it turned out that i had to rewrite it completely from scratch. the 'smart' tweaks and optimisations i had done in the old sc2 version in combination with the complexity of the engine made it necessary to redesign the thing bottom up. even the generative patterns parts. last i also dropped crucial library for the synthesised instruments and did it all in bare-bone sc3.

but i guess it was worth the extra weeks of work. in the end the system as a whole became more robust and better sounding. and standalone not to mention so hopefully it will survive a few years longer.
but i can also think of more creative work than rewriting old code. i've been doing that quite a lot recently. it feels like these old installations i've worked on earlier comes back to haunt me at regular intervals. and there is more and more of them for each year :-)

proof: intelligent street running happily under os x...

work with mark: shadowplay

yet another system mark d'inverno and i worked on but never finished had the working title 'shadowplay'. we had this idea about an audiovisual installation where people's limbs (or outlines of bodies) would represent grid worlds. agents would live in these worlds and evolve differently depending on things like limb size, limb movement over time, limb shape and limb position. the agents would make different music/sounds depending on the world they live in. a limb world could be thought of as a musical part in a score. the worlds would sound simultaneously but panned to different speakers to help interaction.
the visitors would see the outline of their bodies projected on a big screen together with the agents represented visually in this picture as tiny dots. hopefully people could then hear the agents that got caught or breed inside their own limbs. we hoped to active a very direct feeling of caressing and breeding your own sounding agents.
there were plans for multi user interaction: if different limbs/outlines touched (e.g. users shaking hands), agents could migrate from one world to another. there they would inject new genes in the population, inflicting the sound, maybe die or take over totally. though to keep agents within the worlds they were made to bounce off outlines. but one could shake off agents by moving quickly or just leave the area. these 'lost' agents would then starve to death if not adopted by other users.

the whole thing was written in processing and supercollider. processing did the video and graphics: getting the dv input stream, doing blobtracking (using the 3rd party library blobdetection) and drawing the agents and lines for the limbs. supercollider handled rest: the sound synthesis, the genetics, agent state and behaviours, keeping track of the worlds etc etc. we used a slightly modified version of our A4 agent framework i wrote about here.
the two programs communicated via network (osc) and would ideally run on different machines.
i had major problems with programming. the math was hairy and all the features were very taxing on the cpu. we never got further than a rough implementation.

work with mark: array primitives and benchmarking the framework

i spent a lot of time benchmarking the code for the agents in the different versions of the multi-agent frameworks i posted about here earlier. best performance boost was (of course) when i ported some vital parts of the supercollider code to c. so for example one cpu-intensive task that the agents had to do a lot was to find out about their surroundings. and another 'heavy' task was to calculate the distance to other objects. each operation wasn't very demanding on its own, but when hundreds of agents would do this at the same time, we really needed the speed of the c primitives.

below is the c code i came up with. it replaces some of the computational heavy parts in the Surroundings and ALoaction supercollider classes. to try them out you'd need to download the supercollider source code from github, add my code to the file PyrArrayPrimitives.cpp and then recompile the whole application. to use these primitives you also need to edit the file from the package posted here earlier.
one issue we then had was to distribute this 'hack'. supercollider doesn't have an api for adding extensions like this to the language (but there's a nice plugin architecture for the server). so i had to build dedicated sc applications including this speed hack.

int prArrayAsALocationIndex(struct VMGlobals *g, int numArgsPushed)
        PyrSlot *a, *b;
        PyrObject *obj;
        int size, i;
        double asIndex, w, worldSize;
        a = g->sp - 1;
        b = g->sp;
        worldSize = b->ui;
        obj = a->uo;
        size = obj->size;
        asIndex= 0;
        for (i=0; i<size; ++i) {
                getIndexedDouble(obj, i, &w);
                asIndex= asIndex+(pow(worldSize, i)*w);
        SetFloat(a, asIndex);
        return errNone;
int prArrayAsALocationRoundedIndex(struct VMGlobals *g, int numArgsPushed)
        PyrSlot *a, *b;
        PyrObject *obj;
        int size, i;
        double asIndex, w, worldSize;
        a = g->sp - 1;
        b = g->sp;
        worldSize = b->ui;
        obj = a->uo;
        size = obj->size;
        asIndex= 0;
        for (i=0; i<size; ++i) {
                getIndexedDouble(obj, i, &w);
                w= sc_round(w, 1.0);
                w= sc_clip(w, 0, worldSize-1);
                asIndex= asIndex+(pow(worldSize, i)*w);
        SetFloat(a, asIndex);
        return errNone;
int prArrayDistance(struct VMGlobals *g, int numArgsPushed)
        PyrSlot *a, *b;
        PyrObject *obj1, *obj2;
        int size, i;
        double w1, w2, distance;
        a = g->sp - 1;
        b = g->sp;
        if (b->utag != tagObj || a->uo->classptr != b->uo->classptr) return errWrongType;
        obj1 = a->uo;
        obj2 = b->uo;
        size = obj1->size;
        distance= 0;
        for (i=0; i<size; ++i) {
                getIndexedDouble(obj1, i, &w1);
                getIndexedDouble(obj2, sc_mod(i, size), &w2);
                distance= distance+pow(w2-w1, 2);
        SetFloat(a, fabs(sqrt(distance)));
        return errNone;
int prArraySurroundings(struct VMGlobals *g, int numArgsPushed)
        PyrSlot *a, *b, *c, *d, *areaArraySlots, *indexArraySlots, *outArraySlots;
        PyrObject *obj, *areaArray, *indexArray, *outArray;
        int areaSize, outSize, i, j, worldDim, area;
        double w;
        a = g->sp - 3;          //list
        b = g->sp - 2;          //worldDim
        c = g->sp - 1;          //area - as float possible later?
        d = g->sp;                      //boolean - exclude/include fix later
        if (b->utag != tagInt) return errWrongType;
        if (c->utag != tagInt) return errWrongType;
        if (d->utag != tagTrue && d->utag != tagFalse) return errWrongType;
        obj = a->uo;
        worldDim = b->ui;
        area = c->ui;
        areaSize = area*2+1;
        indexArray = newPyrArray(g->gc, worldDim, 0, true);
        indexArraySlots = indexArray->slots;
        indexArray->size = worldDim;
        if (IsTrue(d)) {        //--build index array excluding
                areaArray = newPyrArray(g->gc, areaSize-1, 0, true);
                areaArraySlots = areaArray->slots;
                areaArray->size = areaSize-1;
                int j = 0;
                for (i=0; i<areaSize-1; ++i) {
                        int temp = 0-area+i;
                        if (temp==0) {j++;}
                        areaArraySlots[i].ucopy = temp+j;
                outSize = pow(areaSize, worldDim)-1;
        } else {                        //--build index array including
                areaArray = newPyrArray(g->gc, areaSize, 0, true);
                areaArraySlots = areaArray->slots;
                areaArray->size = areaSize;
                for (i=0; i<areaSize; ++i) {
                        areaArraySlots[i].ucopy = 0-area+i;
                outSize = pow(areaSize, worldDim);
        for (i=0; i<worldDim; ++i) {
                SetObject(indexArraySlots+i, areaArray);
        //indexArray is here... [[-1, 0, 1]] or [[-1, 0, 1], [-1, 0, 1]] etc. for area=1
        //or [[-2, -1, 0, 1, 2]] or [[-2, -1, 0, 1, 2], [-2, -1, 0, 1, 2]] etc. for area=2
        //--all tuples
        outArray = newPyrArray(g->gc, outSize*sizeof(PyrObject), 0, true);
        outArraySlots = outArray->slots;
        outArray->size = outSize;
        for (i=0; i<outSize; ++i) {
                int k = i;
                PyrObject *tempArray = newPyrArray(g->gc, worldDim, 0, true);
                PyrSlot *tempArraySlots = tempArray->slots;
                tempArray->size = worldDim;
                for (j=worldDim-1; j>=0; --j) {
                        tempArraySlots[j].ucopy = areaArraySlots[k%areaSize].ucopy;
                        getIndexedDouble(obj, j, &w);
                        tempArraySlots[j].ucopy += w;
                        k /= areaSize;
                SetObject(outArraySlots+i, tempArray);
        a->uo = outArray;
        return errNone;

edit (from posted in an earlier blog entry) to look like this...

+ ArrayedCollection {
        asALocationIndex {|size|
        asALocationRoundedIndex {|size|
        distance {|list|
        surroundings {|dimensions= 2, area= 1, exclude= true|

supercollider code for benchmarking...

        var size= 100, cSize= 2, rule= 30;
        var world, agents, y= 0, dict;
        dict= ();               /*lookup dictionary for rules*/{|i| dict.put(i.asBinaryDigits(3).join.asSymbol, rule.asBinaryDigits[7-i])};
        ACell.rules= dict;
        world= APattern(size);                                          /*create 1d world*/{|i| ACell(ALocation(world, [i]))};      /*fill up 1d grid with agents*/
        world.get(ALocation(world, [(size/2).round])).value= 1; /*middle agent value=1 as init*/
        agents= world.items;
        while({y<size}, {
                /*here update.  first all agents.sense then all agents.act*/
      {|a| a.sense};
                /*{|a| a.location.list_([(size/2).round])};*/
                /*{|a| a.location= ALocation(, [(size/2).round])};*/
      {|a| a.act};
                y= y+1;

work with mark: bottom-up approach

after some time mark d'inverno and i shifted focus and decided to simplify our ideas. we agreed to work more from a bottom-up approach - letting the agents live within a grid world and visualise their behaviours. other people have been doing quite some work in this area before, but not particularly many of them have been incorporating sound and music. so we had literature and examples to study and people to ask. it was of great help and i learned a lot about designing multi-agent systems from analysing for example jon mccormack's nice eden.

so starting out writing our own system, i did a set of classes for handling agents running around in a grid world of 1-3 dimensions. all agents were very simple minded. they were visually represented with just dots and oh, they could bleep too.
setting up simple scenarios for these classes helped to pinpoint different system models. it also showed my biggest problems coding this usually boiled down to in which order to do things. the model i tried in turn to 'model' was suggested by rob saunders in an article called 'smash, bam and cell', in were all agents first sense their surroundings and then act. but i constantly had to restructure the code and design. this was harder than i had thought and i think i never came up with an all around solution.

one example scenario we came up with was the runaway test. it is very simple but can help trying out different designs. it works something like this... imagine a grid world of say 2 dimensions (we also coded this in 1 and 3D). agents move about at random speed and direction. if an agent encounters another one blocking its next step, it turns around 180 degrees and flee i.e. moving away in the opposite direction. so far the sense/act cycle is simple: for every update (world tick) it first sense, then acts. but what happens if there's another agent blocking the escape route? so the agents really needs to first sense, then if something is ahead, act and turn around 180, sense again and decide if it is possible to flee. here it'll sense within the act method and that clutters the design. the better solution would probably be to let the agent just turn 180 and wait to flee until the next tick. but perhaps it could also sense behind itself in the first sense round and pause if both escape routes are blocked. there are many possible solutions and creating these small test scenarios helped me to generalise my classes. we also tried the classes by coding the test scenarios as discrete and continuous i.e. if the world was a rigid grid in which the agents only were allowed to move stepwise, or if the world allowed them to move about more smoothly in non-integer directions and speeds.

the supercollider code for version 4, including test scenarios and small examples is attached at the bottom of this post and below is some text trying to describe the classes in more detail.

also see these quicktime movies of some test scenarios...
bounce 1D 2 one dimensional continuous world.
bounce 2D 2 two dimensional continuous world.
runaway 1D 3 one dimensional discrete world.
runaway 1D 4 one dimensional discrete world.
runaway 1D 5 one dimensional discrete world.
runaway 1D 6 one dimensional discrete world.
runaway 2D 1 two dimensional discrete world.


A4 description
there are 3 basic classes. ALocation, AWorld and AnItem. i first describe them and their immediate subclasses. then AnAgent, AProxyAgent and some subclasses of AWorld. then i explain a few of the classes used in the test/example programs. last i write a little about the main loop.
may look at the files and for completion.


a place within a world.
.new takes 2 arguments: world and list
instance variable world is any (sub)class of AWorld. can be get (ie accessed from outside).
instance variable list is a list of coordinates. can be get and set. example: [10] for x=10 in an 1 dimensional world. [10, 20, 30] for x, y, z in a 3d world. the length of the list must correspond to the number of dimensions in the world.

locations can be compared with the == method. it takes another location as an argument. a location is equal to another if they exist in the same world and have the same list of coordinates.

the != method is the negation of ==.

distance between 2 locations can be found with the distance method. it'll return the shortest distance between locations in any dimension.

with the at method one can query single dimensions. eg. in a 2d world, will return which column and will return row. the argument is really just index in list above.

the surroundingLocations method. with arguments exclude(boolean) and area(int) returns a list of new location objects. this is used for collecting locations to be searched for neighbours. if exclude argument flag is false, this (ie current) location will be counted and included in the list. the locations returned are all positioned next to this location in a cube like way, covering an area of size: area steps away. to put it in another way: with an area of 1, only directly adjacent locations are returned. an area of 2 gives adjacent and their adjacent locations (as a set of ALocation objects) and so on.
so in a 1d world a location at (0) sent the message .surroundingLocations(false, 1) will give us [loc[-1], loc[0], loc[1]]. and likewise in a 2d world a location at (4, 5) sent the message .surroundingLocations(false, 1) will return [loc[3, 4], loc[3, 5], loc[3, 6], loc[4, 4], loc[4, 5], loc[4, 6], loc[5, 4], loc[5, 5], loc[5, 6]]. here's the code that resembles this: ALocation(AWorld(2, 10), [4, 5]).surroundingLocations(false, 1). last example: a location within a 3d world asked to return its surroundings with an area of 3 like this: ALocation(AWorld(3, 100), [40, 50]).surroundingLocations(false, 3).size will return a list of 343 unique locations.

when a location object is created it check its world's size and wrap around borders (by doing modulo(size) on the coordinates in the list).

the location class expects the world to be of uniform size in all dimensions.

050712 - distance might need to go in a subclass if we do networked worlds - rob's comment. how to calculate distance between worlds?
050712 - at robs suggestion: i'll try to rewrite this and the world classes using hashtable lookup. the matrix/location duality causes trouble keeping same thing at 2 places.
050712 - guess naming needs to be improved. specially AQLocation - what to call it?
050726 - removed the maxDimension and surroundMaxArea limitations and its classvariable
050726 - now hashtable lookup + c primitives. quite a lot faster overall and easier to keep things at the same place.

AQLocation - a quantified location. the coordinates for this class can be flotingpoint but when it places itself in the matrix it rounds of to nearest integer.


a placeholder for items. superclass for APattern, AGrid, ACube, BugWorld etc.
.new takes 3 arguments: dimensions, size and location
instance variable dimensions is an integer specifying the number of dimensions for this world. can be get.
instance variable size will decide size of 1 dimension. the world is then created with uniform size in all dimensions. can be get.
instance variable location if defined, will place the world at a location. if left out - no parent. can be get.

the clear method takes a location object as argument and puts a nil there.

with the remove method - argument: an item - you remove an item from this world.

with put you place an item in this world. argument: item

the get method returns whatever item(s) is in a location. argument: location

method items returns a list of all items in this world.

neighbours - arguments: item, exclude and area. returns a list of any items within an item's area (1=adjcent locations) including or excluding the item's own location. if no items nearby then empty list.

neighboursSparse is similar to neighbours above (same arguments and function) but uses a different algo for finding nearby items. where neighbours calculates locations around the item in question and then check these locations for any items, this method might be quicker in a sparse world. it looks through all items in the world and checks if they're nearby.

running the update method goes through all items in this world, copies them to their own locations. this is to make sure all item's locations and the hashtable stays in sync.

save will write this world, its current settings and all its items and their settings to disk. this allow for backup of longrunning tasks or 'presets' to be loaded.



AWorld2 - remove and put methods are modified to allow for multiple items at the same location. AWorld can only do one item in once location. hopefully i can merge the two classes later.

ASmartWorld - is a world that can resolve location conflicts. there's a resolve method to be called after the sense cycle but before act. this goes through all items and if more than one intend to move to the same location, only let one move - others stay put and their request is ignored. (comment: need to find a better name than ASmartWorld)

and many other subclasses. eg BugWorld, APattern, AGrid, ACube. almost every test program has its own specialised class inheriting from these two subclasses.


lowest level thing that exist in a world. abstract superclass class for ARock, AMoss, AnAgent
.new takes 1 argument: location

the method remove will remove this item from its world.

the abstract init method is used by some subclasses for initialisation.

050726 - is remove needed? will the agent remove itself or will the world handle that eg if energy=0.

ARock - does nothing different. just exists at a location
AMoss - has energy that can be get/set.
AnAgent - is an abstract class. see below


subclass of AnItem but is also an abstract class.
makes sure the sense and act methods are there for all agents to follow.

many. eg ACell, ARunaway, ABounce, Bug. every test program has its own specialised class inheriting from this one. they all do sense and act in their own way.

subclass of AnAgent. it allows to replace sense and act methods while running.

when asked to sense and act, sense and act in this class instead evaluates functions stored in the 2 class variables senseFunc and actFunc. these can be replaced and coded on the fly! so while the system is running, we can try out, completely rewrite or just slightly modify, behaviour for all agents. their state are kept (individually) but behaviour changes.

this is unique to other frameworks i've seen so far. i'd like to explore more and hopefully we can use it in practice too - not just as convenience for developing. with this feature it's easy to replace the rules on the fly.
perhaps i redesign the whole framework to use proxies. so the AnItem class is really a placeholder (proxy) for anything. then one can code whole agents with state and behaviour while running i think. and maybe proxy worlds too but i can't find a reason for that now.


a subclass of AWorld that has 1 dimension.


a subclass of AWorld that has 2 dimension.


a subclass of AWorld that has 3 dimension.

ACell - used in A4_test1_cellautomata.scd

subclass of AnAgent. it doesn't move and is used for cellular automatas and gameoflife.
instance variable value can be 0 or 1. can be get/set.
there's also a rules class variable that contains a dictionary for rule lookup.

the sense method here collects and stores values from nearby neighbours (by asking the world for neighbours) including the cell's own value.
the act method set the cell's own value to what is returned from the rules dictionary.

ALifeCell - used in A4_test2_gameoflife.scd

subclass of ACell. just implements different sense and act methods.

the sense method here is the same as ACell.sense but excludes the cell's own value.
act will first calculate the total sum of all neighbour's values and then do lookup in the rules dictionary. the cell's own values is set to 0 or 1 depending on what the dictionary returns.

ARunaway - used in A4_test3_runaway1D.scd, A4_test4_runaway2D.scd and A4_test8_runaway3D.scd

a subclass of AnAgent that sense if something at next location and if so, bleep, turn around 90 and flee.
instance variable direction is a list of directions in any dimension. in a 2D world: [0, 0] stand still, [-1, 0] go west, [1, 1] go northeast and so on. can be get/set
instance variable freq decides which bleep frequency to play.

the sense method updates the 2 private nextLocation and nextPos instance variables to figure out where to go and if that location is taken.
helper method clearAhead returns true if there's nothing in nextPos
getNextLocation returns a new location object at here + directionlist.
getNewDirection method turns directionlist around 90 degrees.
the move method sets this location to nextLocation
the play method will beep at a frequency. and pan the sound left/right depending on location.

ABounce1D - used in A4_test6_bounce1D.scd

subclass of ARunaway. implements getNextLocation and getNewDirection differently so that the agents bounces of eachother rather that turn 90 degrees.
direction is here a vector of angle and degree.

ABounce2D and ABounce3D for vector math in other dimensions.


the main loop of the program is usually very simple. for the ca and gameoflife examples it just draws a rectangle if the cell's value is 1, then call .sense on all agents and last call .act for all agents.

while {true}, {{|a| if(a.value==1, {a.paintRect})}{|a| a.sense};{|a| a.act};

agents that move around (ie all other examples) need to resolve conflicts and update the world. also they always draw themselves.

while {true}, {{|a| a.paintRect}{|a| a.sense};
        world.resolve;{|a| a.act};

update 061017: bugfix to the distance method in
update 171228: new helpfile and scd instead of rtf

Package icon A4.zip24.02 KB


Subscribe to f0blog RSS