[go: up one dir, main page]

Menu

[r74]: / trunk / rlgomain / RlMain.cpp  Maximize  Restore  History

Download this file

194 lines (164 with data), 5.0 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//----------------------------------------------------------------------------
/** @file RlMain.cpp
Main function for Rl.
*/
//----------------------------------------------------------------------------
#include "SgSystem.h"
#include <iostream>
#include <dirent.h>
#include "GoInit.h"
#include "RlEngine.h"
#include "RlAgent.h"
#include "RlSetup.h"
#include "RlSimulator.h"
#include "SgDebug.h"
#include "SgException.h"
#include "SgInit.h"
#undef PACKAGE
#undef PACKAGE_VERSION
#undef VERSION
#include "RlConfig.h"
using namespace std;
using namespace boost::filesystem;
//----------------------------------------------------------------------------
namespace {
/** @name Settings from command line options */
// @{
vector<pair<string, string> > g_overrides;
string g_config;
bfs::path g_execFile; // Executable filename
bfs::path g_execDir; // Executable directory
bfs::path g_rlgoDir; // Root directory of RLGO distribution
bfs::path g_settingsFile("tourney.set"); // Default settings file
// @} // @name
void MainLoop()
{
RlEngine engine(cin, cout,
g_execDir,
g_rlgoDir,
g_settingsFile,
g_overrides);
GoGtpAssertionHandler assertionHandler(engine);
if (g_config != "")
engine.ExecuteFile(g_config);
RlSimulator* simulator = RlSetup::Get()->GetSelfPlay();
if (simulator)
{
SgDebug() << "Running in self-play mode\n";
simulator->Simulate();
}
else
{
// Call new game once on startup,
// so that GTP genmove will work immediately
RlSetup::Get()->GetMainAgent()->NewGame();
SgDebug() << "Ready\n";
engine.MainLoop();
}
}
void ParseOptions(int argc, char** argv)
{
// Arguments must be specified in pairs:
// -setting value
// There is one special pair specifying the settings file
// All other pairs specify overrides of settings
if (argc % 2 != 1)
throw SgException("Settings must be specified in pairs");
for (int i = 1; i < argc; i += 2)
{
if (argv[i][0] != '-')
throw SgException("Expected setting to start with '-'");
string setting = argv[i] + 1;
string value = argv[i + 1];
if (setting == "settings" || setting == "Settings")
g_settingsFile = value;
else
g_overrides.push_back(pair<string, string>(setting, value));
if (setting == "config")
g_config = value;
}
}
void PrintStartupMessage()
{
SgDebug() << "RLGO version " << VERSION << '\n' <<
"Copyright (C) 2009 David Silver.\n"
"Built on top of the Fuego 0.4 library (see http://fuego.sf.net)\n"
"This program comes with ABSOLUTELY NO WARRANTY. This is\n"
"free software and you are welcome to redistribute it under\n"
"certain conditions. Type `rlgo-license' for details.\n\n";
}
bool SearchPath(const bfs::path& rlgoPath)
{
return exists(rlgoPath / "README")
&& exists(rlgoPath / "INSTALL")
&& exists(rlgoPath / "rlgo")
&& exists(rlgoPath / "rlgomain");
}
void GetProgramDir(const char* programPath)
{
if (!programPath)
throw SgException("Could not find program path");
bfs::path execpath = bfs::path(programPath, boost::filesystem::native);
g_execDir = execpath.branch_path();
g_execFile = execpath.leaf();
g_rlgoDir = g_execDir / "..";
if (!SearchPath(g_rlgoDir))
{
cout << "Executable directory = " << g_execDir << "\n:";
cout << "Executable filename = " << g_execFile << "\n:";
cout << "RLGO directory = " << g_rlgoDir << "\n:";
throw SgException("Could not find root directory of RLGO distribution");
}
}
void FindSettingsFile()
{
string settings = g_settingsFile.file_string();
if (exists(g_rlgoDir / "settings" / settings))
return;
if (exists(g_rlgoDir / "settings" / (settings + ".set")))
{
g_settingsFile = bfs::path(settings + ".set");
return;
}
throw SgException("Could not find settings file");
}
} // namespace
//----------------------------------------------------------------------------
int main(int argc, char** argv)
{
if (argc > 0 && argv != 0)
{
GetProgramDir(argv[0]);
try
{
ParseOptions(argc, argv);
}
catch (const SgException& e)
{
SgDebug() << e.what() << "\n";
return 1;
}
}
FindSettingsFile();
try
{
SgInit();
GoInit();
PrintStartupMessage();
MainLoop();
GoFini();
SgFini();
}
catch (const GtpFailure& e)
{
SgDebug() << e.Response() << '\n';
return 1;
}
catch (const std::exception& e)
{
SgDebug() << e.what() << '\n';
return 1;
}
return 0;
}
//----------------------------------------------------------------------------