1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.IO.Compression;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Threading;
|
---|
6 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
7 | using HeuristicLab.Analysis;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
11 | using HeuristicLab.Persistence.Default.Xml;
|
---|
12 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Algorithms.GeneticProgramming {
|
---|
15 | public class OffspringSelectionGP : SolverBase, IGPSolver {
|
---|
16 | public int PopulationSize { get; set; }
|
---|
17 | public double MutationRate { get; set; }
|
---|
18 | public int MaxSolutionSize { get; set; }
|
---|
19 | public int MaxSolutionDepth { get; set; }
|
---|
20 | public double ComparisonFactorLowerBound { get; set; }
|
---|
21 |
|
---|
22 | private readonly ISymbolicExpressionTreeProblem problem;
|
---|
23 | private readonly Random random;
|
---|
24 | private readonly bool saveAlg;
|
---|
25 |
|
---|
26 | public OffspringSelectionGP(ISymbolicExpressionTreeProblem problem, Random random, bool saveAlg = false) {
|
---|
27 | this.problem = problem;
|
---|
28 | this.random = random;
|
---|
29 | // default parameter values
|
---|
30 | PopulationSize = 100;
|
---|
31 | MutationRate = 0.15;
|
---|
32 | MaxSolutionSize = 100;
|
---|
33 | MaxSolutionDepth = 17;
|
---|
34 | ComparisonFactorLowerBound = 1.0;
|
---|
35 | this.saveAlg = saveAlg;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public override void Run(int maxEvaluations) {
|
---|
39 | var hlProblem = new GenericSymbExprProblem(problem);
|
---|
40 | var onEvalLocker = new object();
|
---|
41 | hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
|
---|
42 | hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
|
---|
43 |
|
---|
44 | using (var wh = new AutoResetEvent(false)) {
|
---|
45 | var osga = new OffspringSelectionGeneticAlgorithm.OffspringSelectionGeneticAlgorithm();
|
---|
46 | // osga.Engine = new ParallelEngine.ParallelEngine();
|
---|
47 | osga.Engine = new SequentialEngine.SequentialEngine();
|
---|
48 | //osga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
|
---|
49 | //osga.Stopped += (sender, args) => { wh.Set(); };
|
---|
50 |
|
---|
51 | int numEvals = 0;
|
---|
52 | hlProblem.Evaluator.SolutionEvaluated += (sentence, quality) => {
|
---|
53 | // raise solution evaluated event for each GP solution, don't scale quality to 0..1
|
---|
54 | // need to synchronize in case we are using a parallel engine
|
---|
55 | lock (onEvalLocker) {
|
---|
56 | OnSolutionEvaluated(sentence, quality);
|
---|
57 |
|
---|
58 | // stop when maxEvals has been reached
|
---|
59 | if (numEvals++ >= maxEvaluations) {
|
---|
60 | osga.Stop();
|
---|
61 | }
|
---|
62 | }
|
---|
63 | };
|
---|
64 |
|
---|
65 |
|
---|
66 | osga.Problem = hlProblem;
|
---|
67 | var mutator = (MultiSymbolicExpressionTreeManipulator)osga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
|
---|
68 | foreach (var op in mutator.Operators) {
|
---|
69 | if (op.Name == "ChangeNodeTypeManipulation"
|
---|
70 | || op.Name == "ReplaceBranchManipulation") mutator.Operators.SetItemCheckedState(op, true);
|
---|
71 | else mutator.Operators.SetItemCheckedState(op, false);
|
---|
72 | }
|
---|
73 | osga.Mutator = mutator;
|
---|
74 | osga.Crossover = osga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
|
---|
75 | osga.Selector = osga.SelectorParameter.ValidValues.Single(op => op.Name == "GenderSpecificSelection");
|
---|
76 | var multiAnalzer = (MultiAnalyzer)osga.Analyzer;
|
---|
77 | multiAnalzer.Operators.Add(new BestSymbolicExpressionTreeAnalyzer());
|
---|
78 |
|
---|
79 | osga.PopulationSize.Value = PopulationSize;
|
---|
80 | osga.MaximumGenerations.Value = 1000000; // some very large value (we stop based on evaluations)
|
---|
81 | osga.MaximumSelectionPressure.Value = 1000000;
|
---|
82 | osga.MaximumEvaluatedSolutions.Value = maxEvaluations;
|
---|
83 | osga.MutationProbability.Value = MutationRate;
|
---|
84 | osga.ComparisonFactorLowerBound.Value = 1.0;
|
---|
85 | osga.ComparisonFactorUpperBound.Value = ComparisonFactorLowerBound;
|
---|
86 | osga.SuccessRatio.Value = 1.0;
|
---|
87 |
|
---|
88 | osga.SetSeedRandomly = new BoolValue(false);
|
---|
89 | osga.Seed = new IntValue(random.Next());
|
---|
90 |
|
---|
91 | osga.Prepare();
|
---|
92 | osga.Start();
|
---|
93 |
|
---|
94 | wh.Dispose();
|
---|
95 |
|
---|
96 | if (saveAlg) {
|
---|
97 | var path = @"C:\Users\P24581\Desktop";
|
---|
98 | var fileName = string.Format("osgp-{0}{1:D2}{2:D2}{3:D2}{4:D2}.hl", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute);
|
---|
99 | var fullPath = Path.Combine(path, fileName);
|
---|
100 | HeuristicLab.Persistence.Core.ConfigurationService.Instance.LoadSettings();
|
---|
101 | XmlGenerator.Serialize(osga, fullPath, CompressionLevel.Fastest);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|