Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2993_UserDefinedEvaluators/HeuristicLab.Problems.DataAnalysis.Symbolic.UserDefinedEval/SymbolicDataAnalysisSingleObjectiveUserDefinedEvaluator.cs @ 17506

Last change on this file since 17506 was 16687, checked in by bburlacu, 6 years ago

#2993: Initial implementation of user-defined evaluators as separate plugin.

File size: 3.8 KB
Line 
1using HEAL.Attic;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6using HeuristicLab.Parameters;
7using System;
8using System.Collections.Generic;
9
10namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
11  [Item("User-defined Single-objective Evaluator", "A single-objective evaluator that aggregates the output of its evaluator collection.")]
12  [StorableType("1E668115-AF2A-4037-8E3C-7B279EC72770")]
13  public class SymbolicDataAnalysisSingleObjectiveUserDefinedEvaluator<T> : SymbolicDataAnalysisSingleObjectiveEvaluator<T> where T : class, IDataAnalysisProblemData {
14    private const string EvaluatorsParameterName = "Evaluators";
15    private const string MaximizationParameterName = "Maximization";
16    private const string ObjectiveWeightsParameterName = "Weights";
17
18    public IValueParameter<DoubleArray> ObjectiveWeightsParameter {
19      get { return (IValueParameter<DoubleArray>)Parameters[ObjectiveWeightsParameterName]; }
20    }
21
22    public IFixedValueParameter<ItemList<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>> EvaluatorsParameter {
23      get { return (IFixedValueParameter<ItemList<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>>)Parameters[EvaluatorsParameterName]; }
24    }
25
26    public IFixedValueParameter<BoolValue> MaximizationParameter {
27      get { return (IFixedValueParameter<BoolValue>)Parameters[MaximizationParameterName]; }
28    }
29
30    public ItemList<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> Evaluators {
31      get { return EvaluatorsParameter.Value; }
32    }
33
34    public DoubleArray ObjectiveWeights => ObjectiveWeightsParameter.Value;
35
36    public override bool Maximization => false;
37
38    public SymbolicDataAnalysisSingleObjectiveUserDefinedEvaluator() : base() {
39      Parameters.Add(new FixedValueParameter<ItemList<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>>(EvaluatorsParameterName, new ItemList<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>()));
40      Parameters.Add(new FixedValueParameter<BoolValue>(MaximizationParameterName, new BoolValue(false)));
41      Parameters.Add(new ValueParameter<DoubleArray>(ObjectiveWeightsParameterName, new DoubleArray()));
42    }
43
44    public SymbolicDataAnalysisSingleObjectiveUserDefinedEvaluator(SymbolicDataAnalysisSingleObjectiveUserDefinedEvaluator<T> original, Cloner cloner) : base(original, cloner) { }
45
46    [StorableConstructor]
47    protected SymbolicDataAnalysisSingleObjectiveUserDefinedEvaluator(StorableConstructorFlag deserializing) : base(deserializing) { }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new SymbolicDataAnalysisSingleObjectiveUserDefinedEvaluator<T>(this, cloner);
51    }
52
53    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, T problemData, IEnumerable<int> rows) {
54      var weights = ObjectiveWeights;
55
56      double eval(ISymbolicDataAnalysisSingleObjectiveEvaluator<T> e) {
57        var q = e.Evaluate(context, tree, problemData, rows);
58        return e.Maximization ? -q : q;
59      }
60
61      var quality = 0d;
62      for (int i = 0; i < weights.Length; ++i) {
63        quality += weights[i] * eval(Evaluators[i]);
64      }
65
66      if (double.IsNaN(quality) || double.IsInfinity(quality)) {
67        quality = double.MaxValue;
68      }
69
70      if (DecimalPlaces >= 0) {
71        quality = Math.Round(quality, DecimalPlaces);
72      }
73
74      return quality;
75    }
76
77    public override IOperation InstrumentedApply() {
78      var solution = SymbolicExpressionTreeParameter.ActualValue;
79      var problemData = ProblemDataParameter.ActualValue;
80
81      var quality = Evaluate(ExecutionContext, solution, problemData, problemData.TrainingIndices);
82      QualityParameter.ActualValue = new DoubleValue(quality);
83      return base.InstrumentedApply();
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.