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("Single-objective Analyzer", "Calls the script's Analyze method to be able to write into the results collection.")] [StorableClass] public class SingleObjectiveAnalyzer : SingleSuccessorOperator, ISingleObjectiveProgrammableProblemAnalyzer { public bool EnabledByDefault { get { return true; } } public ILookupParameter ProblemDefinitionParameter { get { return (ILookupParameter)Parameters["ProblemDefinition"]; } } public ILookupParameter EncodingParameter { get { return (ILookupParameter)Parameters["Encoding"]; } } public IScopeTreeLookupParameter QualityParameter { get { return (IScopeTreeLookupParameter)Parameters["Quality"]; } } public ILookupParameter ResultsParameter { get { return (ILookupParameter)Parameters["Results"]; } } [StorableConstructor] protected SingleObjectiveAnalyzer(bool deserializing) : base(deserializing) { } protected SingleObjectiveAnalyzer(SingleObjectiveAnalyzer original, Cloner cloner) : base(original, cloner) { } public SingleObjectiveAnalyzer() { Parameters.Add(new LookupParameter("Random", "The random number generator to use.")); Parameters.Add(new LookupParameter("ProblemDefinition", "The host that holds the problem definition.")); Parameters.Add(new LookupParameter("Encoding", "An item that holds the problem's encoding.")); Parameters.Add(new ScopeTreeLookupParameter("Quality", "The quality of the parameter vector.")); Parameters.Add(new LookupParameter("Results", "The results collection to write to.")); } public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveAnalyzer(this, cloner); } public override IOperation Apply() { var definition = ProblemDefinitionParameter.ActualValue; if (definition == null) throw new InvalidOperationException("Problem definition is null"); var encoding = EncodingParameter.ActualValue; var results = ResultsParameter.ActualValue; IEnumerable scopes = new[] { ExecutionContext.Scope }; for (var i = 0; i < QualityParameter.Depth; i++) scopes = scopes.Select(x => (IEnumerable)x.SubScopes).Aggregate((a, b) => a.Concat(b)); var individuals = scopes.Select(encoding.CreateIndividual).ToArray(); definition.Analyze(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results); return base.Apply(); } } }