Ejemplo n.º 1
0
// play CA1 event (us vs nature game for one group)
void event_CA1()
{
  int i, g;  
  // choose a group randomly
  g = rnd(G); 
  // play us vs nature game
  B = normal(Bo, Sigma_B);                                     // randomly choose Benefit B
  B = MAX(B,0);                                                // check if B is less than zero
  update_XP_CA1(g);                                            // updates group effort and probability of success of the group
  updateIndividualPayoffAfterProduction(g, 1, P[g]);           // update payoff of individuals in the group and of the group 
#if PUNISH
  punish(g);
#endif
  updateGroupPayoff(g);
  
  Tx = malloc( GS[g]*sizeof(double [TRAITS]));  
  Xmax  = malloc(GS[g]*sizeof(double));
  // get state of strategies before update
  for(i = 0; i < GS[g]; i++){
    Tx[i][0]   = x[g][i];
    Tx[i][1] = dxi[g][i];
    Tx[i][2] = dsi[g][i];  
    #if Accumulatedpayoff      
      Xmax[i] = pow( ( (1-Discount)*Api[g][i] + (1.0 + B*V[g][i]) )/C, I_Alpha );
    #else
      Xmax[i] = pow( (1.0 + B*V[g][i])/C, I_Alpha);                                 // upper bound of x at each role 
    #endif
  }  
#if PUNISH
  Lookup = malloc(2*(GS[g]-1)*sizeof(int[3]));                                     // allocate memory for lookup table  
  Lidx   = malloc(2*(GS[g]-1)*sizeof(int));
  #if FORESIGHT
    Strat = malloc(GS[g]*sizeof(double[TRAITS]));    
    FS = malloc(GS[g]*sizeof(double[TRAITS]));
    Lt = malloc(2*(GS[g]-1)*sizeof(int[3])); 
    Idx = malloc(2*(GS[g]-1)*sizeof(int));
  #endif
#endif  
  // update strategies of individual with probability Mu
  for(i = 0; i < GS[g]; i++){   // through every individual in the group
    if( U01() < Mu)          // if random number is less than Mu, then update strategy
      updateStrategy(g, i, 1, X0_Be);    // use quantal response approach to update strategy (each individual strategy is updated believing other individuals do not change)    
  }   
  free(Tx); free(Xmax);
#if PUNISH
  free(Lookup); free(Lidx);  
  #if FORESIGHT
    free(Strat); free(FS); free(Lt); free(Idx);    
  #endif
#endif  
}
Ejemplo n.º 2
0
void meetPictureClerk(int* isPicCleared, int socialNumber) {
	int i, currentClerkIndex, likingPicture;

	currentClerkIndex = -1;
	while (currentClerkIndex == -1) {
		for (i = 0; i < pictureClerkAmount; i++){
			Acquire(pictureLocks[i]);
			if (GetMonitor(pictureClerkState[i]) == AVAILABLE){
				currentClerkIndex = i;
				SetMonitor(pictureClerkState[i], BUSY);
				break;
			} else {
				Release(pictureLocks[i]);
			}
		}
		if (currentClerkIndex == -1) {
			punish(10, 100); /* wait a little bit, someone will be available soon */
		}
	}

		/* has pictureLocks[i] stil acquired at this point */

		Write("Senator ", 8, ConsoleOutput);
		WriteInt(socialNumber);
		Write(" has gotten in regular line for PictureClerk ", sizeof(" has gotten in regular line for PictureClerk "), ConsoleOutput);
		WriteInt(currentClerkIndex);
		Write("\n", 1, ConsoleOutput);

		SetMonitor(pictureClerkDB[currentClerkIndex], socialNumber);

	  Write("Senator ", 8, ConsoleOutput);
		WriteInt(socialNumber);
		Write(" has given SSN ", sizeof(" has given SSN "), ConsoleOutput);
		WriteInt(socialNumber);
		Write(" to PictureClerk ", sizeof(" to PictureClerk "), ConsoleOutput);
		WriteInt(currentClerkIndex);
		Write("\n", 1, ConsoleOutput);

		Signal(pictureCV[currentClerkIndex], pictureLocks[currentClerkIndex]);
		Wait(pictureCV[currentClerkIndex], pictureLocks[currentClerkIndex]);

		/*awoken */

		SetMonitor(pictureClerkDBCleared[currentClerkIndex], TRUE);
		Signal(pictureCV[currentClerkIndex], pictureLocks[currentClerkIndex]);
		Wait(pictureCV[currentClerkIndex], pictureLocks[currentClerkIndex]);

	Release(pictureLocks[currentClerkIndex]);
	*isPicCleared = TRUE;
}
Ejemplo n.º 3
0
static void adv_sleep(int usec)
{
        /*
         * usleep is depracated by linux, but nanosleep is a bit clumpsy.
         * here implement usleep interface by nanosleep . nanosleep accept
         * long, but it cannot prevent passing in a signed value, because the
         * compiler perform implict type conversion. So, assert
         *
         * From the man page, nanosleep only sleep a thread.
         */

        assert(usec >= 0);
        struct timespec req = { .tv_sec = 0,
                                .tv_nsec = usec * 1e3 };
        nanosleep(&req, NULL);
}

static void *stealer(void *amountptr)
{
        int amount = *(int*)amountptr;
        char name[15];
        sprintf(name, "stealer %d", amount);

        notify("born\n", name);
        int *mypocketptr = malloc(sizeof(int));    /* no money, so have to steal */
        int trial = 0;

        while (trial < MAXTRIAL){
                pthread_mutex_lock(&acc1.mutex);
                trial++;
                notify("start trial %d\n", name, trial);
                if (amount > acc1.balance){
                        notify("failed to steal\n", name);
                        pthread_mutex_unlock(&acc1.mutex);
                        punish(amount);
                } else {
                        steal(name, amount, &acc1);
                        if (sleepedguy != 0){
                                pthread_cond_broadcast(&acc1.cond_full);
                        }
                        pthread_mutex_unlock(&acc1.mutex);
                        save(amount, mypocketptr);
                }
        }
        return mypocketptr;
}
Ejemplo n.º 4
0
void doCashierStuff(int mySSN, int* cash){

	int socialSecurityNum;
	int myLine;
	int workLock;
	int workCV;
	int readyToPay;

	socialSecurityNum = mySSN;
	
	/*First get in line with a generic method*/
	myLine = getInLine(&cashier,socialSecurityNum,cash);
	workLock = cashier.clerkLock[myLine];
	workCV = cashier.clerkCV[myLine];
	Acquire(workLock);

	/*Tell Clerk CV, then wait*/
	Uprintf("Customer #%d has given SSN %d to Cashier #%d.\n", 47, socialSecurityNum, socialSecurityNum, myLine, 0);
	tellCashierSSN(mySSN,myLine);
	Signal(workCV, workLock);
	Wait(workCV, workLock);

	/*Decide weather to self-punish*/
	readyToPay = cashierChecked[mySSN];	
	if(readyToPay == 0) {
		/*Release, punish, and leave*/
		Uprintf("Customer #%d has gone to Cashier #%d too soon. They are going to the back of the line.\n", 87, socialSecurityNum, myLine,0, 0);
		Signal(workCV, workLock);
		Release(workLock);
		punish(punishTime);
		return;
	}

	/*Now you can pay*/
	Uprintf("Customer #%d has given Cashier #%d $100\n", 41, socialSecurityNum, myLine,0, 0);
	payCashier(mySSN,cash);
	Signal(workCV, workLock);
	Wait(workCV, workLock);

	/*Now you've been woken up because you have the passport, so leave*/
	Signal(workCV, workLock);
	Release(workLock);
	return;
}
Ejemplo n.º 5
0
void meetApplicationClerk(int* isAppCleared, int socialNumber) {
	int i, currentClerkIndex;

	/* Find an AVAILABLE applicationClerk or wait */
	currentClerkIndex = -1;
	while (currentClerkIndex == -1) {
		for (i = 0; i < applicationClerkAmount; i++){
			Acquire(applicationLocks[i]);
			if (GetMonitor(applicationClerkState[i]) == AVAILABLE){
				currentClerkIndex = i;
				SetMonitor(applicationClerkState[i], BUSY);
				break;
			} else {
				Release(applicationLocks[i]);
			}
		}
		if (currentClerkIndex == -1) {
			punish(10,100); /* wait a little bit, someone will be available soon */
		}
	}

  Write("Senator ", 8, ConsoleOutput);
  WriteInt(socialNumber);
  Write(" has gotten in regular line for ApplicationClerk ", sizeof(" has gotten in regular line for ApplicationClerk "), ConsoleOutput);
  WriteInt(currentClerkIndex);
  Write("\n", 1, ConsoleOutput);

	/* Give the application clerk your social in global variable */ 
	SetMonitor(applicationClerkDB[currentClerkIndex], socialNumber);

	Write("Senator ", 8, ConsoleOutput);
	WriteInt(socialNumber);
	Write(" has given SSN ", sizeof(" has given SSN "), ConsoleOutput);
	WriteInt(socialNumber);
	Write(" to ApplicationClerk ", sizeof(" to ApplicationClerk "), ConsoleOutput);
	WriteInt(currentClerkIndex);
	Write("\n", 1, ConsoleOutput);

	Signal(applicationCV[currentClerkIndex], applicationLocks[currentClerkIndex]);
	Wait(applicationCV[currentClerkIndex], applicationLocks[currentClerkIndex]);
	Release(applicationLocks[currentClerkIndex]);
	*isAppCleared = TRUE;
}
Ejemplo n.º 6
0
void doPassportClerkStuff(int socialSecurityNum,int*cash){

	int mySSN;
	int myLine;
	int workLock;
	int workCV;
	int myPassportChecked;

	mySSN = socialSecurityNum;

	/*First get in line with a generic method*/
	myLine = getInLine(&passPClerk,socialSecurityNum,cash);
	workLock = passPClerk.clerkLock[myLine];
	workCV = passPClerk.clerkCV[myLine];
	
	Acquire(workLock);

	/*Tell Clerk CV, then wait*/
	Uprintf("Customer #%d has given SSN %d to PassportClerk #%d.\n", 52, socialSecurityNum, socialSecurityNum, myLine, 0);
	tellPassportClerkSSN(mySSN,myLine);
	Signal(workCV, workLock);
	Wait(workCV, workLock);

	/*Now leave*/
	Signal(workCV, workLock);
	Release(workLock);

	/*Decide whether to self-punish*/
	myPassportChecked = passportClerkChecked[mySSN];
	
	if(myPassportChecked == 0) {
		Uprintf("Customer #%d has gone to PassportClerk #%d too soon. They are going to the back of the line.\n", 93, socialSecurityNum, myLine,0, 0);
		punish(punishTime);
	}
	return;
}
Ejemplo n.º 7
0
// play CA2 event (us vs them game for ng no. of groups)
void event_CA2(int ng)
{
  int i, j, k, *g, *sg, preexists;
  double wgx, xb;    // sum of groups contribution with Beta exponent
  
  g = malloc(ng*sizeof(int));       // array for group index of selected group in its polity  
  sg = malloc(ng*sizeof(int));      // array for selected unique group indices
  
  // choose ng no. of groups randomly
  for(k = 0; k < ng; k++){
    do{
      g[k] = rnd(G);            // selection from uniform distribution; // generate random no. less than total no. of groups
      preexists = 0;
      for(j = 0; j < k; j++){      // check if it already exists in the selected group array 'sg' for playing game
	  if(g[k] == sg[j]){
	    preexists = 1; break;
	  }
	}
    }while(preexists == 1);

    sg[k] = g[k];
  }
   
  // play us vs them game
  B = normal(Bo, Sigma_B);                // randomly choose Benefit B
  if(B < 0) B = 0;                     // check if B is less than zero
  update_XP_CA2(g, ng);        // caculate probability of success of ng groups in array pbs    
  
  for(k = 0; k < ng; k++){
    updateIndividualPayoffAfterProduction(g[k], ng, P[g[k]]);          // update payoff of individuals in each group and of each group 
#if PUNISH
    punish(g[k]);
#endif
    updateGroupPayoff(g[k]);
  } 
    
  for(wgx = 0, k = 0; k < ng; k++)
    wgx += pow(X[g[k]], Beta);        // sum of group contribution with Beta exponent
  
  for(k = 0; k < ng; k++){    
    Tx    = malloc( GS[g[k]]*sizeof(double [TRAITS]));
    Xmax  = malloc(GS[g[k]]*sizeof(double));
    // get state of strategies before update
    for(i = 0; i < GS[g[k]]; i++){
      Tx[i][0] = x[g[k]][i];
      Tx[i][1] = dxi[g[k]][i];
      Tx[i][2] = dsi[g[k]][i];      
#if Accumulatedpayoff      
  Xmax[i] = pow( ( (1-Discount)*Api[g[k]][i] + (1.0 + B*V[g[k]][i]) )/C, I_Alpha );         // upper bound of x at each role 
#else
  Xmax[i] = pow( (1.0 + B*V[g[k]][i])/C, I_Alpha);                                 
#endif 
    }    
#if PUNISH
    Lookup = malloc(2*(GS[g[k]]-1)*sizeof(int[3]));                                     // allocate memory for lookup table  
    Lidx   = malloc(2*(GS[g[k]]-1)*sizeof(int));
    #if FORESIGHT
      Strat = malloc(GS[g[k]]*sizeof(double[TRAITS]));      
      FS = malloc(GS[g[k]]*sizeof(double[TRAITS]));
      Lt = malloc(2*(GS[g[k]]-1)*sizeof(int[3])); 
      Idx = malloc(2*(GS[g[k]]-1)*sizeof(int));
    #endif
#endif  
    // update strategies of individual with probability Mu
    for(i = 0; i < GS[g[k]]; i++){   // through every individual in group
      if( U01() < Mu){          // if random number is less than Mu, then update strategy
	xb = wgx - pow( X[g[k]], Beta );
	xb = MAX(xb, 0.001);
	updateStrategy(g[k], i, ng, xb);    // use quantal response approach to update strategy (each individual strategy is updated believing other individuals do not change)
      }
    }
    free(Tx); 
#if PUNISH
    free(Lookup); free(Lidx);  
    #if FORESIGHT
      free(Strat); free(FS); free(Lt); free(Idx);    
    #endif
#endif 
  }
  free(g); free(sg); free(Xmax); 
}
Ejemplo n.º 8
0
void meetCashier(int* isCashCleared, int* cash, int socialNumber) {
	int i, currentClerkIndex;

	currentClerkIndex = -1;
	while (currentClerkIndex == -1) {
		for (i = 0; i < cashierAmount; i++) {
			Acquire(cashierLocks[i]);
			if (GetMonitor(cashierClerkState[i]) == AVAILABLE) {
				currentClerkIndex = i;
				/* set clerk to busy, helping us now */
				SetMonitor(cashierClerkState[i], BUSY);
				break;
			} else { /* either on break or busy, can't do anything with this clerk atm */
				Release(cashierLocks[i]);
			}
		}
		if (currentClerkIndex == -1) {
			punish(10,100); /* wait a little bit, someone will be available soon */
		}
	}
	
	Write("Senator ", 8, ConsoleOutput);
	WriteInt(socialNumber);
	Write(" has gotten in regular line for CashierClerk ", sizeof(" has gotten in regular line for CashierClerk "), ConsoleOutput);
	WriteInt(currentClerkIndex);
	Write("\n", 1, ConsoleOutput);

	/* customer is at cashier now */
	SetMonitor(cashierDB[currentClerkIndex], socialNumber);

	Write("Senator ", 8, ConsoleOutput);
	WriteInt(socialNumber);
	Write(" has given SSN ", sizeof(" has given SSN "), ConsoleOutput);
	WriteInt(socialNumber);
	Write(" to cashier ", sizeof(" to cashier "), ConsoleOutput);
	WriteInt(currentClerkIndex);
	Write("\n", 1, ConsoleOutput);

	Signal(cashierCV[currentClerkIndex], cashierLocks[currentClerkIndex]);
	Wait(cashierCV[currentClerkIndex], cashierLocks[currentClerkIndex]);

	if (GetMonitor(cashierDBCleared[currentClerkIndex])){
		/* that means cashier is notified that the previous */
		/* requirements have been completed and is cleared to continue */
		/* for user to pay and get checked off */
		
		Write("Senator ", 8, ConsoleOutput);
		WriteInt(socialNumber);
		Write(" has given Cashier ", sizeof(" has given Cashier "), ConsoleOutput);
		WriteInt(currentClerkIndex);
		Write(" $100", sizeof(" $100"), ConsoleOutput);
		Write("\n", 1, ConsoleOutput);

		*cash -= 100;
		*isCashCleared = TRUE;
		Release(cashierLocks[currentClerkIndex]);
	} else {
		/* customer should NOT be here, punish the user */		
		Write("Senator ", 8, ConsoleOutput);
		WriteInt(socialNumber);
		Write(" has gone to Cashier ", sizeof(" has gone to Cashier "), ConsoleOutput);
		WriteInt(currentClerkIndex);
		Write(" too soon. They are going to the back of the line.", sizeof(" too soon. They are going to the back of the line."), ConsoleOutput);
		Write("\n", 1, ConsoleOutput);

		Release(cashierLocks[currentClerkIndex]);
		punish(100, 1000);
	}
}
Ejemplo n.º 9
0
void meetPassportClerk(int* isPassCleared, int socialNumber) {
	int i, currentClerkIndex;

	/* will talk to clerk now */
	currentClerkIndex = -1;
	while (currentClerkIndex == -1) {
		for (i = 0; i < passportClerkAmount; i++){
			Acquire(passportLocks[i]);
			if (GetMonitor(passportClerkState[i]) == AVAILABLE){
				currentClerkIndex = i;
				SetMonitor(passportClerkState[i], BUSY);
				break;
			} else {
				Release(passportLocks[i]);
			}
		}
		if (currentClerkIndex == -1) {
			punish(10,100); /* wait a little bit, someone will be available soon */
		}
	}

  Write("Senator ", 8, ConsoleOutput);
  WriteInt(socialNumber);
  Write(" has gotten in regular line for PassportClerk ", sizeof(" has gotten in regular line for PassportClerk "), ConsoleOutput);
  WriteInt(currentClerkIndex);
  Write("\n", 1, ConsoleOutput);

	SetMonitor(passportClerkDB[currentClerkIndex], socialNumber);

  Write("Senator ", 8, ConsoleOutput);
	WriteInt(socialNumber);
	Write(" has given SSN ", sizeof(" has given SSN "), ConsoleOutput);
	WriteInt(socialNumber);
	Write(" to passportClerk ", sizeof(" to passportClerk "), ConsoleOutput);
	WriteInt(currentClerkIndex);
	Write("\n", 1, ConsoleOutput);

	Signal(passportCV[currentClerkIndex], passportLocks[currentClerkIndex]);
	Wait(passportCV[currentClerkIndex], passportLocks[currentClerkIndex]);

	/*awoken */

	if (passportClerkDBCleared[currentClerkIndex]){
		/* that means cashier is notified that the previous */
		/* requirements have been completed and is cleared to continue */
		/* for user to pay and get checked off */
		Release(passportLocks[currentClerkIndex]);
		*isPassCleared = TRUE;
	} else {
		/* customer should NOT be here, punish the user */
		
		Write("Senator ", 8, ConsoleOutput);
		WriteInt(socialNumber);
		Write(" has gone to passportClerk ", sizeof(" has gone to passportClerk "), ConsoleOutput);
		WriteInt(currentClerkIndex);
		Write(" too soon. They are going to the back of the line.", sizeof(" too soon. They are going to the back of the line."), ConsoleOutput);
		Write("\n", 1, ConsoleOutput);

		Release(passportLocks[currentClerkIndex]);
		punish(100, 1000);
	}
}