[11846] | 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;
|
---|
[11895] | 8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[11846] | 9 | using HeuristicLab.PluginInfrastructure;
|
---|
| 10 |
|
---|
| 11 | namespace HeuristicLab.Problems.GrammaticalOptimization {
|
---|
| 12 | [NonDiscoverableType]
|
---|
[11895] | 13 | [StorableClass]
|
---|
[11857] | 14 | [Item("GenericSymbExprEvaluator", "Evaluator for grammatical optimization problems (using a symbolic expression tree encoding).")]
|
---|
| 15 | public class GenericSymbExprEvaluator : SingleSuccessorOperator, IGenericSymbExprEvaluator {
|
---|
[11846] | 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 |
|
---|
[11895] | 25 | // cannot save these (eval won't work when loaded from file
|
---|
[11846] | 26 | private Func<string, double> evalFunc;
|
---|
| 27 | private Func<ISymbolicExpressionTree, string> toStringFunc;
|
---|
| 28 |
|
---|
[11895] | 29 | [StorableConstructor]
|
---|
| 30 | private GenericSymbExprEvaluator(bool deserializing) : base(deserializing) { }
|
---|
[11857] | 31 | public GenericSymbExprEvaluator(GenericSymbExprEvaluator original, Cloner cloner)
|
---|
[11846] | 32 | : base(original, cloner) {
|
---|
| 33 | this.evalFunc = original.evalFunc;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[11857] | 36 | public GenericSymbExprEvaluator(Func<ISymbolicExpressionTree, string> toStringFunc, Func<string, double> evalFunc)
|
---|
[11846] | 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) {
|
---|
[11857] | 45 | return new GenericSymbExprEvaluator(this, cloner);
|
---|
[11846] | 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 | }
|
---|