Changeset 10385 for branches/HeuristicLab.Problems.GPDL/CodeGenerator
- Timestamp:
- 01/22/14 18:22:09 (11 years ago)
- Location:
- branches/HeuristicLab.Problems.GPDL/CodeGenerator
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs
r10384 r10385 18 18 private const string problemTemplate = @" 19 19 namespace ?PROBLEMNAME? { 20 #region class definitions for tree 20 21 public class Tree { 21 22 public int altIdx; … … 31 32 32 33 ?TERMINALNODECLASSDEFINITIONS? 33 34 // generic interface for communication from problem interpretation to solver 35 // public interface ISolverState { 36 // void Reset(); 37 // int PeekNextAlternative(); // in alternative nodes returns the index of the alternative that should be followed 38 // void Follow(int idx); // next: derive the NT symbol with index=idx; 39 // void Unwind(); // finished with deriving the NT symbol 40 // } 34 #endregion 41 35 42 36 public sealed class ?IDENT?Problem { … … 221 215 foreach (var constr in terminal.Constraints) { 222 216 if (constr.Type == ConstraintNodeType.Set) { 223 sb.Append Line("{").BeginBlock();217 sb.Append("{").BeginBlock(); 224 218 sb.AppendFormat("var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine(); 225 219 sb.AppendFormat("{0} = elements[random.Next(elements.Length)]; ", constr.Ident).AppendLine(); 226 sb.Append Line("}").EndBlock();220 sb.Append("}").EndBlock(); 227 221 } else { 228 sb.Append Line("{").BeginBlock();222 sb.Append("{").BeginBlock(); 229 223 sb.AppendFormat(" var max = problem.GetMax{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); 230 224 sb.AppendFormat(" var min = problem.GetMin{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); 231 sb.AppendFormat("{0} = random.NextDouble() * (max - min) + min;", constr.Ident). AppendLine();232 sb.Append Line("}").EndBlock();233 } 234 } 235 sb.Append Line("}").EndBlock();236 sb.AppendLine("}") .EndBlock();225 sb.AppendFormat("{0} = random.NextDouble() * (max - min) + min;", constr.Ident).EndBlock(); 226 sb.Append("}").EndBlock(); 227 } 228 } 229 sb.Append("}").EndBlock(); 230 sb.AppendLine("}"); 237 231 } 238 232 -
branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs
r10384 r10385 29 29 public int steps; 30 30 public int depth; 31 // private readonly Stack<Tree> nodes;32 31 private readonly ?IDENT?Problem problem; 33 32 private Random random; … … 35 34 public SolverState(?IDENT?Problem problem, int seed) { 36 35 this.problem = problem; 37 // this.nodes = new Stack<Tree>();38 39 36 this.random = new Random(seed); 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 // } 37 } 47 38 48 39 public Tree SampleTree() { … … 87 78 } 88 79 } 89 90 // public int PeekNextAlternative() {91 // // this must only be called nodes that contain alternatives and therefore must only have single-symbols alternatives92 // System.Diagnostics.Debug.Assert(nodes.Peek().subtrees.Count == 1);93 // return nodes.Peek().subtrees[0].altIdx;94 // }95 //96 // public void Follow(int idx) {97 // nodes.Push(nodes.Peek().subtrees[idx]);98 // }99 //100 // public void Unwind() {101 // nodes.Pop();102 // }103 80 104 81 private int SampleAlternative(Random random, int state) { … … 167 144 var sw = new System.Diagnostics.Stopwatch(); 168 145 sw.Start(); 169 while ( true) {146 while (n <= 10000) { 170 147 171 148 var _state = new SolverState(problem, seedRandom.Next()); … … 184 161 sw.Stop(); 185 162 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); 186 sw.Reset();187 163 sumSize = 0; 188 164 sumDepth = 0; 189 165 sumF = 0.0; 190 sw. Start();166 sw.Restart(); 191 167 } 192 168 } … … 202 178 var solverSourceCode = new SourceBuilder(); 203 179 solverSourceCode.Append(solverTemplate) 204 // .Replace("?TERMINALFIELDS?", GenerateTerminalFields(grammar))205 // .Replace("?CONSTRUCTORCODE?", GenerateConstructorCode(grammar))206 180 .Replace("?MAXIMIZATION?", maximization.ToString().ToLowerInvariant()) 207 181 .Replace("?SYMBOLNAMES?", grammar.Symbols.Select(s => s.Name).Aggregate(string.Empty, (str, symb) => str + "\"" + symb + "\", ")) … … 210 184 .Replace("?SUBTREECOUNTTABLE?", GenerateSubtreeCountTable(grammar)) 211 185 .Replace("?SAMPLEALTERNATIVECODE?", GenerateSampleAlternativeSource(grammar)) 212 // .Replace("?SAMPLETERMINALCODE?", GenerateSampleTerminalSource(grammar))213 186 ; 214 187 … … 217 190 218 191 219 //private string GenerateSampleTerminalSource(IGrammar grammar) {220 // StringBuilder sb = new StringBuilder();221 // foreach (var t in grammar.TerminalSymbols) {222 // sb.AppendFormat("public void {0}(ISolverState _state, {1}) {{", t.Name, t.GetAttributeString()).AppendLine();223 // foreach (var att in t.Attributes) {224 // // need constraints225 // sb.AppendFormat("{0}", att.Name);226 // }227 // sb.AppendLine("}");228 // }229 // return sb.ToString();230 //}231 192 private string GenerateCreateTerminalCode(IGrammar grammar) { 232 193 Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol)); -
branches/HeuristicLab.Problems.GPDL/CodeGenerator/SourceBuilder.cs
r10100 r10385 33 33 } 34 34 public SourceBuilder AppendLine() { 35 sb.Append Line();35 sb.Append(' ', indent).AppendLine(); 36 36 return this; 37 37 } 38 38 public SourceBuilder AppendLine(string str) { 39 sb.Append Line(str);39 sb.Append(' ', indent).AppendLine(str); 40 40 return this; 41 41 }
Note: See TracChangeset
for help on using the changeset viewer.