Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10149 was 10100, checked in by gkronber, 11 years ago

#2026: major refactoring of example GPDL solver (random search complete except for RANGE constrained terminals)

File size: 14.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using HeuristicLab.Grammars;
6
7namespace CodeGenerator {
8  public class RandomSearchCodeGen {
9
10    private string solverTemplate = @"
11namespace ?PROBLEMNAME? {
12  public sealed class ?IDENT?Solver {
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;
28      public int depth;
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);
48      }
49
50      public void Reset() {
51        // stack must contain only the root of the tree
52        System.Diagnostics.Debug.Assert(nodes.Count == 1);
53      }
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;
80      }
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      }
108    }
109
110    public static void Main(string[] args) {
111      if(args.Length >= 1) ParseArguments(args);
112
113      var problem = new ?IDENT?Problem();
114      var solver = new ?IDENT?Solver(problem);
115      solver.Start();
116    }
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      }
137    }
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]"");
144    }
145
146
147    private readonly ?IDENT?Problem problem;
148    public ?IDENT?Solver(?IDENT?Problem problem) {
149      this.problem = problem;
150    }
151
152    private void Start() {
153      var seedRandom = new Random();
154      var bestF = ?MAXIMIZATION? ? double.NegativeInfinity : double.PositiveInfinity;
155      int n = 0;
156      long sumDepth = 0;
157      long sumSize = 0;
158      var sumF = 0.0;
159      var sw = new System.Diagnostics.Stopwatch();
160      sw.Start();
161      while (true) {
162
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
165       
166        var _state = new SolverState(problem, seedRandom.Next());
167
168        var f = problem.Evaluate(_state);
169 
170        n++;
171        sumSize += _state.steps;
172        sumDepth += _state.depth;
173        sumF += f;
174        if (IsBetter(f, bestF)) {
175          // evaluate again with tracing to console
176          // problem.Evaluate(new SolverState(_state.seed, true));
177          bestF = f;
178          Console.WriteLine(""{0}\t{1}\t(size={2}, depth={3})"", n, bestF, _state.steps, _state.depth);
179        }
180        if (n % 1000 == 0) {
181          sw.Stop();
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);
183          sw.Reset();
184          sumSize = 0;
185          sumDepth = 0;
186          sumF = 0.0;
187          sw.Start();
188        }
189      }
190    }
191
192    private bool IsBetter(double a, double b) {
193      return ?MAXIMIZATION? ? a > b : a < b;
194    }
195  }
196}";
197
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      ;
207
208      problemSourceCode.Append(solverSourceCode.ToString());
209    }
210
211
212
213    private string GenerateTransitionTable(IGrammar grammar) {
214      Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol));
215      var sb = new SourceBuilder();
216
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          }
239        }
240
241        var targetStateString = targetStates.Aggregate(string.Empty, (str, state) => str + state + ", ");
242
243        var idxOfSourceState = allSymbols.IndexOf(s);
244        sb.AppendFormat("// {0}", s).AppendLine();
245        sb.AppendFormat("{{ {0} , new int[] {{ {1} }} }},", idxOfSourceState, targetStateString).AppendLine();
246      }
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();
252    }
253    private string GenerateSubtreeCountTable(IGrammar grammar) {
254      Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol));
255      var sb = new SourceBuilder();
256
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          }
272        }
273
274        sb.AppendFormat("// {0}", s).AppendLine();
275        sb.AppendFormat("{{ {0} , {1} }},", allSymbols.IndexOf(s), subtreeCount).AppendLine();
276      }
277
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();
281      }
282      return sb.ToString();
283    }
284
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          }
315        }
316      }
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();
321      }
322      return sb.ToString();
323    }
324    private void GenerateReturnStatement(IEnumerable<int> idxs, SourceBuilder sb) {
325      if (idxs.Count() == 1) {
326        sb.AppendFormat("return {0};", idxs.Single()).AppendLine();
327      } else {
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();
330      }
331    }
332
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; ");
338      } else {
339        sb.AppendLine("throw new InvalidProgramException();");
340      }
341    }
342  }
343}
Note: See TracBrowser for help on using the repository browser.