Changeset 9872
- Timestamp:
- 08/09/13 16:32:42 (11 years ago)
- Location:
- branches/HeuristicLab.Problems.GPDL
- Files:
-
- 3 deleted
- 5 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL.Views/3.4/Resources/LawnMower.txt
r9847 r9872 102 102 int[] ints = Enumerable.Range(-100, 201).ToArray(); 103 103 >> 104 INIT <<105 >>106 104 107 105 NONTERMINALS -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL/3.4/GPDef.atg
r9846 r9872 150 150 myTNode.Ident = identStr; 151 151 myTNode.FormalParameters = src; 152 myTNode.FieldDefinitions = SourceReader.ExtractFormalParameters(src);152 myTNode.FieldDefinitions = Util.ExtractFormalParameters(src); 153 153 .) 154 154 [ "CONSTRAINTS" ConstraintDef<out constraints> -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL/3.4/HeuristicLab.Problems.GPDL-3.4.csproj
r9724 r9872 192 192 <Compile Include="AST.cs" /> 193 193 <Compile Include="Grammar.cs" /> 194 <Compile Include="InterpreterBuilder.cs" />195 <Compile Include="MethodBuilder.cs" />196 194 <Compile Include="Parser.cs" /> 197 195 <Compile Include="ProblemGenerator.cs" /> 198 196 <Compile Include="Scanner.cs" /> 199 <Compile Include="SourceReader.cs" /> 200 <Compile Include="SymbolBuilder.cs" /> 197 <Compile Include="Util.cs" /> 201 198 <None Include="GPDef.atg" /> 202 199 <None Include="HeuristicLab.snk" /> -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL/3.4/Parser.cs
r9846 r9872 218 218 myTNode.Ident = identStr; 219 219 myTNode.FormalParameters = src; 220 myTNode.FieldDefinitions = SourceReader.ExtractFormalParameters(src);220 myTNode.FieldDefinitions = Util.ExtractFormalParameters(src); 221 221 222 222 if (la.kind == 15) { -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL/3.4/ProblemGenerator.cs
r9846 r9872 53 53 "; 54 54 55 private string mainClassTemplate = @"56 namespace ?PROBLEMNAME? {57 public sealed class ?IDENT?Main {58 public ?IDENT?Main() {59 Initialize();60 }61 public void Initialize() {62 var problem = new ?IDENT?Problem();63 var wh = new AutoResetEvent(false);64 var ga = new OffspringSelectionGeneticAlgorithm();65 ga.Engine = new SequentialEngine();66 ga.Problem = problem;67 ga.PopulationSize.Value = 1000;68 ga.MaximumGenerations.Value = 100;69 ga.ComparisonFactorLowerBound.Value = 0;70 ga.ComparisonFactorUpperBound.Value = 0;71 ga.MutationProbability.Value = 0.15;72 ga.Mutator = ga.MutatorParameter.ValidValues.OfType<MultiSymbolicExpressionTreeManipulator>().First();73 var ts = ga.SelectorParameter.ValidValues.OfType<GenderSpecificSelector>().First();74 ga.Selector = ts;75 ga.Prepare();76 ga.ExceptionOccurred += (sender, args) => {77 Console.WriteLine(args.Value);78 wh.Set();79 };80 ga.Stopped += (sender, args) => {81 Console.WriteLine(""Finished"");82 Console.WriteLine(""Evaluated solutions: "" + ((IntValue)(ga.Results[""EvaluatedSolutions""]).Value).Value);83 Console.WriteLine(""Best quality: "" + ((DoubleValue)(ga.Results[""BestQuality""]).Value).Value);84 wh.Set();85 };86 ga.Crossover = ga.CrossoverParameter.ValidValues.First();87 ga.Start();88 while(!wh.WaitOne(500)) {89 if(ga.Results.ContainsKey(""BestQuality"")) {90 int evalSolutions = ((IntValue)(ga.Results[""EvaluatedSolutions""]).Value).Value;91 double quality = ((DoubleValue)(ga.Results[""BestQuality""]).Value).Value;92 Console.WriteLine(""{0}\t{1} "", evalSolutions, quality);93 }94 };95 }96 }97 }";98 99 55 private string problemClassTemplate = @" 100 56 namespace ?PROBLEMNAME? { … … 207 163 208 164 ?CONSTRAINTSSOURCE? 209 210 165 } 211 166 }"; … … 408 363 GenerateEvaluator(definition); 409 364 GenerateProblem(definition); 410 GenerateMain(definition);411 365 412 366 problemSourceCode.Replace("?PROBLEMNAME?", definition.Name); 413 367 368 // write to a file for debugging 414 369 using (var stream = new StreamWriter(definition.Name + ".cs")) { 415 370 stream.WriteLine(problemSourceCode.ToString()); 416 371 } 417 372 373 // compile generated source 418 374 using (var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } })) { 419 375 var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll", "System.dll", "System.Drawing.dll" }, Path.ChangeExtension(Path.GetRandomFileName(), ".dll"), true); … … 442 398 443 399 CompilerResults results = csc.CompileAssemblyFromSource(parameters, problemSourceCode.ToString()); 444 results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.Line + " " + error.ErrorText)); 445 400 results.Errors.Cast<CompilerError>() 401 .ToList() 402 .ForEach(error => Console.WriteLine(error.Line + " " + error.ErrorText)); 403 404 // load the assembly, and create an instance from the generate problem class 446 405 var asm = results.CompiledAssembly; 447 406 AppDomain.CurrentDomain.Load(asm.GetName()); … … 460 419 problemSourceCode.AppendLine(evaluatorClassCode).AppendLine(); 461 420 } 462 463 private void GenerateMain(GPDefNode definition) {464 var mainClassCode =465 mainClassTemplate466 .Replace("?IDENT?", definition.Name);467 468 problemSourceCode.AppendLine(mainClassCode).AppendLine();469 }470 471 421 472 422 private void GenerateProblem(GPDefNode definition) { … … 511 461 string formalParameter = definition.NonTerminals.Single(nt => nt.Ident == g.RootSymbol).FormalParameters; 512 462 var actualParameterEnumerable = 513 SourceReader.ExtractFormalParameters(formalParameter).Select(e => e.RefOrOut + " " + e.Identifier);463 Util.ExtractFormalParameters(formalParameter).Select(e => e.RefOrOut + " " + e.Identifier); 514 464 string actualParameter = string.Empty; 515 465 if (actualParameterEnumerable.Any()) { … … 524 474 } 525 475 foreach (var s in definition.Terminals) { 526 sb.AppendLine(GenerateTerminalInterpreterMethod( g,(TerminalNode)s));527 } 528 return sb.ToString(); 529 } 530 531 private string GenerateTerminalInterpreterMethod( Grammar g,TerminalNode s) {476 sb.AppendLine(GenerateTerminalInterpreterMethod((TerminalNode)s)); 477 } 478 return sb.ToString(); 479 } 480 481 private string GenerateTerminalInterpreterMethod(TerminalNode s) { 532 482 var sb = new StringBuilder(); 533 483 if (!s.FormalParameters.Any()) -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL/3.4/Util.cs
r9858 r9872 24 24 using System.Text.RegularExpressions; 25 25 26 public class SourceReader { 27 26 public class Util { 28 27 private static Regex formalParameterExpr = new Regex(@"\s*,?\s*((?<refOrOut>(out|ref))\s+)?(?<type>[_\w]*)\s+(?<id>[_\w]*)\s*"); 29 28 public static IEnumerable<TerminalNode.FieldDefinition> ExtractFormalParameters(string formalParameterSrc) {
Note: See TracChangeset
for help on using the changeset viewer.