using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using HeuristicLab.Algorithms.Bandits; using HeuristicLab.Common; using HeuristicLab.Problems.GrammaticalOptimization; namespace HeuristicLab.Algorithms.GrammaticalOptimization { public class MctsSampler { private class TreeNode { public string ident; public int randomTries; public IBanditPolicyActionInfo actionInfo; public TreeNode parent; public TreeNode[] children; public bool done = false; public TreeNode(string id, TreeNode parent) { this.ident = id; this.parent = parent; } public override string ToString() { return string.Format("Node({0} tries: {1}, done: {2}, policy: {3})", ident, actionInfo.Tries, done, actionInfo); } } public event Action FoundNewBestSolution; public event Action SolutionEvaluated; private readonly int maxLen; private readonly IProblem problem; private readonly Random random; private readonly int randomTries; private readonly IBanditPolicy policy; private TreeNode lastNode; // the bottom node in one episode private TreeNode rootNode; public int treeDepth; public int treeSize; private double bestQuality; public MctsSampler(IProblem problem, int maxLen, Random random, int randomTries, IBanditPolicy policy) { this.maxLen = maxLen; this.problem = problem; this.random = random; this.randomTries = randomTries; this.policy = policy; } public void Run(int maxIterations) { bestQuality = double.MinValue; InitPolicies(problem.Grammar); for (int i = 0; !rootNode.done && i < maxIterations; i++) { var sentence = SampleSentence(problem.Grammar).ToString(); var quality = problem.Evaluate(sentence) / problem.BestKnownQuality(maxLen); Debug.Assert(quality >= 0 && quality <= 1.0); DistributeReward(quality); RaiseSolutionEvaluated(sentence, quality); if (quality > bestQuality) { bestQuality = quality; RaiseFoundNewBestSolution(sentence, quality); } } // clean up InitPolicies(problem.Grammar); GC.Collect(); } public void PrintStats() { var n = rootNode; Console.WriteLine("depth: {0,5} size: {1,10} root tries {2,10}, rootQ {3:F3}, bestQ {4:F3}", treeDepth, treeSize, n.actionInfo.Tries, n.actionInfo.Value, bestQuality); while (n.children != null) { Console.WriteLine("{0,-30}", n.ident); double maxVForRow = n.children.Select(ch => ch.actionInfo.Value).Max(); if (maxVForRow == 0) maxVForRow = 1.0; for (int i = 0; i < n.children.Length; i++) { var ch = n.children[i]; SetColorForChild(ch, maxVForRow); Console.Write("{0,5}", ch.ident); } Console.WriteLine(); for (int i = 0; i < n.children.Length; i++) { var ch = n.children[i]; SetColorForChild(ch, maxVForRow); Console.Write("{0,5:F2}", ch.actionInfo.Value * 10); } Console.WriteLine(); for (int i = 0; i < n.children.Length; i++) { var ch = n.children[i]; SetColorForChild(ch, maxVForRow); Console.Write("{0,5}", ch.done ? "X" : ch.actionInfo.Tries.ToString()); } Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(); //n.policy.PrintStats(); //n = n.children.Where(ch => !ch.done).OrderByDescending(c => c.actionInfo.Value).First(); n = n.children.Where(ch=>!ch.done).OrderByDescending(c => c.actionInfo.Value).First(); } Console.WriteLine("-----------------------"); } private void SetColorForChild(TreeNode ch, double maxVForRow) { //if (ch.done) Console.ForegroundColor = ConsoleColor.White; //else Console.ForegroundColor = ConsoleEx.ColorForValue(ch.actionInfo.Value / maxVForRow); } private void InitPolicies(IGrammar grammar) { rootNode = new TreeNode(grammar.SentenceSymbol.ToString(), null); rootNode.actionInfo = policy.CreateActionInfo(); treeDepth = 0; treeSize = 0; } private Sequence SampleSentence(IGrammar grammar) { lastNode = null; var startPhrase = new Sequence(grammar.SentenceSymbol); //var startPhrase = new Sequence("a*b+c*d+e*f+E"); return CompleteSentence(grammar, startPhrase); } private Sequence CompleteSentence(IGrammar g, Sequence phrase) { if (phrase.Length > maxLen) throw new ArgumentException(); if (g.MinPhraseLength(phrase) > maxLen) throw new ArgumentException(); TreeNode n = rootNode; var curDepth = 0; while (!phrase.IsTerminal) { if (n.randomTries < randomTries) { n.randomTries++; treeDepth = Math.Max(treeDepth, curDepth); lastNode = n; return g.CompleteSentenceRandomly(random, phrase, maxLen); } else { char nt = phrase.FirstNonTerminal; int maxLenOfReplacement = maxLen - (phrase.Length - 1); // replacing aAb with maxLen 4 means we can only use alternatives with a minPhraseLen <= 2 Debug.Assert(maxLenOfReplacement > 0); var alts = g.GetAlternatives(nt).Where(alt => g.MinPhraseLength(alt) <= maxLenOfReplacement); if (n.randomTries == randomTries && n.children == null) { n.children = alts.Select(alt => new TreeNode(alt.ToString(), n)).ToArray(); // create a new node for each alternative foreach (var ch in n.children) ch.actionInfo = policy.CreateActionInfo(); treeSize += n.children.Length; } // => select using bandit policy int selectedAltIdx = policy.SelectAction(random, n.children.Select(c => c.actionInfo)); Sequence selectedAlt = alts.ElementAt(selectedAltIdx); // replace nt with alt phrase.ReplaceAt(phrase.FirstNonTerminalIndex, 1, selectedAlt); curDepth++; // prepare for next iteration n = n.children[selectedAltIdx]; } } // while lastNode = n; // the last node is a leaf node (sentence is done), so we never need to visit this node again n.done = true; treeDepth = Math.Max(treeDepth, curDepth); return phrase; } private void DistributeReward(double reward) { // iterate in reverse order (bottom up) var node = lastNode; while (node != null) { if (node.done) node.actionInfo.Disable(reward); if (node.children != null && node.children.All(c => c.done)) { node.done = true; var bestActionValue = node.children.Select(c => c.actionInfo.Value).Max(); node.actionInfo.Disable(bestActionValue); } if (!node.done) { node.actionInfo.UpdateReward(reward); } node = node.parent; } } private void RaiseSolutionEvaluated(string sentence, double quality) { var handler = SolutionEvaluated; if (handler != null) handler(sentence, quality); } private void RaiseFoundNewBestSolution(string sentence, double quality) { var handler = FoundNewBestSolution; if (handler != null) handler(sentence, quality); } } }