using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.Programmable { [Item("Multi-objective Analyzer", "Calls the Analyze method of the problem definition.")] [StorableClass] public class MultiObjectiveAnalyzer : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator { public bool EnabledByDefault { get { return true; } } public ILookupParameter EncodingParameter { get { return (ILookupParameter)Parameters["Encoding"]; } } public IScopeTreeLookupParameter QualitiesParameter { get { return (IScopeTreeLookupParameter)Parameters["Qualities"]; } } public ILookupParameter ResultsParameter { get { return (ILookupParameter)Parameters["Results"]; } } public Action AnalyzeAction { get; set; } [StorableConstructor] protected MultiObjectiveAnalyzer(bool deserializing) : base(deserializing) { } protected MultiObjectiveAnalyzer(MultiObjectiveAnalyzer original, Cloner cloner) : base(original, cloner) { } public MultiObjectiveAnalyzer() { Parameters.Add(new LookupParameter("Random", "The random number generator to use.")); Parameters.Add(new LookupParameter("Encoding", "An item that holds the problem's encoding.")); Parameters.Add(new ScopeTreeLookupParameter("Qualities", "The qualities of the parameter vector.")); Parameters.Add(new LookupParameter("Results", "The results collection to write to.")); } public override IDeepCloneable Clone(Cloner cloner) { return new MultiObjectiveAnalyzer(this, cloner); } public override IOperation Apply() { var encoding = EncodingParameter.ActualValue; var results = ResultsParameter.ActualValue; IEnumerable scopes = new[] { ExecutionContext.Scope }; for (var i = 0; i < QualitiesParameter.Depth; i++) scopes = scopes.Select(x => (IEnumerable)x.SubScopes).Aggregate((a, b) => a.Concat(b)); var individuals = scopes.Select(encoding.GetIndividual).ToArray(); AnalyzeAction(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results); return base.Apply(); } } }