⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 behav.imp

📁 Gambit 是一个游戏库理论软件
💻 IMP
📖 第 1 页 / 共 3 页
字号:
//// $Source: /home/gambit/CVS/gambit/sources/game/behav.imp,v $// $Date: 2002/08/26 05:50:05 $// $Revision: 1.6 $//// DESCRIPTION:// Implementation of behavior profile classes//// This file is part of Gambit// Copyright (c) 2002, The Gambit Project//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//#include "math/gvector.h"#include "efg.h"#include "efgutils.h"#include "behav.h"#include "nfg.h"#include "mixed.h"#include "lexicon.h"//-------------------------------------------------------------------------//                    PureBehavProfile<T> member functions//-------------------------------------------------------------------------template <class T> PureBehavProfile<T>::PureBehavProfile(const efgGame &efg)  : E(&efg), profile(efg.NumPlayers()){  for (int pl = 1; pl <= efg.NumPlayers(); pl++)  {    EFPlayer *player = efg.Players()[pl];    profile[pl] = new gArray<const Action *>(player->NumInfosets());    for (int iset = 1; iset <= player->NumInfosets(); iset++)      (*profile[pl])[iset] = player->Infosets()[iset]->Actions()[1];  }}template <class T>PureBehavProfile<T>::PureBehavProfile(const PureBehavProfile<T> &p)  : E(p.E), profile(p.profile.Length()){  for (int pl = 1; pl <= profile.Length(); pl++)   {    profile[pl] = new gArray<const Action *>(p.profile[pl]->Length());    for (int iset = 1; iset <= profile[pl]->Length(); iset++)      (*profile[pl])[iset] = (*p.profile[pl])[iset];  }}template <class T> PureBehavProfile<T>::~PureBehavProfile(){  for (int pl = 1; pl <= profile.Length(); delete profile[pl++]);}template <class T> PureBehavProfile<T> &PureBehavProfile<T>::operator=(const PureBehavProfile<T> &p) {  if (this != &p && E == p.E)   {    for(int pl = 1; pl <= profile.Length(); pl++)    for(int iset = 1; iset <= profile[pl]->Length(); iset++)    (*profile[pl])[iset] = (*p.profile[pl])[iset];  }  return *this;}template <class T> T PureBehavProfile<T>::operator()(Action *action) const{  if ((*profile[action->BelongsTo()->GetPlayer()->GetNumber()])      [action->BelongsTo()->GetNumber()] == action)    return (T) 1;  else    return (T) 0;}template <class T> void PureBehavProfile<T>::Set(const Action *action){  (*profile[action->BelongsTo()->GetPlayer()->GetNumber()])    [action->BelongsTo()->GetNumber()] = action;}template <class T>void PureBehavProfile<T>::Set(const EFPlayer *player,                              const gArray<const Action *> &actions){  *profile[player->GetNumber()] = actions;}template <class T>const Action *PureBehavProfile<T>::GetAction(const Infoset *infoset) const{  return (*profile[infoset->GetPlayer()->GetNumber()])[infoset->GetNumber()];}/* - The following is an attempt to eliminate all the extra work ofcomputing a vector of payoffs when only one agent's payoff is ofinterest.  It runs into a problem that I don't know how to solve,namely that compilation leads to a request for a cast from gNumber togRational. amm-8.98template <class T>void PureBehavProfile<T>::IndPayoff(const Node *n, 				 const int &pl, 				 const T prob, 				       T &payoff) const{  if (n->IsTerminal())    payoff += prob * (*payoffs)(n->GetOutcome()->GetNumber(), pl);    if (n->IsNonterminal() && n->GetPlayer()->IsChance())    for (int i = 1; i <= n->NumChildren(); i++) {      IndPayoff(n->GetChild(i), pl,	     prob * (T) E->GetChanceProb(n->GetInfoset(), i), payoff);    }  else if (n->IsNonterminal())    IndPayoff(n->GetChild((*profile[n->GetPlayer()->GetNumber()])[n->GetInfoset()->GetNumber()]->GetNumber()), pl,	   prob, payoff);}*/// The following could be shortened by using the player-specific one above.// Whether this would be more efficient in fact is unknown. - AMM 7.6.98template <class T>const T PureBehavProfile<T>::Payoff(const Node *n, const int &pl) const{  gArray<T> payoff(E->NumPlayers());  for (int i = 1; i <= E->NumPlayers(); i++)    payoff[i] = (T)0;  Payoff(n, (T)1, payoff);  return payoff[pl];}template <class T>void PureBehavProfile<T>::Payoff(const Node *n, const T prob, 				 gArray<T> &payoff) const{  if (n->IsTerminal()) {    for (int pl = 1; pl <= E->NumPlayers(); pl++) {      payoff[pl] += prob * Payoff(E->GetOutcome(n), pl);    }  }    else    if (n->GetPlayer()->IsChance())	      for (int i = 1; i <= E->NumChildren(n); i++)	Payoff(n->GetChild(i),	       prob * (T) E->GetChanceProb(n->GetInfoset(), i), payoff);    else      Payoff(n->GetChild(GetAction(n->GetInfoset())), prob, payoff);}template <class T>void PureBehavProfile<T>::InfosetProbs(Node *n, T prob, gPVector<T> &probs) const{  if (n->GetInfoset() && n->GetPlayer()->IsChance())    for (int i = 1; i <= E->NumChildren(n); i++)      InfosetProbs(n->GetChild(i),		   prob * (T) E->GetChanceProb(n->GetInfoset(), i), probs);  else if (n->GetInfoset())  {    probs(n->GetPlayer()->GetNumber(), n->GetInfoset()->GetNumber()) += prob;    InfosetProbs(n->GetChild((*profile[n->GetPlayer()->GetNumber()])[n->GetInfoset()->GetNumber()]->GetNumber()),		 prob, probs);  }}template <class T>void PureBehavProfile<T>::Payoff(gArray<T> &payoff) const{  for (int pl = 1; pl <= payoff.Length(); payoff[pl++] = (T) 0);  Payoff(E->RootNode(), (T) 1, payoff);}template <class T>void PureBehavProfile<T>::InfosetProbs(gPVector<T> &probs) const{  ((gVector<T> &) probs).operator=((T) 0);  InfosetProbs(E->RootNode(), (T) 1, probs);}template <class T>const T PureBehavProfile<T>::Payoff(efgOutcome *p_outcome,				    const int &pl) const{  if (p_outcome) {    return E->Payoff(p_outcome)[pl];  }  else {    return (T) 0;  }}//-------------------------------------------------------------------------//   BehavProfile<T>: Constructors, Destructor//-------------------------------------------------------------------------template <class T>BehavProfile<T>::BehavProfile(const BehavProfile<T> &p_profile)  : gDPVector<T>(p_profile), m_efg(p_profile.m_efg),    m_support(p_profile.m_support),    m_cached_data(false),    m_realizProbs(p_profile.m_realizProbs), m_beliefs(p_profile.m_beliefs),    m_nvals(p_profile.m_nvals), m_bvals(p_profile.m_bvals),    m_nodeValues(p_profile.m_nodeValues),    m_infosetValues(p_profile.m_infosetValues),    m_actionValues(p_profile.m_actionValues),    m_gripe(p_profile.m_gripe){  InitProfile();}template <class T> BehavProfile<T>::BehavProfile(const EFSupport &p_support)   : gDPVector<T>(p_support.NumActions()),     m_efg(&p_support.GetGame()),    m_support(p_support),    m_cached_data(false),    m_realizProbs(NumNodes(p_support.GetGame())),    m_beliefs(NumNodes(p_support.GetGame())),    m_nvals(NumNodes(p_support.GetGame())),     m_bvals(NumNodes(p_support.GetGame())),    m_nodeValues(NumNodes(p_support.GetGame()),		 p_support.GetGame().NumPlayers()),    m_infosetValues(p_support.GetGame().NumInfosets()),    m_actionValues(p_support.GetGame().NumActions()),    m_gripe(p_support.GetGame().NumActions()){  InitProfile();  Centroid();}template <class T>BehavProfile<T>::BehavProfile(const MixedProfile<T> &p_profile)  : gDPVector<T>(p_profile.GetGame().AssociatedEfg()->NumActions()),     m_efg(p_profile.GetGame().AssociatedEfg()),    m_support(*m_efg),      m_cached_data(false),    m_realizProbs(NumNodes(*p_profile.GetGame().AssociatedEfg())),    m_beliefs(NumNodes(*p_profile.GetGame().AssociatedEfg())),    m_nvals(NumNodes(*p_profile.GetGame().AssociatedEfg())),    m_bvals(NumNodes(*p_profile.GetGame().AssociatedEfg())),    m_nodeValues(NumNodes(*p_profile.GetGame().AssociatedEfg()),		 p_profile.GetGame().AssociatedEfg()->NumPlayers()),    m_infosetValues(p_profile.GetGame().AssociatedEfg()->NumInfosets()),    m_actionValues(p_profile.GetGame().AssociatedEfg()->NumActions()),    m_gripe(p_profile.GetGame().AssociatedEfg()->NumActions()){  //  gout << "\nin BehavProfile(const MixedProfile<T> &p_profile)";  InitProfile();  if (m_efg->AssociatedAfg() == &p_profile.GetGame())   {    ((gVector<T> &) *this).operator=((gVector<T> &) p_profile);    return;  }  ((gVector<T> &) *this).operator=((T)0);   Node *n = m_efg->RootNode();  const NFSupport &support = p_profile.Support();  const Nfg &nfg = p_profile.GetGame();  for (int pl = 1; pl <= nfg.NumPlayers(); pl++)   {    m_nvals = (T) 0;    m_bvals = (T) 0;    for (int st = 1; st <= support.NumStrats(pl); st++)  {      int snum = support.Strategies(pl)[st]->Number();      if (p_profile(pl, st) > (T) 0)  {	const gArray<int> *const actions = m_efg->GetLexicon()->strategies[pl][snum];	m_bvals[n->number] = p_profile(pl, st);	RealizationProbs(p_profile, *m_efg, pl, actions, m_efg->RootNode());      }    }     m_nvals[1] = (T) 1;   // set the root nval    BehaviorStrat(*m_efg, pl, n);  }}template <class T> BehavProfile<T>::~BehavProfile(){ }//-------------------------------------------------------------------------//   BehavProfile<T>: Operator overloading//-------------------------------------------------------------------------template <class T>BehavProfile<T> &BehavProfile<T>::operator=(const BehavProfile<T> &p_profile){  if (this != &p_profile && m_efg == p_profile.m_efg)   {    Invalidate();    // note that a dimensionality change will trigger an exception    // in the gDPVector assignment operator    gDPVector<T>::operator=(p_profile);    m_support = p_profile.m_support;  }  return *this;}template <class T>bool BehavProfile<T>::operator==(const BehavProfile<T> &p_profile) const{  return (m_efg == p_profile.m_efg &&	  (gDPVector<T> &) *this == (gDPVector<T> &) p_profile);}//-------------------------------------------------------------------------//   BehavProfile<T>: Initialization, Validation//-------------------------------------------------------------------------template <class T>void BehavProfile<T>::InitPayoffs(void) const{  m_realizProbs = (T) 0.0;  m_beliefs = (T) 0.0;  m_nodeValues = (T) 0.0;  m_infosetValues = (T) 0.0;  m_actionValues = (T) 0.0;  m_gripe = (T) 0.0;}template <class T>void BehavProfile<T>::InitProfile(void){  InitPayoffs();}template <class T> void BehavProfile<T>::Centroid(void) const{  T center;  for (int pl = 1; pl <= dvlen.Length(); pl++)    for (int iset = 1; iset <= dvlen[pl]; iset++)      if (m_support.NumActions(pl,iset) > 0) {	center = ((T) 1 / (T) m_support.NumActions(pl, iset));	int act;	for (act = 1; act <= svlen[dvidx[pl] + iset - 1]; act++)	  dvptr[pl][iset][act] = center;      }}//-------------------------------------------------------------------------//   BehavProfile<T>: General data access -- private functions//-------------------------------------------------------------------------template <class T>T BehavProfile<T>::ActionProb(const Action *action) const{  if (action->BelongsTo()->GetPlayer()->IsChance()) {    return m_efg->GetChanceProb(action);  }  else if (!m_support.Find(action)) {    return (T) 0.0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -