[10062] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[10067] | 3 | using System.Diagnostics;
|
---|
[10062] | 4 | using System.Linq;
|
---|
[10067] | 5 | using HeuristicLab.Grammars;
|
---|
[10062] | 6 |
|
---|
| 7 | namespace CodeGenerator {
|
---|
[10080] | 8 | public class RandomSearchCodeGen {
|
---|
[10062] | 9 |
|
---|
| 10 | private string solverTemplate = @"
|
---|
| 11 | namespace ?PROBLEMNAME? {
|
---|
| 12 | public sealed class ?IDENT?Solver {
|
---|
[10100] | 13 | private static double baseTerminalProbability = 0.05; // 5% of all samples are only a terminal node
|
---|
| 14 | private static double terminalProbabilityInc = 0.05; // for each level the probability to sample a terminal grows by 5%
|
---|
| 15 |
|
---|
| 16 | public sealed class SolverState : ISolverState {
|
---|
| 17 | private class Tree {
|
---|
| 18 | public int altIdx;
|
---|
| 19 | // public string symbol; // for debugging
|
---|
| 20 | public List<Tree> subtrees;
|
---|
| 21 | public Tree(int state, int altIdx) {
|
---|
| 22 | subtrees = new List<Tree>(subtreeCount[state]);
|
---|
| 23 | this.altIdx = altIdx;
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 | public int curDepth;
|
---|
| 27 | public int steps;
|
---|
[10086] | 28 | public int depth;
|
---|
[10100] | 29 | private readonly Stack<Tree> nodes;
|
---|
| 30 | private readonly IGpdlProblem problem;
|
---|
| 31 |
|
---|
| 32 | private static Dictionary<int, int[]> transition = new Dictionary<int, int[]>() {
|
---|
| 33 | ?TRANSITIONTABLE?
|
---|
| 34 | };
|
---|
| 35 | private static Dictionary<int, int> subtreeCount = new Dictionary<int, int>() {
|
---|
| 36 | { -1, 0 }, // terminals
|
---|
| 37 | ?SUBTREECOUNTTABLE?
|
---|
| 38 | };
|
---|
| 39 | private static string[] symb = new string[] { ?SYMBOLNAMES? };
|
---|
| 40 |
|
---|
| 41 | public SolverState(IGpdlProblem problem, int seed) {
|
---|
| 42 | this.problem = problem;
|
---|
| 43 | this.nodes = new Stack<Tree>();
|
---|
| 44 |
|
---|
| 45 | // create a random tree
|
---|
| 46 | var tree = SampleTree(new Random(seed), 0, -1); // state 0 is the state for the start symbol
|
---|
| 47 | nodes.Push(tree);
|
---|
[10086] | 48 | }
|
---|
[10062] | 49 |
|
---|
[10100] | 50 | public void Reset() {
|
---|
| 51 | // stack must contain only the root of the tree
|
---|
| 52 | System.Diagnostics.Debug.Assert(nodes.Count == 1);
|
---|
[10086] | 53 | }
|
---|
[10100] | 54 |
|
---|
| 55 | private Tree SampleTree(Random random, int state, int altIdx) {
|
---|
| 56 | // Console.Write(state + "" "");
|
---|
| 57 | curDepth += 1;
|
---|
| 58 | steps += 1;
|
---|
| 59 | depth = Math.Max(depth, curDepth);
|
---|
| 60 | var t = new Tree(state, altIdx);
|
---|
| 61 | // t.symbol = symb.Length > state ? symb[state] : ""TERM"";
|
---|
| 62 | // if the symbol has alternatives then we must choose one randomly (only one sub-tree in this case)
|
---|
| 63 | if(subtreeCount[state] == 1) {
|
---|
| 64 | var targetStates = transition[state];
|
---|
| 65 | var i = SampleAlternative(random, state);
|
---|
| 66 | if(targetStates.Length == 0) {
|
---|
| 67 | //terminal
|
---|
| 68 | t.subtrees.Add(SampleTree(random, -1, i));
|
---|
| 69 | } else {
|
---|
| 70 | t.subtrees.Add(SampleTree(random, targetStates[i], i));
|
---|
| 71 | }
|
---|
| 72 | } else {
|
---|
| 73 | // if the symbol contains only one sequence we must use create sub-trees for each symbol in the sequence
|
---|
| 74 | for(int i = 0; i < subtreeCount[state]; i++) {
|
---|
| 75 | t.subtrees.Add(SampleTree(random, transition[state][i], i));
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | curDepth -=1;
|
---|
| 79 | return t;
|
---|
[10086] | 80 | }
|
---|
[10100] | 81 |
|
---|
| 82 | public int PeekNextAlternative() {
|
---|
| 83 | // this must only be called nodes that contain alternatives and therefore must only have single-symbols alternatives
|
---|
| 84 | System.Diagnostics.Debug.Assert(nodes.Peek().subtrees.Count == 1);
|
---|
| 85 | return nodes.Peek().subtrees[0].altIdx;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | public void Follow(int idx) {
|
---|
| 89 | nodes.Push(nodes.Peek().subtrees[idx]);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public void Unwind() {
|
---|
| 93 | nodes.Pop();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | private int SampleAlternative(Random random, int state) {
|
---|
| 97 | switch(state) {
|
---|
| 98 |
|
---|
| 99 | ?SAMPLEALTERNATIVECODE?
|
---|
| 100 |
|
---|
| 101 | default: throw new InvalidOperationException();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | private double TerminalProbForDepth(int depth) {
|
---|
| 106 | return baseTerminalProbability + depth * terminalProbabilityInc;
|
---|
| 107 | }
|
---|
[10086] | 108 | }
|
---|
| 109 |
|
---|
[10062] | 110 | public static void Main(string[] args) {
|
---|
[10100] | 111 | if(args.Length >= 1) ParseArguments(args);
|
---|
| 112 |
|
---|
| 113 | var problem = new ?IDENT?Problem();
|
---|
| 114 | var solver = new ?IDENT?Solver(problem);
|
---|
[10062] | 115 | solver.Start();
|
---|
| 116 | }
|
---|
[10100] | 117 | private static void ParseArguments(string[] args) {
|
---|
| 118 | var baseTerminalProbabilityRegex = new Regex(@""--terminalProbBase=(?<prob>.+)"");
|
---|
| 119 | var terminalProbabilityIncRegex = new Regex(@""--terminalProbInc=(?<prob>.+)"");
|
---|
| 120 | var helpRegex = new Regex(@""--help|/\?"");
|
---|
| 121 |
|
---|
| 122 | foreach(var arg in args) {
|
---|
| 123 | var baseTerminalProbabilityMatch = baseTerminalProbabilityRegex.Match(arg);
|
---|
| 124 | var terminalProbabilityIncMatch = terminalProbabilityIncRegex.Match(arg);
|
---|
| 125 | var helpMatch = helpRegex.Match(arg);
|
---|
| 126 | if(helpMatch.Success) { PrintUsage(); Environment.Exit(0); }
|
---|
| 127 | else if(baseTerminalProbabilityMatch.Success) {
|
---|
| 128 | baseTerminalProbability = double.Parse(baseTerminalProbabilityMatch.Groups[""prob""].Captures[0].Value, System.Globalization.CultureInfo.InvariantCulture);
|
---|
| 129 | if(baseTerminalProbability < 0.0 || baseTerminalProbability > 1.0) throw new ArgumentException(""base terminal probability must lie in range [0.0 ... 1.0]"");
|
---|
| 130 | } else if(terminalProbabilityIncMatch.Success) {
|
---|
| 131 | terminalProbabilityInc = double.Parse(terminalProbabilityIncMatch.Groups[""prob""].Captures[0].Value, System.Globalization.CultureInfo.InvariantCulture);
|
---|
| 132 | if(terminalProbabilityInc < 0.0 || terminalProbabilityInc > 1.0) throw new ArgumentException(""terminal probability increment must lie in range [0.0 ... 1.0]"");
|
---|
| 133 | } else {
|
---|
| 134 | Console.WriteLine(""Unknown switch {0}"", arg); PrintUsage(); Environment.Exit(0);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
[10062] | 137 | }
|
---|
[10100] | 138 | private static void PrintUsage() {
|
---|
| 139 | Console.WriteLine(""Find a solution using random tree search."");
|
---|
| 140 | Console.WriteLine();
|
---|
| 141 | Console.WriteLine(""Parameters:"");
|
---|
| 142 | Console.WriteLine(""\t--terminalProbBase=<prob>\tSets the probability of sampling a terminal alternative in a rule [Default: 0.05]"");
|
---|
| 143 | Console.WriteLine(""\t--terminalProbInc=<prob>\tSets the increment for the probability of sampling a terminal alternative for each level in the syntax tree [Default: 0.05]"");
|
---|
[10062] | 144 | }
|
---|
| 145 |
|
---|
[10100] | 146 |
|
---|
| 147 | private readonly ?IDENT?Problem problem;
|
---|
| 148 | public ?IDENT?Solver(?IDENT?Problem problem) {
|
---|
| 149 | this.problem = problem;
|
---|
[10086] | 150 | }
|
---|
[10067] | 151 |
|
---|
[10062] | 152 | private void Start() {
|
---|
[10086] | 153 | var seedRandom = new Random();
|
---|
[10062] | 154 | var bestF = ?MAXIMIZATION? ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
[10067] | 155 | int n = 0;
|
---|
[10100] | 156 | long sumDepth = 0;
|
---|
| 157 | long sumSize = 0;
|
---|
| 158 | var sumF = 0.0;
|
---|
[10074] | 159 | var sw = new System.Diagnostics.Stopwatch();
|
---|
| 160 | sw.Start();
|
---|
[10080] | 161 | while (true) {
|
---|
[10074] | 162 |
|
---|
[10080] | 163 | // must make sure that calling the start-symbol multiple times in the fitness function always leads to the same path through the grammar
|
---|
| 164 | // so we use a PRNG for generating seeds for a separate PRNG that is reset each time the start symbol is called
|
---|
[10086] | 165 |
|
---|
[10100] | 166 | var _state = new SolverState(problem, seedRandom.Next());
|
---|
[10086] | 167 |
|
---|
[10100] | 168 | var f = problem.Evaluate(_state);
|
---|
| 169 |
|
---|
[10067] | 170 | n++;
|
---|
[10100] | 171 | sumSize += _state.steps;
|
---|
| 172 | sumDepth += _state.depth;
|
---|
| 173 | sumF += f;
|
---|
[10086] | 174 | if (IsBetter(f, bestF)) {
|
---|
[10100] | 175 | // evaluate again with tracing to console
|
---|
| 176 | // problem.Evaluate(new SolverState(_state.seed, true));
|
---|
[10086] | 177 | bestF = f;
|
---|
[10100] | 178 | Console.WriteLine(""{0}\t{1}\t(size={2}, depth={3})"", n, bestF, _state.steps, _state.depth);
|
---|
[10086] | 179 | }
|
---|
| 180 | if (n % 1000 == 0) {
|
---|
[10074] | 181 | sw.Stop();
|
---|
[10100] | 182 | Console.WriteLine(""{0}\tbest: {1:0.000}\t(avg: {2:0.000})\t(avg size: {3:0.0})\t(avg. depth: {4:0.0})\t({5:0.00} sols/ms)"", n, bestF, sumF/1000.0, sumSize/1000.0, sumDepth/1000.0, 1000.0 / sw.ElapsedMilliseconds);
|
---|
[10074] | 183 | sw.Reset();
|
---|
[10100] | 184 | sumSize = 0;
|
---|
| 185 | sumDepth = 0;
|
---|
| 186 | sumF = 0.0;
|
---|
[10074] | 187 | sw.Start();
|
---|
| 188 | }
|
---|
[10062] | 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[10100] | 192 | private bool IsBetter(double a, double b) {
|
---|
| 193 | return ?MAXIMIZATION? ? a > b : a < b;
|
---|
[10062] | 194 | }
|
---|
| 195 | }
|
---|
| 196 | }";
|
---|
| 197 |
|
---|
[10100] | 198 | public void Generate(IGrammar grammar, bool maximization, IEnumerable<SymbolNode> terminalSymbols, SourceBuilder problemSourceCode) {
|
---|
| 199 | var solverSourceCode = new SourceBuilder();
|
---|
| 200 | solverSourceCode.Append(solverTemplate)
|
---|
| 201 | .Replace("?MAXIMIZATION?", maximization.ToString().ToLowerInvariant())
|
---|
| 202 | .Replace("?SYMBOLNAMES?", grammar.Symbols.Select(s => s.Name).Aggregate(string.Empty, (str, symb) => str + "\"" + symb + "\", "))
|
---|
| 203 | .Replace("?TRANSITIONTABLE?", GenerateTransitionTable(grammar))
|
---|
| 204 | .Replace("?SUBTREECOUNTTABLE?", GenerateSubtreeCountTable(grammar))
|
---|
| 205 | .Replace("?SAMPLEALTERNATIVECODE?", GenerateSampleAlternativeSource(grammar))
|
---|
| 206 | ;
|
---|
[10062] | 207 |
|
---|
[10100] | 208 | problemSourceCode.Append(solverSourceCode.ToString());
|
---|
[10062] | 209 | }
|
---|
| 210 |
|
---|
| 211 |
|
---|
| 212 |
|
---|
[10100] | 213 | private string GenerateTransitionTable(IGrammar grammar) {
|
---|
| 214 | Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol));
|
---|
| 215 | var sb = new SourceBuilder();
|
---|
[10086] | 216 |
|
---|
[10100] | 217 | // state idx = idx of the corresponding symbol in the grammar
|
---|
| 218 | var allSymbols = grammar.Symbols.ToList();
|
---|
| 219 | var attributes = new List<string>();
|
---|
| 220 | foreach (var s in grammar.Symbols) {
|
---|
| 221 | var targetStates = new List<int>();
|
---|
| 222 | if (grammar.IsTerminal(s)) {
|
---|
| 223 | foreach (var att in s.Attributes) {
|
---|
| 224 | targetStates.Add(allSymbols.Count + attributes.Count);
|
---|
| 225 | attributes.Add(s.Name + "_" + att);
|
---|
| 226 | }
|
---|
| 227 | } else {
|
---|
| 228 | if (grammar.NumberOfAlternatives(s) > 1) {
|
---|
| 229 | foreach (var alt in grammar.GetAlternatives(s)) {
|
---|
| 230 | // only single-symbol alternatives are supported
|
---|
| 231 | Debug.Assert(alt.Count() == 1);
|
---|
| 232 | targetStates.Add(allSymbols.IndexOf(alt.Single()));
|
---|
| 233 | }
|
---|
| 234 | } else {
|
---|
| 235 | // rule is a sequence of symbols
|
---|
| 236 | var seq = grammar.GetAlternatives(s).Single();
|
---|
| 237 | targetStates.AddRange(seq.Select(symb => allSymbols.IndexOf(symb)));
|
---|
| 238 | }
|
---|
[10067] | 239 | }
|
---|
| 240 |
|
---|
[10100] | 241 | var targetStateString = targetStates.Aggregate(string.Empty, (str, state) => str + state + ", ");
|
---|
[10067] | 242 |
|
---|
[10100] | 243 | var idxOfSourceState = allSymbols.IndexOf(s);
|
---|
| 244 | sb.AppendFormat("// {0}", s).AppendLine();
|
---|
| 245 | sb.AppendFormat("{{ {0} , new int[] {{ {1} }} }},", idxOfSourceState, targetStateString).AppendLine();
|
---|
[10067] | 246 | }
|
---|
[10100] | 247 | for (int attIdx = 0; attIdx < attributes.Count; attIdx++) {
|
---|
| 248 | sb.AppendFormat("// {0}", attributes[attIdx]).AppendLine();
|
---|
| 249 | sb.AppendFormat("{{ {0} , new int[] {{ }} }},", attIdx + allSymbols.Count).AppendLine();
|
---|
| 250 | }
|
---|
| 251 | return sb.ToString();
|
---|
[10067] | 252 | }
|
---|
[10100] | 253 | private string GenerateSubtreeCountTable(IGrammar grammar) {
|
---|
| 254 | Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol));
|
---|
| 255 | var sb = new SourceBuilder();
|
---|
[10067] | 256 |
|
---|
[10100] | 257 | // state idx = idx of the corresponding symbol in the grammar
|
---|
| 258 | var allSymbols = grammar.Symbols.ToList();
|
---|
| 259 | var attributes = new List<string>();
|
---|
| 260 | foreach (var s in grammar.Symbols) {
|
---|
| 261 | int subtreeCount;
|
---|
| 262 | if (grammar.IsTerminal(s)) {
|
---|
| 263 | subtreeCount = s.Attributes.Count();
|
---|
| 264 | attributes.AddRange(s.Attributes.Select(att => s.Name + "_" + att.Name));
|
---|
| 265 | } else {
|
---|
| 266 | if (grammar.NumberOfAlternatives(s) > 1) {
|
---|
| 267 | Debug.Assert(grammar.GetAlternatives(s).All(alt => alt.Count() == 1));
|
---|
| 268 | subtreeCount = 1;
|
---|
| 269 | } else {
|
---|
| 270 | subtreeCount = grammar.GetAlternative(s, 0).Count();
|
---|
| 271 | }
|
---|
[10067] | 272 | }
|
---|
[10100] | 273 |
|
---|
| 274 | sb.AppendFormat("// {0}", s).AppendLine();
|
---|
| 275 | sb.AppendFormat("{{ {0} , {1} }},", allSymbols.IndexOf(s), subtreeCount).AppendLine();
|
---|
[10067] | 276 | }
|
---|
| 277 |
|
---|
[10100] | 278 | for (int attIdx = 0; attIdx < attributes.Count; attIdx++) {
|
---|
| 279 | sb.AppendFormat("// {0}", attributes[attIdx]).AppendLine();
|
---|
| 280 | sb.AppendFormat("{{ {0} , 1 }},", attIdx + allSymbols.Count).AppendLine();
|
---|
[10062] | 281 | }
|
---|
| 282 | return sb.ToString();
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[10100] | 285 | private string GenerateSampleAlternativeSource(IGrammar grammar) {
|
---|
| 286 | Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol));
|
---|
| 287 | var sb = new SourceBuilder();
|
---|
| 288 | int stateCount = 0;
|
---|
| 289 | var attributes = new List<Tuple<string, string>>();
|
---|
| 290 | foreach (var s in grammar.Symbols) {
|
---|
| 291 | sb.AppendFormat("case {0}: ", stateCount++);
|
---|
| 292 | if (grammar.IsTerminal(s)) {
|
---|
| 293 | GenerateReturnStatement(s.Attributes.Count(), sb);
|
---|
| 294 | attributes.AddRange(s.Attributes.Select(att => Tuple.Create(s.Name, att.Name)));
|
---|
| 295 | } else {
|
---|
| 296 | var terminalAltIndexes = grammar.GetAlternatives(s)
|
---|
| 297 | .Select((alt, idx) => new { alt, idx })
|
---|
| 298 | .Where((p) => p.alt.All(symb => grammar.IsTerminal(symb)))
|
---|
| 299 | .Select(p => p.idx);
|
---|
| 300 | var nonTerminalAltIndexes = grammar.GetAlternatives(s)
|
---|
| 301 | .Select((alt, idx) => new { alt, idx })
|
---|
| 302 | .Where((p) => p.alt.Any(symb => grammar.IsNonTerminal(symb)))
|
---|
| 303 | .Select(p => p.idx);
|
---|
| 304 | var hasTerminalAlts = terminalAltIndexes.Any();
|
---|
| 305 | var hasNonTerminalAlts = nonTerminalAltIndexes.Any();
|
---|
| 306 | if (hasTerminalAlts && hasNonTerminalAlts) {
|
---|
| 307 | sb.Append("if(random.NextDouble() < TerminalProbForDepth(depth)) {").BeginBlock();
|
---|
| 308 | GenerateReturnStatement(terminalAltIndexes, sb);
|
---|
| 309 | sb.Append("} else {");
|
---|
| 310 | GenerateReturnStatement(nonTerminalAltIndexes, sb);
|
---|
| 311 | sb.Append("}").EndBlock();
|
---|
| 312 | } else {
|
---|
| 313 | GenerateReturnStatement(grammar.NumberOfAlternatives(s), sb);
|
---|
| 314 | }
|
---|
[10062] | 315 | }
|
---|
| 316 | }
|
---|
[10100] | 317 | for (int attIdx = 0; attIdx < attributes.Count; attIdx++) {
|
---|
| 318 | var terminalName = attributes[attIdx].Item1;
|
---|
| 319 | var attributeName = attributes[attIdx].Item2;
|
---|
| 320 | sb.AppendFormat("case {0}: return random.Next(problem.GetCardinality(\"{1}\", \"{2}\"));", attIdx + stateCount, terminalName, attributeName).AppendLine();
|
---|
[10062] | 321 | }
|
---|
| 322 | return sb.ToString();
|
---|
| 323 | }
|
---|
[10100] | 324 | private void GenerateReturnStatement(IEnumerable<int> idxs, SourceBuilder sb) {
|
---|
| 325 | if (idxs.Count() == 1) {
|
---|
| 326 | sb.AppendFormat("return {0};", idxs.Single()).AppendLine();
|
---|
[10062] | 327 | } else {
|
---|
[10100] | 328 | var idxStr = idxs.Aggregate(string.Empty, (str, idx) => str + idx + ", ");
|
---|
| 329 | sb.AppendFormat("return new int[] {{ {0} }}[random.Next({1})]; ", idxStr, idxs.Count()).AppendLine();
|
---|
[10062] | 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[10100] | 333 | private void GenerateReturnStatement(int nAlts, SourceBuilder sb) {
|
---|
| 334 | if (nAlts > 1) {
|
---|
| 335 | sb.AppendFormat("return random.Next({0});", nAlts).AppendLine();
|
---|
| 336 | } else if (nAlts == 1) {
|
---|
| 337 | sb.AppendLine("return 0; ");
|
---|
[10067] | 338 | } else {
|
---|
[10100] | 339 | sb.AppendLine("throw new InvalidProgramException();");
|
---|
[10062] | 340 | }
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 | }
|
---|