Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/OffspringSelectionGP.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: 2.8 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 OffspringSelectionGP : SolverBase {
15    public int PopulationSize { get; set; }
16    public double MutationRate { get; set; }
17    public int MaxSolutionSize { get; set; }
18    public int MaxSolutionDepth { get; set; }
19
20    private readonly ISymbolicExpressionTreeProblem problem;
21    private readonly Random random;
22
23    public OffspringSelectionGP(ISymbolicExpressionTreeProblem problem, Random random) {
24      this.problem = problem;
25      this.random = random;
26      // default parameter values
27      PopulationSize = 300;
28      MutationRate = 0.15;
29      MaxSolutionSize = 100;
30      MaxSolutionDepth = 17;
31    }
32
33    public override void Run(int maxEvaluations) {
34      var hlProblem = new GenericSymbExprProblem(problem);
35      hlProblem.Evaluator.SolutionEvaluated += OnSolutionEvaluated; // raise solution evaluated event for each GP solution
36
37      using (var wh = new AutoResetEvent(false)) {
38        var ga = new GeneticAlgorithm.GeneticAlgorithm();
39        ga.Engine = new SequentialEngine.SequentialEngine();
40        ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
41        ga.Stopped += (sender, args) => { wh.Set(); };
42
43        ga.Problem = hlProblem;
44        var mutator = (MultiSymbolicExpressionTreeManipulator)ga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
45        foreach (var op in mutator.Operators) {
46          if (op.Name == "ChangeNodeTypeManipulation"
47            || op.Name == "ReplaceBranchManipulation") mutator.Operators.SetItemCheckedState(op, true);
48          else mutator.Operators.SetItemCheckedState(op, false);
49        }
50        ga.Mutator = mutator;
51        ga.Crossover = ga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
52        ga.Selector = ga.SelectorParameter.ValidValues.Single(op => op.Name == "GenderSpecificSelector");
53
54        ga.PopulationSize.Value = PopulationSize;
55        ga.MaximumGenerations.Value = maxEvaluations / PopulationSize + 1; // one extra generation in case maxEvaluations is not a multiple of PopulationSize
56        ga.MutationProbability.Value = MutationRate;
57
58        ga.SetSeedRandomly = new BoolValue(false);
59        ga.Seed = new IntValue(random.Next());
60
61        ga.Prepare();
62        ga.Start();
63
64        wh.WaitOne();
65      }
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.