#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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 System.Linq;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Optimization.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.PluginInfrastructure;
using HEAL.Attic;
namespace HeuristicLab.Analysis.FitnessLandscape {
[Item("QualityTrailAnalyzer", "An analyzer that creates a quality trail which can be further analyzed by several analyzers.")]
[StorableType("E02FC8FB-85AD-42AB-8E14-EA509579C8BE")]
public class QualityTrailMultiAnalyzer : CheckedMultiOperator, IAnalyzer {
public bool EnabledByDefault {
get { return false; }
}
public override bool CanChangeName {
get { return false; }
}
public ValueLookupParameter UpdateIntervalParameter {
get { return (ValueLookupParameter)Parameters["QualityTrailUpdateInterval"]; }
}
public LookupParameter UpdateCounterParameter {
get { return (LookupParameter)Parameters["QualityTrailUpdateCounter"]; }
}
public ValueLookupParameter QualityTrailParameter {
get { return (ValueLookupParameter)Parameters["Quality Trail"]; }
}
public LookupParameter ResultsParameter {
get { return (LookupParameter)Parameters["Results"]; }
}
public LookupParameter QualityParameter {
get { return (LookupParameter)Parameters["Quality"]; }
}
public ScopeTreeLookupParameter QualitiesParameter {
get { return (ScopeTreeLookupParameter)Parameters["Qualities"]; }
}
public OperatorParameter ResultsCollector {
get { return (OperatorParameter)Parameters["ResultsCollector"]; }
}
public LookupParameter MaximizationParameter {
get { return (LookupParameter)Parameters["Maximization"]; }
}
public IntValue UpdateInterval {
get { return UpdateIntervalParameter.Value; }
set { UpdateIntervalParameter.Value = value; }
}
[StorableConstructor]
protected QualityTrailMultiAnalyzer(StorableConstructorFlag _) : base(_) { }
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 ValueLookupParameter("Quality Trail", "All historical quality values throughout the algorithm run"));
Parameters.Add(new LookupParameter("Quality", "Single quality value from the current iteration/generation"));
Parameters.Add(new ScopeTreeLookupParameter("Qualities", "Quality values of the whole population", "Quality", 1));
Parameters.Add(new LookupParameter("Results", "The collected results"));
Parameters.Add(new OperatorParameter("ResultsCollector"));
Parameters.Add(new LookupParameter("Maximization", "Whether the problem is a maximization problem"));
foreach (var analyzer in ApplicationManager.Manager.GetInstances())
Operators.Add(analyzer);
var resultsCollector = new ResultsCollector();
resultsCollector.CollectedValues.Add(new LookupParameter(QualityTrailParameter.Name));
ResultsCollector.Value = resultsCollector;
}
public override IDeepCloneable Clone(Cloner cloner) {
return new QualityTrailMultiAnalyzer(this, cloner);
}
public override IOperation InstrumentedApply() {
UpdateQualityTrail();
IntValue interval = UpdateIntervalParameter.ActualValue;
if (interval == null) interval = new IntValue(1);
IntValue counter = UpdateCounterParameter.ActualValue;
if (counter == null) {
counter = new IntValue(interval.Value);
UpdateCounterParameter.ActualValue = counter;
} else counter.Value++;
OperationCollection next = new OperationCollection();
next.Add(ExecutionContext.CreateOperation(ResultsCollector.Value));
if (counter.Value == interval.Value) {
counter.Value = 0;
foreach (IndexedItem item in Operators.CheckedItems)
next.Add(ExecutionContext.CreateOperation(item.Value));
}
next.Add(base.InstrumentedApply());
return next;
}
private void UpdateQualityTrail() {
DataTable qualityTrail = QualityTrailParameter.ActualValue;
if (qualityTrail == null) {
qualityTrail = new DataTable("Quality Trail");
qualityTrail.Rows.Add(new DataRow("Qualities"));
QualityTrailParameter.ActualValue = qualityTrail;
}
if (QualityParameter.ActualValue != null)
qualityTrail.Rows["Qualities"].Values.Add(QualityParameter.ActualValue.Value);
if (QualitiesParameter.ActualName != null &&
QualitiesParameter.ActualValue.Count() > 0 &&
MaximizationParameter != null)
if (MaximizationParameter.ActualValue.Value)
qualityTrail.Rows["Qualities"].Values.Add(QualitiesParameter.ActualValue.Select(dv => dv.Value).Max());
else
qualityTrail.Rows["Qualities"].Values.Add(QualitiesParameter.ActualValue.Select(dv => dv.Value).Min());
}
}
}