topn.cc
Go to the documentation of this file.
00001 /* probability.cc
00002  */
00003 #include "osl/rating/featureSet.h"
00004 #include "osl/rating/ratingEnv.h"
00005 #include "osl/effect_util/effectUtil.h"
00006 #include "osl/state/numEffectState.h"
00007 #include "osl/stl/vector.h"
00008 #include "osl/record/csa.h"
00009 #include "osl/record/csaRecord.h"
00010 #include "osl/record/kisen.h"
00011 #include "osl/progress/effect5x3.h"
00012 #include "osl/stat/average.h"
00013 #include "osl/stat/histogram.h"
00014 #include <boost/program_options.hpp>
00015 #include <iostream>
00016 #include <cmath>
00017 using namespace osl;
00018 using namespace osl::rating;
00019 namespace po = boost::program_options;
00020 
00021 size_t num_kisen, opening_skip, target_limit;
00022 
00023 void run(NumEffectState& state, const vector<Move>& moves);
00024 void show_statistics();
00025 
00026 int main(int argc, char **argv)
00027 {
00028   std::string kisen_filename;
00029   std::vector<std::string> filenames;
00030   po::options_description options("Options");
00031   options.add_options()
00032     ("csa-file", 
00033      po::value<std::vector<std::string> >(),
00034      "csa filename")
00035     ("kisen,k", 
00036      po::value<std::string>(&kisen_filename),
00037      "kisen filename")
00038     ("num-kisen", 
00039      po::value<size_t>(&num_kisen)->default_value(0),
00040      "number of records in kisen to be processed")
00041     ("target-limit", 
00042      po::value<size_t>(&target_limit)->default_value(1000),
00043      "ignore moves whose log-probability is greater than this threshold")
00044     ("opening-skip", 
00045      po::value<size_t>(&opening_skip)->default_value(20),
00046      "number of opening moves ignored in analysis");
00047   po::variables_map vm;
00048   po::positional_options_description p;
00049   p.add("csa-file", -1);
00050   try
00051   {
00052     po::store(po::command_line_parser(argc, argv).
00053               options(options).positional(p).run(), vm);
00054     notify(vm);
00055     if (vm.count("help")) {
00056       std::cout << options << std::endl;
00057       return 0;
00058     }
00059     if (vm.count("csa-file"))
00060       filenames = vm["csa-file"].as<std::vector<std::string> >();
00061   }
00062   catch (std::exception& e)
00063   {
00064     std::cerr << "error in parsing options" << std::endl
00065               << e.what() << std::endl;
00066     std::cerr << options << std::endl;
00067     throw;
00068   }
00069   if (kisen_filename != "") {
00070     std::cerr << "kisen " << kisen_filename << "\n";
00071     KisenFile kisen_file(kisen_filename.c_str());
00072     if (num_kisen == 0)
00073       num_kisen = kisen_file.size();
00074     for (size_t i=0; i<num_kisen; i++) {
00075       if (i % 16 == 0) 
00076         std::cerr << '.';
00077       NumEffectState state(kisen_file.getInitialState());
00078       const osl::vector<Move> moves = kisen_file.getMoves(i);
00079       run(state, moves);
00080     }
00081   }
00082   for (size_t i=0; i<filenames.size(); ++i) {
00083     if (i % 16 == 0) 
00084       std::cerr << '.';
00085     CsaFile file(filenames[i]);
00086     NumEffectState state(file.getInitialState());
00087     const osl::vector<Move> moves = file.getRecord().getMoves();
00088     run(state, moves);
00089   }
00090   std::cerr << "\n";
00091   show_statistics();
00092 }
00093 
00094 CArray<stat::Average,8> top_rated, active;
00095 
00096 void show(const NumEffectState& state, Move next)
00097 {
00098   static const StandardFeatureSet& feature_set = StandardFeatureSet::instance();
00099   MoveLogProbVector moves;
00100   RatingEnv env;
00101   env.make(state);
00102 
00103   feature_set.generateLogProb(state, env, 2000, moves);
00104   const MoveLogProb *rm = moves.find(next);
00105   if (! rm)
00106     return;
00107   int index = rm - &*moves.begin();
00108   for (size_t i=0; i<top_rated.size(); ++i) {
00109     if (i >= moves.size())
00110       break;
00111     bool a = moves[i].logProb() <= (int)target_limit;
00112     active[i].add(a);
00113     if (! a)
00114       continue;
00115     bool found = index == (int)i;
00116     top_rated[i].add(found);
00117   }
00118 }
00119 
00120 void run(NumEffectState& state, const vector<Move>& moves)
00121 {
00122   for (size_t i=0; i<moves.size(); ++i) {
00123     if (state.inCheck(alt(state.turn())))
00124       break;                    // illegal
00125 
00126     const Move move = moves[i];
00127     if (i >= opening_skip)
00128       show(state, move);
00129     state.makeMove(move);
00130   }
00131 }
00132 
00133 void show_statistics()
00134 {
00135   for (size_t i=0; i<top_rated.size(); ++i)
00136     std::cout << "top " << i 
00137               << " " << top_rated[i].getAverage()*100.0 
00138               << " " << active[i].getAverage()*100.0 
00139               << "\n";
00140 }
00141 
00142 
00143 // ;;; Local Variables:
00144 // ;;; mode:c++
00145 // ;;; c-basic-offset:2
00146 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines