all-states.cc
Go to the documentation of this file.
00001 #include "osl/state/numEffectState.h"
00002 #include "osl/hash/hashKey.h"
00003 
00004 #include <boost/program_options.hpp>
00005 #include "osl/record/kisen.h"
00006 #include "osl/record/csa.h"
00007 #include "osl/stl/hash_map.h"
00008 
00009 #include <iostream>
00010 #include <fstream>
00011 #include <sstream>
00012 
00013 struct hash
00014 {
00015   unsigned long operator() (const osl::state::SimpleState &state) const
00016   {
00017     return osl::hash::HashKey(state).signature();
00018   }
00019 };
00020 
00021 struct equalKey
00022 {
00023   bool operator() (const osl::state::SimpleState &s1, const osl::state::SimpleState &s2) const
00024   {
00025     return s1 == s2;
00026   }
00027 };
00028 
00029 struct State
00030 {
00031   State() : count(0)
00032   {
00033   }
00034   int count;
00035   osl::stl::vector<osl::Move> moves;
00036 };
00037 
00038 void find_all(const int num_ply, const int threshold,
00039               bool save, const std::vector<std::string> &files)
00040 {
00041   osl::stl::hash_map<osl::state::SimpleState, State, hash, equalKey> states;
00042 
00043   for (size_t index = 0; index < files.size(); index++)
00044   {
00045     osl::record::KisenFile kisen(files[index]);
00046     for (size_t i = 0; i < kisen.size(); i++)
00047     {
00048       const osl::vector<osl::Move> moves = kisen.getMoves(i);
00049       osl::state::NumEffectState state(kisen.getInitialState());
00050 
00051       size_t j = 0;
00052       for (; j < moves.size() && (int)j < num_ply; j++)
00053       {
00054         const osl::Square opKingSquare 
00055           = state.kingSquare(alt(state.turn()));
00056         if (state.hasEffectAt(state.turn(), opKingSquare))
00057         {
00058           break;
00059         }
00060         state.makeMove(moves[j]);
00061       }
00062       if ((int)j == num_ply)
00063       {
00064         osl::stl::hash_map<osl::state::SimpleState, State, hash, equalKey>::iterator it = states.find(state);
00065         if (it != states.end())
00066         {
00067           (it->second.count)++;
00068         }
00069         else
00070         {
00071           State s;
00072           s.count = 1;
00073           for (int k = 0; k < num_ply; k++)
00074           {
00075             s.moves.push_back(moves[k]);
00076           }
00077           states[state] = s;
00078         }
00079       }
00080     }
00081   }
00082 
00083   int index = 1;
00084   for (osl::stl::hash_map<osl::state::SimpleState, State, hash, equalKey>::const_iterator it = states.begin();
00085        it != states.end();
00086        ++it)
00087   {
00088     if (it->second.count >= threshold)
00089     {
00090       std::cout << index << " (" << it->second.count << ")" << std::endl;
00091       std::cout << it->first;
00092       std::ofstream output;
00093       if (save)
00094       {
00095         std::ostringstream oss(std::ostringstream::out);
00096         oss << index << ".csa";
00097         const std::string &filename = oss.str();
00098         output.open(filename.c_str());
00099         output << "PI" << std::endl
00100                << "+" << std::endl;
00101       }
00102       const osl::stl::vector<osl::Move> &moves = it->second.moves;
00103       for (size_t i = 0; i < moves.size(); i++)
00104       {
00105         std::cout << osl::record::csa::show(moves[i]) << " ";
00106         if (save)
00107         {
00108           output << osl::record::csa::show(moves[i]) << std::endl;
00109         }
00110       }
00111       if (save)
00112       {
00113         output.close();
00114       }
00115       std::cout << std::endl;
00116       index++;
00117     }
00118   }
00119 }
00120 
00121 int main(int argc, char **argv)
00122 {
00123   int num_ply;
00124   int threshold;
00125   bool save_moves;
00126   boost::program_options::options_description command_line_options;
00127   command_line_options.add_options()
00128     ("num-ply",
00129      boost::program_options::value<int>(&num_ply)->default_value(10),
00130      "Show states after this number of plies are played")
00131     ("threshold",
00132      boost::program_options::value<int>(&threshold)->default_value(10),
00133      "Each state must appear this number of times to be shown")
00134     ("save",
00135      boost::program_options::value<bool>(&save_moves)->default_value(false),
00136      "Save moves leading to states to files in CSA format")
00137     ("input-file", boost::program_options::value< std::vector<std::string> >(),
00138      "input files in kisen format")
00139     ("help", "Show help message");
00140   boost::program_options::variables_map vm;
00141   boost::program_options::positional_options_description p;
00142   p.add("input-file", -1);
00143 
00144   try
00145   {
00146     boost::program_options::store(
00147       boost::program_options::command_line_parser(
00148         argc, argv).options(command_line_options).positional(p).run(), vm);
00149     boost::program_options::notify(vm);
00150     if (vm.count("help"))
00151     {
00152       std::cerr << "Usage: " << argv[0] << " [options] kisen-file"
00153                 << std::endl;
00154       std::cout << command_line_options << std::endl;
00155       return 0;
00156     }
00157   }
00158   catch (std::exception &e)
00159   {
00160     std::cerr << "error in parsing options" << std::endl
00161               << e.what() << std::endl;
00162     std::cerr << "Usage: " << argv[0] << " [options] kisen-file" << std::endl;
00163     std::cerr << command_line_options << std::endl;
00164     return 1;
00165   }
00166 
00167   const std::vector<std::string> files =
00168     vm["input-file"].as< std::vector<std::string> >();
00169   find_all(num_ply, threshold, save_moves, files);
00170 
00171   return 0;
00172 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines