#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.PluginInfrastructure;
using System.Linq;
namespace HeuristicLab.Analysis.FitnessLandscape {
[Item("QualityTrailAnalyzer", "An analyzer that creates a quality trail which can be further analyzed by several analyzers.")]
[StorableClass]
public class QualityTrailMultiAnalyzer : CheckedMultiOperator, IAnalyzer {
public bool EnabledByDefault {
get { return false; }
}
public override bool CanChangeName {
get { return false; }
}
public IValueLookupParameter UpdateIntervalParameter {
get { return (IValueLookupParameter)Parameters["QualityTrailUpdateInterval"]; }
}
public ILookupParameter UpdateCounterParameter {
get { return (ILookupParameter)Parameters["QualityTrailUpdateCounter"]; }
}
public ILookupParameter QualityTrailParameter {
get { return (ILookupParameter)Parameters["Quality Trail"]; }
}
public ILookupParameter ResultsParameter {
get { return (ILookupParameter)Parameters["Results"]; }
}
public IScopeTreeLookupParameter QualityParameter {
get { return (IScopeTreeLookupParameter)Parameters["Quality"]; }
}
public ILookupParameter MaximizationParameter {
get { return (ILookupParameter)Parameters["Maximization"]; }
}
public IntValue UpdateInterval {
get { return UpdateIntervalParameter.Value; }
set { UpdateIntervalParameter.Value = value; }
}
[StorableConstructor]
protected QualityTrailMultiAnalyzer(bool deserializing) : base(deserializing) { }
protected QualityTrailMultiAnalyzer(QualityTrailMultiAnalyzer original, Cloner cloner) : base(original, cloner) { }
public QualityTrailMultiAnalyzer()
: base() {
Parameters.Add(new ValueLookupParameter("QualityTrailUpdateInterval", "The interval at which the contained analyzers should be applied.", new IntValue(1)));
Parameters.Add(new LookupParameter("QualityTrailUpdateCounter", "The number of times the Analyzer was called since the last update."));
Parameters.Add(new LookupParameter("Quality Trail", "All historical quality values throughout the algorithm run"));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "Quality values of the whole population", 1));
Parameters.Add(new LookupParameter("Results", "The collected results"));
Parameters.Add(new LookupParameter("Maximization", "Whether the problem is a maximization problem"));
foreach (var analyzer in ApplicationManager.Manager.GetInstances()) {
Operators.Add(analyzer, !(analyzer is FitnessCloudAnalyzer));
}
}
public override IDeepCloneable Clone(Cloner cloner) {
return new QualityTrailMultiAnalyzer(this, cloner);
}
public override IOperation InstrumentedApply() {
var maximization = MaximizationParameter.ActualValue.Value;
var qualityTrail = QualityTrailParameter.ActualValue;
if (qualityTrail == null) {
qualityTrail = new DataTable("Quality Trail");
qualityTrail.Rows.Add(new DataRow("Qualities"));
QualityTrailParameter.ActualValue = qualityTrail;
}
var qualities = QualityParameter.ActualValue.Select(x => x.Value);
qualityTrail.Rows["Qualities"].Values.Add(maximization ? qualities.Max() : qualities.Min());
var interval = UpdateIntervalParameter.ActualValue.Value;
var counter = UpdateCounterParameter.ActualValue;
if (counter == null) {
counter = new IntValue(0);
UpdateCounterParameter.ActualValue = counter;
}
counter.Value++;
var next = new OperationCollection();
if (counter.Value % interval == 0) {
counter.Value = 0;
foreach (var item in Operators.CheckedItems) next.Add(ExecutionContext.CreateOperation(item.Value));
}
next.Add(base.InstrumentedApply());
return next;
}
}
}