Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2993_UserDefinedEvaluators/HeuristicLab.Problems.DataAnalysis.Symbolic.UserDefinedEval/SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator.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.3 KB
Line 
1using HEAL.Attic;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6using HeuristicLab.Parameters;
7using System.Collections.Generic;
8using System.Linq;
9
10namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
11  [Item("User-defined Multi-objective Evaluator", "A multi-objective evaluator that aggregates the output of its evaluator collection.")]
12  [StorableType("779F7C15-63E0-4407-A28D-F4DCB1AA8267")]
13  public class SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator<T> : SymbolicDataAnalysisMultiObjectiveEvaluator<T> where T : class, IDataAnalysisProblemData {
14    private const string EvaluatorsParameterName = "Evaluators";
15    private const string MaximizationParameterName = "Maximization";
16
17    public IFixedValueParameter<ItemList<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>> EvaluatorsParameter {
18      get { return (IFixedValueParameter<ItemList<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>>)Parameters[EvaluatorsParameterName]; }
19    }
20
21    public ILookupParameter<BoolArray> MaximizationParameter {
22      get { return (ILookupParameter<BoolArray>)Parameters[MaximizationParameterName]; }
23    }
24
25    public ItemList<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> Evaluators {
26      get { return EvaluatorsParameter.Value; }
27    }
28
29    public override IEnumerable<bool> Maximization => Evaluators.Select(x => x.Maximization);
30
31    public SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator() {
32      Parameters.Add(new FixedValueParameter<ItemList<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>>(EvaluatorsParameterName, new ItemList<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>()));
33      Parameters.Add(new LookupParameter<BoolArray>(MaximizationParameterName));
34    }
35
36    public SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator(SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator<T> original, Cloner cloner) : base(original, cloner) { }
37
38    [StorableConstructor]
39    protected SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator(StorableConstructorFlag deserializing) : base(deserializing) { }
40
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator<T>(this, cloner);
43    }
44
45    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, T problemData, IEnumerable<int> rows) {
46      var evaluators = Evaluators;
47      var qualities = new double[evaluators.Count];
48      for (int i = 0; i < qualities.Length; ++i) {
49        qualities[i] = evaluators[i].Evaluate(context, tree, problemData, rows);
50      }
51      QualitiesParameter.ActualValue = new DoubleArray(qualities);
52      return qualities;
53    }
54
55    public override IOperation InstrumentedApply() {
56      var solution = SymbolicExpressionTreeParameter.ActualValue;
57      var problemData = ProblemDataParameter.ActualValue;
58
59      if (MaximizationParameter.ActualValue.Length != Evaluators.Count) {
60        MaximizationParameter.ActualValue = new BoolArray(Maximization.ToArray());
61      }
62
63      var qualities = Evaluate(ExecutionContext, solution, problemData, problemData.TrainingIndices);
64      QualitiesParameter.ActualValue = new DoubleArray(qualities);
65      return base.InstrumentedApply();
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.