Changeset 12762 for branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch
- Timestamp:
- 07/14/15 20:42:55 (9 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/Base/TreeNode.cs
r12503 r12762 16 16 public List<TreeNode> children; 17 17 public IBanditPolicyActionInfo actionInfo; 18 public int level; 18 19 19 public TreeNode(TreeNode parent, string phrase, IBanditPolicyActionInfo actionInfo )20 public TreeNode(TreeNode parent, string phrase, IBanditPolicyActionInfo actionInfo, int level) 20 21 { 21 22 this.parent = parent; 22 23 this.phrase = phrase; 23 24 this.actionInfo = actionInfo; 25 this.level = level; 24 26 } 25 27 public bool IsLeaf() -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/HeuristicLab.Algorithms.MonteCarloTreeSearch.csproj
r12098 r12762 33 33 <Reference Include="System" /> 34 34 <Reference Include="System.Core" /> 35 <Reference Include="System.Drawing" /> 35 36 <Reference Include="System.Xml.Linq" /> 36 37 <Reference Include="System.Data.DataSetExtensions" /> … … 40 41 </ItemGroup> 41 42 <ItemGroup> 43 <Compile Include="Base\TreeInfos.cs" /> 42 44 <Compile Include="Simulation\ISimulation.cs" /> 43 45 <Compile Include="MonteCarloTreeSearch.cs" /> … … 47 49 </ItemGroup> 48 50 <ItemGroup> 51 <ProjectReference Include="..\GraphVizWrapper\GraphVizWrapper.csproj"> 52 <Project>{cfec60dc-14e0-47e4-a60e-8919fb5fef5d}</Project> 53 <Name>GraphVizWrapper</Name> 54 </ProjectReference> 49 55 <ProjectReference Include="..\HeuristicLab.Algorithms.Bandits\HeuristicLab.Algorithms.Bandits.csproj"> 50 56 <Project>{24408f7d-ee0f-4886-a08b-ec324d662e47}</Project> -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/MonteCarloTreeSearch.cs
r12503 r12762 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Drawing; 3 4 using System.Linq; 5 using System.Net.Mail; 6 using System.Text; 7 using System.Threading; 8 using GraphVizWrapper; 9 using GraphVizWrapper.Commands; 10 using GraphVizWrapper.Queries; 4 11 using HeuristicLab.Algorithms.Bandits; 5 12 using HeuristicLab.Algorithms.GrammaticalOptimization; … … 20 27 private readonly ISimulation simulation; 21 28 private TreeNode rootNode; 22 23 public MonteCarloTreeSearch(IProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy, ISimulation simulationPolicy) 29 private bool isPaused = false; 30 private object pauseLock = new object(); 31 32 public MonteCarloTreeSearch(IProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy, 33 ISimulation simulationPolicy) 24 34 { 25 35 this.problem = problem; … … 31 41 } 32 42 33 public bool StopRequested 34 { 35 get; 36 set; 43 public bool StopRequested { get; set; } 44 45 public bool IsPaused 46 { 47 get { return this.isPaused; } 37 48 } 38 49 … … 42 53 for (int i = 0; !StopRequested && i < maxIterations; i++) 43 54 { 55 lock (pauseLock) 56 { 57 if (isPaused) 58 { 59 Monitor.Wait(pauseLock); 60 } 61 } 44 62 TreeNode currentNode = rootNode; 45 63 … … 53 71 string phrase = currentNode.phrase; 54 72 55 if (!grammar.IsTerminal(phrase) && phrase.Length <= maxLen) 56 { 73 if (phrase.Length <= maxLen) 74 { 75 // Version 2: 76 if (currentNode.children != null && !currentNode.children.Any()) 77 { 78 // already removed all child nodes so remove it too.. 79 currentNode.parent.children.Remove(currentNode); 80 continue; 81 } 57 82 ExpandTreeNode(currentNode); 58 59 currentNode = 60 currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos())]; 83 if (currentNode.children.Any()) 84 { 85 currentNode = 86 currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos()) 87 ]; 88 } 89 else 90 { 91 // Version 2: 92 // remove currentNode from tree.. 93 currentNode.parent.children.Remove(currentNode); 94 } 61 95 } 62 96 if (currentNode.phrase.Length <= maxLen) 63 97 { 64 double quality = simulation.Simulate(currentNode); 65 OnSolutionEvaluated(phrase, quality); 98 string simulatedPhrase; 99 double quality = simulation.Simulate(currentNode, out simulatedPhrase); 100 OnSolutionEvaluated(simulatedPhrase, quality); 66 101 67 102 Propagate(currentNode, quality); 68 } 69 } 103 104 } 105 } 106 } 107 108 private bool HasNonTerminal(string phrase) 109 { 110 foreach (char symbol in phrase) 111 { 112 if (grammar.IsNonTerminal(symbol)) 113 { 114 return true; 115 } 116 } 117 118 return false; 70 119 } 71 120 … … 91 140 if (newSequence.Length <= maxLen) 92 141 { 93 TreeNode childNode = new TreeNode(treeNode, newSequence.ToString(), behaviourPolicy.CreateActionInfo()); 142 TreeNode childNode = new TreeNode(treeNode, newSequence.ToString(), 143 behaviourPolicy.CreateActionInfo(), treeNode.level + 1); 94 144 treeNode.children.Add(childNode); 95 145 } … … 104 154 StopRequested = false; 105 155 bestQuality = 0.0; 106 rootNode = new TreeNode(null, grammar.SentenceSymbol.ToString(), behaviourPolicy.CreateActionInfo() );156 rootNode = new TreeNode(null, grammar.SentenceSymbol.ToString(), behaviourPolicy.CreateActionInfo(), 0); 107 157 } 108 158 … … 117 167 } 118 168 119 public void PrintStats() 120 { 121 //Console.WriteLine("depth: {0,5} tries: {1,5} best phrase {2,50} bestQ {3:F3}", maxSearchDepth, tries, bestPhrase, bestQuality); 122 123 //// use behaviour strategy to generate the currently prefered sentence 124 //var policy = behaviourPolicy; 125 126 //var n = rootNode; 127 128 //while (n != null) 129 //{ 130 // var phrase = n.phrase; 131 // Console.ForegroundColor = ConsoleColor.White; 132 // Console.WriteLine("{0,-30}", phrase); 133 // var children = n.children; 134 // if (children == null || !children.Any()) break; 135 // var values = children.Select(ch => policy.GetValue(ch.phrase)); 136 // var maxValue = values.Max(); 137 // if (maxValue == 0) maxValue = 1.0; 138 139 // // write phrases 140 // foreach (var ch in children) 141 // { 142 // SetColorForValue(policy.GetValue(ch.phrase) / maxValue); 143 // Console.Write(" {0,-4}", ch.phrase.Substring(Math.Max(0, ch.phrase.Length - 3), Math.Min(3, ch.phrase.Length))); 144 // } 145 // Console.WriteLine(); 146 147 // // write values 148 // foreach (var ch in children) 149 // { 150 // SetColorForValue(policy.GetValue(ch.phrase) / maxValue); 151 // Console.Write(" {0:F2}", policy.GetValue(ch.phrase) * 10.0); 152 // } 153 // Console.WriteLine(); 154 155 // // write tries 156 // foreach (var ch in children) 157 // { 158 // SetColorForValue(policy.GetValue(ch.phrase) / maxValue); 159 // Console.Write(" {0,4}", policy.GetTries(ch.phrase)); 160 // } 161 // Console.WriteLine(); 162 // int selectedChildIdx; 163 // if (!policy.TrySelect(random, phrase, children.Select(ch => ch.phrase), out selectedChildIdx)) 164 // { 165 // break; 166 // } 167 // n = n.children[selectedChildIdx]; 168 //} 169 170 //Console.ForegroundColor = ConsoleColor.White; 171 //Console.WriteLine("-------------------"); 172 } 173 174 private void SetColorForValue(double v) 175 { 176 Console.ForegroundColor = ConsoleEx.ColorForValue(v); 169 public TreeInfos GetTreeInfos() 170 { 171 int totalNodes = 0; 172 int unexpandedNodes = 0; 173 int expandedNodes = 0; 174 int leaveNodes = 0; 175 int deepestLevel = 0; 176 int totalLevel = 0; 177 int totalChildren = 0; 178 179 List<TreeNode> toDoNodes = new List<TreeNode>(); 180 if (rootNode != null) 181 { 182 toDoNodes.Add(rootNode); 183 } 184 while (toDoNodes.Any()) 185 { 186 TreeNode currentNode = toDoNodes[0]; 187 toDoNodes.RemoveAt(0); 188 totalNodes++; 189 if (currentNode.level > deepestLevel) 190 { 191 deepestLevel = currentNode.level; 192 } 193 totalLevel += currentNode.level; 194 195 if (currentNode.children != null) 196 { 197 totalChildren += currentNode.children.Count; 198 toDoNodes.AddRange(currentNode.children); 199 expandedNodes++; 200 } 201 else 202 { 203 if (!HasNonTerminal(currentNode.phrase)) 204 { 205 leaveNodes++; 206 } 207 else 208 { 209 unexpandedNodes++; 210 } 211 } 212 } 213 return new TreeInfos(totalNodes, unexpandedNodes, expandedNodes, leaveNodes, deepestLevel, 214 totalLevel / totalNodes, totalChildren / expandedNodes); 215 } 216 217 public byte[] GenerateSvg() 218 { 219 IGetStartProcessQuery getStartProcessQuery = new GetStartProcessQuery(); 220 IGetProcessStartInfoQuery getProcessStartInfoQuery = new GetProcessStartInfoQuery(); 221 IRegisterLayoutPluginCommand registerLayoutPluginCommand = 222 new RegisterLayoutPluginCommand(getProcessStartInfoQuery, getStartProcessQuery); 223 224 GraphGeneration wrapper = new GraphGeneration(getStartProcessQuery, 225 getProcessStartInfoQuery, 226 registerLayoutPluginCommand); 227 wrapper.GraphvizPath = @"../../../Graphviz2.38/bin"; 228 StringBuilder dotFile = new StringBuilder("digraph {"); 229 dotFile.AppendLine(); 230 dotFile.AppendLine("splines=ortho;"); 231 dotFile.AppendLine("concentrate=true;"); 232 dotFile.AppendLine("ranksep=1.2;"); 233 234 List<TreeNode> toDoNodes = new List<TreeNode>(); 235 if (rootNode != null) 236 { 237 toDoNodes.Add(rootNode); 238 // declare node 239 string hexColor = GetHexNodeColor(Color.White, Color.OrangeRed, rootNode.actionInfo.Value); 240 241 dotFile.AppendLine(string.Format("{0} [label=\"{1}\\n{2:0.00}/{3}\", style=filled, fillcolor=\"{4}\"]", 242 rootNode.GetHashCode(), 243 rootNode.phrase, rootNode.actionInfo.Value, rootNode.actionInfo.Tries, hexColor)); 244 } 245 246 // to put nodes on the same level in graph 247 Dictionary<int, List<TreeNode>> levelMap = new Dictionary<int, List<TreeNode>>(); 248 249 List<TreeNode> sameLevelNodes; 250 251 while (toDoNodes.Any()) 252 { 253 TreeNode currentNode = toDoNodes[0]; 254 toDoNodes.RemoveAt(0); 255 // put currentNode into levelMap 256 if (levelMap.TryGetValue(currentNode.level, out sameLevelNodes)) 257 { 258 sameLevelNodes.Add(currentNode); 259 } 260 else 261 { 262 sameLevelNodes = new List<TreeNode>(); 263 sameLevelNodes.Add(currentNode); 264 levelMap.Add(currentNode.level, sameLevelNodes); 265 } 266 267 // draw line from current node to all its children 268 if (currentNode.children != null) 269 { 270 foreach (TreeNode childNode in currentNode.children) 271 { 272 toDoNodes.Add(childNode); 273 // declare node 274 275 string hexColor = GetHexNodeColor(Color.White, Color.OrangeRed, childNode.actionInfo.Value); 276 dotFile.AppendLine( 277 string.Format("{0} [label=\"{1}\\n{2:0.00}/{3}\", style=filled, fillcolor=\"{4}\"]", 278 childNode.GetHashCode(), 279 childNode.phrase, childNode.actionInfo.Value, childNode.actionInfo.Tries, hexColor)); 280 // add edge 281 dotFile.AppendLine(string.Format("{0} -> {1}", currentNode.GetHashCode(), 282 childNode.GetHashCode())); 283 } 284 } 285 } 286 287 // set same level ranks.. 288 foreach (KeyValuePair<int, List<TreeNode>> entry in levelMap) 289 { 290 dotFile.Append("{rank = same;"); 291 foreach (TreeNode node in entry.Value) 292 { 293 dotFile.Append(string.Format(" {0};", node.GetHashCode())); 294 } 295 dotFile.AppendLine("}"); 296 } 297 298 dotFile.Append(" }"); 299 byte[] output = wrapper.GenerateGraph(dotFile.ToString(), Enums.GraphReturnType.Svg); 300 return output; 301 } 302 303 private String HexConverter(Color c) 304 { 305 return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); 306 } 307 308 private String GetHexNodeColor(Color weakColor, Color strongColor, double quality) 309 { 310 // convert quality to value between 0 and 1 311 double bestKnownQuality = problem.BestKnownQuality(this.maxLen); 312 double q = quality / bestKnownQuality; 313 314 // calculate difference between colors 315 byte rDiff = (byte)Math.Abs(weakColor.R - strongColor.R); 316 byte bDiff = (byte)Math.Abs(weakColor.B - strongColor.B); 317 byte gDiff = (byte)Math.Abs(weakColor.G - strongColor.G); 318 319 byte newR = weakColor.R > strongColor.R 320 ? Convert.ToByte(weakColor.R - Math.Round(rDiff * q)) 321 : Convert.ToByte(weakColor.R + Math.Round(rDiff * q)); 322 323 byte newB = weakColor.B > strongColor.B 324 ? Convert.ToByte(weakColor.B - Math.Round(bDiff * q)) 325 : Convert.ToByte(weakColor.B + Math.Round(bDiff * q)); 326 327 byte newG = weakColor.G > strongColor.G 328 ? Convert.ToByte(weakColor.G - Math.Round(gDiff * q)) 329 : Convert.ToByte(weakColor.G + Math.Round(gDiff * q)); 330 331 return HexConverter(Color.FromArgb(newR, newG, newB)); 332 } 333 334 public void PauseContinue() 335 { 336 lock (pauseLock) 337 { 338 if (isPaused) 339 { 340 isPaused = false; 341 Monitor.Pulse(pauseLock); 342 } 343 else 344 { 345 isPaused = true; 346 } 347 } 177 348 } 178 349 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/Simulation/ISimulation.cs
r12098 r12762 10 10 public interface ISimulation 11 11 { 12 double Simulate(TreeNode node );12 double Simulate(TreeNode node, out string simulatedPhrase); 13 13 } 14 14 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/Simulation/RandomSimulation.cs
r12098 r12762 22 22 } 23 23 24 public double Simulate(TreeNode node )24 public double Simulate(TreeNode node, out string simulatedPhrase) 25 25 { 26 26 Sequence sequence = new Sequence(node.phrase); … … 48 48 } 49 49 } 50 simulatedPhrase = sequence.ToString(); 50 51 return problem.Evaluate(sequence.ToString()); 51 52 }
Note: See TracChangeset
for help on using the changeset viewer.