int FSM::getDefaultState (float relationship) const{ if (relationship<-1)relationship=-1; if (relationship>1) relationship=1;//clamp it float mood=relationship; float randomresponse=.01; int curstate=0; const FSM::Node *n = &nodes[curstate]; mood+=-randomresponse+2*randomresponse*((float)rand())/RAND_MAX; int choice=0; float bestchoice=16; bool fitmood=false; for (unsigned i=0;i<n->edges.size();i++) { float md = nodes[n->edges[i]].messagedelta; bool newfitmood=nonneg(mood)==nonneg(md); if ((!fitmood)||newfitmood) { float newbestchoice=sq(md-mood); if ((newbestchoice<=bestchoice)||(fitmood==false&&newfitmood==true)) { if ((newbestchoice==bestchoice&&rand()%2)||newbestchoice<bestchoice) { //to make sure some variety happens fitmood=newfitmood; choice =i; bestchoice = newbestchoice; } } } }// (0,relationship,.01) return nodes[0].edges[choice]; }
int FSM::getCommMessageMood (int curstate, float mood, float randomresponse,float relationship) const{ const FSM::Node *n = curstate<nodes.size()?(&nodes[curstate]):(&nodes[getDefaultState(relationship)]); mood+=-randomresponse+2*randomresponse*((float)rand())/RAND_MAX; int choice=0; #if 0 float bestchoice=4; bool fitmood=false; for (unsigned i=0;i<n->edges.size();i++) { float md = nodes[n->edges[i]].messagedelta; bool newfitmood=nonneg(mood)==nonneg(md); if ((!fitmood)||newfitmood) { float newbestchoice=sq(md-mood); if ((newbestchoice<=bestchoice)||(fitmood==false&&newfitmood==true)) { if ((newbestchoice==bestchoice&&rand()%2)||newbestchoice<bestchoice) { //to make sure some variety happens fitmood=newfitmood; choice =i; bestchoice = newbestchoice; } } } } #endif vector<unsigned int> g; vector <unsigned int>b; static float pos_limit =XMLSupport::parse_float(vs_config->getVariable ("AI", "LowestPositiveCommChoice", "0")); static float neg_limit =XMLSupport::parse_float(vs_config->getVariable ("AI", "LowestNegativeCommChoice", "-.00001")); for (unsigned int i=0;i<n->edges.size();i++) { float md=nodes[n->edges[i]].messagedelta; if (md>=pos_limit) { g.push_back(i); } if (md<=neg_limit) { b.push_back(i); } } if(g.size()!=0&&(relationship>0||(b.size()==0))) { choice=g[(rand()%g.size())]; }else { if (b.size()) { choice = b[rand()%b.size()]; } } return choice; }
wupdate() { register float *exptr, *nexptr, *pexptr, *wexptr; register float *sumptr, *isumptr; register int *countptr; register int wstart; WORD *wp, **wptr; int wmin, wmax; float *wpextotptr; float *wmaxptr; float t, tt; for (sumptr = wsum, isumptr = wisum, countptr = wcount; sumptr < wsum + PSLICES; ) { *sumptr++ = *isumptr++ = *countptr++ = 0; } for ALLWORDPTRS { wp = *wptr; wpextotptr = &wp->pextot; *wpextotptr = 0; for (wstart = 0, exptr = wp->ex, nexptr = wp->nex, pexptr = wp->pex, wexptr = wp->wex, sumptr = wsum, countptr = wcount; wstart < PSLICES; wstart++) { t = *exptr; if ( *nexptr > 0) { t += (max - t)*(*nexptr); } else if (*nexptr < 0) { t += (t - min)*(*nexptr); } if ( (tt = *exptr - wp->rest) ) { t -= decay[W]*tt; } if (t > 0) { if (t > max) t = max; *wpextotptr += *pexptr++ = t*alpha[WP]; tt = t * t; *wexptr++ = tt*ga[W]; *countptr++ += 1; *sumptr++ += t; wmin = nonneg(wstart + wp->start); wmax = wp->end + wstart; if (wmax > PSLICES-1) wmax = PSLICES-1; wmaxptr = &wisum[wmax]; for (isumptr = &wisum[wmin]; isumptr < wmaxptr;) { *isumptr++ += tt; } } else { if (t < min) t = min; *wexptr++ = *pexptr++ = 0; countptr++; sumptr++; } *exptr++ = t; *nexptr++ = 0; } } }