void gensf(void) { int pl, i, j; Outcome z; allocsf(); behavtorealprob(0); /* get realization probabilities of leaves */ /* sf payoff matrices */ for (z=outcomes; z < lastoutcome; z++) { Node u = z->whichnode; i = u->defseq[1] - firstmove[1]; j = u->defseq[2] - firstmove[2]; for (pl=1; pl < PLAYERS; pl++) sfpay[i][j][pl-1] = ratadd(sfpay[i][j][pl-1], ratmult(u->defseq[0]->realprob, z->pay[pl-1]) ); } /* sf constraint matrices, sparse fill */ for (pl=1; pl < PLAYERS; pl++) { sfconstr[pl][0][0] = 1; /* empty sequence */ for (i=0; i < nisets[pl]; i++) sfconstr[pl][i+1][(firstiset[pl]+i)->seqin - firstmove[pl]] = -1; for (j=1; j < nseqs[pl]; j++) sfconstr[pl][(firstmove[pl]+j)->atiset - firstiset[pl]+1][j] = 1; } } /* end of gensf() */
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 gennf(void) { int pl, i, j; int *slist[PLAYERS]; /* list of strategies for nf generation */ int nl[PLAYERS]; /* no of strategies compatible with leave seq */ Outcome z; Payvec v; Payvec *nfrow; int nfcolpos; /* determine nf size and allocate */ for (pl=0; pl < PLAYERS; pl++) nstrats[pl] = numstratsnfpre(pl); allocnf(); for (pl=0; pl < PLAYERS; pl++) slist[pl] = TALLOC (nstrats[pl], int); behavtorealprob(0); /* get realization probabilities of leaves */ /* nf payoff matrices */ for (z=outcomes; z < lastoutcome; z++) { Node u = z->whichnode; for (pl=1; pl < PLAYERS; pl++) { v[pl-1] = ratmult(u->defseq[0]->realprob, z->pay[pl-1] ); nl[pl] = seqtostratlist (u->defseq[pl], pl, slist[pl]); } for (i=0; i < nl[1]; i++) { nfrow = nfpay[slist[1][i]]; for (j=0; j < nl[2]; j++) { nfcolpos = slist[2][j]; for (pl=1; pl < PLAYERS; pl++) nfrow[nfcolpos][pl-1] = ratadd(nfrow[nfcolpos][pl-1], v[pl-1]); } } } /* end of for all outcomes z */ for (pl=0; pl < PLAYERS; pl++) free(slist[pl]); } /* end of gennf() */
static /* reads score{} section */ void readscore(Inst *insttop) { char s[128]; Inst *p; Rat grpmul, timesig, curtime, lastbar, lastnote, rattmp; Tempo *tempotop, *tempop; Note *notetop, *pn, *qn, *ln; Nextp *nextlist, *nextpp; int accidentals, octaves, vertical, key[PITCHCLASSES], barkey[PITCHCLASSES], transpose, z; double maxtime, fcurtime; #ifdef DEBUG printf("Reading score section\n"); #endif maxtime = 0.0; if (expectchar('{')) scotferror(Str("Syntax error: no {")); tempotop = (Tempo *) malloc(sizeof(Tempo)); tempotop->time.num = 0; tempotop->time.denom = 1; tempotop->val = 60; tempotop->next = NULL; for (;;) { tempop = tempotop; efindword(s); if (s[0] == '}') break; if (s[0] != '$') scotferror(Str("No instrument specified")); p = insttop; while ((p != NULL) && (strcmp(&s[1], p->name))) p = p->next; if (p == NULL) scotferror(Str("Instrument not defined")); notetop = ln = NULL; grpmul.num = 1; grpmul.denom = 1; timesig.num = 0; timesig.denom = 1; curtime.num = 0; curtime.denom = 1; lastbar.num = 0; lastbar.denom = 1; lastnote.num = 0; lastnote.denom = 1; accidentals = octaves = vertical = TRUE; for (z = 0; z < PITCHCLASSES; z++) key[z] = barkey[z] = 0; transpose = 0; nextlist = NULL; readinstsec(p, &nextlist, &grpmul, ×ig, &curtime, &lastbar, &lastnote, ¬etop, &ln, &tempop, &accidentals, &octaves, &vertical, key, barkey, &transpose, "}$"); for (pn = notetop; pn; pn = pn->next) { if (!pn->written) { char *ps, *ps2; for (nextpp = nextlist; nextpp; nextpp = nextpp->next) { ps = findparam(nextpp->dst, &pn->carryp); if (!strcmp(ps, NEXTP)) { ps2 = findparam(nextpp->src, &pn->p); if (!strcmp(ps2, ".")) ps2 = findparam(nextpp->src, &pn->carryp); strcpy(ps, ps2); } } writenote(pn); } if (pn->tie) scoterror(Str("unresolved tie")); if (pn->slur & 1) scoterror(Str("unresolved slur")); ratadd(&rattmp, &pn->start, &pn->dur); if (ratcmp(&rattmp, &curtime) > 0) ratass(&curtime, &rattmp); #ifdef DEBUG if (pn == pn->next) scotferror(Str("Circular note list\n")); #endif } while (nextlist) { nextpp = nextlist; nextlist = nextlist->next; free((char *) nextpp); } pn = notetop; while (pn) { qn = pn; pn = pn->next; freenote(qn); } fcurtime = ratval(&curtime); if (fcurtime > maxtime) maxtime = fcurtime; } tempop = tempotop; putc('t', outfile); for (;;) { tempotop = tempop; tempop = tempop->next; fprintf(outfile, "%g %d", ratval(&tempotop->time), tempotop->val); free((char *) tempotop); if (!tempop) break; putc(' ', outfile); } fprintf(outfile, "\nf0 %g\ns\n", maxtime); }
static /* reads from one $instrument to the next */ void readinstsec(Inst *inst, Nextp **nextlist, Rat *grpmul, Rat *timesig, Rat *curtime, Rat *lastbar, Rat *lastnote, Note **notetop, Note **ln, Tempo **tempop, int *accidentals, int *octaves, int *vertical, int *key, int *barkey, int *transpose, char *endchar) { static Rat durdiv = { 4L, 1L }; int c, z, lastpitchclass; char s[128], *sp; Rat ratstack, rattmp; Note *pn, *nn, *pn2 = NULL; Strlist *ps; Nextp *nextpp; #ifdef DEBUG printf("Reading instrument section: %s\n", inst->name); #endif pn = (*notetop); for (;;) { findchar(&c); if (strchr(endchar, c)) break; #ifdef DEBUG printf("Processing char: %c\n", c); #endif switch (c) { case 't': if (findint(&c)) { scoterror(Str("Tempo must be specified")); break; } if ((*tempop)->next) { scoterror(Str("Redefinition of tempo")); break; } (*tempop)->next = (Tempo *) malloc(sizeof(Tempo)); *tempop = (*tempop)->next; (*tempop)->next = NULL; ratass(&((*tempop)->time), curtime); (*tempop)->val = c; break; case '!': efindword(s); if ((c = strlen(s)) < 2) scoterror(Str("Must specify 2 or more letters of keyword")); if (!strncmp(s, "accidentals", c)) { if (findonoff(accidentals)) scoterror(Str("Must be \"on\" or \"off\"")); #ifdef DEBUG printf(" accidentals %s\n", accidentals ? "on" : "off"); #endif } else if (!strncmp(s, "octaves", c)) { if (findonoff(octaves)) scoterror(Str("Must be \"on\" or \"off\"")); #ifdef DEBUG printf(" ocatves %s\n", *octaves ? "on" : "off"); #endif } else if (!strncmp(s, "vertical", c)) { if (findonoff(vertical)) scoterror(Str("Must be \"on\" or \"off\"")); #ifdef DEBUG printf(" vertical %s\n", *vertical ? "on" : "off"); #endif } else if (!strncmp(s, "timesignature", c)) { efindword(s); if ((sscanf(s, "%lu/%lu", ×ig->num, ×ig->denom) != 2) || (&(timesig->denom) == 0) ) { scoterror(Str("Invalid time signature")); timesig->num = 0; timesig->denom = 1; } #ifdef DEBUG printf(" time sig=%lu/%lu\n", timesig->num, timesig->denom); #endif ratstack.num = 4; ratstack.denom = 1; ratmul(timesig, timesig, &ratstack); #ifdef DEBUG printf(" measure length=%f\n", ratval(timesig)); #endif } else if (!strncmp(s, "key", c)) { int y; efindword(s); for (z = 0; z < PITCHCLASSES; z++) key[z] = 0; c = y = 0; for (z = 0; s[z] != (char) 0; z++) switch ((int) s[z]) { case '#': c = y + 1; y++; break; case '-': c = y - 1; y--; break; default: if (!isalpha(s[z])) scoterror(Str("Bad key signature")); key[letterval((int) s[z])] = c; y = 0; } for (z = 0; z < PITCHCLASSES; z++) barkey[z] = key[z]; } else if (!strncmp(s, "transpose", c)) { efindword(s); *transpose = 0; for (z = 0; s[z]; z++) { switch (s[z]) { case ',': (*transpose) -= NOTESPEROCT; break; case '\'': (*transpose) += NOTESPEROCT; break; case '=': (*transpose) = 0; break; case '#': (*transpose)++; break; case '-': (*transpose)--; break; default: (*transpose) += naturals[letterval((int) s[z])]; } } } else if (!strncmp(s, "next", c)) { efindword(s); if (sscanf(s, "p%d", &c) != 1) { scoterror(Str("Invalid field")); efindword(s); break; } efindword(s); if (sscanf(s, "p%d", &z) != 1) { scoterror(Str("Invalid field")); break; } if (*nextlist == NULL) { *nextlist = (Nextp *) malloc(sizeof(Nextp)); nextpp = (*nextlist); nextpp->next = NULL; } else { nextpp = (*nextlist); if ((c == nextpp->dst) || (z == nextpp->src)) scoterror(Str("Nested next-parameter passing")); while (nextpp->next) { nextpp = nextpp->next; if ((c == nextpp->dst) || (z == nextpp->src)) scoterror(Str("Nested next-parameter passing")); } nextpp->next = (Nextp *) malloc(sizeof(Nextp)); nextpp = nextpp->next; nextpp->next = NULL; } nextpp->src = c; nextpp->dst = z; } else scoterror(Str("Unrecognised keyword")); break; case '{': findint(&c); expectchar(':'); if (!c) { ratstack.num = 2L; ratstack.denom = 3L; } else { ratstack.denom = (unsigned long) c; findint(&c); if (!c) { for (z = 1; (unsigned long) z < ratstack.denom; z *= 2); z /= 2; ratstack.num = (unsigned long) z; } else ratstack.num = (unsigned long) c; expectchar(':'); } ratmul(grpmul, grpmul, &ratstack); readinstsec(inst, nextlist, grpmul, timesig, curtime, lastbar, lastnote, notetop, ln, tempop, accidentals, octaves, vertical, key, barkey, transpose, ":"); ratdiv(grpmul, grpmul, &ratstack); expectchar(':'); expectchar('}'); break; case '(': ratass(&ratstack, curtime); if (pn == (*notetop)) { readinstsec(inst, nextlist, grpmul, timesig, curtime, lastbar, lastnote, notetop, ln, tempop, accidentals, octaves, vertical, key, barkey, transpose, ")"); pn = (*notetop); } else { readinstsec(inst, nextlist, grpmul, timesig, curtime, lastbar, lastnote, &pn2->next, ln, tempop, accidentals, octaves, vertical, key, barkey, transpose, ")"); pn = pn2->next; } expectchar(')'); ratass(lastnote, &ratstack); break; case '/': ratadd(lastbar, lastbar, timesig); if ((timesig->num) && (ratcmp(lastbar, curtime))) { scoterror(Str("Wrong number of beats in bar")); ratass(lastbar, curtime); } for (z = 0; z < PITCHCLASSES; z++) barkey[z] = key[z]; break; case '<': if (pn == NULL) { scoterror(Str("Syntax error: cannot back up")); break; } if (pn->next == NULL) { pn->next = (Note *) malloc(sizeof(Note)); initnote(pn->next); pn->next->instrum = pn->instrum + 0.01; } pn2 = pn; pn = pn->next; ratass(curtime, lastnote); break; default: #ifdef DEBUG printf("Reading note\n"); printf(" time=%lu/%lu\n", curtime->num, curtime->denom); printf(" =%f\n", ratval(curtime)); #endif scotungetc(); nn = (Note *) malloc(sizeof(Note)); nn->p = NULL; nn->written = FALSE; if (*notetop == NULL) { pn = (*ln) = (*notetop) = (Note *) malloc(sizeof(Note)); initnote(*notetop); (*notetop)->instrum = (double) inst->number + 0.01; } else if (ratcmp(curtime, lastnote)) pn = (*notetop); nn->instrum = pn->instrum; #ifdef DEBUG printf(" instrument #%f\n", nn->instrum); #endif if (*vertical) strlistcopy(&nn->carryp, &(*ln)->carryp); else strlistcopy(&nn->carryp, &pn->carryp); for (nextpp = (*nextlist); nextpp; nextpp = nextpp->next) { sp = findparam(nextpp->dst, &nn->carryp); if (!strcmp(sp, ".")) strcpy(sp, NEXTP); } ratass(&nn->start, curtime); if (!findint(&c)) { ratstack.num = (unsigned long) c; ratstack.denom = 1L; ratdiv(&nn->dur, &durdiv, &ratstack); ratass(&ratstack, &nn->dur); rattmp.num = 1L; rattmp.denom = 2L; for (;;) { findchar(&c); if (c != '.') break; ratmul(&ratstack, &ratstack, &rattmp); ratadd(&nn->dur, &nn->dur, &ratstack); } } else { if (*vertical) ratass(&nn->dur, &((*ln)->lastdur)); else ratass(&nn->dur, &pn->lastdur); findchar(&c); } ratass(&nn->lastdur, &nn->dur); ratmul(&nn->dur, &nn->dur, grpmul); #ifdef DEBUG printf(" duration=%f\n", ratval(&nn->dur)); printf(" c=%c\n", c); #endif if (c == '=') { nn->octave = 8; lastpitchclass = 0; } else { nn->octave = pn->octave; lastpitchclass = pn->pitchclass; scotungetc(); } for (;;) { findchar(&c); if (c == '\'') nn->octave++; else if (c == ',') nn->octave--; else break; } if (c == 'r') { ratass(lastnote, curtime); ratmul(&rattmp, &nn->lastdur, grpmul); ratadd(curtime, curtime, &rattmp); ratass(&(*ln)->lastdur, &nn->lastdur); ratass(&pn->lastdur, &nn->lastdur); freenote(nn); break; } else { nn->pitchclass = letterval(c); if (*octaves) { c = nn->pitchclass - lastpitchclass; if (c < -(PITCHCLASSES / 2)) nn->octave++; else if (c > PITCHCLASSES / 2) nn->octave--; } } nn->accid = 0; nn->accmod = FALSE; for (;;) { findchar(&c); if (c == '#') { nn->accid++; nn->accmod = TRUE; } else if (c == '-') { nn->accid--; nn->accmod = TRUE; } else if (c == 'n') { nn->accid = 0; nn->accmod = TRUE; } else break; } if (!nn->accmod) nn->accid = barkey[nn->pitchclass]; else if (*accidentals) barkey[nn->pitchclass] = nn->accid; #ifdef DEBUG printf(" transpose=%d\n", *transpose); printf(" octave=%d pitchclass=%d accid=%d transpose=%d pitch=%f\n", nn->octave, nn->pitchclass, nn->accid, *transpose, pitchval(nn->octave, nn->pitchclass, nn->accid, *transpose)); #endif if (c == '_') { findchar(&c); if (c == '_') { nn->tie = TRUE; nn->slur = 0; findchar(&c); } else { nn->slur = 1; nn->tie = FALSE; } } else { nn->slur = 0; nn->tie = FALSE; } if (pn->slur & 1) nn->slur += 2; #ifdef DEBUG printf(" slur=%d tie=%d\n", nn->slur, nn->tie); #endif if (pn->tie) { ratadd(&rattmp, &pn->start, &pn->dur); if (ratcmp(&rattmp, curtime)) scoterror(Str("Improper tie")); if (((nn->octave != pn->octave) || (nn->pitchclass != pn->pitchclass) || ((nn->accid != pn->accid) && (nn->accmod))) && (pitchval(nn->octave, nn->pitchclass, nn->accid, *transpose) != pitchval(pn->octave, pn->pitchclass, pn->accid, *transpose))) scoterror(Str("Tie between different pitches")); ratadd(&pn->dur, &pn->dur, &nn->dur); ratass(&pn->lastdur, &nn->lastdur); pn->slur += nn->slur; pn->tie = nn->tie; freenote(nn); nn = pn; if (c == (char) '[') scoterror(Str("Warning: params changed on tie")); } else { ps = nn->p = (Strlist *) malloc(sizeof(Strlist)); for (z = 0; z < 4; z++) { ps->next = (Strlist *) malloc(sizeof(Strlist)); ps = ps->next; } ps->next = NULL; } ps = nn->p; sprintf(ps->str, "%.02f", nn->instrum); ps = ps->next; sprintf(ps->str, "%g", ratval(&nn->start)); ps = ps->next; sprintf(ps->str, "%g", ratval(&nn->dur)); ps = ps->next; sprintf(ps->str, "%d", nn->slur); ps = ps->next; sprintf(ps->str, "%.02f", pitchval(nn->octave, nn->pitchclass, nn->accid, *transpose)); if (c == '[') { char *pars; int pnum; pars = readparams(inst); #ifdef DEBUG printf("Params: %s\n", pars); #endif z = 0; pnum = 6; while (strchr(" \t\r\n", (int) pars[z])) z++; for (;;) { if (pars[z] == (char) ']') break; c = 0; while (!strchr(" \t\r\n:]", (int) pars[z])) s[c++] = pars[z++]; s[c] = (char) 0; #ifdef DEBUG printf("Read: %s\n", s); #endif while (strchr(" \t\r\n", (int) pars[z])) z++; if (pars[z] == (char) ':') { pnum = atoi(s); if (pnum < 6) scoterror(Str("Parameter number out of range")); z++; while (strchr(" \t\r\n", (int) pars[z])) z++; continue; } #ifdef DEBUG printf("Param #%d: %s\n", pnum, s); #endif if (s[0] == (char) '\'') { addparam(pnum, &s[1], &nn->p); addparam(pnum, ".", &nn->carryp); } else { addparam(pnum, s, &nn->p); addparam(pnum, s, &nn->carryp); } pnum++; } free(pars); } else scotungetc(); if ((nn != pn) && (!pn->written)) { #ifdef DEBUG printf(" doing nextp stuff:\n"); #endif for (nextpp = (*nextlist); nextpp; nextpp = nextpp->next) { #ifdef DEBUG printf(" carrying p%d to p%d?\n", nextpp->src, nextpp->dst); #endif if (!strcmp(findparam(nextpp->dst, &pn->carryp), NEXTP)) { sp = findparam(nextpp->dst, &pn->p); if (!strcmp(sp, ".")) { char *sp2; sp2 = findparam(nextpp->src, &nn->p); if (!strcmp(sp2, ".")) sp2 = findparam(nextpp->src, &nn->carryp); strcpy(sp, sp2); #ifdef DEBUG printf(" Yes.\n"); #endif } } } writenote(pn); } if ((!(*nextlist)) && (!nn->tie)) writenote(nn); if (nn != pn) { if (!pn->written) scoterror(Str("Lost previous note: not written")); #ifdef DEBUG if (pn->next == nn) printf("* pn->next==nn\n"); #endif nn->next = pn->next; #ifdef DEBUG if (pn2 == nn) printf("* pn2==nn\n"); #endif if (pn == *notetop) *notetop = nn; else pn2->next = nn; freenote(pn); pn = nn; #ifdef DEBUG if (nn->next == nn) printf("* Circular list created\n"); #endif } #ifdef DEBUG printf(" nn linked into note list\n"); printf(" curtime=%lu/%lu\n", curtime->num, curtime->denom); printf(" nn->dur=%lu/%lu\n", nn->dur.num, nn->dur.denom); #endif *ln = nn; ratass(lastnote, curtime); ratmul(&rattmp, &nn->lastdur, grpmul); ratadd(curtime, curtime, &rattmp); #ifdef DEBUG printf(" curtime=%lu/%lu\n", curtime->num, curtime->denom); printf(" Done with note\n"); #endif } } scotungetc(); }
/* returns Boolean condition that a > b */ Bool ratgreat (Rat a, Rat b) { Rat c = ratadd(a, ratneg(b)); return (positive(c.num)); }
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); }
Bool ratgreat (Rat a, Rat b) { Rat c = ratadd(a, ratneg(b)); return (c.num > 0); }