Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/OffspringSelectionGP.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.1 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 = 100;
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, don't scale quality to 0..1
36      hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
37      hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
38
39      using (var wh = new AutoResetEvent(false)) {
40        var osga = new OffspringSelectionGeneticAlgorithm.OffspringSelectionGeneticAlgorithm();
41        osga.Engine = new ParallelEngine.ParallelEngine();
42        osga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
43        osga.Stopped += (sender, args) => { wh.Set(); };
44
45        osga.Problem = hlProblem;
46        var mutator = (MultiSymbolicExpressionTreeManipulator)osga.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        osga.Mutator = mutator;
53        osga.Crossover = osga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
54        osga.Selector = osga.SelectorParameter.ValidValues.Single(op => op.Name == "GenderSpecificSelection");
55
56        osga.PopulationSize.Value = PopulationSize;
57        osga.MaximumGenerations.Value = 100000; // some very large value (we stop based on evaluations)
58        osga.MaximumSelectionPressure.Value = 1000;
59        osga.MaximumEvaluatedSolutions.Value = maxEvaluations;
60        osga.MutationProbability.Value = MutationRate;
61
62        osga.SetSeedRandomly = new BoolValue(false);
63        osga.Seed = new IntValue(random.Next());
64
65        osga.Prepare();
66        osga.Start();
67
68        wh.WaitOne();
69      }
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.