Adding a system to compare two AI implementations (AIPlayerBaka and AIPlayerBakaB). This can be used to make sure a change to the AI is not making the AI weaker, for example.

This commit is contained in:
wagic.the.homebrew
2011-09-20 11:32:24 +00:00
parent 4c45e971c6
commit 44d9951246
14 changed files with 1921 additions and 19 deletions

View File

@@ -16,6 +16,7 @@
#define _IAPLAYER_H
#include "Player.h"
#include "config.h"
#include <queue>
using std::queue;
@@ -68,7 +69,9 @@ public:
class AIPlayer: public Player{
protected:
bool mFastTimerMode;
queue<AIAction *> clickstream;
int clickMultiTarget(TargetChooser * tc,vector<Targetable*>&potentialTargets);
int clickSingleTarget(TargetChooser * tc,vector<Targetable*>&potentialTargets, MTGCardInstance * Choosencard = NULL);
@@ -93,6 +96,8 @@ public:
int isAI(){return 1;};
void setFastTimerMode(bool mode = true) { mFastTimerMode = mode; };
};
@@ -100,6 +105,9 @@ public:
class AIPlayerFactory{
public:
AIPlayer * createAIPlayer(MTGAllCards * collection, Player * opponent, int deckid = 0);
#ifdef AI_CHANGE_TESTING
AIPlayer * createAIPlayerTest(MTGAllCards * collection, Player * opponent, int deckid = 0);
#endif
};

View File

@@ -61,11 +61,6 @@ typedef std::map<OrderedAIAction, int, CmpAbilities> RankingContainer;
class AIPlayerBaka: public AIPlayer{
private:
MTGCardInstance * nextCardToPlay;
AIHints * hints;
AIStats * stats;
int orderBlockers();
int combatDamages();
int interruptIfICan();
@@ -91,6 +86,9 @@ private:
AIStats * getStats();
protected:
MTGCardInstance * nextCardToPlay;
AIHints * hints;
AIStats * stats;
int oldGamePhase;
float timer;
MTGCardInstance * FindCardToPlay(ManaCost * potentialMana, const char * type);
@@ -122,7 +120,7 @@ private:
virtual ~AIPlayerBaka();
int affectCombatDamages(CombatStep step);
int canHandleCost(MTGAbility * ability);
int chooseTarget(TargetChooser * tc = NULL, Player * forceTarget =NULL,MTGCardInstance * Choosencard = NULL,bool checkonly = false);
int chooseTarget(TargetChooser * tc = NULL, Player * forceTarget = NULL,MTGCardInstance * Chosencard = NULL,bool checkonly = false);
//used by AIHInts, therefore public instead of private :/
int createAbilityTargets(MTGAbility * a, MTGCardInstance * c, RankingContainer& ranking);

View File

@@ -0,0 +1,68 @@
#include "config.h"
#ifdef AI_CHANGE_TESTING
#ifndef _AI_PLAYER_BAKA_B_H_
#define _AI_PLAYER_BAKA_B_H_
#include "AIPlayerBaka.h"
class AIStats;
class AIHints;
class AIPlayerBakaB: public AIPlayerBaka{
private:
int orderBlockers();
int combatDamages();
int interruptIfICan();
int chooseAttackers();
int chooseBlockers();
int canFirstStrikeKill(MTGCardInstance * card, MTGCardInstance *ennemy);
int effectBadOrGood(MTGCardInstance * card, int mode = MODE_PUTINTOPLAY, TargetChooser * tc = NULL);
// returns 1 if the AI algorithm supports a given cost (ex:simple mana cost), 0 otherwise (ex: cost involves Sacrificing a target)
int CanHandleCost(ManaCost * cost);
//Tries to play an ability recommended by the deck creator
int selectHintAbility();
vector<MTGAbility*> canPayMana(MTGCardInstance * card = NULL,ManaCost * mCost = NULL);
vector<MTGAbility*> canPaySunBurst(ManaCost * mCost = NULL);
MTGCardInstance * chooseCard(TargetChooser * tc, MTGCardInstance * source, int random = 0);
int selectMenuOption();
int useAbility();
AIStats * getStats();
protected:
MTGCardInstance * FindCardToPlay(ManaCost * potentialMana, const char * type);
//used by MomirPlayer, hence protected instead of private
virtual int getEfficiency(OrderedAIAction * action);
bool payTheManaCost(ManaCost * cost, MTGCardInstance * card = NULL,vector<MTGAbility*> gotPayment = vector<MTGAbility*>());
int getCreaturesInfo(Player * player, int neededInfo = INFO_NBCREATURES , int untapMode = 0, int canAttack = 0);
ManaCost * getPotentialMana(MTGCardInstance * card = NULL);
int selectAbility();
public:
AIPlayerBakaB(string deckFile, string deckfileSmall, string avatarFile, MTGDeck * deck = NULL);
virtual int Act(float dt);
void initTimer();
virtual int computeActions();
virtual void Render();
virtual int receiveEvent(WEvent * event);
virtual ~AIPlayerBakaB();
int affectCombatDamages(CombatStep step);
int canHandleCost(MTGAbility * ability);
int chooseTarget(TargetChooser * tc = NULL, Player * forceTarget =NULL,MTGCardInstance * Choosencard = NULL,bool checkonly = false);
//used by AIHInts, therefore public instead of private :/
int createAbilityTargets(MTGAbility * a, MTGCardInstance * c, RankingContainer& ranking);
};
#endif
#endif

View File

@@ -30,10 +30,11 @@ class Rules;
enum
{
PLAYER_TYPE_CPU = 0,
PLAYER_TYPE_HUMAN=1,
PLAYER_TYPE_TESTSUITE=2,
PLAYER_TYPE_HUMAN = 1,
PLAYER_TYPE_TESTSUITE = 2,
PLAYER_TYPE_CPU_TEST = 3,
#ifdef NETWORK_SUPPORT
PLAYER_TYPE_REMOTE=3
PLAYER_TYPE_REMOTE = 4
#endif //NETWORK_SUPPORT
};

View File

@@ -53,6 +53,13 @@ public:
#ifdef TESTSUITE
void loadTestSuitePlayers();
#endif
#ifdef AI_CHANGE_TESTING
int totalTestGames;
int testPlayer2Victories;
int totalAIDecks;
#endif
virtual void ButtonPressed(int ControllerId, int ControlId);
virtual void Start();
virtual void End();

View File

@@ -40,6 +40,7 @@ enum
#endif //NETWORK_SUPPORT
SUBMENUITEM_DEMO,
SUBMENUITEM_TESTSUITE,
SUBMENUITEM_TESTAI,
SUBMENUITEM_END_OFFSET
};

View File

@@ -32,6 +32,9 @@
// Debug options - comment/uncomment as needed
//#define DEBUG_CACHE
#ifdef _DEBUG
#ifdef WIN32
#define AI_CHANGE_TESTING
#endif
//#define RENDER_AI_STATS
#endif

View File

@@ -9,6 +9,12 @@
// Instances for Factory
#include "AIPlayerBaka.h"
#ifdef AI_CHANGE_TESTING
#include "AIPlayerBakaB.h"
#endif
const char * const MTG_LAND_TEXTS[] = { "artifact", "forest", "island", "mountain", "swamp", "plains", "other lands" };
int AIAction::currentId = 0;
@@ -123,6 +129,7 @@ AIPlayer::AIPlayer(string file, string fileSmall, MTGDeck * deck) :
agressivity = 50;
forceBestAbilityUse = false;
playMode = Player::MODE_AI;
mFastTimerMode = false;
}
@@ -278,5 +285,60 @@ void AIPlayer::Render()
#endif
}
#ifdef AI_CHANGE_TESTING
AIPlayer * AIPlayerFactory::createAIPlayerTest(MTGAllCards * collection, Player * opponent, int deckid)
{
char deckFile[512];
string avatarFilename; // default imagename
char deckFileSmall[512];
if (deckid == GameStateDuel::MENUITEM_EVIL_TWIN)
{ //Evil twin
sprintf(deckFile, "%s", opponent->deckFile.c_str());
DebugTrace("Evil Twin => " << opponent->deckFile);
avatarFilename = "avatar.jpg";
sprintf(deckFileSmall, "%s", "ai_baka_eviltwin");
}
else
{
if (!deckid)
{
//random deck
int nbdecks = 0;
int found = 1;
while (found && nbdecks < options[Options::AIDECKS_UNLOCKED].number)
{
found = 0;
char buffer[512];
sprintf(buffer, "ai/baka/deck%i.txt", nbdecks + 1);
if (FileExists(buffer))
{
found = 1;
nbdecks++;
}
}
if (!nbdecks)
return NULL;
deckid = 1 + WRand() % (nbdecks);
}
sprintf(deckFile, "ai/baka/deck%i.txt", deckid);
DeckMetaData *aiMeta = DeckManager::GetInstance()->getDeckMetaDataByFilename( deckFile, true);
avatarFilename = aiMeta->getAvatarFilename();
sprintf(deckFileSmall, "ai_baka_deck%i", deckid);
}
int deckSetting = EASY;
if ( opponent )
{
bool isOpponentAI = opponent->isAI() == 1;
DeckMetaData *meta = DeckManager::GetInstance()->getDeckMetaDataByFilename( opponent->deckFile, isOpponentAI);
if ( meta->getVictoryPercentage() >= 65)
deckSetting = HARD;
}
// AIPlayerBaka will delete MTGDeck when it's time
AIPlayerBakaB * baka = NEW AIPlayerBakaB(deckFile, deckFileSmall, avatarFilename, NEW MTGDeck(deckFile, collection,0, deckSetting));
baka->deckId = deckid;
return baka;
}
#endif

View File

@@ -1729,7 +1729,10 @@ MTGCardInstance * AIPlayerBaka::FindCardToPlay(ManaCost * pMana, const char * ty
void AIPlayerBaka::initTimer()
{
timer = 0.1f;
if (mFastTimerMode)
timer = 0;
else
timer = 0.1f;
}
int AIPlayerBaka::computeActions()
@@ -2243,12 +2246,16 @@ int AIPlayerBaka::Act(float dt)
oldGamePhase = currentGamePhase;
timer -= dt;
if (mFastTimerMode)
timer -= 1;
else
timer -= dt;
if (timer > 0)
{
return 0;
}
initTimer();
if (combatDamages())
{
return 0;

File diff suppressed because it is too large Load Diff

View File

@@ -84,6 +84,27 @@ GameState(parent, "duel")
testSuite = NULL;
#endif
#ifdef AI_CHANGE_TESTING
totalTestGames = 0;
testPlayer2Victories = 0;
totalAIDecks = 0;
{
int found = 1;
while (found)
{
found = 0;
char buffer[512];
sprintf(buffer, "ai/baka/deck%i.txt", totalAIDecks + 1);
if (FileExists(buffer))
{
found = 1;
totalAIDecks++;
}
}
}
#endif
credits = NULL;
#ifdef NETWORK_SUPPORT
@@ -216,7 +237,17 @@ void GameStateDuel::loadPlayer(int playerId, int decknb, bool isAI, bool isNetwo
AIPlayerFactory playerCreator;
Player * opponent = NULL;
if (playerId == 1) opponent = mPlayers[0];
mPlayers[playerId] = playerCreator.createAIPlayer(MTGCollection(), opponent);
#ifdef AI_CHANGE_TESTING
if (mParent->players[1] == PLAYER_TYPE_CPU_TEST && playerId == 1)
mPlayers[playerId] = playerCreator.createAIPlayerTest(MTGCollection(), opponent);
else
#endif
{
mPlayers[playerId] = playerCreator.createAIPlayer(MTGCollection(), opponent);
}
if (mParent->players[playerId] == PLAYER_TYPE_CPU_TEST)
((AIPlayer *) mPlayers[playerId])->setFastTimerMode();
}
}
@@ -479,11 +510,21 @@ void GameStateDuel::Update(float dt)
}
else
#endif
if (mParent->players[0] == PLAYER_TYPE_CPU && mParent->players[1] == PLAYER_TYPE_CPU)
{
End();
Start();
}
#ifdef AI_CHANGE_TESTING
if (mParent->players[0] == PLAYER_TYPE_CPU_TEST && mParent->players[1] == PLAYER_TYPE_CPU_TEST)
{
totalTestGames++;
if (game->gameOver == game->players[0])
testPlayer2Victories++;
End();
Start();
}
#endif
if (mParent->players[0] == PLAYER_TYPE_CPU && mParent->players[1] == PLAYER_TYPE_CPU)
{
End();
Start();
}
}
if (mEngine->GetButtonClick(JGE_BTN_MENU))
{
@@ -571,6 +612,29 @@ void GameStateDuel::Render()
if (game) game->Render();
#ifdef AI_CHANGE_TESTING
if (game && totalTestGames)
{
char buf[4096];
if (totalTestGames < 2.5 * totalAIDecks)
{
mFont->SetColor(ARGB(255,255,255,0));
sprintf(buf, "Results are not significant, you should let at least %i more games run", totalAIDecks * 2.5 - totalTestGames);
mFont->DrawString(buf,0,SCREEN_HEIGHT/2 - 20);
}
mFont->SetColor(ARGB(255,255,255,255));
float ratio = float(testPlayer2Victories) / float(totalTestGames);
if (ratio < 0.48)
mFont->SetColor(ARGB(255,255,0,0));
if (ratio > 0.52)
mFont->SetColor(ARGB(255,0,255,0));
sprintf(buf, "Victories Player 2/total Games: %i/%i", testPlayer2Victories, totalTestGames);
mFont->DrawString(buf,0,SCREEN_HEIGHT/2);
}
#endif
switch (mGamePhase)
{
case DUEL_STATE_END:

View File

@@ -801,6 +801,10 @@ void GameStateMenu::ButtonPressed(int controllerId, int controlId)
if (Rules::getRulesByFilename("testsuite.txt"))
subMenuController->Add(SUBMENUITEM_TESTSUITE, "Test Suite");
#endif
#ifdef AI_CHANGE_TESTING
subMenuController->Add(SUBMENUITEM_TESTAI, "AI A/B Testing");
#endif
currentState = MENU_STATE_MAJOR_SUBMENU | MENU_STATE_MINOR_NONE;
}
break;
@@ -877,7 +881,16 @@ void GameStateMenu::ButtonPressed(int controllerId, int controlId)
#endif //NETWORK_SUPPORT
currentState = MENU_STATE_MAJOR_MAINMENU | MENU_STATE_MINOR_SUBMENU_CLOSING;
break;
#ifdef AI_CHANGE_TESTING
case SUBMENUITEM_TESTAI:
options[Options::AIDECKS_UNLOCKED].number = 5000; //hack to force-test all decks
mParent->players[0] = PLAYER_TYPE_CPU_TEST;
mParent->players[1] = PLAYER_TYPE_CPU_TEST;
mParent->gameType = GAME_TYPE_DEMO;
subMenuController->Close();
currentState = MENU_STATE_MAJOR_DUEL | MENU_STATE_MINOR_SUBMENU_CLOSING;
break;
#endif
#ifdef TESTSUITE
case SUBMENUITEM_TESTSUITE:
mParent->rules = Rules::getRulesByFilename("testsuite.txt");

View File

@@ -309,6 +309,7 @@
<ClCompile Include="src\AIMomirPlayer.cpp" />
<ClCompile Include="src\AIPlayer.cpp" />
<ClCompile Include="src\AIPlayerBaka.cpp" />
<ClCompile Include="src\AIPlayerBakaB.cpp" />
<ClCompile Include="src\AIStats.cpp" />
<ClCompile Include="src\AllAbilities.cpp" />
<ClCompile Include="src\CardDescriptor.cpp" />
@@ -444,6 +445,7 @@
<ClInclude Include="include\AIMomirPlayer.h" />
<ClInclude Include="include\AIPlayer.h" />
<ClInclude Include="include\AIPlayerBaka.h" />
<ClInclude Include="include\AIPlayerBakaB.h" />
<ClInclude Include="include\AIStats.h" />
<ClInclude Include="include\AllAbilities.h" />
<ClInclude Include="include\CacheEngine.h" />

View File

@@ -316,6 +316,9 @@
<ClCompile Include="src\AIPlayerBaka.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\AIPlayerBakaB.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\ActionElement.h">
@@ -657,6 +660,9 @@
<ClInclude Include="include\AIPlayerBaka.h">
<Filter>inc</Filter>
</ClInclude>
<ClInclude Include="include\AIPlayerBakaB.h">
<Filter>inc</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="Makefile" />