#include #include #include #define RAND_INT(l,h) (((int)(random() * ((double)(h)-(l)+1))) + (l)) /* Function prototypes */ double random (void); void rand_seed (unsigned int); double u1(int s1,int s2); double u2(int s1,int s2); int BR1(int s2, int myS1); int BR2(int s1, int myS2); int CHECK(int myS1,int myS2); int norepetition(int h, int x, int y, int * ptr1, int * ptr2); int norepetitionglq(int h, int x, int y, int * ptr1, int * ptr2); int no_smaller(int h, int g, int * ptr1, int * ptr2) ; void *malloc(size_t size); void *realloc(void *ptr,size_t size); void free(void *ptr); static unsigned int SEED = 93186752; double A1=0.5; double A2=0.5; double B1=0.5; double B2=0.5; int N = 100; int largest_eq1 = 100; int largest_eq2 = 100; /* ---- MAIN ---- */ int main(int argc, char *argv[]) { int S1, S2, i, numbeq, x, y, f, g, count, games; double showme1, showme2; time_t tinic, tfinal; int total_neq = 0; double total_time = 0.0; /* Output file */ FILE *fp; FILE *fp2; fp = fopen("store_eq", "w"); fprintf(fp, "\t n_eq. \t time \n"); if (argc !=3) { printf("Wrong number of arguments. \n"); exit(0); } /* Size of the game (number of strategies of each p.).*/ /* printf("Enter the number of strategies: "); scanf("%i", &N); */ N = atoi(*(argv + 1)); largest_eq1 = N; largest_eq2 = N; /* Number of iterations (games). */ games = atoi(*(argv + 2)); /* games = 1; printf("Enter the number of iterations: "); scanf("%i", &games); */ for (i = 1; i <= games; i++){ /* Get parameter values for payoffs. */ A1=(double)random(); A2= (double)random(); B1=(double)random(); B2= (double)random(); /* Initialize time. */ tinic =time(NULL); numbeq=0; for (x = 0; x <= N; x++) { /* for (y = 0; y <= N; y++) { */ y=BR1(x, 0); numbeq += CHECK(y,x); /* } */ } tfinal =time(NULL); fprintf(fp, "\t %d \t %f \n", numbeq, difftime(tfinal,tinic)); total_time += (double)difftime(tfinal,tinic); total_neq += numbeq; } printf("Average number of eq. %5.2f \n", (float)total_neq/games); printf("Average time %5.2f \n", (float)total_time/games); /* Store summary results */ fp2 = fopen("summary", "w"); fprintf(fp2, "Dumb algorithm: \n"); fprintf(fp2, "Number of iterations: %d \n", games); fprintf(fp2, "Number of strategies: %d \n", N); fprintf(fp2, "Average number of eq. %5.2f \n", (float)total_neq/games); fprintf(fp2, "Average time %5.2f \n", (float)total_time/games); fclose(fp); fclose(fp2); return 0; } /* This function checks if 2 wants to deviate from S2 when 1 plays S1 */ int CHECK(int myS1, int myS2) { int br2, iseq; iseq=0; br2=BR2(myS1, 0); if(br2 == myS2) { iseq=1; } return iseq; } /* This function finds the best-response by 1 to a strategy s2 by 2 */ /* when 1 is restricted to strategies greater than or equal to S1 */ int BR1(int mys2, int myS1) { int x, curmax; double payoff, curmaxv; curmax=myS1; curmaxv=u1(myS1,mys2); for(x=myS1; x <= largest_eq1; x++) { payoff = u1(x,mys2); if(payoff > curmaxv) { curmaxv=payoff; curmax=x; } } return curmax; } /* This function finds the best-response by 2 to a strategy s1 by 1 */ /* when 2 is restricted to strategies greater than or equal to S2 */ int BR2(int mys1, int myS2) { int x, curmax; double payoff, curmaxv; curmax=myS2; curmaxv=u2(mys1,myS2); for(x=myS2; x <= largest_eq2; x++) { payoff = u2(mys1,x); if(payoff > curmaxv) { curmaxv=payoff; curmax=x; } } return curmax; } /* This function is the payoff function of player 1*/ double u1(int s1,int s2) { double value, s1here, s2here, ls1; s1here= (double) 100*s1/N; s2here= (double) 100*s2/N; value= -0.01*A1*(s1here-s2here)*(s1here-s2here)+2*A2*sin(100*s1here) + 0.0001*( (1-A1)*s1here*(1+s2here) - 0.01*(0.5-A2)*s1here*s1here ); return value; } /* This function is the payoff function of player 2*/ double u2(int s1,int s2) { double value, s1here, s2here; s1here= (double) 100*s1/N; s2here= (double) 100*s2/N; value= -0.01*B1*(s1here-s2here)*(s1here-s2here)+2*B2*sin(100*s2here) + 0.0001*( (1-B1)*s1here*(1+s2here) - 0.01*(0.5-B2)*s2here*s2here ); return value; } int norepetition(int h, int x, int y, int * ptr1, int * ptr2) { int norep = 1; int i; if (h > 0) { for (i=0; i < h; i++) { if( (*(ptr1 + i) == x) && (*(ptr2 + i) == y)) norep = 0; } } return norep; } int norepetitionglq(int h, int x, int y, int * ptr1, int * ptr2) { int norep = 1; int i; if (h > 0) { for (i=0; i < h; i++) { if( (*(ptr1 + i) <= x) && (*(ptr2 + i) <= y)) norep = 0; } } return norep; } int no_smaller(int h, int g, int * ptr1, int * ptr2) { int nosmall = 1; int i; for (i=0; i < h; i++) { if( ( (*(ptr1 + i) < *(ptr1 + g)) && (*(ptr2 + i) <= *(ptr2 + g)) ) || ( (*(ptr1 + i) <= *(ptr1 + g)) && (*(ptr2 + i) < *(ptr2 + g)) ) ) nosmall = 0; } return nosmall; } double random () { static unsigned int a = 1588635695, m = 4294967291U, q = 2, r = 1117695901; SEED = a*(SEED % q) - r*(SEED / q); return ((double)SEED / (double)m); } void rand_seed (unsigned int init) {if (init != 0) SEED = init;} /* These examples produce unique eq. (in the limit grid) see my proof */ /* value=10*exp((-1)*(s2here-s1here)*(s2here-s1here))+ */ /* 0.004*s1here*(100- s1here); */ /* value=10*exp((-1)*(s2here-s1here)*(s2here-s1here)) + */ /* 0.004*s2here*(100- s2here); */ /* value=(-5)*(s2here-s1here)*(s2here-s1here)+ */ /* 0.4*s1here*(100- s1here); */ /* value=(-5)*(s2here-s1here)*(s2here-s1here)+ */ /* 0.4*s2here*(100- s2here); */