/* * * MAML TUTORIAL (Model 4.2) -- The file of the observer unit * For full description refer to * http://www.syslab.ceu.hu/maml/tutorial/ * * 'Segregation' a model by Thomas Schelling * * This simulation is based on a model published by T. Schelling, and consists * of agents living in a 2d grid. The agents are of two colors (blue and red) * and stay at a given location only if the ratio of the number of their * neighbors the same color and the number of neighbors is more than a * specified limit. What is interesting is that if this limit is fairly low * (appr. 0.5), this fairly tolerant behaviour leads to surprisingly high * rate of segregation at the global level. * It was implemented in MAML v0.03 as part of its tutorial. * * (c) 1998, CEU Systems Laboratory * */ // Taking the file of the model $import "m5.maml"; @observe Segregation { @uses ; // We need this Swarm package // for the 2d graphics display // Extending the behavior of the resident (agent) // by a routine to draw itself on the display // (It is clearly not connected to the model itself, // but with its observation only. That's why it is // done through the extension mechanism and not // coded in the model files. The model should run // without this subroutine!) @extendAgent Resident { @sub: (void) drawSelfOn: (id) r { [r drawPointX: posX Y: posY Color: color]; } } //////////////////////////////////////////////////////////////////////// // Model variables to be probed @probe: var "citySize", var "numOfResidents", var "ratio", var "minLimit", var "maxLimit", var "minAgeLimit", var "maxAgeLimit", var "emptyIsOK"; //////////////////////////////////////////////////////////////////////// // Variables of the display @var: id colormap; // Color scale @var: id cityRaster; // Graphics window @var: id cityDisplay; // Utility to draw the residents // Variable of the observer, which is to be probed @var: int displayFrequency; @probe: var "displayFrequency"; //////////////////////////////////////////////////////////////////////// // The observer's schedule @schedule cyclic (displayFrequency) { // To be executed according at // the frequency set by the variable 0: @planDef { @to cityRaster erase; // Clear the display @to cityDisplay display; // Use the utility to draw it... @to cityRaster drawSelf; // ... and refresh it on the screen } } //////////////////////////////////////////////////////////////////////// // Initialization of the observer @init: // Setting the default values of the parameters citySize = 50; numOfResidents = 2000; ratio = 50; minAgeLimit = 0; maxAgeLimit = 80; minLimit = 0.25; maxLimit = 0.70; emptyIsOK = true; displayFrequency = 1; // Displaying the probes @buildProbes; [model probe]; // Stopping the model to let the user change the parameters [controlPanel setStateStopped]; // Initializing the model with the (possibly new) parameters @initModel; // Creating the observer's graphics gadgets @create Colormap colormap; [colormap setColor: 1 ToName: "red"]; [colormap setColor: 2 ToName: "blue"]; @create ZoomRaster cityRaster; [cityRaster setColormap: colormap]; [cityRaster setZoomFactor: 6]; [cityRaster setWidth: citySize Height: citySize]; [cityRaster setWindowTitle: "Schelling's Segregation City"]; [cityRaster pack]; @create Object2dDisplay cityDisplay { [cityDisplay setDisplayWidget: cityRaster]; [cityDisplay setDiscrete2dToDisplay: city]; [cityDisplay setObjectCollection: groupOfResident]; [cityDisplay setDisplayMessage: M(drawSelfOn:)]; } }