[11846] | 1 | using System;
|
---|
[11895] | 2 | using System.IO;
|
---|
| 3 | using System.IO.Compression;
|
---|
[11846] | 4 | using System.Linq;
|
---|
| 5 | using System.Threading;
|
---|
| 6 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
[11895] | 7 | using HeuristicLab.Analysis;
|
---|
| 8 | using HeuristicLab.Common;
|
---|
[11846] | 9 | using HeuristicLab.Data;
|
---|
| 10 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[11895] | 11 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[11846] | 12 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
| 13 |
|
---|
| 14 | namespace HeuristicLab.Algorithms.GeneticProgramming {
|
---|
[11895] | 15 | public class OffspringSelectionGP : SolverBase, IGPSolver {
|
---|
[11846] | 16 | public int PopulationSize { get; set; }
|
---|
| 17 | public double MutationRate { get; set; }
|
---|
| 18 | public int MaxSolutionSize { get; set; }
|
---|
| 19 | public int MaxSolutionDepth { get; set; }
|
---|
[12023] | 20 | public double ComparisonFactorLowerBound { get; set; }
|
---|
[11846] | 21 |
|
---|
| 22 | private readonly ISymbolicExpressionTreeProblem problem;
|
---|
| 23 | private readonly Random random;
|
---|
[11895] | 24 | private readonly bool saveAlg;
|
---|
[11846] | 25 |
|
---|
[11895] | 26 | public OffspringSelectionGP(ISymbolicExpressionTreeProblem problem, Random random, bool saveAlg = false) {
|
---|
[11846] | 27 | this.problem = problem;
|
---|
| 28 | this.random = random;
|
---|
| 29 | // default parameter values
|
---|
[11847] | 30 | PopulationSize = 100;
|
---|
[11846] | 31 | MutationRate = 0.15;
|
---|
| 32 | MaxSolutionSize = 100;
|
---|
| 33 | MaxSolutionDepth = 17;
|
---|
[12023] | 34 | ComparisonFactorLowerBound = 1.0;
|
---|
[11895] | 35 | this.saveAlg = saveAlg;
|
---|
[11846] | 36 | }
|
---|
| 37 |
|
---|
| 38 | public override void Run(int maxEvaluations) {
|
---|
| 39 | var hlProblem = new GenericSymbExprProblem(problem);
|
---|
[11851] | 40 | var onEvalLocker = new object();
|
---|
[11847] | 41 | hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
|
---|
| 42 | hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
|
---|
[11846] | 43 |
|
---|
| 44 | using (var wh = new AutoResetEvent(false)) {
|
---|
[11847] | 45 | var osga = new OffspringSelectionGeneticAlgorithm.OffspringSelectionGeneticAlgorithm();
|
---|
[11895] | 46 | // osga.Engine = new ParallelEngine.ParallelEngine();
|
---|
| 47 | osga.Engine = new SequentialEngine.SequentialEngine();
|
---|
[12503] | 48 | //osga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
|
---|
| 49 | //osga.Stopped += (sender, args) => { wh.Set(); };
|
---|
[11846] | 50 |
|
---|
[11895] | 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 |
|
---|
[11847] | 66 | osga.Problem = hlProblem;
|
---|
| 67 | var mutator = (MultiSymbolicExpressionTreeManipulator)osga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
|
---|
[11846] | 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 | }
|
---|
[11847] | 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");
|
---|
[11895] | 76 | var multiAnalzer = (MultiAnalyzer)osga.Analyzer;
|
---|
| 77 | multiAnalzer.Operators.Add(new BestSymbolicExpressionTreeAnalyzer());
|
---|
[11846] | 78 |
|
---|
[11847] | 79 | osga.PopulationSize.Value = PopulationSize;
|
---|
[11895] | 80 | osga.MaximumGenerations.Value = 1000000; // some very large value (we stop based on evaluations)
|
---|
| 81 | osga.MaximumSelectionPressure.Value = 1000000;
|
---|
[11847] | 82 | osga.MaximumEvaluatedSolutions.Value = maxEvaluations;
|
---|
| 83 | osga.MutationProbability.Value = MutationRate;
|
---|
[11895] | 84 | osga.ComparisonFactorLowerBound.Value = 1.0;
|
---|
[12023] | 85 | osga.ComparisonFactorUpperBound.Value = ComparisonFactorLowerBound;
|
---|
[11895] | 86 | osga.SuccessRatio.Value = 1.0;
|
---|
[11846] | 87 |
|
---|
[11847] | 88 | osga.SetSeedRandomly = new BoolValue(false);
|
---|
| 89 | osga.Seed = new IntValue(random.Next());
|
---|
[11846] | 90 |
|
---|
[11847] | 91 | osga.Prepare();
|
---|
| 92 | osga.Start();
|
---|
[11846] | 93 |
|
---|
[12023] | 94 | wh.Dispose();
|
---|
[11895] | 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 | }
|
---|
[11846] | 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|