[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;
|
---|
[11972] | 31 | TournamentGroupSize = 4;
|
---|
[11846] | 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;
|
---|
[11972] | 67 | //ga.Problem.SolutionCreatorParameter.ActualValue = new FullTreeCreator();
|
---|
[11846] | 68 | var mutator = (MultiSymbolicExpressionTreeManipulator)ga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
|
---|
| 69 | foreach (var op in mutator.Operators) {
|
---|
| 70 | if (op.Name == "ChangeNodeTypeManipulation"
|
---|
| 71 | || op.Name == "ReplaceBranchManipulation") mutator.Operators.SetItemCheckedState(op, true);
|
---|
| 72 | else mutator.Operators.SetItemCheckedState(op, false);
|
---|
| 73 | }
|
---|
| 74 | ga.Mutator = mutator;
|
---|
| 75 | ga.Crossover = ga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
|
---|
| 76 | var selector = (TournamentSelector)ga.SelectorParameter.ValidValues.Single(op => op.Name == "TournamentSelector");
|
---|
| 77 | selector.GroupSizeParameter.Value = new IntValue(TournamentGroupSize);
|
---|
| 78 | ga.Selector = selector;
|
---|
| 79 |
|
---|
| 80 | ga.PopulationSize.Value = PopulationSize;
|
---|
[11895] | 81 | ga.MaximumGenerations.Value = 1000000; // very large value (we stop in the evaluate handler)
|
---|
[11846] | 82 | ga.MutationProbability.Value = MutationRate;
|
---|
| 83 |
|
---|
| 84 | ga.SetSeedRandomly = new BoolValue(false);
|
---|
| 85 | ga.Seed = new IntValue(random.Next());
|
---|
| 86 |
|
---|
| 87 | ga.Prepare();
|
---|
| 88 | ga.Start();
|
---|
| 89 |
|
---|
| 90 | wh.WaitOne();
|
---|
[11895] | 91 |
|
---|
| 92 |
|
---|
| 93 | if (saveAlg) {
|
---|
| 94 | var path = @"C:\Users\P24581\Desktop";
|
---|
| 95 | 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);
|
---|
| 96 | var fullPath = Path.Combine(path, fileName);
|
---|
| 97 | HeuristicLab.Persistence.Core.ConfigurationService.Instance.LoadSettings();
|
---|
| 98 | XmlGenerator.Serialize(ga, fullPath, CompressionLevel.Fastest);
|
---|
| 99 | }
|
---|
[11846] | 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|