📄 worker.m
字号:
// Worker.m Coevo#import "Worker.h"#import "ModelSwarm.h"#import "WorkerSpace.h"#import <simtools.h>#import <space/Grid2d.h>#import <random.h>@implementation Workerint replies[6][4] = {{1,1,-1,-1},{1,-1,1,-1},{1,-1,-1,-1},{1,1,-1,1}, {-1,-1,-1,1},{1,-1,1,1}};int expected[4] = {-1, 1 , 1, -1};-setMonth: (int) mo {month=mo;return self;}-setPrint: (int) pp {stampa = pp;return self;}-setWorld: (id) w WorkSpace: (id) f Mod:(id) m { world = w; workerSpace = f; model= m; successes = 0.0; return self;}-(int)getWorkerStatus {return internalState;} -setStatus: (int) st { internalState = st; return self;}-createEnd { [super createEnd]; worldXSize = [world getSizeX]; worldYSize = [world getSizeY]; return self;}-(int)reactTo: (int) stimulus {return replies[humanCapital][stimulus];}-createNeighborsAddressAtX: (int) xu Y: (int) yu { int s2, t2, index; index=0; for(s2 = yu+1; s2 >= yu-1; s2--) for(t2 = xu-1; t2 <= xu+1; t2++) { if(s2 != yu || t2 != xu) { neighborsAddress[index][0] = ((t2 + worldXSize) % worldXSize); neighborsAddress[index][1] = ((s2 + worldYSize) % worldYSize); index++; }; /*end if*/ }; /*end for */ if (stampa==1) { printf ("intervalli \n"); for (index=0; index<8; index++) {printf ("x = %d y = %d\n",neighborsAddress[index][0],neighborsAddress[index][0]); } }return self;}//It assigns a particular boolean function to the agent when it is created-setHumanCapital: (int) hCapital { humanCapital = hCapital; newHumanCapital = hCapital;//to avoid problems with the update method // printf ("humancapital = %d newhumanCapital = %d \n",humanCapital,newHumanCapital); return self;}-(int)getHumanCapital { return humanCapital;}-setX: (int) x Y: (int) y { xPos = x; yPos = y; return self;}-(float) getSuccesses { return successes; }-checkSuccesses { int i; float tmpSuccesses=0; if ((([model getTime] %month) == 0) && (internalState !=2)) { for (i=0;i<8;i++){ tmpSuccesses = [[world getObjectAtX: neighborsAddress[i][0] Y:neighborsAddress[i][1]] getSuccesses];if (stampa ==1){printf ("newX %d,newY %d \n ",neighborsAddress[i][0], neighborsAddress[i][1]); printf ("successi %f5.2 con tmp = %f5.2 \n", successes, tmpSuccesses);}if (tmpSuccesses>successes) {newHumanCapital = [[world getObjectAtX: neighborsAddress[i][0] Y:neighborsAddress[i][1]] getHumanCapital];if (stampa ==1){printf (" nuovo capitale del vicino -> %d", newHumanCapital);printf ("status vicino --> %ld \n", [workerSpace getValueAtX:neighborsAddress[i][0] Y: neighborsAddress[i][1]]); } } } }// end if tim%monthreturn self;}-setBossX: (int)xx Y:(int) yy {BossX = xx;BossY = yy;return self;}-incrSuccesses: (float) inc{ successes += inc;return self;}-updateHumanCapital {int tim;tim = [model getTime];if ((tim % month) == 0){ if (stampa ==1) { printf(" now I am Updating HC from %d to %d \n", humanCapital,newHumanCapital); } [workerSpace check]; humanCapital=newHumanCapital; [workerSpace putValue:newHumanCapital atX: xPos Y: yPos]; //printf("Updated \n");successes=0.0; } return self;}-step { int stimulus = 0; int tim; int temp; tim = [model getTime]; if ((tim % month) !=0) { if (internalState ==0) { //available { //<procedura controllo periodo IMPORTANTE //choose the stimulus //stimulus = [uniformIntRand getIntegerWithMin: (int) 0 withMax: (int) 3]; stimulus = [model getStimulus]; temp = replies[humanCapital][stimulus]* expected[stimulus]; if (temp >0) { successes+=1; } if (stampa ==1) { printf (" reply successo?? --> %d from rep %d and exp %d\n ",temp, replies[humanCapital][stimulus], expected[stimulus]); } } } else // end if internal =0 if (internalState ==1) { //engaged [[[model getFirmWorld] getObjectAtX:BossX Y: BossY] coordinateStimulus:stimulus withExp:expected[stimulus]]; } } else { //endif internal =1 successes=20; } return self;}-step1 {int stimulus=0;int tim;int temp; tim = [model getTime];if ((tim % month) !=0) { if (internalState ==0) { //available {//<procedura controllo periodo IMPORTANTE//choose the stimulusstimulus = [uniformIntRand getIntegerWithMin: (int) 0 withMax: (int) 3]; //stimulus = [model getStimulus]; temp = replies[humanCapital][stimulus]* expected[stimulus]; if (temp >0) { successes+=1; } if (stampa ==1) { printf (" reply successo?? --> %d from rep %d and exp %d\n ",temp, replies[humanCapital][stimulus], expected[stimulus]); } } } } else // end if internal =0 if (internalState ==1) { //engaged //pj: following caused nil_method exception // [[[model getFirmWorld] getObjectAtX:BossX Y: // BossY]coordinateStimulus:stimulus withExp:expected[stimulus]]; //pj: so insert these 3, track the error id aWorld = [model getFirmWorld]; id anObject = [aWorld getObjectAtX:BossX Y: BossY]; if (anObject != nil) [anObject coordinateStimulus:stimulus withExp:expected[stimulus]]; } else { //enif internal =1 successes=20; } return self;} @end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -