Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/StandardGP.cs @ 11847

Last change on this file since 11847 was 11847, checked in by gkronber, 9 years ago

#2283 various fixes for tree-based GP

File size: 3.2 KB
Line 
1using System;
2using System.Linq;
3using System.Threading;
4using HeuristicLab.Algorithms.GrammaticalOptimization;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7using HeuristicLab.Optimization;
8using HeuristicLab.Problems.GrammaticalOptimization;
9using HeuristicLab.Selection;
10using HeuristicLab.SequentialEngine;
11using HeuristicLab.Algorithms.GeneticAlgorithm;
12
13namespace HeuristicLab.Algorithms.GeneticProgramming {
14  public class StandardGP : SolverBase {
15    public int PopulationSize { get; set; }
16    public double MutationRate { get; set; }
17    public int TournamentGroupSize { get; set; }
18    public int MaxSolutionSize { get; set; }
19    public int MaxSolutionDepth { get; set; }
20
21    private readonly ISymbolicExpressionTreeProblem problem;
22    private readonly Random random;
23
24    public StandardGP(ISymbolicExpressionTreeProblem problem, Random random) {
25      this.problem = problem;
26      this.random = random;
27      // default parameter values
28      PopulationSize = 1000;
29      TournamentGroupSize = 7;
30      MutationRate = 0.15;
31      MaxSolutionSize = 100;
32      MaxSolutionDepth = 17;
33    }
34
35    public override void Run(int maxEvaluations) {
36      var hlProblem = new GenericSymbExprProblem(problem);
37      hlProblem.Evaluator.SolutionEvaluated += OnSolutionEvaluated; // raise solution evaluated event for each GP solution, don't scale quality to 0..1
38      hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
39      hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
40
41
42      using (var wh = new AutoResetEvent(false)) {
43        var ga = new GeneticAlgorithm.GeneticAlgorithm();
44        ga.Engine = new ParallelEngine.ParallelEngine();
45        ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
46        ga.Stopped += (sender, args) => { wh.Set(); };
47
48        ga.Problem = hlProblem;
49        var mutator = (MultiSymbolicExpressionTreeManipulator)ga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
50        foreach (var op in mutator.Operators) {
51          if (op.Name == "ChangeNodeTypeManipulation"
52            || op.Name == "ReplaceBranchManipulation") mutator.Operators.SetItemCheckedState(op, true);
53          else mutator.Operators.SetItemCheckedState(op, false);
54        }
55        ga.Mutator = mutator;
56        ga.Crossover = ga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
57        var selector = (TournamentSelector)ga.SelectorParameter.ValidValues.Single(op => op.Name == "TournamentSelector");
58        selector.GroupSizeParameter.Value = new IntValue(TournamentGroupSize);
59        ga.Selector = selector;
60
61        ga.PopulationSize.Value = PopulationSize;
62        ga.MaximumGenerations.Value = maxEvaluations / PopulationSize + 1; // one extra generation in case maxEvaluations is not a multiple of PopulationSize
63        ga.MutationProbability.Value = MutationRate;
64
65        ga.SetSeedRandomly = new BoolValue(false);
66        ga.Seed = new IntValue(random.Next());
67
68        ga.Prepare();
69        ga.Start();
70
71        wh.WaitOne();
72      }
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.