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.Algorithms.GeneticAlgorithm;
|
---|
11 |
|
---|
12 | namespace 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);
|
---|
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 | };
|
---|
44 | hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
|
---|
45 | hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
|
---|
46 |
|
---|
47 |
|
---|
48 | using (var wh = new AutoResetEvent(false)) {
|
---|
49 | var ga = new GeneticAlgorithm.GeneticAlgorithm();
|
---|
50 | ga.Engine = new ParallelEngine.ParallelEngine();
|
---|
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 | }
|
---|