algfunc.cc

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

CC
1,541
字号
    gsm.OutputStream() << "WARNING: Solving game of imperfect recall with PolEnum; results not guaranteed\n";  }  gsm.StartAlgorithmMonitor("PolEnumSolve Progress");  gList<BehavSolution> solutions;  SubgameSolver algorithm;  if (AsBool(param[1])) {    nfgPolEnum *nfgAlgorithm = new nfgPolEnum;    nfgAlgorithm->SetStopAfter(AsNumber(param[2]));    algorithm.SetAlgorithm(nfgAlgorithm);  }  else {    efgPolEnum *efgAlgorithm = new efgPolEnum;    efgAlgorithm->SetStopAfter(AsNumber(param[2]));    algorithm.SetAlgorithm(efgAlgorithm);  }  try {    solutions = algorithm.Solve(support, gsm.GetStatusMonitor());  }  catch (gSignalBreak &) { }  catch (...) {    gsm.EndAlgorithmMonitor();    throw;  }  gsm.EndAlgorithmMonitor();  return new Behav_ListPortion(solutions);}//---------// Nfg//---------static Portion *GSM_Nfg(GSM &, Portion **param){  const efgGame &efg = AsEfg(param[0]);  Nfg *nfg = MakeReducedNfg(EFSupport(efg));  if (nfg) {    return new NfgPortion(nfg);  }  else {    throw gclRuntimeError("Conversion to reduced nfg failed");  }}//----------// Payoff//----------Portion* GSM_Payoff_Behav(GSM &, Portion** param){  BehavSolution *bp = ((BehavPortion *) param[0])->Value();  EFPlayer *player = ((EfPlayerPortion *) param[1])->Value();  return new NumberPortion(bp->Payoff(player->GetNumber()));}Portion* GSM_Payoff_Mixed(GSM &, Portion** param){  MixedProfile<gNumber> mp(*(*((MixedPortion*) param[0])->Value()).Profile());  NFPlayer *player = ((NfPlayerPortion *) param[1])->Value();  return new NumberPortion(mp.Payoff(player->GetNumber()));}//------------------// QreGridSolve//------------------static Portion *GSM_QreGrid_Support(GSM &gsm, Portion **param){  NFSupport &support = *((NfSupportPortion*) param[0])->Value();  gOutput *pxiFile = 0;  if (((TextPortion *) param[1])->Value() != "") {    pxiFile = new gFileOutput(((TextPortion *) param[1])->Value());  }  else {    pxiFile = new gNullOutput;  }  QreNfgGrid qre;  qre.SetMinLambda(((NumberPortion *) param[2])->Value());  qre.SetMaxLambda(((NumberPortion *) param[3])->Value());  qre.SetDelLambda(((NumberPortion *) param[4])->Value());  qre.SetPowLambda(((NumberPortion *) param[5])->Value());  qre.SetFullGraph(((BoolPortion *) param[6])->Value());  qre.SetDelP1(((NumberPortion *) param[7])->Value());  qre.SetTol1(((NumberPortion *) param[8])->Value());  qre.SetDelP2(((NumberPortion *) param[9])->Value());  qre.SetTol2(((NumberPortion *) param[10])->Value());  gList<MixedSolution> solutions;  gsm.StartAlgorithmMonitor("QreGridSolve Progress");  try {    qre.Solve(support, *pxiFile, 	      gsm.GetStatusMonitor(), solutions);  }  catch (gSignalBreak &) { }  catch (...) {    delete pxiFile;    gsm.EndAlgorithmMonitor();    throw;  }  delete pxiFile;  gsm.EndAlgorithmMonitor();  return new Mixed_ListPortion(solutions);}//---------------// QreSolve//---------------static Portion *GSM_Qre_Start(GSM &gsm, Portion **param){  gOutput *pxiFile = 0;  if (((TextPortion *) param[1])->Value() != "") {    pxiFile = new gFileOutput(((TextPortion *) param[1])->Value());  }  else {    pxiFile = new gNullOutput;  }  if (param[0]->Spec().Type == porMIXED)  {    const MixedSolution &start = AsMixed(param[0]);    nfgQre algorithm;    algorithm.SetMaxLambda(AsNumber(param[3]));    algorithm.SetFullGraph(AsBool(param[6]));    gList<MixedSolution> solutions;    gsm.StartAlgorithmMonitor("QreSolve Progress");    try {      solutions = algorithm.Solve(start.Support(), gsm.GetStatusMonitor());    }    catch (gSignalBreak &) { }    catch (...) {      delete pxiFile;        gsm.EndAlgorithmMonitor();      throw;    }    delete pxiFile;    gsm.EndAlgorithmMonitor();    return new Mixed_ListPortion(solutions);  }  else  {     // BEHAV    const BehavSolution &start = AsBehav(param[0]);    efgGame &efg = start.GetGame();      if (!IsPerfectRecall(efg)) {      gsm.OutputStream() << "WARNING: Solving game of imperfect recall with Qre; results not guaranteed\n";    }    efgQre algorithm;    algorithm.SetMaxLambda(AsNumber(param[3]));    algorithm.SetFullGraph(AsNumber(param[6]));        gList<BehavSolution> solutions;    gsm.StartAlgorithmMonitor("QreSolve Progress");    try {      solutions = algorithm.Solve(start.Support(), gsm.GetStatusMonitor());    }    catch (gSignalBreak &) { }    catch (...) {      delete pxiFile;      gsm.EndAlgorithmMonitor();      throw;    }    delete pxiFile;    gsm.EndAlgorithmMonitor();    return new Behav_ListPortion(solutions);  }}//----------------// SimpDivSolve//----------------static nfgNashAlgorithm *GSM_Simpdiv_Nfg_Double(int p_stopAfter,						int p_numRestarts,						int p_leashLength){  nfgSimpdiv<double> *algorithm = new nfgSimpdiv<double>;  //  algorithm->SetStopAfter(p_stopAfter);  algorithm->SetNumRestarts(p_numRestarts);  algorithm->SetLeashLength(p_leashLength);  return algorithm;}static nfgNashAlgorithm *GSM_Simpdiv_Nfg_Rational(int p_stopAfter,						  int p_numRestarts,						  int p_leashLength){  nfgSimpdiv<gRational> *algorithm = new nfgSimpdiv<gRational>;  //  algorithm->SetStopAfter(p_stopAfter);  algorithm->SetNumRestarts(p_numRestarts);  algorithm->SetLeashLength(p_leashLength);  return algorithm;}static Portion *GSM_Simpdiv_Nfg(GSM &gsm, Portion **param){  const NFSupport &support = AsNfgSupport(param[0]);  nfgNashAlgorithm *algorithm;  if (AsPrecision(param[4]) == precDOUBLE) {    algorithm = GSM_Simpdiv_Nfg_Double(AsNumber(param[1]),				       AsNumber(param[2]),				       AsNumber(param[3]));  }  else {    algorithm = GSM_Simpdiv_Nfg_Rational(AsNumber(param[1]),					 AsNumber(param[2]),					 AsNumber(param[3]));  }  gsm.StartAlgorithmMonitor("SimpDivSolve Progress");  gList<MixedSolution> solutions;  try {    solutions = algorithm->Solve(support, gsm.GetStatusMonitor());  }  catch (gSignalBreak &) { }  catch (...) {    delete algorithm;    gsm.EndAlgorithmMonitor();    throw;  }  delete algorithm;  gsm.EndAlgorithmMonitor();  return new Mixed_ListPortion(solutions);}static Portion *GSM_Simpdiv_Efg(GSM &gsm, Portion **param){  const EFSupport &support = AsEfgSupport(param[0]);  if (!AsBool(param[1])) {    throw gclRuntimeError("algorithm not implemented for extensive forms");  }  if (!IsPerfectRecall(support.GetGame())) {    gsm.OutputStream() << "WARNING: Solving game of imperfect recall with Simpdiv; results not guaranteed\n";  }  nfgNashAlgorithm *nfgAlgorithm = 0;  if (((PrecisionPortion *) param[5])->Value() == precDOUBLE) {    nfgAlgorithm = GSM_Simpdiv_Nfg_Double(AsNumber(param[2]),					  AsNumber(param[3]),					  AsNumber(param[4]));  }  else {    nfgAlgorithm = GSM_Simpdiv_Nfg_Rational(AsNumber(param[2]),					    AsNumber(param[3]),					    AsNumber(param[4]));  }  if (!IsPerfectRecall(support.GetGame())) {    gsm.OutputStream() << "WARNING: Solving game of imperfect recall with Simpdiv; results not guaranteed\n";  }  SubgameSolver algorithm;  algorithm.SetAlgorithm(nfgAlgorithm);  gsm.StartAlgorithmMonitor("SimpDivSolve Progress");  gList<BehavSolution> solutions;  try {    solutions = algorithm.Solve(support, gsm.GetStatusMonitor());  }  catch (gSignalBreak &) { }  catch (...) {    gsm.EndAlgorithmMonitor();     throw;  }  gsm.EndAlgorithmMonitor();  return new Behav_ListPortion(solutions);}static Portion *GSM_VertEnum(GSM &gsm, Portion** param){  if (((PrecisionPortion *) param[2])->Value() == precDOUBLE)  {    gMatrix<double>* A = ListToMatrix_Float((ListPortion*) param[0]);    gVector<double>* b = ListToVector_Float((ListPortion*) param[1]);        gList< gVector< double > > verts;        VertEnum<double> *vertenum = new VertEnum<double>(*A, *b,						      gsm.GetStatusMonitor());    vertenum->Vertices( verts );    delete vertenum;    delete A;    delete b;        ListPortion* list = new ListPortion();    for (int i = 1; i <= verts.Length(); i++)        list->Append( ArrayToList( verts[i] ) );        return list;  }  else {    gMatrix<gRational>* A = ListToMatrix_Rational((ListPortion*) param[0]);    gVector<gRational>* b = ListToVector_Rational((ListPortion*) param[1]);        gList< gVector< gRational > > verts;        VertEnum<gRational> *vertenum = new VertEnum<gRational>(*A, *b,							    gsm.GetStatusMonitor());    vertenum->Vertices( verts );    delete vertenum;    delete A;    delete b;        ListPortion* list = new ListPortion();    for (int i = 1; i <= verts.Length(); i++)        list->Append( ArrayToList( verts[i] ) );        return list;  }}void Init_algfunc(GSM *gsm){  gclFunction *FuncObj;  FuncObj = new gclFunction(*gsm, "AgentForm", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_AgentForm, porNFG, 2));  FuncObj->SetParamInfo(0, 0, gclParameter("efg", porEFG));  FuncObj->SetParamInfo(0, 1, gclParameter("time", porNUMBER,					    new NumberPortion(0), BYREF));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "Behav", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Behav, porBEHAV, 1));  FuncObj->SetParamInfo(0, 0, gclParameter("mixed", porMIXED));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "EnumMixedSolve", 2);  FuncObj->SetFuncInfo(0, gclSignature(GSM_EnumMixed_Nfg, 				       PortionSpec(porMIXED, 1), 8));  FuncObj->SetParamInfo(0, 0, gclParameter("support", porNFSUPPORT));  FuncObj->SetParamInfo(0, 1, gclParameter("stopAfter", porINTEGER,					    new NumberPortion(0)));  FuncObj->SetParamInfo(0, 2, gclParameter("precision", porPRECISION,              new PrecisionPortion(precDOUBLE)));  FuncObj->SetParamInfo(0, 3, gclParameter("nPivots", porINTEGER,					    new NumberPortion(0), BYREF));  FuncObj->SetParamInfo(0, 4, gclParameter("time", porNUMBER,					    new NumberPortion(0.0), BYREF));  FuncObj->SetParamInfo(0, 5, gclParameter("traceFile", porOUTPUT,					    new OutputPortion(*new gNullOutput), 					    BYREF));  FuncObj->SetParamInfo(0, 6, gclParameter("traceLevel", porNUMBER,					    new NumberPortion(0)));  FuncObj->SetParamInfo(0, 7, gclParameter("cliques", porBOOLEAN,					    new BoolPortion(false)));  FuncObj->SetFuncInfo(1, gclSignature(GSM_EnumMixed_Efg, 				       PortionSpec(porBEHAV, 1), 9));  FuncObj->SetParamInfo(1, 0, gclParameter("support", porEFSUPPORT));  FuncObj->SetParamInfo(1, 1, gclParameter("asNfg", porBOOLEAN,					    new BoolPortion(true)));  FuncObj->SetParamInfo(1, 2, gclParameter("stopAfter", porINTEGER,					    new NumberPortion(0)));  FuncObj->SetParamInfo(1, 3, gclParameter("precision", porPRECISION,              new PrecisionPortion(precDOUBLE)));  FuncObj->SetParamInfo(1, 4, gclParameter("nPivots", porINTEGER,					    new NumberPortion(0), BYREF));  FuncObj->SetParamInfo(1, 5, gclParameter("time", porNUMBER,					    new NumberPortion(0.0), BYREF));  FuncObj->SetParamInfo(1, 6, gclParameter("traceFile", porOUTPUT,					    new OutputPortion(*new gNullOutput), 

⌨️ 快捷键说明

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