00001 //---------------------------------------------------------------------------- 00002 /** @file SgUctTreeUtil.cpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include "SgSystem.h" 00007 #include "SgUctTreeUtil.h" 00008 00009 #include <iomanip> 00010 #include "SgUctSearch.h" 00011 #include "SgWrite.h" 00012 00013 using namespace std; 00014 00015 //---------------------------------------------------------------------------- 00016 00017 SgUctTreeStatistics::SgUctTreeStatistics() 00018 { 00019 Clear(); 00020 } 00021 00022 void SgUctTreeStatistics::Clear() 00023 { 00024 m_nuNodes = 0; 00025 for (size_t i = 0; i < MAX_MOVECOUNT; ++i) 00026 m_moveCounts[i] = 0; 00027 m_biasRave.Clear(); 00028 } 00029 00030 void SgUctTreeStatistics::Compute(const SgUctTree& tree) 00031 { 00032 Clear(); 00033 for (SgUctTreeIterator it(tree); it; ++it) 00034 { 00035 const SgUctNode& node = *it; 00036 ++m_nuNodes; 00037 size_t count = node.MoveCount(); 00038 if (count < SgUctTreeStatistics::MAX_MOVECOUNT) 00039 ++m_moveCounts[count]; 00040 if (! node.HasChildren()) 00041 continue; 00042 for (SgUctChildIterator it(tree, node); it; ++it) 00043 { 00044 const SgUctNode& child = *it; 00045 if (child.HasRaveValue() && child.HasMean()) 00046 { 00047 float childValue = SgUctSearch::InverseEval(child.Mean()); 00048 float biasRave = child.RaveValue() - childValue; 00049 m_biasRave.Add(biasRave); 00050 } 00051 } 00052 } 00053 } 00054 00055 void SgUctTreeStatistics::Write(ostream& out) const 00056 { 00057 out << SgWriteLabel("NuNodes") << m_nuNodes << '\n'; 00058 for (size_t i = 0; i < MAX_MOVECOUNT; ++i) 00059 { 00060 ostringstream label; 00061 label << "MoveCount[" << i << ']'; 00062 int percent = m_moveCounts[i] * 100 / m_nuNodes; 00063 out << SgWriteLabel(label.str()) << setw(2) << right << percent 00064 << "%\n"; 00065 } 00066 out << SgWriteLabel("BiasRave"); 00067 m_biasRave.Write(out); 00068 out << '\n'; 00069 } 00070 00071 std::ostream& operator<<(ostream& out, const SgUctTreeStatistics& stat) 00072 { 00073 stat.Write(out); 00074 return out; 00075 } 00076 00077 //---------------------------------------------------------------------------- 00078 00079 void SgUctTreeUtil::ExtractSubtree(const SgUctTree& tree, SgUctTree& target, 00080 const std::vector<SgMove>& sequence, 00081 bool warnTruncate, double maxTime) 00082 { 00083 target.Clear(); 00084 const SgUctNode* node = &tree.Root(); 00085 for (vector<SgMove>::const_iterator it = sequence.begin(); 00086 it != sequence.end(); ++it) 00087 { 00088 SgMove mv = *it; 00089 node = SgUctTreeUtil::FindChildWithMove(tree, *node, mv); 00090 if (node == 0) 00091 return; 00092 } 00093 tree.ExtractSubtree(target, *node, warnTruncate, maxTime); 00094 } 00095 00096 const SgUctNode* SgUctTreeUtil::FindChildWithMove(const SgUctTree& tree, 00097 const SgUctNode& node, 00098 SgMove move) 00099 { 00100 if (! node.HasChildren()) 00101 return 0; 00102 for (SgUctChildIterator it(tree, node); it; ++it) 00103 { 00104 const SgUctNode& child = *it; 00105 if (child.Move() == move) 00106 return &child; 00107 } 00108 return 0; 00109 } 00110 00111 //----------------------------------------------------------------------------