Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2283: solution reorganization

File size: 3.4 KB
RevLine 
[11846]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.Algorithms.GeneticAlgorithm;
11
12namespace HeuristicLab.Algorithms.GeneticProgramming {
13  public class StandardGP : SolverBase {
14    public int PopulationSize { get; set; }
15    public double MutationRate { get; set; }
16    public int TournamentGroupSize { 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 StandardGP(ISymbolicExpressionTreeProblem problem, Random random) {
24      this.problem = problem;
25      this.random = random;
26      // default parameter values
27      PopulationSize = 1000;
28      TournamentGroupSize = 7;
29      MutationRate = 0.15;
30      MaxSolutionSize = 100;
31      MaxSolutionDepth = 17;
32    }
33
34    public override void Run(int maxEvaluations) {
35      var hlProblem = new GenericSymbExprProblem(problem);
[11851]36      var onEvalLocker = new object();
37      hlProblem.Evaluator.SolutionEvaluated += (sentence, quality) => {
38        // raise solution evaluated event for each GP solution, don't scale quality to 0..1
39        // need to synchronize in case we are using a parallel engine
40        lock (onEvalLocker) {
41          OnSolutionEvaluated(sentence, quality);
42        }
43      };
[11847]44      hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
45      hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
[11846]46
[11847]47
[11846]48      using (var wh = new AutoResetEvent(false)) {
49        var ga = new GeneticAlgorithm.GeneticAlgorithm();
[11847]50        ga.Engine = new ParallelEngine.ParallelEngine();
[11846]51        ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
52        ga.Stopped += (sender, args) => { wh.Set(); };
53
54        ga.Problem = hlProblem;
55        var mutator = (MultiSymbolicExpressionTreeManipulator)ga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
56        foreach (var op in mutator.Operators) {
57          if (op.Name == "ChangeNodeTypeManipulation"
58            || op.Name == "ReplaceBranchManipulation") mutator.Operators.SetItemCheckedState(op, true);
59          else mutator.Operators.SetItemCheckedState(op, false);
60        }
61        ga.Mutator = mutator;
62        ga.Crossover = ga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
63        var selector = (TournamentSelector)ga.SelectorParameter.ValidValues.Single(op => op.Name == "TournamentSelector");
64        selector.GroupSizeParameter.Value = new IntValue(TournamentGroupSize);
65        ga.Selector = selector;
66
67        ga.PopulationSize.Value = PopulationSize;
68        ga.MaximumGenerations.Value = maxEvaluations / PopulationSize + 1; // one extra generation in case maxEvaluations is not a multiple of PopulationSize
69        ga.MutationProbability.Value = MutationRate;
70
71        ga.SetSeedRandomly = new BoolValue(false);
72        ga.Seed = new IntValue(random.Next());
73
74        ga.Prepare();
75        ga.Start();
76
77        wh.WaitOne();
78      }
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.