⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 observerswarm.m

📁 仿真人工金融市场Jackson代码
💻 M
字号:
// ObserverSwarm.m#import "ObserverSwarm.h"#import "ModelSwarm.h"#import <collections.h>#import <objectbase.h>#import <analysis.h>static const char *histogramFirmColors[] = {  "yellow", "green", "red", "blue", "purple","grey50", "black",  "red", "orange", "yellow", "green", "blue", "purple", "grey50", "black"};static const char *histogramWorkerColors[] = { "yellow", "green", "red", "blue", "purple","grey50", "black",  "red", "orange", "yellow", "green", "blue", "purple", "grey50", "black"};static const char *histogramHCColors[] = {"yellow", "green", "red", "blue", "purple","grey50", "black",  "red", "orange", "yellow", "green", "blue", "purple", "grey50", "black"}; const char *structures[] = {"Due    ", "Tre","Quattro", "Cinque", "Sei"}; const char *structures2[] = {"Available   ", "Engaged", "Efficient"}; const char *structures3[] = {"Uno ", "Due", "Tre", "Quattro", "Cinque", "Sei"}; int  data[] = { 0, 0, 0, 0, 0 }; int  data2[] = { 0, 0, 0}; int  data3[] = {0,0,0,0,0,0};  //per il capitale umano@implementation ObserverSwarm+ createBegin: aZone{  ObserverSwarm * obj;  id <ProbeMap> probeMap;  // createBegin: here we set up the default simulation parameters.  // Superclass createBegin to allocate ourselves.  obj = [super createBegin: aZone];  // Fill in the relevant parameters (only one, in this case).  obj->displayFrequency = 1;  // Also, build a customized probe map. Without a probe map, the default  // is to show all variables and messages. Here we choose to  // customize the appearance of the probe, give a nicer interface.  probeMap = [EmptyProbeMap createBegin: aZone];  [probeMap setProbedClass: [self class]];  probeMap = [probeMap createEnd];  // Add in a bunch of variables, one per simulation parameters  [probeMap addProbe: [probeLibrary getProbeForVariable: "displayFrequency"                                    inClass: [self class]]];  // Now install our custom probeMap into the probeLibrary.  [probeLibrary setProbeMap: probeMap For: [self class]];  return obj;}-createEnd {  return [super createEnd];}- buildObjects{  id modelZone;                                   // zone for model.  [super buildObjects];  // First, we create the model that we're actually observing. The  // model is a subswarm of the observer. We also create the model in  // its own zone, so storage is segregated.  modelZone = [Zone create: [self getZone]];  modelSwarm = [ModelSwarm create: modelZone];  // Now create probe objects on the model and ourselves. This gives a  // simple user interface to let the user change parameters.  CREATE_PROBE_DISPLAY (modelSwarm);  CREATE_PROBE_DISPLAY (self);  // Instruct the control panel to wait for a button event.  // We halt here until someone hits a control panel button.  // Now that we're using Probes, the user can set the parameters  // in the ModelSwarm probe window - we halt here to allow  // the user to change parameters.  [controlPanel setStateStopped];  // When the user hits "go" on the control panel, we resume here  // OK - the user said "go" so we're ready to start  [modelSwarm buildObjects];  // Now get down to building our own display objects.  // First, create a colormap: this is a global resource, the information  // here is used by lots of different objects.  colorMap = [Colormap create: [self getZone]];  [colorMap setColor: 0 ToName: "yellow"];  [colorMap setColor: 1 ToName: "green"];  [colorMap setColor: 2 ToName: "red"];  [colorMap setColor: 3 ToName: "blue"];  [colorMap setColor: 4 ToName: "purple"];  [colorMap setColor: 5 ToName: "grey50"];  [colorMap setColor: 6 ToName: "black"];  [colorMap setColor: 7 ToName: "orange"];  // Next, create a 2d window for display, set its size, zoom factor, title.  worldRaster = [ZoomRaster create: [self getZone]];  [worldRaster setColormap: colorMap];  [worldRaster setZoomFactor: 8];  [worldRaster setWidth: [[modelSwarm getWorld] getSizeX]                 Height: [[modelSwarm getWorld] getSizeY]];  [worldRaster setWindowTitle: "Human Capital Distribution"];  [worldRaster pack];                             // draw the window.  // Now create a Value2dDisplay: this is a special object that will  // display arbitrary 2d value arrays on a given Raster widget.  workerDisplay = [Value2dDisplay createBegin: [self getZone]];  [workerDisplay setDisplayWidget: worldRaster colormap: colorMap];  [workerDisplay setDiscrete2dToDisplay: [modelSwarm getWorkerSpace]];  workerDisplay = [workerDisplay createEnd];// creiamo un altro raster con la relativca ColorMap per gli imprenditori firmColorMap = [Colormap create: [self getZone]];  [firmColorMap setColor: 0 ToName: "black"]; // nil  [firmColorMap setColor: 1 ToName: "purple"]; //  non usato  [firmColorMap setColor: 2 ToName: "blue"];//struc2  [firmColorMap setColor: 3 ToName: "green"];//struc3  [firmColorMap setColor: 4 ToName: "yellow"];//struc4  [firmColorMap setColor: 5 ToName: "orange"];//struc5  [firmColorMap setColor: 6 ToName: "red"];  //struc6  [firmColorMap setColor: 7 ToName: "grey50"];  firmRaster = [ZoomRaster create: [self getZone]];  [firmRaster setColormap: firmColorMap];  [firmRaster setZoomFactor: 8];  [firmRaster setWidth: [[modelSwarm getWorld] getSizeX]                 Height: [[modelSwarm getWorld] getSizeY]];  [firmRaster setWindowTitle: "Firms Size Distribution"];  [firmRaster pack];                             // draw the window.  // Now create a Value2dDisplay: this is a special object that will  // display arbitrary 2d value arrays on a given Raster widget.  firmDisplay = [Value2dDisplay createBegin: [self getZone]];  [firmDisplay setDisplayWidget: firmRaster colormap: colorMap];  [firmDisplay setDiscrete2dToDisplay: [modelSwarm getFirm]];  firmDisplay = [firmDisplay createEnd]; useHisto = [Histogram createBegin: globalZone];  SET_WINDOW_GEOMETRY_RECORD_NAME (useHisto);    [useHisto setBinCount: 5];    useHisto = [useHisto createEnd];    [useHisto setWidth: 400 Height: 250];    [useHisto setLabels: structures count:5];    [useHisto setColors: histogramFirmColors count:5];    //pj: [useHisto setNumPoints: 5  Labels: structures    //pj:     Colors:histogramFirmColors];    [useHisto setTitle: "Diagram of Firms Structures"];    [useHisto setAxisLabelsX: "X" Y: "Y"];    [useHisto pack];  // creo il secondo grafico a barre  useHisto2 = [Histogram createBegin: globalZone];  SET_WINDOW_GEOMETRY_RECORD_NAME (useHisto2);    [useHisto2 setBinCount: 3];  useHisto2 = [useHisto2 createEnd];    [useHisto2 setWidth: 400 Height: 250]; // [useHisto2 setLabels: structures2 count:5]; // [useHisto2 setColors: histogramWorkerColors count:5];  //pj:[useHisto2 setNumPoints: 3 Labels: structures2 Colors:  // histogramWorkerColors];  //pj: 3 needed here, not 5  [useHisto2 setColors: histogramWorkerColors count: 3];  [useHisto2 setLabels: structures2 count: 3];  [useHisto2 setTitle: "Diagram of Workers"];  [useHisto2 setAxisLabelsX: "X:" Y: "Y:"];  [useHisto2 pack];  // creo il terzo grafico a barre relativo al Capitale Umano  useHisto3 = [Histogram createBegin: globalZone];  SET_WINDOW_GEOMETRY_RECORD_NAME (useHisto3);    //pj:  [useHisto3 setBinCount: 6];  useHisto3 = [useHisto3 createEnd];  [useHisto3 setWidth: 400 Height: 250];  [useHisto3 setLabels: structures3 count:3];  [useHisto3 setColors: histogramHCColors count:3];  // [useHisto3 setNumPoints: 6 Labels: structures3 Colors: histogramHCColors];  [useHisto3 setTitle: "Diagram of Human Capital of Workers"];  [useHisto3 setAxisLabelsX: "X:" Y: "Y:"];  [useHisto3 pack];  //fine grafici a barre  return self;}-buildActions{// Create the actions necessary for the simulation.   [super buildActions];  // First, let our model swarm build its own schedule.  [modelSwarm buildActions];  // Create an ActionGroup for display.   displayActions = [ActionGroup create: [self getZone]];  // Schedule up the methods to draw the display of the world   [displayActions createActionTo: workerDisplay         message: M(display)];  [displayActions createActionTo: worldRaster         message: M(drawSelf)]; [displayActions createActionTo: firmDisplay         message: M(display)];  [displayActions createActionTo: firmRaster message:M(drawSelf)];  [displayActions createActionTo: self              message:M(updateHisto)];  [displayActions createActionTo: self               message:M(updateHisto2)]; [displayActions createActionTo: self               message:M(updateHisto3)]; [displayActions createActionTo: actionCache         message: M(doTkEvents)];  // And the display schedule. Note the repeat interval is set from our  // own Swarm data structure. Display is frequently the slowest part of a  // simulation, so redrawing less frequently can be a help.  displaySchedule = [Schedule createBegin: [self getZone]];  [displaySchedule setRepeatInterval: displayFrequency]; // note frequency!  displaySchedule = [displaySchedule createEnd];  [displaySchedule at: 0 createAction: displayActions];   return self;}-activateIn: (id) swarmContext {// activateIn: - activate the schedules so they're ready to run.  [super activateIn: swarmContext];  // Activate the model swarm in ourselves. The model swarm is a  // subswarm of the observer swarm.  [modelSwarm activateIn: self];  // Now activate our schedule in ourselves. This arranges for the  // execution of the schedule we built.  [displaySchedule activateIn: self];  // Activate returns the swarm activity - the thing that's ready to run.  return [self getSwarmActivity];}-updateHisto {int i; for (i=0;i<5;i++) data[i] = ([modelSwarm getFirmData:i]);// printf ("FirmData %d %d %d %d %d \n", data[0], data[1],data[2],data[3], data[4]); [useHisto drawHistogramWithInt: data];return self;}-updateHisto2 {int i;for (i=0;i<3;i++) data2[i] = ([modelSwarm getWorkerData:i]);//printf ("worker %d %d %d \n", data2[0], data2[1], data2[2]); [useHisto2 drawHistogramWithInt: data2];return self;}-updateHisto3 {int i;for (i=0;i<6;i++) data3[i] = ([modelSwarm getHCData:i]);//printf ("worker %d %d %d %d %d %d\n", data3[0], data3[1], data3[2],data3[3], data3[4], data3[5] ); [useHisto3 drawHistogramWithInt: data3];return self;}@end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -