using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Parameters; using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Problems.DataAnalysis.Symbolic { [Item("User-defined Multi-objective Evaluator", "A multi-objective evaluator that aggregates the output of its evaluator collection.")] [StorableType("779F7C15-63E0-4407-A28D-F4DCB1AA8267")] public class SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator : SymbolicDataAnalysisMultiObjectiveEvaluator where T : class, IDataAnalysisProblemData { private const string EvaluatorsParameterName = "Evaluators"; private const string MaximizationParameterName = "Maximization"; public IFixedValueParameter>> EvaluatorsParameter { get { return (IFixedValueParameter>>)Parameters[EvaluatorsParameterName]; } } public ILookupParameter MaximizationParameter { get { return (ILookupParameter)Parameters[MaximizationParameterName]; } } public ItemList> Evaluators { get { return EvaluatorsParameter.Value; } } public override IEnumerable Maximization => Evaluators.Select(x => x.Maximization); public SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator() { Parameters.Add(new FixedValueParameter>>(EvaluatorsParameterName, new ItemList>())); Parameters.Add(new LookupParameter(MaximizationParameterName)); } public SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator(SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator original, Cloner cloner) : base(original, cloner) { } [StorableConstructor] protected SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator(StorableConstructorFlag deserializing) : base(deserializing) { } public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisMultiObjectiveUserDefinedEvaluator(this, cloner); } public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, T problemData, IEnumerable rows) { var evaluators = Evaluators; var qualities = new double[evaluators.Count]; for (int i = 0; i < qualities.Length; ++i) { qualities[i] = evaluators[i].Evaluate(context, tree, problemData, rows); } QualitiesParameter.ActualValue = new DoubleArray(qualities); return qualities; } public override IOperation InstrumentedApply() { var solution = SymbolicExpressionTreeParameter.ActualValue; var problemData = ProblemDataParameter.ActualValue; if (MaximizationParameter.ActualValue.Length != Evaluators.Count) { MaximizationParameter.ActualValue = new BoolArray(Maximization.ToArray()); } var qualities = Evaluate(ExecutionContext, solution, problemData, problemData.TrainingIndices); QualitiesParameter.ActualValue = new DoubleArray(qualities); return base.InstrumentedApply(); } } }