Skip to content

Commit 18c49f4

Browse files
committed
Some tidying in the command-line algorithm drivers:
* Refactor creation of renderers for DRY. * Minor C++ modernisations
1 parent 00ac256 commit 18c49f4

12 files changed

Lines changed: 115 additions & 155 deletions

File tree

src/tools/convert/convert.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222

2323
#include <iostream>
2424
#include <fstream>
25-
#include <cstdlib>
2625
#include <getopt.h>
2726

2827
#include "gambit.h"
2928
#include "games/writer.h"
3029

30+
using namespace Gambit;
31+
3132
void PrintBanner(std::ostream &p_stream)
3233
{
3334
p_stream << "Convert games among various file formats\n";
@@ -79,7 +80,7 @@ int main(int argc, char *argv[])
7980
break;
8081
case '?':
8182
if (isprint(optopt)) {
82-
std::cerr << argv[0] << ": Unknown option `-" << ((char)optopt) << "'.\n";
83+
std::cerr << argv[0] << ": Unknown option `-" << static_cast<char>(optopt) << "'.\n";
8384
}
8485
else {
8586
std::cerr << argv[0] << ": Unknown option character `\\x" << optopt << "`.\n";
@@ -122,7 +123,7 @@ int main(int argc, char *argv[])
122123
}
123124

124125
try {
125-
const Gambit::Game game = Gambit::ReadGame(*input_stream);
126+
const Game game = ReadGame(*input_stream);
126127

127128
if (rowPlayer < 1 || rowPlayer > static_cast<int>(game->NumPlayers())) {
128129
std::cerr << argv[0] << ": Player " << rowPlayer << " does not exist.\n";

src/tools/enummixed/enummixed.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2121
//
2222

23-
#include <cstdlib>
2423
#include <getopt.h>
2524
#include <iostream>
2625
#include <fstream>
@@ -34,7 +33,7 @@ using namespace Gambit::Nash;
3433

3534
template <class T>
3635
void PrintCliques(const List<List<MixedStrategyProfile<T>>> &p_cliques,
37-
std::shared_ptr<StrategyProfileRenderer<T>> p_renderer)
36+
std::shared_ptr<MixedProfileRenderer<T>> p_renderer)
3837
{
3938
for (size_t cl = 1; cl <= p_cliques.size(); cl++) {
4039
for (size_t i = 1; i <= p_cliques[cl].size(); i++) {
@@ -75,7 +74,7 @@ int main(int argc, char *argv[])
7574
int numDecimals = 6;
7675

7776
int long_opt_index = 0;
78-
struct option long_options[] = {
77+
option long_options[] = {
7978
{"help", 0, nullptr, 'h'}, {"version", 0, nullptr, 'v'}, {nullptr, 0, nullptr, 0}};
8079
while ((c = getopt_long(argc, argv, "d:vhqcS", long_options, &long_opt_index)) != -1) {
8180
switch (c) {
@@ -99,7 +98,7 @@ int main(int argc, char *argv[])
9998
break;
10099
case '?':
101100
if (isprint(optopt)) {
102-
std::cerr << argv[0] << ": Unknown option `-" << ((char)optopt) << "'.\n";
101+
std::cerr << argv[0] << ": Unknown option `-" << static_cast<char>(optopt) << "'.\n";
103102
}
104103
else {
105104
std::cerr << argv[0] << ": Unknown option character `\\x" << optopt << "`.\n";
@@ -130,8 +129,7 @@ int main(int argc, char *argv[])
130129
try {
131130
const Game game = ReadGame(*input_stream);
132131
if (useFloat) {
133-
std::shared_ptr<StrategyProfileRenderer<double>> renderer =
134-
std::make_shared<MixedStrategyCSVRenderer<double>>(std::cout, numDecimals);
132+
auto renderer = MakeMixedStrategyProfileRenderer<double>(std::cout, numDecimals, false);
135133
auto solution = EnumMixedStrategySolveDetailed<double>(
136134
game, [&](const MixedStrategyProfile<double> &p, const std::string &label) {
137135
renderer->Render(p, label);
@@ -141,8 +139,7 @@ int main(int argc, char *argv[])
141139
}
142140
}
143141
else {
144-
std::shared_ptr<StrategyProfileRenderer<Rational>> renderer(
145-
new MixedStrategyCSVRenderer<Rational>(std::cout));
142+
auto renderer = MakeMixedStrategyProfileRenderer<Rational>(std::cout, numDecimals, false);
146143
auto solution = EnumMixedStrategySolveDetailed<Rational>(
147144
game, [&](const MixedStrategyProfile<Rational> &p, const std::string &label) {
148145
renderer->Render(p, label);

src/tools/enumpoly/enumpoly.cc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222

2323
#include <iostream>
2424
#include <fstream>
25-
#include <cstdlib>
2625
#include <getopt.h>
2726
#include "gambit.h"
2827
#include "solvers/enumpoly/enumpoly.h"
2928

3029
using namespace Gambit;
31-
using namespace Gambit::Nash;
30+
using namespace Nash;
3231

3332
int g_numDecimals = 6;
3433
bool g_verbose = false;
@@ -128,10 +127,10 @@ int main(int argc, char *argv[])
128127
int stopAfter = 0;
129128

130129
int long_opt_index = 0;
131-
struct option long_options[] = {{"help", 0, nullptr, 'h'},
132-
{"version", 0, nullptr, 'v'},
133-
{"verbose", 0, nullptr, 'V'},
134-
{nullptr, 0, nullptr, 0}};
130+
option long_options[] = {{"help", 0, nullptr, 'h'},
131+
{"version", 0, nullptr, 'v'},
132+
{"verbose", 0, nullptr, 'V'},
133+
{nullptr, 0, nullptr, 0}};
135134
int c;
136135
while ((c = getopt_long(argc, argv, "d:hSm:e:qvV", long_options, &long_opt_index)) != -1) {
137136
switch (c) {
@@ -161,7 +160,7 @@ int main(int argc, char *argv[])
161160
break;
162161
case '?':
163162
if (isprint(optopt)) {
164-
std::cerr << argv[0] << ": Unknown option `-" << ((char)optopt) << "'.\n";
163+
std::cerr << argv[0] << ": Unknown option `-" << static_cast<char>(optopt) << "'.\n";
165164
}
166165
else {
167166
std::cerr << argv[0] << ": Unknown option character `\\x" << optopt << "`.\n";
@@ -190,9 +189,9 @@ int main(int argc, char *argv[])
190189
}
191190

192191
try {
193-
const Gambit::Game game = Gambit::ReadGame(*input_stream);
192+
const Game game = ReadGame(*input_stream);
194193
if (!game->IsPerfectRecall()) {
195-
throw Gambit::UndefinedException(
194+
throw UndefinedException(
196195
"Computing equilibria of games with imperfect recall is not supported.");
197196
}
198197

src/tools/enumpure/enumpure.cc

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2121
//
2222

23-
#include <cstdlib>
2423
#include <getopt.h>
2524
#include <iostream>
2625
#include <fstream>
@@ -62,7 +61,7 @@ int main(int argc, char *argv[])
6261
bool printDetail = false;
6362

6463
int long_opt_index = 0;
65-
struct option long_options[] = {
64+
option long_options[] = {
6665
{"help", 0, nullptr, 'h'}, {"version", 0, nullptr, 'v'}, {nullptr, 0, nullptr, 0}};
6766
int c;
6867
while ((c = getopt_long(argc, argv, "DvhqAS", long_options, &long_opt_index)) != -1) {
@@ -87,7 +86,7 @@ int main(int argc, char *argv[])
8786
break;
8887
case '?':
8988
if (isprint(optopt)) {
90-
std::cerr << argv[0] << ": Unknown option `-" << ((char)optopt) << "'.\n";
89+
std::cerr << argv[0] << ": Unknown option `-" << static_cast<char>(optopt) << "'.\n";
9190
}
9291
else {
9392
std::cerr << argv[0] << ": Unknown option character `\\x" << optopt << "`.\n";
@@ -117,22 +116,12 @@ int main(int argc, char *argv[])
117116

118117
try {
119118
const Game game = ReadGame(*input_stream);
120-
std::shared_ptr<StrategyProfileRenderer<Rational>> renderer;
119+
std::shared_ptr<MixedProfileRenderer<Rational>> renderer;
121120
if (reportStrategic || !game->IsTree()) {
122-
if (printDetail) {
123-
renderer = std::make_shared<MixedStrategyDetailRenderer<Rational>>(std::cout);
124-
}
125-
else {
126-
renderer = std::make_shared<MixedStrategyCSVRenderer<Rational>>(std::cout);
127-
}
121+
renderer = MakeMixedStrategyProfileRenderer<Rational>(std::cout, 0, printDetail);
128122
}
129123
else {
130-
if (printDetail) {
131-
renderer = std::make_shared<BehavStrategyDetailRenderer<Rational>>(std::cout);
132-
}
133-
else {
134-
renderer = std::make_shared<BehavStrategyCSVRenderer<Rational>>(std::cout);
135-
}
124+
renderer = MakeMixedBehaviorProfileRenderer<Rational>(std::cout, 0, printDetail);
136125
}
137126

138127
if (game->IsTree() && solveAgent) {

src/tools/gt/nfggnm.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ int main(int argc, char *argv[])
8181
std::string startFile;
8282

8383
int long_opt_index = 0;
84-
struct option long_options[] = {{"help", 0, nullptr, 'h'},
85-
{"version", 0, nullptr, 'v'},
86-
{"verbose", 0, nullptr, 'V'},
87-
{nullptr, 0, nullptr, 0}};
84+
option long_options[] = {{"help", 0, nullptr, 'h'},
85+
{"version", 0, nullptr, 'v'},
86+
{"verbose", 0, nullptr, 'V'},
87+
{nullptr, 0, nullptr, 0}};
8888
int c;
8989
while ((c = getopt_long(argc, argv, "d:n:s:m:f:i:c:qvVhS", long_options, &long_opt_index)) !=
9090
-1) {
@@ -126,7 +126,7 @@ int main(int argc, char *argv[])
126126
break;
127127
case '?':
128128
if (isprint(optopt)) {
129-
std::cerr << argv[0] << ": Unknown option `-" << ((char)optopt) << "'.\n";
129+
std::cerr << argv[0] << ": Unknown option `-" << static_cast<char>(optopt) << "'.\n";
130130
}
131131
else {
132132
std::cerr << argv[0] << ": Unknown option character `\\x" << optopt << "`.\n";
@@ -173,9 +173,7 @@ int main(int argc, char *argv[])
173173

174174
try {
175175
const Game game = ReadGame(*input_stream);
176-
const std::shared_ptr<StrategyProfileRenderer<double>> renderer(
177-
new MixedStrategyCSVRenderer<double>(std::cout, numDecimals));
178-
176+
auto renderer = MakeMixedStrategyProfileRenderer<double>(std::cout, numDecimals, false);
179177
List<MixedStrategyProfile<double>> perts;
180178
if (!startFile.empty()) {
181179
std::ifstream startPerts(startFile.c_str());

src/tools/gt/nfgipa.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int main(int argc, char *argv[])
6767
const std::string startFile;
6868

6969
int long_opt_index = 0;
70-
struct option long_options[] = {
70+
option long_options[] = {
7171
{"help", 0, nullptr, 'h'}, {"version", 0, nullptr, 'v'}, {nullptr, 0, nullptr, 0}};
7272
int c;
7373
while ((c = getopt_long(argc, argv, "d:n:vqh", long_options, &long_opt_index)) != -1) {
@@ -89,7 +89,7 @@ int main(int argc, char *argv[])
8989
break;
9090
case '?':
9191
if (isprint(optopt)) {
92-
std::cerr << argv[0] << ": Unknown option `-" << ((char)optopt) << "'.\n";
92+
std::cerr << argv[0] << ": Unknown option `-" << static_cast<char>(optopt) << "'.\n";
9393
}
9494
else {
9595
std::cerr << argv[0] << ": Unknown option character `\\x" << optopt << "`.\n";
@@ -119,8 +119,7 @@ int main(int argc, char *argv[])
119119

120120
try {
121121
const Game game = ReadGame(*input_stream);
122-
const std::shared_ptr<StrategyProfileRenderer<double>> renderer(
123-
new MixedStrategyCSVRenderer<double>(std::cout, numDecimals));
122+
auto renderer = MakeMixedStrategyProfileRenderer<double>(std::cout, numDecimals, false);
124123

125124
List<MixedStrategyProfile<double>> perts;
126125
if (!startFile.empty()) {

src/tools/lcp/lcp.cc

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
#include <iostream>
2424
#include <fstream>
25-
#include <cstdlib>
2625
#include <memory>
2726
#include <getopt.h>
2827
#include "gambit.h"
@@ -69,7 +68,7 @@ int main(int argc, char *argv[])
6968
int numDecimals = 6, stopAfter = 0, maxDepth = 0;
7069

7170
int long_opt_index = 0;
72-
struct option long_options[] = {
71+
option long_options[] = {
7372
{"help", 0, nullptr, 'h'}, {"version", 0, nullptr, 'v'}, {nullptr, 0, nullptr, 0}};
7473
while ((c = getopt_long(argc, argv, "d:DvhqSe:r:", long_options, &long_opt_index)) != -1) {
7574
switch (c) {
@@ -100,7 +99,7 @@ int main(int argc, char *argv[])
10099
break;
101100
case '?':
102101
if (isprint(optopt)) {
103-
std::cerr << argv[0] << ": Unknown option `-" << ((char)optopt) << "'.\n";
102+
std::cerr << argv[0] << ": Unknown option `-" << static_cast<char>(optopt) << "'.\n";
104103
}
105104
else {
106105
std::cerr << argv[0] << ": Unknown option character `\\x" << optopt << "`.\n";
@@ -132,51 +131,31 @@ int main(int argc, char *argv[])
132131
const Game game = ReadGame(*input_stream);
133132
if (!game->IsTree() || useStrategic) {
134133
if (useFloat) {
135-
std::shared_ptr<StrategyProfileRenderer<double>> renderer;
136-
if (printDetail) {
137-
renderer = std::make_shared<MixedStrategyDetailRenderer<double>>(std::cout, numDecimals);
138-
}
139-
else {
140-
renderer = std::make_shared<MixedStrategyCSVRenderer<double>>(std::cout, numDecimals);
141-
}
134+
auto renderer =
135+
MakeMixedStrategyProfileRenderer<double>(std::cout, numDecimals, printDetail);
142136
LcpStrategySolve<double>(game, stopAfter, maxDepth,
143137
[&](const MixedStrategyProfile<double> &p,
144138
const std::string &label) { renderer->Render(p, label); });
145139
}
146140
else {
147-
std::shared_ptr<StrategyProfileRenderer<Rational>> renderer;
148-
if (printDetail) {
149-
renderer = std::make_shared<MixedStrategyDetailRenderer<Rational>>(std::cout);
150-
}
151-
else {
152-
renderer = std::make_shared<MixedStrategyCSVRenderer<Rational>>(std::cout);
153-
}
141+
auto renderer =
142+
MakeMixedStrategyProfileRenderer<Rational>(std::cout, numDecimals, printDetail);
154143
LcpStrategySolve<Rational>(game, stopAfter, maxDepth,
155144
[&](const MixedStrategyProfile<Rational> &p,
156145
const std::string &label) { renderer->Render(p, label); });
157146
}
158147
}
159148
else {
160149
if (useFloat) {
161-
std::shared_ptr<StrategyProfileRenderer<double>> renderer;
162-
if (printDetail) {
163-
renderer = std::make_shared<BehavStrategyDetailRenderer<double>>(std::cout, numDecimals);
164-
}
165-
else {
166-
renderer = std::make_shared<BehavStrategyCSVRenderer<double>>(std::cout, numDecimals);
167-
}
150+
auto renderer =
151+
MakeMixedBehaviorProfileRenderer<double>(std::cout, numDecimals, printDetail);
168152
LcpBehaviorSolve<double>(game, stopAfter, maxDepth,
169153
[&](const MixedBehaviorProfile<double> &p,
170154
const std::string &label) { renderer->Render(p, label); });
171155
}
172156
else {
173-
std::shared_ptr<StrategyProfileRenderer<Rational>> renderer;
174-
if (printDetail) {
175-
renderer = std::make_shared<BehavStrategyDetailRenderer<Rational>>(std::cout);
176-
}
177-
else {
178-
renderer = std::make_shared<BehavStrategyCSVRenderer<Rational>>(std::cout);
179-
}
157+
auto renderer =
158+
MakeMixedBehaviorProfileRenderer<Rational>(std::cout, numDecimals, printDetail);
180159
LcpBehaviorSolve<Rational>(game, stopAfter, maxDepth,
181160
[&](const MixedBehaviorProfile<Rational> &p,
182161
const std::string &label) { renderer->Render(p, label); });

src/tools/liap/liap.cc

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
#include <iostream>
2424
#include <fstream>
25-
#include <cstdlib>
2625
#include <getopt.h>
2726
#include "gambit.h"
2827
#include "tools/util.h"
@@ -137,10 +136,10 @@ int main(int argc, char *argv[])
137136
std::string startFile;
138137

139138
int long_opt_index = 0;
140-
struct option long_options[] = {{"help", 0, nullptr, 'h'},
141-
{"version", 0, nullptr, 'v'},
142-
{"verbose", 0, nullptr, 'V'},
143-
{nullptr, 0, nullptr, 0}};
139+
option long_options[] = {{"help", 0, nullptr, 'h'},
140+
{"version", 0, nullptr, 'v'},
141+
{"verbose", 0, nullptr, 'V'},
142+
{nullptr, 0, nullptr, 0}};
144143
int c;
145144
while ((c = getopt_long(argc, argv, "d:n:i:s:m:hqVvS", long_options, &long_opt_index)) != -1) {
146145
switch (c) {
@@ -173,7 +172,7 @@ int main(int argc, char *argv[])
173172
break;
174173
case '?':
175174
if (isprint(optopt)) {
176-
std::cerr << argv[0] << ": Unknown option `-" << ((char)optopt) << "'.\n";
175+
std::cerr << argv[0] << ": Unknown option `-" << static_cast<char>(optopt) << "'.\n";
177176
}
178177
else {
179178
std::cerr << argv[0] << ": Unknown option character `\\x" << optopt << "`.\n";
@@ -215,9 +214,7 @@ int main(int argc, char *argv[])
215214
}
216215

217216
for (size_t i = 1; i <= starts.size(); i++) {
218-
const std::shared_ptr<StrategyProfileRenderer<double>> renderer(
219-
new MixedStrategyCSVRenderer<double>(std::cout, numDecimals));
220-
217+
auto renderer = MakeMixedStrategyProfileRenderer<double>(std::cout, numDecimals, false);
221218
LiapStrategySolve(starts[i], maxregret, maxitsN,
222219
[renderer, verbose](const MixedStrategyProfile<double> &p_profile,
223220
const std::string &p_label) {
@@ -239,8 +236,7 @@ int main(int argc, char *argv[])
239236
}
240237

241238
for (size_t i = 1; i <= starts.size(); i++) {
242-
const std::shared_ptr<StrategyProfileRenderer<double>> renderer(
243-
new BehavStrategyCSVRenderer<double>(std::cout, numDecimals));
239+
auto renderer = MakeMixedBehaviorProfileRenderer<double>(std::cout, numDecimals, false);
244240
LiapBehaviorSolve(starts[i], maxregret, maxitsN,
245241
[renderer, verbose](const MixedBehaviorProfile<double> &p_profile,
246242
const std::string &p_label) {

0 commit comments

Comments
 (0)