Changeset 11895 for branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/StandardGP.cs
- Timestamp:
- 02/05/15 07:03:15 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/StandardGP.cs
r11851 r11895 1 1 using System; 2 using System.IO; 3 using System.IO.Compression; 2 4 using System.Linq; 3 5 using System.Threading; 4 6 using HeuristicLab.Algorithms.GrammaticalOptimization; 7 using HeuristicLab.Common; 5 8 using HeuristicLab.Data; 6 9 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 7 using HeuristicLab. Optimization;10 using HeuristicLab.Persistence.Default.Xml; 8 11 using HeuristicLab.Problems.GrammaticalOptimization; 9 12 using HeuristicLab.Selection; 10 using HeuristicLab.Algorithms.GeneticAlgorithm;11 13 12 14 namespace HeuristicLab.Algorithms.GeneticProgramming { 13 public class StandardGP : SolverBase {15 public class StandardGP : SolverBase, IGPSolver { 14 16 public int PopulationSize { get; set; } 15 17 public double MutationRate { get; set; } … … 20 22 private readonly ISymbolicExpressionTreeProblem problem; 21 23 private readonly Random random; 24 private readonly bool saveAlg; 22 25 23 public StandardGP(ISymbolicExpressionTreeProblem problem, Random random ) {26 public StandardGP(ISymbolicExpressionTreeProblem problem, Random random, bool saveAlg = false) { 24 27 this.problem = problem; 25 28 this.random = random; … … 30 33 MaxSolutionSize = 100; 31 34 MaxSolutionDepth = 17; 35 this.saveAlg = saveAlg; 32 36 } 33 37 … … 35 39 var hlProblem = new GenericSymbExprProblem(problem); 36 40 var onEvalLocker = new object(); 37 hlProblem.Evaluator.SolutionEvaluated += (sentence, quality) => {38 // raise solution evaluated event for each GP solution, don't scale quality to 0..139 // need to synchronize in case we are using a parallel engine40 lock (onEvalLocker) {41 OnSolutionEvaluated(sentence, quality);42 }43 };44 41 hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize; 45 42 hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth; … … 51 48 ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); }; 52 49 ga.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 ga.Stop(); 61 } 62 } 63 }; 64 53 65 54 66 ga.Problem = hlProblem; … … 66 78 67 79 ga.PopulationSize.Value = PopulationSize; 68 ga.MaximumGenerations.Value = maxEvaluations / PopulationSize + 1; // one extra generation in case maxEvaluations is not a multiple of PopulationSize80 ga.MaximumGenerations.Value = 1000000; // very large value (we stop in the evaluate handler) 69 81 ga.MutationProbability.Value = MutationRate; 70 82 … … 76 88 77 89 wh.WaitOne(); 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 } 78 99 } 79 100 }
Note: See TracChangeset
for help on using the changeset viewer.