Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/Push.Thesis/Program.cs @ 15334

Last change on this file since 15334 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

File size: 6.1 KB
Line 
1using System.Linq;
2using System.Text;
3
4namespace Push.Thesis {
5  using System.IO;
6  using System.Threading.Tasks;
7
8  using HeuristicLab.Algorithms.GeneticAlgorithm;
9  using HeuristicLab.BenchmarkSuite;
10  using HeuristicLab.Optimization;
11  using HeuristicLab.ParallelEngine;
12  using HeuristicLab.Persistence.Default.Xml;
13  using HeuristicLab.Problems.ProgramSynthesis.Push.Analyzer;
14  using HeuristicLab.Problems.ProgramSynthesis.Push.Crossover;
15  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
16  using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
17  using HeuristicLab.Problems.ProgramSynthesis.Push.Manipulator;
18  using HeuristicLab.Problems.ProgramSynthesis.Push.Problem.BenchmarkSuite;
19  using HeuristicLab.Problems.ProgramSynthesis.Push.Selector;
20
21  class Program {
22    static void Main(string[] args) {
23      //BuildListOfInstructions();
24      GenerateRuns();
25      GenerateExperiments();
26      //GenerateExperimentWithAllProblems();
27    }
28
29    private static void GenerateExperimentWithAllProblems() {
30      var provider = new BenchmarkSuiteInstanceProvider();
31
32      var algs = provider
33        .GetDataDescriptors()
34        .AsParallel()
35        .Select(descriptor => new Run(GetAlgorithm((IBenchmarkSuiteDataDescriptor)descriptor)))
36        .ToList();
37
38      var experiment = new Experiment("Experiment Benchmark Suite");
39      experiment.Runs.AddRange(algs);
40
41      XmlGenerator.Serialize(experiment, @"C:\tmp\Plush - GA\Experiment Benchmark Suite.hl");
42    }
43
44    static void GenerateRuns() {
45      var provider = new BenchmarkSuiteInstanceProvider();
46      var descriptors = provider.GetDataDescriptors();
47
48      Parallel.ForEach(descriptors, descriptor => {
49        var benchmarkSuiteProblemDescriptor = (BenchmarkSuiteDataDescriptor)descriptor;
50        var alg = GetAlgorithm(benchmarkSuiteProblemDescriptor);
51
52        XmlGenerator.Serialize(alg, $@"C:\tmp\Plush - GA\Runs\{descriptor.Name}.hl");
53      });
54    }
55
56    static void GenerateExperiments() {
57      var provider = new BenchmarkSuiteInstanceProvider();
58      var descriptors = provider.GetDataDescriptors();
59
60      Parallel.ForEach(descriptors, descriptor => {
61        var benchmarkSuiteProblemDescriptor = (BenchmarkSuiteDataDescriptor)descriptor;
62        var alg = GetAlgorithm(benchmarkSuiteProblemDescriptor);
63        var experiment = GetExperiment(alg, 10);
64
65        XmlGenerator.Serialize(experiment, $@"C:\tmp\Plush - GA\Experiments\Experiment_{descriptor.Name}.hl");
66      });
67    }
68
69    static Experiment GetExperiment(IAlgorithm alg, int repeats) {
70      var experiment = new Experiment("Experiment: " + alg.Problem.Name);
71      experiment.Optimizers.Add(new BatchRun {
72        Optimizer = alg,
73        Repetitions = repeats
74      });
75
76      return experiment;
77    }
78
79    static IAlgorithm GetAlgorithm(IBenchmarkSuiteDataDescriptor descriptor) {
80      var ga = new GeneticAlgorithm();
81
82      var problemData = descriptor.CreateProblemData();
83      var pushProblem = new PlushPushBenchmarkSuiteProblem();
84
85      pushProblem.Load(problemData);
86      ga.Name = "GA " + pushProblem.Name;
87
88      ga.Problem = pushProblem;
89
90      var crossover = ga.CrossoverParameter.ValidValues.OfType<AlternationCrossover>().First();
91
92      switch (problemData.ProblemType) {
93        case ProblemType.NumberIO:
94        case ProblemType.SmallOrLarge:
95        case ProblemType.Median:
96        case ProblemType.Smallest:
97          crossover.AlignmentDeviation = 5;
98          break;
99
100        default:
101          crossover.AlignmentDeviation = 10;
102          break;
103      }
104
105      crossover.AlternationRate = 0.01;
106      ga.Crossover = crossover;
107
108      ga.Selector = ga.SelectorParameter.ValidValues.OfType<LexicaseSelector>().First();
109
110      var mutator = ga.MutatorParameter.ValidValues.OfType<UniformMutation>().First();
111      mutator.InstructionMutationProbability = 0.875;
112      mutator.CloseMutationProbability = 0.125;
113      mutator.CloseIncrementRate = 0.1;
114      ga.Mutator = mutator;
115
116      ga.MutationProbability.Value = 0.8;
117      ga.SetSeedRandomly.Value = true;
118      ga.PopulationSize.Value = 1000;
119
120      ga.MaximumGenerations.Value = problemData.ProgramExecutionBudget / problemData.TrainingCount / 1000;
121
122      var zeroErrorAnalyzer = pushProblem.OperatorsParameter.Value
123        .OfType<IndividualZeroErrorAnalyzer>().SingleOrDefault();
124
125      if (zeroErrorAnalyzer != null) {
126        zeroErrorAnalyzer.StoreHistoryParameter.Value.Value = true;
127        zeroErrorAnalyzer.UpdateIntervalParameter.Value.Value = 5;
128      }
129
130      ga.Engine = new ParallelEngine();
131
132      return ga;
133    }
134
135    static void BuildListOfInstructions() {
136      var sb = new StringBuilder();
137      var attributes = ExpressionTable.StatelessExpressionTable.Values.Select(e => {
138        var type = e.GetType();
139        var attribute = ExpressionTable.TypeToAttributeTable[type];
140
141        return attribute;
142      }).ToList();
143
144      var attributesGrouped = attributes.GroupBy(a => a.StackType).OrderBy(g => g.Key.ToString());
145
146      foreach (var entry in attributesGrouped) {
147        sb.AppendLine($"\\textbf{{{entry.Key}}}");
148        sb.AppendLine("\\begin{description}");
149
150        foreach (var attribute in entry) {
151          var name = attribute.Name
152            .Replace("_", "\\_")
153            .Replace("%", "\\%");
154
155          sb.AppendLine($"\\item[{{{name}}}] {attribute.Description}");
156
157          if (attribute.RequiredBlockCount > 0)
158            sb.AppendLine($"\\\\block wants: {attribute.RequiredBlockCount}");
159
160          if (attribute.AdditionalStackDependencies != default(HeuristicLab.Problems.ProgramSynthesis.Push.Stack.StackTypes)) {
161            var dependenciesNames = attribute.AdditionalStackDependencies.ToValues().Select(v => v.ToString()).OrderBy(x => x);
162            var dependenciesStr = string.Join(", ", dependenciesNames);
163
164            sb.AppendLine($"\\\\dependencies: {dependenciesStr}");
165          }
166
167        }
168
169        sb.AppendLine("\\end{description}");
170      }
171
172
173      File.WriteAllText(@"T:\BTSync\projects\git\bitbucket\master\Paper\appendix\instructions.tex", sb.ToString());
174    }
175  }
176}
Note: See TracBrowser for help on using the repository browser.