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 OffspringSelectionGP : SolverBase {
|
---|
14 | public int PopulationSize { get; set; }
|
---|
15 | public double MutationRate { get; set; }
|
---|
16 | public int MaxSolutionSize { get; set; }
|
---|
17 | public int MaxSolutionDepth { get; set; }
|
---|
18 |
|
---|
19 | private readonly ISymbolicExpressionTreeProblem problem;
|
---|
20 | private readonly Random random;
|
---|
21 |
|
---|
22 | public OffspringSelectionGP(ISymbolicExpressionTreeProblem problem, Random random) {
|
---|
23 | this.problem = problem;
|
---|
24 | this.random = random;
|
---|
25 | // default parameter values
|
---|
26 | PopulationSize = 100;
|
---|
27 | MutationRate = 0.15;
|
---|
28 | MaxSolutionSize = 100;
|
---|
29 | MaxSolutionDepth = 17;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public override void Run(int maxEvaluations) {
|
---|
33 | var hlProblem = new GenericSymbExprProblem(problem);
|
---|
34 | var onEvalLocker = new object();
|
---|
35 | hlProblem.Evaluator.SolutionEvaluated += (sentence, quality) => {
|
---|
36 | // raise solution evaluated event for each GP solution, don't scale quality to 0..1
|
---|
37 | // need to synchronize in case we are using a parallel engine
|
---|
38 | lock (onEvalLocker) {
|
---|
39 | OnSolutionEvaluated(sentence, quality);
|
---|
40 | }
|
---|
41 | };
|
---|
42 | hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
|
---|
43 | hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
|
---|
44 |
|
---|
45 | using (var wh = new AutoResetEvent(false)) {
|
---|
46 | var osga = new OffspringSelectionGeneticAlgorithm.OffspringSelectionGeneticAlgorithm();
|
---|
47 | osga.Engine = new ParallelEngine.ParallelEngine();
|
---|
48 | osga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
|
---|
49 | osga.Stopped += (sender, args) => { wh.Set(); };
|
---|
50 |
|
---|
51 | osga.Problem = hlProblem;
|
---|
52 | var mutator = (MultiSymbolicExpressionTreeManipulator)osga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
|
---|
53 | foreach (var op in mutator.Operators) {
|
---|
54 | if (op.Name == "ChangeNodeTypeManipulation"
|
---|
55 | || op.Name == "ReplaceBranchManipulation") mutator.Operators.SetItemCheckedState(op, true);
|
---|
56 | else mutator.Operators.SetItemCheckedState(op, false);
|
---|
57 | }
|
---|
58 | osga.Mutator = mutator;
|
---|
59 | osga.Crossover = osga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
|
---|
60 | osga.Selector = osga.SelectorParameter.ValidValues.Single(op => op.Name == "GenderSpecificSelection");
|
---|
61 |
|
---|
62 | osga.PopulationSize.Value = PopulationSize;
|
---|
63 | osga.MaximumGenerations.Value = 100000; // some very large value (we stop based on evaluations)
|
---|
64 | osga.MaximumSelectionPressure.Value = 1000;
|
---|
65 | osga.MaximumEvaluatedSolutions.Value = maxEvaluations;
|
---|
66 | osga.MutationProbability.Value = MutationRate;
|
---|
67 |
|
---|
68 | osga.SetSeedRandomly = new BoolValue(false);
|
---|
69 | osga.Seed = new IntValue(random.Next());
|
---|
70 |
|
---|
71 | osga.Prepare();
|
---|
72 | osga.Start();
|
---|
73 |
|
---|
74 | wh.WaitOne();
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|