1 | using System;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
6 | using HeuristicLab.Operators;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.PluginInfrastructure;
|
---|
10 |
|
---|
11 | namespace 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 | }
|
---|