Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2283 implemented bridge to HL (solve grammatical optimization problem instances with StandardGP and OffspringSelectionGP)

File size: 3.0 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
38
39      using (var wh = new AutoResetEvent(false)) {
40        var ga = new GeneticAlgorithm.GeneticAlgorithm();
41        ga.Engine = new SequentialEngine.SequentialEngine();
42        ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
43        ga.Stopped += (sender, args) => { wh.Set(); };
44
45        ga.Problem = hlProblem;
46        var mutator = (MultiSymbolicExpressionTreeManipulator)ga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
47        foreach (var op in mutator.Operators) {
48          if (op.Name == "ChangeNodeTypeManipulation"
49            || op.Name == "ReplaceBranchManipulation") mutator.Operators.SetItemCheckedState(op, true);
50          else mutator.Operators.SetItemCheckedState(op, false);
51        }
52        ga.Mutator = mutator;
53        ga.Crossover = ga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
54        var selector = (TournamentSelector)ga.SelectorParameter.ValidValues.Single(op => op.Name == "TournamentSelector");
55        selector.GroupSizeParameter.Value = new IntValue(TournamentGroupSize);
56        ga.Selector = selector;
57
58        ga.PopulationSize.Value = PopulationSize;
59        ga.MaximumGenerations.Value = maxEvaluations / PopulationSize + 1; // one extra generation in case maxEvaluations is not a multiple of PopulationSize
60        ga.MutationProbability.Value = MutationRate;
61
62        ga.SetSeedRandomly = new BoolValue(false);
63        ga.Seed = new IntValue(random.Next());
64
65        ga.Prepare();
66        ga.Start();
67
68        wh.WaitOne();
69      }
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.