Free cookie consent management tool by TermsFeed Policy Generator

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

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

Location:
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming
Files:
1 added
4 edited

Legend:

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

    r11857 r11895  
    66using HeuristicLab.Operators;
    77using HeuristicLab.Parameters;
     8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    89using HeuristicLab.PluginInfrastructure;
    910
    1011namespace HeuristicLab.Problems.GrammaticalOptimization {
    11   // this type is not storable because we use a func<ITree,double> for evaluation, which references back to the original grammatical optimization problem
    1212  [NonDiscoverableType]
     13  [StorableClass]
    1314  [Item("GenericSymbExprEvaluator", "Evaluator for grammatical optimization problems (using a symbolic expression tree encoding).")]
    1415  public class GenericSymbExprEvaluator : SingleSuccessorOperator, IGenericSymbExprEvaluator {
     
    2223    }
    2324
     25    // cannot save these (eval won't work when loaded from file
    2426    private Func<string, double> evalFunc;
    2527    private Func<ISymbolicExpressionTree, string> toStringFunc;
    2628
     29    [StorableConstructor]
     30    private GenericSymbExprEvaluator(bool deserializing) : base(deserializing) { }
    2731    public GenericSymbExprEvaluator(GenericSymbExprEvaluator original, Cloner cloner)
    2832      : base(original, cloner) {
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/HeuristicLab.Algorithms.GeneticProgramming.csproj

    r11857 r11895  
    3636    <Reference Include="HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3">
    3737      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3.dll</HintPath>
     38    </Reference>
     39    <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     40      <SpecificVersion>False</SpecificVersion>
     41      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Analysis-3.3.dll</HintPath>
    3842    </Reference>
    3943    <Reference Include="HeuristicLab.Collections-3.3">
     
    7579      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Selection-3.3.dll</HintPath>
    7680    </Reference>
     81    <Reference Include="HeuristicLab.SequentialEngine-3.3">
     82      <HintPath>..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.SequentialEngine-3.3.dll</HintPath>
     83    </Reference>
    7784    <Reference Include="System" />
    7885    <Reference Include="System.Core" />
     
    8087  </ItemGroup>
    8188  <ItemGroup>
     89    <Compile Include="IGPSolver.cs" />
    8290    <Compile Include="GenericSymbExprEvaluator.cs" />
    8391    <Compile Include="GenericSymbExprGrammar.cs" />
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/OffspringSelectionGP.cs

    r11851 r11895  
    11using System;
     2using System.IO;
     3using System.IO.Compression;
    24using System.Linq;
    35using System.Threading;
    46using HeuristicLab.Algorithms.GrammaticalOptimization;
     7using HeuristicLab.Analysis;
     8using HeuristicLab.Common;
    59using HeuristicLab.Data;
    610using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    7 using HeuristicLab.Optimization;
     11using HeuristicLab.Persistence.Default.Xml;
    812using HeuristicLab.Problems.GrammaticalOptimization;
    9 using HeuristicLab.Selection;
    10 using HeuristicLab.Algorithms.GeneticAlgorithm;
    1113
    1214namespace HeuristicLab.Algorithms.GeneticProgramming {
    13   public class OffspringSelectionGP : SolverBase {
     15  public class OffspringSelectionGP : SolverBase, IGPSolver {
    1416    public int PopulationSize { get; set; }
    1517    public double MutationRate { get; set; }
     
    1921    private readonly ISymbolicExpressionTreeProblem problem;
    2022    private readonly Random random;
     23    private readonly bool saveAlg;
    2124
    22     public OffspringSelectionGP(ISymbolicExpressionTreeProblem problem, Random random) {
     25    public OffspringSelectionGP(ISymbolicExpressionTreeProblem problem, Random random, bool saveAlg = false) {
    2326      this.problem = problem;
    2427      this.random = random;
     
    2831      MaxSolutionSize = 100;
    2932      MaxSolutionDepth = 17;
     33      this.saveAlg = saveAlg;
    3034    }
    3135
     
    3337      var hlProblem = new GenericSymbExprProblem(problem);
    3438      var onEvalLocker = new object();
    35       hlProblem.Evaluator.SolutionEvaluated += (sentence, quality) => {
    36         // raise solution evaluated event for each GP solution, don't scale quality to 0..1
    37         // need to synchronize in case we are using a parallel engine
    38         lock (onEvalLocker) {
    39           OnSolutionEvaluated(sentence, quality);
    40         }
    41       };
    4239      hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize;
    4340      hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth;
     
    4542      using (var wh = new AutoResetEvent(false)) {
    4643        var osga = new OffspringSelectionGeneticAlgorithm.OffspringSelectionGeneticAlgorithm();
    47         osga.Engine = new ParallelEngine.ParallelEngine();
     44        // osga.Engine = new ParallelEngine.ParallelEngine();
     45        osga.Engine = new SequentialEngine.SequentialEngine();
    4846        osga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };
    4947        osga.Stopped += (sender, args) => { wh.Set(); };
     48
     49        int numEvals = 0;
     50        hlProblem.Evaluator.SolutionEvaluated += (sentence, quality) => {
     51          // raise solution evaluated event for each GP solution, don't scale quality to 0..1
     52          // need to synchronize in case we are using a parallel engine
     53          lock (onEvalLocker) {
     54            OnSolutionEvaluated(sentence, quality);
     55
     56            // stop when maxEvals has been reached
     57            if (numEvals++ >= maxEvaluations) {
     58              osga.Stop();
     59            }
     60          }
     61        };
     62
    5063
    5164        osga.Problem = hlProblem;
     
    5972        osga.Crossover = osga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");
    6073        osga.Selector = osga.SelectorParameter.ValidValues.Single(op => op.Name == "GenderSpecificSelection");
     74        var multiAnalzer = (MultiAnalyzer)osga.Analyzer;
     75        multiAnalzer.Operators.Add(new BestSymbolicExpressionTreeAnalyzer());
    6176
    6277        osga.PopulationSize.Value = PopulationSize;
    63         osga.MaximumGenerations.Value = 100000; // some very large value (we stop based on evaluations)
    64         osga.MaximumSelectionPressure.Value = 1000;
     78        osga.MaximumGenerations.Value = 1000000; // some very large value (we stop based on evaluations)
     79        osga.MaximumSelectionPressure.Value = 1000000;
    6580        osga.MaximumEvaluatedSolutions.Value = maxEvaluations;
    6681        osga.MutationProbability.Value = MutationRate;
     82        osga.ComparisonFactorLowerBound.Value = 1.0;
     83        osga.ComparisonFactorUpperBound.Value = 1.0;
     84        osga.SuccessRatio.Value = 1.0;
    6785
    6886        osga.SetSeedRandomly = new BoolValue(false);
     
    7391
    7492        wh.WaitOne();
     93
     94
     95        if (saveAlg) {
     96          var path = @"C:\Users\P24581\Desktop";
     97          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);
     98          var fullPath = Path.Combine(path, fileName);
     99          HeuristicLab.Persistence.Core.ConfigurationService.Instance.LoadSettings();
     100          XmlGenerator.Serialize(osga, fullPath, CompressionLevel.Fastest);
     101        }
    75102      }
    76103    }
  • 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.