nfgfunc.cc

来自「Gambit 是一个游戏库理论软件」· CC 代码 · 共 682 行 · 第 1/2 页

CC
682
字号
//// $Source: /home/gambit/CVS/gambit/sources/gcl/nfgfunc.cc,v $// $Date: 2002/08/27 18:57:19 $// $Revision: 1.6 $//// DESCRIPTION:// GCL functions for normal form games//// 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 "base/base.h"#include "base/gstatus.h"#include "gsm.h"#include "portion.h"#include "gsmfunc.h"#include "game/nfg.h"#include "game/nfplayer.h"#include "game/nfdom.h"#include "game/mixed.h"//// Implementations of these are provided as necessary in gsmutils.cc//template <class T> Portion *ArrayToList(const gArray<T> &);template <class T> Portion *ArrayToList(const gList<T> &);//---------------// AddStrategy//---------------static Portion *GSM_AddStrategy(GSM &, Portion **param){  NFSupport *support = ((NfSupportPortion *) param[0])->Value();  Strategy *s = ((StrategyPortion *) param[1])->Value();  NFSupport *S = new NFSupport(*support);  S->AddStrategy(s);  return new NfSupportPortion(S);}//------------// Comment//------------static Portion *GSM_Comment(GSM &, Portion **param){  Nfg *nfg = ((NfgPortion *) param[0])->Value();  return new TextPortion(nfg->GetComment());}//---------------// CompressNfg//---------------Nfg *CompressNfg(const Nfg &, const NFSupport &);static Portion *GSM_CompressNfg(GSM &, Portion **param){  NFSupport *S = ((NfSupportPortion *) param[0])->Value();  Nfg *N = (Nfg *) &S->Game();  return new NfgPortion(CompressNfg(*N, *S));}//-----------------// DeleteOutcome//-----------------static Portion *GSM_DeleteOutcome(GSM &gsm, Portion **param){  NFOutcome *outc = ((NfOutcomePortion *) param[0])->Value();  gsm.InvalidateGameProfile(outc->Game(), false);//  _gsm->UnAssignGameElement(outc->BelongsTo(), false, porNFOUTCOME, outc);  outc->Game()->DeleteOutcome(outc);  return new BoolPortion(true);}//-------------// IsDominated//-------------static Portion *GSM_IsDominated_Nfg(GSM &, Portion **param){  Strategy *str = ((StrategyPortion *) param[0])->Value();  NFSupport *S = ((NfSupportPortion *) param[1])->Value();  bool strong = ((BoolPortion *) param[2])->Value();  bool mixed = ((BoolPortion *) param[3])->Value();  gPrecision prec = ((PrecisionPortion *) param[4])->Value();  gWatch watch;  bool ret;  if (mixed)    ret = IsMixedDominated(*S, str,strong, prec, 			   ((OutputPortion *) param[6])->Value());  else   {    ret = S->IsDominated(str, strong);  }  ((NumberPortion *) param[5])->SetValue(watch.Elapsed());    return new BoolPortion(ret);}static Portion *GSM_IsProfileDominated_Nfg(GSM &, Portion **param){  MixedSolution soln(*(*((MixedPortion *) param[0])->Value()).Profile());  MixedProfile<gNumber> pr(*soln.Profile());  bool strong = ((BoolPortion *) param[1])->Value();  /*  bool mixed = ((BoolPortion *) param[2])->Value(); */  gPrecision prec = (((MixedPortion *) param[0])->Value())->Precision();  gWatch watch;  bool ret = IsMixedDominated(pr,strong, prec, 			      ((OutputPortion *) param[4])->Value());  ((NumberPortion *) param[3])->SetValue(watch.Elapsed());    return new BoolPortion(ret);}//----------// Game//----------static Portion *GSM_Game_NfPlayer(GSM &, Portion **param){  Nfg &N = ((NfPlayerPortion *) param[0])->Value()->Game();  return new NfgPortion(&N);}static Portion *GSM_Game_Strategy(GSM &, Portion **param){  Nfg &N = ((StrategyPortion *) param[0])->Value()->Player()->Game();  return new NfgPortion(&N);}static Portion *GSM_Game_NfOutcome(GSM &, Portion **param){  Nfg *N = ((NfOutcomePortion *) param[0])->Value()->Game();  return new NfgPortion(N);}//--------------// IsConstSum//--------------static Portion *GSM_IsConstSum(GSM &, Portion **param){  Nfg *N = ((NfgPortion *) param[0])->Value();  return new BoolPortion(IsConstSum(*N));}//-----------// LoadNfg//-----------extern int ReadNfgFile(gInput &f, Nfg *& N);static Portion *GSM_LoadNfg(GSM &, Portion **param){  gText file = ((TextPortion *) param[0])->Value();  Nfg *nfg = 0;  try {     gFileInput f(file);    if (!ReadNfgFile(f, nfg))      throw gclRuntimeError(file + "is not a valid .nfg file");    return new NfgPortion(nfg);  }  catch (gFileInput::OpenFailed &)  {    throw gclRuntimeError("Unable to open file " + file + " for reading");  }}//--------// Name//--------static Portion* GSM_Name(GSM &, Portion **param){  if (param[0]->Spec().Type == porNULL)    return new TextPortion("");  switch (param[0]->Spec().Type) {  case porNFG:    return new TextPortion(((NfgPortion*) param[0])->Value()->GetTitle());  case porNFPLAYER:    return new TextPortion(((NfPlayerPortion*) param[0])->Value()->			      GetName());  case porSTRATEGY:    return new TextPortion(((StrategyPortion*) param[0])->Value()->Name());  case porNFOUTCOME:    return new TextPortion(((NfOutcomePortion*) param[0])->Value()->			   GetName());  case porNFSUPPORT:    return new TextPortion(((NfSupportPortion*) param[0])->Value()->GetName());  default:    throw gclRuntimeError("Unknown type passed to Name[]");  }}//----------// NewNfg//----------static Portion *GSM_NewNfg(GSM &, Portion **param){  ListPortion *dim = ((ListPortion *) param[0]);  gArray<int> d(dim->Length());    for (int i = 1; i <= dim->Length(); i++)    d[i] = ((NumberPortion *) (*dim)[i])->Value();  return new NfgPortion(new Nfg(d));}//--------------// NewOutcome//--------------static Portion *GSM_NewOutcome(GSM &, Portion **param){  return new NfOutcomePortion(((NfgPortion *) param[0])->Value()->NewOutcome());}//-----------// Outcome//-----------static Portion* GSM_Outcome(GSM &, Portion** param){  int i;  Nfg &nfg =     ((StrategyPortion *) (*((ListPortion *) param[0]))[1])->Value()->Player()->Game();    if (nfg.NumPlayers() != ((ListPortion *) param[0])->Length())    throw gclRuntimeError("Invalid profile");  StrategyProfile profile(nfg);  for (i = 1; i <= nfg.NumPlayers(); i++)  {    Strategy *strat =      ((StrategyPortion *) (*((ListPortion *) param[0]))[i])->Value();    if (strat->Player()->GetNumber() != i)      throw gclRuntimeError("Invalid profile");    profile.Set(i, strat);  }    if (nfg.GetOutcome(profile))    return new NfOutcomePortion(nfg.GetOutcome(profile));  else    return new NullPortion(porNFOUTCOME);}//------------// Outcomes//------------static Portion *GSM_Outcomes(GSM &, Portion **param){  Nfg *N = ((NfgPortion *) param[0])->Value();    return ArrayToList(N->Outcomes());}//-----------// Payoff//-----------static Portion* GSM_Payoff(GSM &, Portion** param){  if (param[0]->Spec().Type == porNULL)    return new NumberPortion(0);  NFOutcome *outcome = ((NfOutcomePortion *) param[0])->Value();  Nfg *nfg = outcome->Game();  NFPlayer *player = ((NfPlayerPortion *) param[1])->Value();  return new NumberPortion(nfg->Payoff(outcome, player->GetNumber()));}//------------// Player//------------static Portion *GSM_Player(GSM &, Portion **param){  if (param[0]->Spec().Type == porNULL)    return new NullPortion(porNFPLAYER);  Strategy *s = ((StrategyPortion *) param[0])->Value();  return new NfPlayerPortion(s->Player());}//------------// Players//------------static Portion *GSM_Players(GSM &, Portion **param){

⌨️ 快捷键说明

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