openingBookConverter.cc
Go to the documentation of this file.
00001 #include "openingBookConverter.h"
00002 
00003 #include "osl/state/simpleState.h"
00004 #include "osl/stl/vector.h"
00005 #include "osl/record/compactBoard.h"
00006 #include "osl/record/record.h"
00007 #include <iostream>
00008 
00009 OpeningBookConverter::OpeningBookConverter(const char *filename)
00010 {
00011   std::ifstream ifs(filename);
00012   int nStates = osl::record::readInt(ifs);
00013   states.reserve(nStates);
00014   for (int i = 0 ; i < nStates; i++)
00015   {
00016     int blackWin = osl::record::readInt(ifs);
00017     int whiteWin = osl::record::readInt(ifs);
00018     int nMove = osl::record::readInt(ifs);
00019     int index = osl::record::readInt(ifs);
00020     states.push_back(OBState(index, nMove, blackWin, whiteWin));
00021   }
00022   while (true)
00023   {
00024     osl::Move move=osl::Move::makeDirect(osl::record::readInt(ifs));
00025     int stateIndex=osl::record::readInt(ifs);
00026     if (!ifs)
00027       break;
00028     moves.push_back(osl::record::opening::OBMove(move,stateIndex));
00029   }
00030 }
00031 
00032 void
00033 OpeningBookConverter::write(const char* filename)
00034 {
00035   std::ofstream ofs(filename);
00036   osl::record::writeInt(ofs, states.size());
00037   for (osl::vector<OBState>::const_iterator it = states.begin();
00038        it != states.end(); ++it)
00039   {
00040     osl::record::writeInt(ofs, it->getOBMoveIndex());
00041     osl::record::writeInt(ofs, it->getNOBMove());
00042     osl::record::writeInt(ofs, it->getBlackWinCount());
00043     osl::record::writeInt(ofs, it->getWhiteWinCount());
00044   }
00045   for (osl::vector<osl::record::opening::OBMove>::const_iterator it = moves.begin();
00046        it != moves.end(); ++it)
00047   {
00048     osl::record::writeInt(ofs, it->getMove().intValue());
00049     osl::record::writeInt(ofs, it->getStateIndex());
00050   }
00051 }
00052 
00053 void
00054 OpeningBookConverter::writeInNewFormat(std::ofstream& ofs)
00055 {
00056   osl::vector<int> weights(moves.size());
00057   osl::record::writeInt(ofs, 1);                // version number
00058   osl::record::writeInt(ofs, states.size());
00059   osl::record::writeInt(ofs, moves.size());
00060   osl::record::writeInt(ofs, 0);                // Start state index
00061   for (osl::vector<OBState>::const_iterator it = states.begin();
00062        it != states.end(); ++it)
00063   {
00064     osl::record::writeInt(ofs, it->getOBMoveIndex());
00065     osl::record::writeInt(ofs, it->getNOBMove());
00066     int total_wins = 0;
00067     osl::vector<int> wins;
00068     wins.reserve(it->getNOBMove());
00069     for (int i = 0; i < it->getNOBMove(); i++)
00070     {
00071       const osl::record::opening::OBMove& move
00072         = moves.at(i + it->getOBMoveIndex());
00073 
00074       const OBState& state = states.at(move.getStateIndex());
00075       if (move.getMove().player() == osl::BLACK)
00076         wins.push_back(state.getBlackWinCount());
00077       else
00078         wins.push_back(state.getWhiteWinCount());
00079 
00080       total_wins += wins.at(i);
00081     }
00082     for (int i = 0; i < it->getNOBMove(); i++)
00083     {
00084       if (total_wins != 0)
00085         weights.at(i + it->getOBMoveIndex()) = (wins.at(i) * 10000 / total_wins);
00086       else
00087         weights.at(i + it->getOBMoveIndex()) = 0;
00088     }
00089     osl::record::writeInt(ofs, it->getBlackWinCount());
00090     osl::record::writeInt(ofs, it->getWhiteWinCount());
00091   }
00092   int i = 0;
00093   for (osl::vector<osl::record::opening::OBMove>::const_iterator it = moves.begin();
00094        it != moves.end(); ++it, ++i)
00095   {
00096     osl::record::opening::WMove wmove(it->getMove(),
00097                                       it->getStateIndex(),
00098                                       weights.at(i));
00099     ofs << wmove;
00100   }
00101 }
00102 
00103 void
00104 OpeningBookConverter::writeInNewFormat(const char* filename)
00105 {
00106   std::ofstream ofs(filename);
00107   writeInNewFormat(ofs);
00108 }
00109 
00110 void
00111 OpeningBookConverter::writeInNewEditFormat(const char* filename)
00112 {
00113   std::ofstream ofs(filename);
00114   writeInNewFormat(ofs);
00115   osl::vector<osl::SimpleState> simpleStates(states.size());
00116   osl::vector<bool> visited(states.size());
00117   osl::vector<int> toTraceIndex;
00118 
00119   for (unsigned int i = 0; i < states.size(); i++)
00120     visited[i] = false;
00121 
00122   assert(states.size() >= 1);
00123 
00124   // First entry is assmed to be the start state.
00125   toTraceIndex.push_back(0);
00126   simpleStates[0] = osl::SimpleState(osl::HIRATE);
00127   visited[0] = true;
00128 
00129   while (!toTraceIndex.empty())
00130   {
00131     const int index = toTraceIndex.back();
00132     toTraceIndex.pop_back();
00133     const OBState& s = states.at(index);
00134     const int moveIndex = s.getOBMoveIndex();
00135     for (int i = 0; i < s.getNOBMove(); i++)
00136     {
00137       const osl::record::opening::OBMove& m = moves.at(moveIndex + i);
00138       const int nextState = m.getStateIndex();
00139       if (!visited[nextState])
00140       {
00141         toTraceIndex.push_back(nextState);
00142         osl::NumEffectState newState(simpleStates[index]);
00143         newState.makeMove(m.getMove());
00144         simpleStates[nextState] = newState;
00145         visited[nextState] = true;
00146       }
00147     }
00148   }
00149   for (unsigned int i = 0; i < states.size(); i++)
00150   {
00151     osl::record::CompactBoard board(simpleStates[i]);
00152     ofs << board;
00153   }
00154 }
00155 // ;;; Local Variables:
00156 // ;;; mode:c++
00157 // ;;; c-basic-offset:2
00158 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines