[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.Common;
|
---|
[11846] | 8 | using HeuristicLab.Data;
|
---|
| 9 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[11895] | 10 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[11846] | 11 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
| 12 | using HeuristicLab.Selection;
|
---|
| 13 |
|
---|
| 14 | namespace HeuristicLab.Algorithms.GeneticProgramming {
|
---|
[11895] | 15 | public class StandardGP : SolverBase, IGPSolver {
|
---|
[11846] | 16 | public int PopulationSize { get; set; }
|
---|
| 17 | public double MutationRate { get; set; }
|
---|
| 18 | public int TournamentGroupSize { get; set; }
|
---|
| 19 | public int MaxSolutionSize { get; set; }
|
---|
| 20 | public int MaxSolutionDepth { get; set; }
|
---|
| 21 |
|
---|
| 22 | private readonly ISymbolicExpressionTreeProblem problem;
|
---|
| 23 | private readonly Random random;
|
---|
[11895] | 24 | private readonly bool saveAlg;
|
---|
[11846] | 25 |
|
---|
[11895] | 26 | public StandardGP(ISymbolicExpressionTreeProblem problem, Random random, bool saveAlg = false) {
|
---|
[11846] | 27 | this.problem = problem;
|
---|
| 28 | this.random = random;
|
---|
| 29 | // default parameter values
|
---|
| 30 | PopulationSize = 1000;
|
---|
| 31 | TournamentGroupSize = 7;
|
---|
| 32 | MutationRate = 0.15;
|
---|
| 33 | MaxSolutionSize = 100;
|
---|
| 34 | MaxSolutionDepth = 17;
|
---|
[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 |
|
---|
[11847] | 44 |
|
---|
[11846] | 45 | using (var wh = new AutoResetEvent(false)) {
|
---|
| 46 | var ga = new GeneticAlgorithm.GeneticAlgorithm();
|
---|
[11847] | 47 | ga.Engine = new ParallelEngine.ParallelEngine();
|
---|
[11846] | 48 | ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
|
---|
| 49 | ga.Stopped += (sender, args) => { wh.Set(); };
|
---|
| 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 | ga.Stop();
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 |
|
---|
[11846] | 66 | ga.Problem = hlProblem;
|
---|
| 67 | var mutator = (MultiSymbolicExpressionTreeManipulator)ga.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 | ga.Mutator = mutator;
|
---|
| 74 | ga.Crossover = ga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
|
---|
| 75 | var selector = (TournamentSelector)ga.SelectorParameter.ValidValues.Single(op => op.Name == "TournamentSelector");
|
---|
| 76 | selector.GroupSizeParameter.Value = new IntValue(TournamentGroupSize);
|
---|
| 77 | ga.Selector = selector;
|
---|
| 78 |
|
---|
| 79 | ga.PopulationSize.Value = PopulationSize;
|
---|
[11895] | 80 | ga.MaximumGenerations.Value = 1000000; // very large value (we stop in the evaluate handler)
|
---|
[11846] | 81 | ga.MutationProbability.Value = MutationRate;
|
---|
| 82 |
|
---|
| 83 | ga.SetSeedRandomly = new BoolValue(false);
|
---|
| 84 | ga.Seed = new IntValue(random.Next());
|
---|
| 85 |
|
---|
| 86 | ga.Prepare();
|
---|
| 87 | ga.Start();
|
---|
| 88 |
|
---|
| 89 | wh.WaitOne();
|
---|
[11895] | 90 |
|
---|
| 91 |
|
---|
| 92 | if (saveAlg) {
|
---|
| 93 | var path = @"C:\Users\P24581\Desktop";
|
---|
| 94 | 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);
|
---|
| 95 | var fullPath = Path.Combine(path, fileName);
|
---|
| 96 | HeuristicLab.Persistence.Core.ConfigurationService.Instance.LoadSettings();
|
---|
| 97 | XmlGenerator.Serialize(ga, fullPath, CompressionLevel.Fastest);
|
---|
| 98 | }
|
---|
[11846] | 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|