[go: up one dir, main page]

Menu

[r9]: / trunk / rlgomain / RlEngine.cpp  Maximize  Restore  History

Download this file

138 lines (111 with data), 3.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//----------------------------------------------------------------------------
/** @file RlEngine.cpp
*/
//----------------------------------------------------------------------------
#include "SgSystem.h"
#include "RlEngine.h"
#include <iostream>
#include <time.h>
#include "GoGtpCommandUtil.h"
#include "RlAgentPlayer.h"
#include "RlForceLink.h"
#include "RlSetup.h"
#include "RlSimulator.h"
#include "RlUtils.h"
#include "SgDebug.h"
#include "SgTimer.h"
#undef PACKAGE
#undef PACKAGE_VERSION
#undef VERSION
#include "RlConfig.h"
using namespace boost;
using namespace std;
//----------------------------------------------------------------------------
RlEngine::RlEngine(istream& in, ostream& out,
const bfs::path& programPath,
const bfs::path& rlgoDir,
const bfs::path& settingsfile,
const vector<pair<string, string> >& overrides)
: GoGtpEngine(in, out, 0, programPath.native_file_string().c_str())
{
m_commands.Register(*this);
RlForceLink();
// Create a single player that is used by both sides
RlAgentPlayer* agentplayer = new RlAgentPlayer(Board());
m_player = agentplayer;
GoGtpEngine::SetPlayer(agentplayer);
SetOverrides(overrides);
m_setup = InitSettings(settingsfile, rlgoDir, m_player->Board());
// Set player to use newly created agent
m_player->SetAgent(m_setup->GetMainAgent());
m_setup->SetGame(&GetGame());
m_commands.Setup(m_setup);
}
RlEngine::~RlEngine()
{
RlGetFactory().Clear();
}
RlSetup* RlEngine::InitSettings(const bfs::path& settingsfile,
const bfs::path& rlgoPath, GoBoard& bd)
{
// Delete all old objects in the factory (agent, feature sets, etc.)
RlGetFactory().Clear();
RlGetFactory().SetDefaultPath(rlgoPath / "settings");
RlGetFactory().Load(bd, settingsfile);
RlSetup* setup = dynamic_cast<RlSetup*>
(RlGetFactory().GetObject("Setup"));
setup->SetMainPath(rlgoPath);
SgRandom::SetSeed(setup->GetRandomSeed());
// Initialise board to specified board size and rules
Init(setup->GetBoardSize());
SetNamedRules(setup->GetRules());
// Initialise all objects in factory
RlGetFactory().Initialise();
// Execute any GTP commands specified in the setup file
for (int i = 0; i < setup->GetNumGtpCommands(); ++i)
ExecuteCommand(setup->GetGtpCommand(i));
return setup;
}
void RlEngine::SetOverrides(const vector<pair<string, string> >& overrides)
{
// DataPath gets set with following priority:
// 1. Command line
// 2. Environment variable
// 3. Settings file
#if UNIX
extern char** environ;
for (int i = 0; environ[i] != NULL; i++)
{
string env(environ[i]);
if (env.substr(0, 2) == "Rl")
{
int i = env.find('=');
string setting = env.substr(2, i - 2);
string value = env.substr(i + 1);
RlGetFactory().SetOverride(setting, value);
}
}
#endif
for (vector<pair<string, string> >::const_iterator
i_overrides = overrides.begin();
i_overrides != overrides.end(); ++i_overrides)
{
RlGetFactory().SetOverride(i_overrides->first, i_overrides->second);
}
}
void RlEngine::CmdAnalyzeCommands(GtpCommand& cmd)
{
GoGtpEngine::CmdAnalyzeCommands(cmd);
m_commands.AddGoGuiAnalyzeCommands(cmd);
string response = cmd.Response();
cmd.SetResponse(GoGtpCommandUtil::SortResponseAnalyzeCommands(response));
}
void RlEngine::CmdName(GtpCommand& cmd)
{
cmd << "RLGO";
}
void RlEngine::CmdVersion(GtpCommand& cmd)
{
cmd << VERSION;
}
//----------------------------------------------------------------------------