Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs @ 10338

Last change on this file since 10338 was 10338, checked in by gkronber, 10 years ago

#2026 changing the way terminal symbols are handled (work in progress)

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