Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/GenericSymbExprEvaluator.cs @ 12563

Last change on this file since 12563 was 11895, checked in by gkronber, 10 years ago

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

File size: 2.5 KB
Line 
1using System;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6using HeuristicLab.Operators;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.PluginInfrastructure;
10
11namespace HeuristicLab.Problems.GrammaticalOptimization {
12  [NonDiscoverableType]
13  [StorableClass]
14  [Item("GenericSymbExprEvaluator", "Evaluator for grammatical optimization problems (using a symbolic expression tree encoding).")]
15  public class GenericSymbExprEvaluator : SingleSuccessorOperator, IGenericSymbExprEvaluator {
16    public event Action<string, double> SolutionEvaluated;
17
18    public ILookupParameter<DoubleValue> QualityParameter {
19      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
20    }
21    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
22      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
23    }
24
25    // cannot save these (eval won't work when loaded from file
26    private Func<string, double> evalFunc;
27    private Func<ISymbolicExpressionTree, string> toStringFunc;
28
29    [StorableConstructor]
30    private GenericSymbExprEvaluator(bool deserializing) : base(deserializing) { }
31    public GenericSymbExprEvaluator(GenericSymbExprEvaluator original, Cloner cloner)
32      : base(original, cloner) {
33      this.evalFunc = original.evalFunc;
34    }
35
36    public GenericSymbExprEvaluator(Func<ISymbolicExpressionTree, string> toStringFunc, Func<string, double> evalFunc)
37      : base() {
38      Parameters.Add(new LookupParameter<DoubleValue>("Quality"));
39      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree"));
40      this.evalFunc = evalFunc;
41      this.toStringFunc = toStringFunc;
42    }
43
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new GenericSymbExprEvaluator(this, cloner);
46    }
47
48    public override IOperation Apply() {
49      var tree = SymbolicExpressionTreeParameter.ActualValue;
50      var sentence = toStringFunc(tree);
51      var q = evalFunc(sentence);
52      QualityParameter.ActualValue = new DoubleValue(q);
53
54      RaiseSolutionEvaluated(sentence, q);
55
56      return base.Apply();
57    }
58
59    private void RaiseSolutionEvaluated(string sentence, double quality) {
60      var handler = SolutionEvaluated;
61      if (handler != null) handler(sentence, quality);
62    }
63  }
64}
Note: See TracBrowser for help on using the repository browser.