All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
make-problems.cc
Go to the documentation of this file.
4 #include "osl/record/csaRecord.h"
6 
7 #include <boost/program_options.hpp>
8 #include <boost/scoped_ptr.hpp>
9 #include <boost/foreach.hpp>
10 #include <boost/progress.hpp>
11 #include <sstream>
12 #include <iostream>
13 #include <iomanip>
14 #include <fstream>
15 
16 namespace po = boost::program_options;
19 void run(const std::string& filename);
20 int main(int argc, char **argv)
21 {
22  po::options_description options("options");
23  options.add_options()
24  ("help", "produce help message")
25  ("maximum-nodes,M",
26  po::value<size_t>(&max_nodes)->default_value(80000),
27  "search proof/disproof positions within this limit")
28  ("min-nodes,m",
29  po::value<size_t>(&min_nodes)->default_value(8000),
30  "ignore positions proven/disproven by search with less than this limit")
31  ("proof,p",
32  po::value<bool>(&search_proof)->default_value(1),
33  "search proof/disproof problems")
34  ("file-number,n",
35  po::value<size_t>(&filenumber)->default_value(1),
36  "start number of filenames for generated problems")
37  ;
38  po::options_description hidden("Hidden options");
39  hidden.add_options()
40  ("target-file", po::value<std::vector<std::string> >());
41  po::options_description command_line_options;
42  command_line_options.add(options).add(hidden);
43  po::options_description visible_options("All options");
44  visible_options.add(options);
45 
46  po::positional_options_description p;
47  p.add("target-file", -1);
48 
49  po::variables_map vm;
50  std::vector<std::string> filenames;
51 
52  try {
53  po::store(po::command_line_parser(argc, argv).
54  options(command_line_options).positional(p).run(), vm);
55  notify(vm);
56  if (vm.count("help")) {
57  std::cerr << "Usage: " << argv[0] << " [options] files" << std::endl;
58  std::cout << visible_options << std::endl;
59  return 0;
60  }
61  filenames = vm["target-file"].as<std::vector<std::string> >();
62  }
63  catch (std::exception& e) {
64  std::cerr << "error in parsing options" << std::endl
65  << e.what() << std::endl;
66  std::cerr << "Usage: " << argv[0] << " [options] files" << std::endl;
67  std::cerr << visible_options << std::endl;
68  return 1;
69  }
70  boost::progress_display progress(filenames.size());
71  BOOST_FOREACH(const std::string& filename, filenames) {
72  run(filename);
73  ++progress;
74  }
75 }
76 
77 using namespace osl;
78 std::string write_file(const NumEffectState& state, Move move, size_t count)
79 {
80  std::ostringstream ss;
81  ss << std::setw(4) << std::setfill('0') << filenumber++ << ".csa";
82  std::ofstream os(ss.str().c_str());
83  os << state;
84  if (search_proof)
85  os << record::csa::show(move) << "\n";
86  os << "' " << count << " nodes\n";
87  return ss.str();
88 }
89 bool find_problem(DualDfpn& dfpn, NumEffectState& state)
90 {
91  HashKey key(state);
92  PathEncoding path(state.turn());
93  Move win_move;
94  const size_t before = dfpn.totalNodeCount();
95  ProofDisproof pdp = dfpn.findProof(max_nodes, state, key, path, win_move);
96  const size_t after = dfpn.totalNodeCount();
97  if ((search_proof && !pdp.isCheckmateSuccess())
98  || (!search_proof && !pdp.isCheckmateFail())
99  || after-before < min_nodes)
100  return false;
101  write_file(state, win_move, after-before);
102  return true;
103 }
104 void run(const std::string& filename)
105 {
106  CsaFile file(filename);
107  const vector<Move> moves=file.getRecord().getMoves();
108  NumEffectState state;
109  DualDfpn dfpn;
110  size_t moved = 0;
111  BOOST_FOREACH(Move move, moves) {
112  state.makeMove(move);
113  if (++moved < 50 || state.inCheck())
114  continue;
115  if (search_proof) {
116  MoveVector legal_moves;
117  LegalMoves::generate(state, legal_moves);
118  std::random_shuffle(legal_moves.begin(), legal_moves.end());
119  BOOST_FOREACH(Move a, legal_moves) {
120  NumEffectState copy(state);
121  copy.makeMove(a);
122  if (copy.inCheck())
123  continue;
124  copy.makeMove(Move::PASS(copy.turn()));
125  if (find_problem(dfpn, copy))
126  return;
127  }
128  }
129  else {
130  DualDfpn dfpn;
131  if (find_problem(dfpn, state))
132  return;
133  }
134  }
135 }
136 
137 
138 /* ------------------------------------------------------------------------- */
139 // ;;; Local Variables:
140 // ;;; mode:c++
141 // ;;; c-basic-offset:2
142 // ;;; End: