1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Threading;
|
---|
4 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
9 | using HeuristicLab.Selection;
|
---|
10 | using HeuristicLab.SequentialEngine;
|
---|
11 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
12 |
|
---|
13 | namespace 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 | }
|
---|