void sflcp(void) { int i; gensf(); setlcp( nseqs[1] + nisets[2]+1 + nseqs[2] + nisets[1]+1 ); /* fill M */ /* -A */ payratmatcpy(sfpay, 0, 1, 0, nseqs[1], nseqs[2], lcpM, 0, nseqs[1] + nisets[2]+1); /* -E\T */ intratmatcpy(sfconstr[1], 1, 1, nisets[1]+1, nseqs[1], lcpM, 0, nseqs[1] + nisets[2]+1 + nseqs[2]); /* F */ intratmatcpy(sfconstr[2], 0, 0, nisets[2]+1, nseqs[2], lcpM, nseqs[1], nseqs[1] + nisets[2]+1 ); /* -B\T */ payratmatcpy(sfpay, 1, 1, 1, nseqs[1], nseqs[2], lcpM, nseqs[1] + nisets[2]+1, 0); /* -F\T */ intratmatcpy(sfconstr[2], 1, 1, nisets[2]+1, nseqs[2], lcpM, nseqs[1] + nisets[2]+1, nseqs[1] ); /* E */ intratmatcpy(sfconstr[1], 0, 0, nisets[1]+1, nseqs[1], lcpM, nseqs[1] + nisets[2]+1 + nseqs[2], 0 ); /* define RHS q, using special shape of SF constraints RHS e,f */ for (i = 0; i < lcpdim; i++) rhsq[i] = ratfromi(0); rhsq[ nseqs[1] ] = ratneg(ratfromi(1)); rhsq[ nseqs[1] + nisets[2]+1 + nseqs[2] ] = ratneg(ratfromi(1)); }
void outbehavstrat(int pl, Rat *rplan, Bool bnewline) { char s[MAXSTRL]; int i; Move c; Iset h; Rat rprob, bprob; for (h = firstiset[pl]; h < firstiset[pl+1]; h++) for (c = h->move0, i=0; i < h->nmoves; c++, i++) { rprob = rplan[ c - firstmove[pl] ]; if ( rprob.num != 0) { movetoa(c, pl, s); printf(" %s", s); bprob = ratdiv( rprob, rplan[ h->seqin - firstmove[pl]]); if (!ratiseq(bprob, ratfromi(1) ) ) { rattoa(bprob, s); printf(":%s", s); } } } if (bnewline) printf("\n"); }
void behavtomixed(int pl) { Rat r; int i, strat; for (strat = 0; strat < nstrats[pl]; strat++) { strattomovetuple(strat, pl); /* multiply behavior probabilities */ r = ratfromi(1); for (i=0; i<nisets[pl]; i++) if(movetuple[pl][i]) r = ratmult(r, movetuple[pl][i]->behavprob) ; /* check if mixed strat probabilities themselves * troublesome. Does not seem so. * ...........comment out code.......... { char s[MAXSTRL]; r = ratmult(r, movetuple[pl][i]->behavprob) ; rattoa(r, s); printf("Debug: mixed prob %2d is %s\n", i, s) ; } */ mixedstrat[pl][strat] = r; } } /* end of behavtomixed(pl) */
void nflcp(void) { int i; gennf(); setlcp( nstrats[1] + 1 + nstrats[2] + 1 ); /* fill M */ /* -A */ payratmatcpy(nfpay, 0, 1, 0, nstrats[1], nstrats[2], lcpM, 0, nstrats[1] + 1); /* -E\T */ for (i = 0; i < nstrats[1]; i++) lcpM[i][nstrats[1] + 1 + nstrats[2]] = ratneg(ratfromi(1)); /* F */ for (i = 0; i < nstrats[2]; i++) lcpM[nstrats[1]][nstrats[1] + 1 + i] = ratfromi(1); /* -B\T */ payratmatcpy(nfpay, 1, 1, 1, nstrats[1], nstrats[2], lcpM, nstrats[1] + 1, 0); /* -F\T */ for (i = 0; i < nstrats[2]; i++) lcpM[ nstrats[1] + 1 + i] [nstrats[1]] = ratneg(ratfromi(1)); /* E */ for (i = 0; i < nstrats[1]; i++) lcpM[ nstrats[1] + 1 + nstrats[2]] [i] = ratfromi(1); /* define RHS q */ for (i = 0; i < lcpdim; i++) rhsq[i] = ratfromi(0); rhsq[ nstrats[1] ] = ratneg(ratfromi(1)); rhsq[ nstrats[1] + 1 + nstrats[2] ] = ratneg(ratfromi(1)); }
/* GSoC12: Tobenna Peter, Igwe */ Rat maxrow(Rat* rat, int n) { int i; Rat Mrow = ratfromi(0); for(i = 0; i < n; ++i) { Mrow = ratgreat(Mrow,rat[i]) ? Mrow : rat[i]; } return Mrow; }
Rat* getStrategies(Equilibrium eq) { int n = eq.lcpdim; Rat* strat; strat = malloc((n) * sizeof(Rat)); int i, row; gmpt num, den; ginit(num); ginit(den); for (i=1; i<=n; i++) { if((row = eq.bascobas[Z(i)]) < n) /* If Z(i) is basic */ { /* value of Z(i): scfa[Z(i)]*rhs[row] / (scfa[RHS]*det) */ gmulint(eq.scfa[Z(i)], eq.A[row][RHS(n)], num); gmulint(eq.det, eq.scfa[RHS(n)], den); greduce(num, den); strat[i-1] = ratinit(); gset(strat[i-1].num, num); gset(strat[i-1].den, den); } else if((row = eq.bascobas[W(i,n)]) < n) { strat[i-1] = ratfromi(0); /* value of W(i-n) is rhs[row] / (scfa[RHS]*det) copy(num, eq.A[row][RHS(n)]); mulint(eq.det, eq.scfa[RHS(n)], den); reduce(num, den); copy(strat[i-1].num, num); copy(strat[i-1].den, den);*/ } else { strat[i-1] = ratfromi(0); } } /* end of for (i=...) */ gclear(num); gclear(den); return strat; }
/* GSoC12: Tobenna Peter, Igwe */ Rat maxMatrix(Rat** rat, int m, int n) { int i; int tmpm = m; int tmpn = n; Rat M = ratfromi(0); for(i = 0; i < tmpm; ++i) { Rat r = maxrow(rat[i], tmpn); M = ratgreat(M, r) ? M : r; } return M; }
void allocnf(void) { int pl, i, j; /* payoff matrices, two players only here, init to pay 0 */ T2ALLOC (nfpay, nstrats[1], nstrats[2], Payvec); for (i=0; i<nstrats[1]; i++) for (j=0; j<nstrats[2]; j++) for (pl=1; pl < PLAYERS; pl++) nfpay[i][j][pl-1] = ratfromi(0); for (pl=0; pl < PLAYERS; pl++) { movetuple[pl] = TALLOC (nisets[pl], Move); mixedstrat[pl] = TALLOC (nstrats[pl], Rat); } }
void genprior(Flagsprior flags) { int pl; Iset h; if (0 == flags.seed) { gencentroid(); return ; } /* generate random priors for all information sets */ srand(FIRSTPRIORSEED + flags.seed); for (pl=1; pl < PLAYERS; pl++) for (h = firstiset[pl]; h < firstiset [pl+1]; h++) if ( h->nmoves > 2) { fprintf(stderr, "Sorry, only binary info sets so far.\n") ; exit(1) ; } else { Rat a; double x; x = rand() / (double) RAND_MAX; a = contfract( x, flags.accuracy) ; /* make sure to get a properly mixed prior, * unless flags.accuracy == 1, * in which case we have a random pure strategy * because this statement flips 0 to 1 and vice versa */ if (a.num == 0) { a.num = 1 ; a.den = flags.accuracy; } else if (a.den == 1) /* "else" for pure strategy */ { a.num = flags.accuracy - 1 ; a.den = flags.accuracy; } h->move0->behavprob = a ; ((h->move0)+1)->behavprob = ratadd(ratfromi(1), ratneg(a)) ; } }
void allocsf(void) { static int oldnseqs1 = 0; static int oldconstrows[PLAYERS] = {0, 0, 0}; int pl, i, j; int nrows; /* payoff matrices, two players only here, init to pay 0 */ FREE2(sfpay, oldnseqs1); oldnseqs1 = nseqs[1]; T2ALLOC (sfpay, nseqs[1], nseqs[2], Payvec); for (i=0; i<nseqs[1]; i++) for (j=0; j<nseqs[2]; j++) for (pl=1; pl < PLAYERS; pl++) sfpay[i][j][pl-1] = ratfromi(0); /* constraint matrices, any number of players */ /* sfconstr[0] stays unused */ for (pl=1; pl < PLAYERS; pl++) { FREE2(sfconstr[pl], oldconstrows[pl]); oldconstrows[pl] = nrows = nisets[pl]+1; /* extra row for seq 0 */ T2ALLOC (sfconstr[pl], nrows, nseqs[pl], int); } } /* end of allocsf() */
void outmixed(int pl, Rat *mixed, Bool bnewline) { int i; char s[MAXSTRL]; Rat prob; for (i = 0; i < nstrats[pl]; i++) { prob = mixed[i] ; if ( prob.num != 0) { strattomovetuple (i, pl); movetupletoa(pl, s); printf(" %s", s); if (!ratiseq(prob, ratfromi(1) ) ) { rattoa(prob, s); printf(":%s", s); } } } if (bnewline) printf("\n"); }
void setrsfcomplconstr (int paytopl, Rat ** intoM, int rowoffset, int coloffset, Bool negpaymatrix, Rat * rhsvec, Bool negrhs) { int othpl = 3 - paytopl; /* other player */ int i,j,k, a; Rat s; Rat * tmprow; /* temporary row for triple matrix product comptn */ tmprow = TALLOC( nseqs[othpl] , Rat); /* first blockrow of dim redsfdim[paytopl] */ for (i = 0; i < redsfdim[paytopl]; i++) { /* compute tmprow (with index j ) as * if paytopl==1: row i of K\T A, * if paytopl==2: row i of L\T B\T */ for (j = 0; j < nseqs[othpl]; j++) { s = ratfromi(0); for (k = 0; k < nseqs[paytopl]; k++) /* a = entry of K\T resp. L\T */ if ((a = realplfromredsf[paytopl][k][i] ) != 0) s = ratadd( s, ratmult( ratfromi(a), (paytopl==1) ? sfpay[k][j][0] : sfpay[j][k][1] )) ; tmprow[j] = s; } /* compute i,k entry of * if paytopl==1: K\T A L = Ahat * if paytopl==2: L\T B\T K = Bhat \T * comments from now only for paytopl==1 */ for (k = 0; k < redsfdim[othpl]; k++) { s = ratfromi(0); for (j = 0; j < nseqs[othpl]; j++) if ( (a = realplfromredsf [othpl][j][k] ) != 0) s = ratadd( s, ratmult( ratfromi(a), tmprow[j] )) ; /* fill matrix entry with -Ahat */ intoM [ rowoffset + i] [ coloffset + k ] = negpaymatrix ? ratneg(s) : s ; } /* set matrix entry for Ehat\T */ for (j = 0; j < irreddim[paytopl]; j++) { s = ratfromi( redsfconstr [paytopl][j][i] ); intoM [ rowoffset + i] [ coloffset + redsfdim[othpl] + j] = negpaymatrix ? s : ratneg(s) ; } /* set LCP rhs entry i of K\T A l = ahat */ s = ratfromi(0); for (j = 0; j < nseqs[othpl]; j++) s = ratadd( s, ratmult( tmprow[j], ratfromi ( realplconst [othpl] [j] ) ) ) ; rhsvec[i] = negrhs ? ratneg(s) : s ; } /* second blockrow of dim irreddim [othpl] */ for (i = 0; i < irreddim [othpl]; i++) { /* set LCP matrix entry for -Fhat */ for (j = 0; j < redsfdim[othpl]; j++) { s = ratfromi( redsfconstr [othpl][i][j] ); intoM [ rowoffset + redsfdim[paytopl] + i ] [ coloffset + j] = negpaymatrix ? ratneg(s) : s ; } /* set LCP rhs entry i of -fhat */ s = ratfromi( redsfrhs [othpl][i] ); rhsvec[ redsfdim[paytopl] + i] = negrhs ? s : ratneg(s) ; } free(tmprow); }