📄 behav.imp
字号:
{ for (int i = 1; i <= n->children.Length(); i++) { Node *child = n->children[i]; if (n->GetPlayer() && n->GetPlayer()->GetNumber() == pl) if (m_nvals[n->number] > (T) 0 && m_nvals[child->number] > (T) 0) { (*this)(n->GetPlayer()->GetNumber(), n->GetInfoset()->GetNumber(), Support().Find(n->GetInfoset()->Actions()[i])) = m_nvals[child->number] / m_nvals[n->number]; } BehaviorStrat(E, pl, child); }}template <class T>void BehavProfile<T>::RealizationProbs(const MixedProfile<T> &mp, const efgGame &E, int pl, const gArray<int> *const actions, Node *node){ static const T tremble = (T) 0; T prob; for (int i = 1; i <= node->children.Length(); i++) { if (node->GetPlayer() && !node->GetPlayer()->IsChance()) { if (node->GetPlayer()->GetNumber() == pl) { if ((*actions)[node->GetInfoset()->GetNumber()] == i) prob = (T) 1 - tremble + tremble / (T) E.NumChildren(node); else prob = tremble / (T) E.NumChildren(node); } else if (Support().Find(node->GetInfoset()->Actions()[i])) prob = (T) 1 / (T) Support().NumActions(node->GetPlayer()->GetNumber(), node->GetInfoset()->GetNumber()); else prob = (T) 0; } else { // n.GetPlayer() == 0 prob = E.GetChanceProb(node->GetInfoset(), i).operator gRational(); } Node *child = node->children[i]; m_bvals[child->number] = prob * m_bvals[node->number]; m_nvals[child->number] += m_bvals[child->number]; RealizationProbs(mp, E, pl, actions, child); } }template <class T> gDPVector<T> BehavProfile<T>::Beliefs(void){ ComputeSolutionData(); gDPVector<T> bprobs(m_efg->NumMembers()); bprobs = (T) 0; for (int pl = 1; pl <= m_efg->NumPlayers(); pl++) { EFPlayer *player = m_efg->Players()[pl]; for (int iset = 1; iset <= player->NumInfosets(); iset++) { Infoset *infoset = player->Infosets()[iset]; for (int i = 1; i <= infoset->Members().Length(); i++) bprobs(player->GetNumber(),infoset->GetNumber(),i) = BeliefProb(infoset->Members()[i]); } } return bprobs;}template <class T> T BehavProfile<T>::LiapValue(void){ static const T BIG1 = (T) 10000; static const T BIG2 = (T) 100; T x, result = ((T) 0), avg, sum; // HACK: force it to recompute data. FIX THIS. m_cached_data = false; ComputeSolutionData(); const gArray<EFPlayer *> &players = m_efg->Players(); for (int i = 1; i <= players.Length(); i++) { const gArray<Infoset *> &infosets = players[i]->Infosets(); for (int iset = 1; iset <= infosets.Length(); iset++) { const gArray<Action *> &acts = m_support.Actions(infosets[iset]); avg = sum = (T)0; for (int act = 1; act <= acts.Length(); act++) { x = ActionProb(acts[act]); avg += x * ActionValue(acts[act]); sum += x; if (x > (T)0) x = (T)0; result += BIG1 * x * x; // add penalty for neg probabilities } for (int act = 1; act <= acts.Length(); act++) { x = ActionValue(acts[act]) - avg; if (x < (T)0) x = (T)0; result += x * x; // add penalty if not best response } x = sum - (T)1; result += BIG2 * x * x; // add penalty for sum not equal to 1 } } return result;}template <class T> T BehavProfile<T>::QreValue(const gVector<T> &lambda, bool &_domain_err){ static const T PENALTY = (T)10000; T BigNum = (T)500; // gDPVector<T> _cpay(m_support.NumActions()); T val = (T)0, prob, psum, z,factor; // HACK: force it to recompute data. FIX THIS. m_cached_data = false; ComputeSolutionData(); const gArray<EFPlayer *> players = m_efg->Players(); for (int pl = 1; pl <= players.Length(); pl++) { const gArray<Infoset *> infosets = players[pl]->Infosets(); for (int iset = 1; iset <= infosets.Length(); iset++) { const gArray<Action *> &acts = Support().Actions(infosets[iset]); prob = (T)0; psum = (T)0; for (int act = 1; act <= acts.Length(); act++) { z = lambda[pl] * ActionValue(acts[act]); factor=(T)1; if(z>BigNum) {factor+=z-BigNum;z=BigNum;_domain_err=true;} if(z<-BigNum) {factor+=z+BigNum;z=-BigNum;_domain_err=true;} z = ((T)exp(z))*factor; psum += z; // _cpay(pl,iset,act) = z; ActionValue(acts[act]) = z; } for (int act = 1; act <= acts.Length(); act++) { z = ActionProb(acts[act]); prob += z; if (z < (T)0) val += PENALTY * z * z; z -= ActionValue(acts[act]) / psum; // z -= _cpay(pl,iset,act) / psum; val += z * z; } z = (T)1 - prob; val += (T)100 * z * z; } } // HACK: this procedure screws up action values. FIX THIS. m_cached_data = false; return val;}template <class T>T BehavProfile<T>::MaxRegret(void){ ComputeSolutionData(); T ret = (T) 0; for (int act = 1; act <= m_gripe.Length(); act++) { if (m_gripe[act] > ret) { ret = m_gripe[act]; } } return ret;}template <class T> void BehavProfile<T>::Dump(gOutput &p_file) const{ gDPVector<T>::Dump(p_file);}template <class T> BehavProfile<T>::BadStuff::~BadStuff(){ }template <class T> gText BehavProfile<T>::BadStuff::Description(void) const{ return "Solution not installed in BehavProfile";}//-----------------// Output//-----------------template <class T> gOutput &operator<<(gOutput &p_file, const BehavProfile<T> &p_profile){ p_profile.Dump(p_file); return p_file;}//------------------------------------------------------------------------// BehavAssessment<T>: Constructors, Destructor, Constructive Operators//------------------------------------------------------------------------template <class T>BehavAssessment<T>::BehavAssessment(const EFSupport &p_support) : BehavProfile<T>(p_support), m_beliefs(p_support.GetGame().NumMembers()){ m_beliefs = BehavProfile<T>::Beliefs();}template <class T>BehavAssessment<T>::BehavAssessment(const BehavProfile<T> &p_profile) : BehavProfile<T>(p_profile), m_beliefs(p_profile.GetGame().NumMembers()){ m_beliefs = BehavProfile<T>::Beliefs();}template <class T>BehavAssessment<T>::BehavAssessment(const BehavAssessment<T> &p_assess) : BehavProfile<T>(p_assess), m_beliefs(p_assess.m_beliefs){ }template <class T> BehavAssessment<T>::~BehavAssessment(){ }template <class T> BehavAssessment<T> &BehavAssessment<T>::operator=(const BehavAssessment<T> &p_assess){ if (this != &p_assess && &GetGame() == &p_assess.GetGame()) { BehavProfile<T>::operator=(p_assess); m_beliefs = p_assess.m_beliefs; } return *this;}//------------------------------------------------------------------------// BehavAssessment<T>: Access and manipulation of beliefs//------------------------------------------------------------------------template <class T> gDPVector<T> BehavAssessment<T>::Beliefs(void) const{ return m_beliefs; }template <class T> gDPVector<T> &BehavAssessment<T>::Beliefs(void){ return m_beliefs; }//----------------// CondPayoff//----------------template <class T>void BehavAssessment<T>::CondPayoff(Node *p_node, T p_prob, gPVector<T> &p_probs, gDPVector<T> &p_payoff) const{ int npl = (p_node->GetInfoset()) ? p_node->GetPlayer()->GetNumber() : -1; int iset = (p_node->GetInfoset()) ? p_node->GetInfoset()->GetNumber() : 0; if (p_node->outcome) { for (int pl = 1; pl <= m_efg->NumPlayers(); pl++) { m_nodeValues(p_node->number, pl) += Payoff(p_node->outcome, pl); } } int nc = p_node->children.Length(); for (int child = 1; child <= nc; child++) { m_nodeValues.SetRow(p_node->children[child]->number, m_nodeValues.Row(p_node->number)); } gVector<T> tmp(m_efg->NumPlayers()); tmp = (T) 0; if (nc == 0) return; if (npl == 0) { for (int child = 1; child <= nc; child++) { CondPayoff(p_node->children[child], p_prob * ActionProb(m_efg->Children(p_node)[child]->GetAction()), // ChanceProb(p_node->GetInfoset(),child), p_probs, p_payoff); for (int pl = 1; pl <= m_efg->NumPlayers(); pl++) tmp[pl] += ActionProb(m_efg->Children(p_node)[child]->GetAction()) * // ChanceProb(p_node->GetInfoset(), child) * m_nodeValues(p_node->children[child]->number, pl); } m_nodeValues.SetRow(p_node->number, tmp); } else { // player decision node // This implementation differs from the one in BehavProfile<T> // since we have well-defined belief probabilities off the equilibrium // path. So, we must traverse the whole tree, and cannot ignore // the (zero-probability) actions which do not appear in the support for (int child = 1; child <= m_efg->NumChildren(p_node); child++) { int act = m_support.Find(p_node->GetInfoset()->Actions()[child]); T newprob; if (act) newprob = (*this)(npl, iset, act); else newprob = (T) 0; CondPayoff(p_node->children[child], p_prob * newprob, p_probs, p_payoff); for (int pl = 1; pl <= m_efg->NumPlayers(); pl++) { tmp[pl] += newprob * m_nodeValues(p_node->children[child]->number, pl); } int mnum; for (mnum = 1; p_node->GetInfoset()->Members()[mnum] != p_node; mnum++); p_payoff(npl, iset, child) += m_beliefs(npl, iset, mnum) * m_nodeValues(p_node->children[child]->number, npl); m_nodeValues.SetRow(p_node->number, tmp); } } if (npl > 0) { p_probs(npl, p_node->GetInfoset()->GetNumber()) += p_prob; }}template <class T>void BehavAssessment<T>::CondPayoff(gDPVector<T> &p_payoff, gPVector<T> &p_probs) const{ ((gVector<T> &) p_payoff).operator=((T) 0); ((gVector<T> &) p_probs).operator=((T) 0); for (int pl = 1; pl <= m_efg->NumPlayers(); pl++) { m_nodeValues(1, pl) = (T) 0; } CondPayoff(m_efg->RootNode(), (T) 1, p_probs, p_payoff); // We can dispense with the normalization step found in the analogous // place in BehavProfile<T> since the beliefs are assumed to be // normalized to sum to 1. (If they don't, things are weird anyway.)}//------------------------------------------------------------------------// BehavAssessment<T>: Output//------------------------------------------------------------------------template <class T> void BehavAssessment<T>::Dump(gOutput &p_file) const{ BehavProfile<T>::Dump(p_file); p_file << ' '; p_file << m_beliefs;}template <class T> gOutput &operator<<(gOutput &p_file, const BehavAssessment<T> &p_assess){ p_assess.Dump(p_file); return p_file;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -