modelswarm.m
来自「仿真人工金融市场Jackson代码」· M 代码 · 共 631 行 · 第 1/2 页
M
631 行
// ModelSwarm.m Simpleworker app#import "ModelSwarm.h"#import <simtools.h>#import <random.h>#include <misc.h>int HCData[]= {0,0,0,0,0,0};int WorkStData[] = { 0, 0,0 };int FirmStData[] = { 0, 0, 0, 0, 0 };@implementation ModelSwarm // These methods provide access to the objects inside the ModelSwarm.// These objects are the ones visible to other classes via message call.// In theory we could just let other objects use Probes to read our state,// but message access is frequently more convenient.-getworkerList { return workerList;}-getFirmList { return firmList;}-getWorld { return world;}-getFirmWorld { return firmWorld;}-(int)getSeed { return seed;}-(int) getDiffer { if (differenciated >1) { differenciated = 1; // printf ("Wrong Value, differenciated set to 1"); } return differenciated;}-getFirm { return firmSpace;}-getWorkerSpace { return workerSpace;}-(int)getWorldXSize { return worldXSize;}-(int)getWorldYSize { return worldXSize;}-(int)getDays { return daysInMonth;}+createBegin: (id) aZone { ModelSwarm * obj; id <ProbeMap> probeMap; // in createBegin, we set up the simulation parameters // First, call our superclass createBegin - the return value is the // allocated workerSwarm object. obj = [super createBegin: aZone]; // Now fill in various simulation parameters with default values. obj->worldXSize = 20; obj->worldYSize = 20; obj->daysInMonth = 30; obj->p0 = 10; obj->p1 = 10; obj->p2 = 20; obj->p3 = 20; obj->p4 = 20; obj->p5 = 20; obj->differenciated = 1; obj->updHC = 1; obj->numFirms = 5; obj->seed = 1; // And 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 parameter [probeMap addProbe: [probeLibrary getProbeForVariable: "worldXSize" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "worldYSize" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "daysInMonth" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "p0" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "p1" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "p2" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "p3" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "p4" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "p5" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "numFirms" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "differenciated" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "updHC" inClass: [self class]]]; [probeMap addProbe: [probeLibrary getProbeForVariable: "seed" inClass: [self class]]]; [probeMap addProbe: [[probeLibrary getProbeForMessage: "saveParameters" inClass: [self class]] setHideResult: 1]]; [probeMap addProbe: [[probeLibrary getProbeForMessage: "loadParameters" inClass: [self class]] setHideResult: 1]]; // Now install our custom probeMap into the probeLibrary. [probeLibrary setProbeMap: probeMap For: [self class]]; // We've created the workerSwarm and initialized it. // return the id of the newly created Swarm return obj;}-createEnd { return [super createEnd];}-saveParameters { [ObjectSaver save: self toFileNamed: "experiment.setup"]; return self;}-loadParameters { [ObjectLoader load: self fromFileNamed: "experiment.setup"]; return self;}-createOneFirmAtX: (int) x Y: (int) y withStat: (int) tmst { int sizeFirm; sizeFirm=[uniformIntRand getIntegerWithMin: (int) 2 withMax: (int) 6]; aFirm = [Firm createBegin: [self getZone]]; [aFirm setToPrint: 0]; [aFirm setSize:sizeFirm]; //assegna dimensione [aFirm setWeights]; [aFirm setModel:self withWorld: world ]; [aFirm setMonth:daysInMonth]; // [aFirm setSize:sizeFirm]; //assegna dimensione [aFirm setFirmStatus:tmst]; aFirm = [aFirm createEnd]; [aFirm setWorkers]; [firmList addLast: aFirm]; [aFirm setX: x Y: y]; [firmWorld putObject:aFirm atX: x Y: y]; [firmSpace putValue: sizeFirm atX: x Y: y]; return self;}-createOneFirm: (int) okToPrint { int sizeFirm; int tmp = 0; int coordX,coordY; sizeFirm=[uniformIntRand getIntegerWithMin: (int) 2 withMax: (int) 6]; while ((tmp!=1)) { coordX=[uniformIntRand getIntegerWithMin: (int)0 withMax:(int)(worldXSize-1)]; coordY=[uniformIntRand getIntegerWithMin: (int)0 withMax:(int)(worldYSize-1)]; if ([firmWorld getObjectAtX: coordX Y: coordY] == nil) { aFirm = [Firm createBegin: [self getZone]]; [aFirm setToPrint:okToPrint]; [aFirm setSize: sizeFirm]; //assegna dimensione [aFirm setWeights]; [aFirm setModel:self withWorld: world ]; [aFirm setMonth:daysInMonth]; [aFirm setFirmStatus:0]; //inizialmente e' inefficente aFirm = [aFirm createEnd]; [aFirm setWorkers]; [firmList addLast: aFirm]; [aFirm setX: coordX Y: coordY]; [firmWorld putObject:aFirm atX: coordX Y: coordY]; [firmSpace putValue: sizeFirm atX: coordX Y: coordY]; tmp=1; }// if } //while return self;}- buildObjects { int x1, y ; int a; // Here, we create the objects in the model // Then, create the food space and initialize it [randomGenerator setStateFromSeed: seed]; time=1; workerSpace = [WorkerSpace createBegin: [self getZone]]; [workerSpace setSizeX: worldXSize Y: worldYSize]; [workerSpace setP0:p0]; [workerSpace setP1:p1]; [workerSpace setP2:p2]; [workerSpace setP3:p3]; [workerSpace setP4:p4]; [workerSpace setP5:p5]; workerSpace= [workerSpace createEnd]; [workerSpace seedHCWithProb]; // Now set up the grid used to represent agent position // Grid2d enforces only 1 worker per site world = [Grid2d createBegin: [self getZone]]; [world setSizeX: worldXSize Y: worldYSize]; world = [world createEnd]; [world fillWithObject: nil]; // now create the World for Firms firmSpace = [WorkerSpace createBegin: [self getZone]]; [firmSpace setSizeX: worldXSize Y: worldYSize]; firmSpace =[firmSpace createEnd]; firmWorld = [Grid2d createBegin: [self getZone]]; [firmWorld setSizeX: worldXSize Y: worldYSize]; firmWorld = [firmWorld createEnd]; [firmWorld fillWithObject: nil]; // Now, create a bunch of workers to live in the world workerList = [List create: [self getZone]]; for (y = 0; y < worldYSize; y++) for (x1 = 0; x1 < worldXSize; x1++) { aworker = [Worker createBegin: [self getZone]]; [aworker setWorld: world WorkSpace: workerSpace Mod:self]; [aworker setMonth: daysInMonth]; [aworker setHumanCapital: [workerSpace getValueAtX:x1 Y:y]]; [aworker setPrint: 0]; aworker = [aworker createEnd]; [aworker setX: x1 Y: y]; [aworker createNeighborsAddressAtX: x1 Y: y]; [world putObject: aworker atX: x1 Y: y]; if ((y==1)&&(x1==0)) { [aworker setPrint:0]; // [aworker setPrint:1]; //0 se si vuole fare il deworker di un oggetto } [workerList addLast: aworker]; } // now let's create the Firms // creo tutte le firm firmList = [List create: [self getZone]]; [firmWorld fillWithObject: nil]; // for (y = 0; y < worldYSize; y++) // for (x1 = 0; x1 < worldXSize; x1++) //if (( (y % 2) == 0 ) && ((x1 % 2) == 0 ) ) { // [self createOneFirmAtX: x1 Y: y withStat:1]; //} else //{ // [self createOneFirmAtX: x1 Y: y withStat:0]; //} for (a = 0; a<numFirms; a++) { if (a==0){ [self createOneFirm:1]; } else { [self createOneFirm:0]; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?