Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/05/15 07:03:15 (9 years ago)
Author:
gkronber
Message:

#2283: constant opt, expressioncompiler, autodiff, fixes in GP solvers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/StandardGP.cs

    r11851 r11895  
    11using System;
     2using System.IO;
     3using System.IO.Compression;
    24using System.Linq;
    35using System.Threading;
    46using HeuristicLab.Algorithms.GrammaticalOptimization;
     7using HeuristicLab.Common;
    58using HeuristicLab.Data;
    69using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    7 using HeuristicLab.Optimization;
     10using HeuristicLab.Persistence.Default.Xml;
    811using HeuristicLab.Problems.GrammaticalOptimization;
    912using HeuristicLab.Selection;
    10 using HeuristicLab.Algorithms.GeneticAlgorithm;
    1113
    1214namespace HeuristicLab.Algorithms.GeneticProgramming {
    13   public class StandardGP : SolverBase {
     15  public class StandardGP : SolverBase, IGPSolver {
    1416    public int PopulationSize { get; set; }
    1517    public double MutationRate { get; set; }
     
    2022    private readonly ISymbolicExpressionTreeProblem problem;
    2123    private readonly Random random;
     24    private readonly bool saveAlg;
    2225
    23     public StandardGP(ISymbolicExpressionTreeProblem problem, Random random) {
     26    public StandardGP(ISymbolicExpressionTreeProblem problem, Random random, bool saveAlg = false) {
    2427      this.problem = problem;
    2528      this.random = random;
     
    3033      MaxSolutionSize = 100;
    3134      MaxSolutionDepth = 17;
     35      this.saveAlg = saveAlg;
    3236    }
    3337
     
    3539      var hlProblem = new GenericSymbExprProblem(problem);
    3640      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..1
    39         // need to synchronize in case we are using a parallel engine
    40         lock (onEvalLocker) {
    41           OnSolutionEvaluated(sentence, quality);
    42         }
    43       };
    4441      hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
    4542      hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
     
    5148        ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
    5249        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
    5365
    5466        ga.Problem = hlProblem;
     
    6678
    6779        ga.PopulationSize.Value = PopulationSize;
    68         ga.MaximumGenerations.Value = maxEvaluations / PopulationSize + 1; // one extra generation in case maxEvaluations is not a multiple of PopulationSize
     80        ga.MaximumGenerations.Value = 1000000; // very large value (we stop in the evaluate handler)
    6981        ga.MutationProbability.Value = MutationRate;
    7082
     
    7688
    7789        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        }
    7899      }
    79100    }
Note: See TracChangeset for help on using the changeset viewer.