make-problems.cc
Go to the documentation of this file.
00001 #include "osl/checkmate/dualDfpn.h"
00002 #include "osl/checkmate/proofDisproof.h"
00003 #include "osl/move_generator/legalMoves.h"
00004 #include "osl/record/csaRecord.h"
00005 #include "osl/state/numEffectState.h"
00006 
00007 #include <boost/program_options.hpp>
00008 #include <boost/scoped_ptr.hpp>
00009 #include <boost/foreach.hpp>
00010 #include <boost/progress.hpp>
00011 #include <sstream>
00012 #include <iostream>
00013 #include <iomanip>
00014 #include <fstream>
00015 
00016 namespace po = boost::program_options;
00017 size_t max_nodes, min_nodes, filenumber;
00018 bool search_proof;
00019 void run(const std::string& filename);
00020 int main(int argc, char **argv)
00021 {
00022   po::options_description options("options");
00023   options.add_options()
00024     ("help", "produce help message")
00025     ("maximum-nodes,M",
00026      po::value<size_t>(&max_nodes)->default_value(80000),
00027      "search proof/disproof positions within this limit")
00028     ("min-nodes,m",
00029      po::value<size_t>(&min_nodes)->default_value(8000),
00030      "ignore positions proven/disproven by search with less than this limit")
00031     ("proof,p",
00032      po::value<bool>(&search_proof)->default_value(1),
00033      "search proof/disproof problems")
00034     ("file-number,n",
00035      po::value<size_t>(&filenumber)->default_value(1),
00036      "start number of filenames for generated problems")
00037     ;
00038   po::options_description hidden("Hidden options");
00039   hidden.add_options()
00040     ("target-file", po::value<std::vector<std::string> >());
00041   po::options_description command_line_options;
00042   command_line_options.add(options).add(hidden);
00043   po::options_description visible_options("All options");
00044   visible_options.add(options);
00045 
00046   po::positional_options_description p;
00047   p.add("target-file", -1);
00048 
00049   po::variables_map vm;
00050   std::vector<std::string> filenames;
00051 
00052   try {
00053     po::store(po::command_line_parser(argc, argv).
00054               options(command_line_options).positional(p).run(), vm);
00055     notify(vm);
00056     if (vm.count("help")) {
00057       std::cerr << "Usage: " << argv[0] << " [options] files" << std::endl;
00058       std::cout << visible_options << std::endl;
00059       return 0;
00060     }
00061     filenames = vm["target-file"].as<std::vector<std::string> >();
00062   }
00063   catch (std::exception& e) {
00064     std::cerr << "error in parsing options" << std::endl
00065               << e.what() << std::endl;
00066     std::cerr << "Usage: " << argv[0] << " [options] files" << std::endl;
00067     std::cerr << visible_options << std::endl;
00068     return 1;
00069   }
00070   boost::progress_display progress(filenames.size());
00071   BOOST_FOREACH(const std::string& filename, filenames) {
00072     run(filename);
00073     ++progress;
00074   }
00075 }
00076 
00077 using namespace osl;
00078 std::string write_file(const NumEffectState& state, Move move, size_t count)
00079 {
00080   std::ostringstream ss;
00081   ss << std::setw(4) << std::setfill('0') << filenumber++ << ".csa";
00082   std::ofstream os(ss.str().c_str());
00083   os << state;
00084   if (search_proof)
00085     os << record::csa::show(move) << "\n";
00086   os << "' " << count << " nodes\n";
00087   return ss.str();
00088 }
00089 bool find_problem(DualDfpn& dfpn, NumEffectState& state)
00090 {
00091   HashKey key(state);
00092   PathEncoding path(state.turn());
00093   Move win_move;
00094   const size_t before = dfpn.totalNodeCount();
00095   ProofDisproof pdp = dfpn.findProof(max_nodes, state, key, path, win_move);
00096   const size_t after = dfpn.totalNodeCount();
00097   if ((search_proof && !pdp.isCheckmateSuccess())
00098       || (!search_proof && !pdp.isCheckmateFail())
00099       || after-before < min_nodes)
00100     return false;
00101   write_file(state, win_move, after-before);
00102   return true;
00103 }
00104 void run(const std::string& filename)
00105 {
00106   CsaFile file(filename);
00107   const vector<Move> moves=file.getRecord().getMoves();
00108   NumEffectState state;
00109   DualDfpn dfpn;
00110   size_t moved = 0;
00111   BOOST_FOREACH(Move move, moves) {
00112     state.makeMove(move);
00113     if (++moved < 50 || state.inCheck())
00114       continue;
00115     if (search_proof) {
00116       MoveVector legal_moves;
00117       LegalMoves::generate(state, legal_moves);
00118       std::random_shuffle(legal_moves.begin(), legal_moves.end());
00119       BOOST_FOREACH(Move a, legal_moves) {
00120         NumEffectState copy(state);
00121         copy.makeMove(a);
00122         if (copy.inCheck())
00123           continue;
00124         copy.makeMove(Move::PASS(copy.turn()));
00125         if (find_problem(dfpn, copy))
00126           return;
00127       }
00128     }
00129     else {
00130       DualDfpn dfpn;
00131       if (find_problem(dfpn, state))
00132         return;
00133     }
00134   }
00135 }
00136 
00137 
00138 /* ------------------------------------------------------------------------- */
00139 // ;;; Local Variables:
00140 // ;;; mode:c++
00141 // ;;; c-basic-offset:2
00142 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines