#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.ParameterConfigurationEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.MetaOptimization {
///
/// TODO
///
[Item("ReferenceQualityAnalyzer", "TODO")]
[StorableClass]
public sealed class ReferenceQualityAnalyzer : SingleSuccessorOperator, IAnalyzer {
public bool EnabledByDefault {
get { return true; }
}
public ValueLookupParameter ResultsParameter {
get { return (ValueLookupParameter)Parameters["Results"]; }
}
public ScopeTreeLookupParameter ParameterConfigurationParameter {
get { return (ScopeTreeLookupParameter)Parameters["ParameterConfigurationTree"]; }
}
public ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
public LookupParameter ReferenceQualityAveragesParameter {
get { return (LookupParameter)Parameters["ReferenceQualityAverages"]; }
}
public LookupParameter ReferenceQualityDeviationsParameter {
get { return (LookupParameter)Parameters["ReferenceQualityDeviations"]; }
}
public LookupParameter ReferenceEvaluatedSolutionAveragesParameter {
get { return (LookupParameter)Parameters["ReferenceEvaluatedSolutionAverages"]; }
}
public LookupParameter> ProblemsParameter {
get { return (LookupParameter>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
}
public LookupParameter MaximizationParameter {
get { return (LookupParameter)Parameters["Maximization"]; }
}
public LookupParameter QualityWeightParameter {
get { return (LookupParameter)Parameters[MetaOptimizationProblem.QualityWeightParameterName]; }
}
public LookupParameter StandardDeviationWeightParameter {
get { return (LookupParameter)Parameters[MetaOptimizationProblem.StandardDeviationWeightParameterName]; }
}
public LookupParameter EvaluatedSolutionsWeightParameter {
get { return (LookupParameter)Parameters[MetaOptimizationProblem.EvaluatedSolutionsWeightParameterName]; }
}
#region Constructors and Cloning
public ReferenceQualityAnalyzer()
: base() {
Parameters.Add(new ScopeTreeLookupParameter("Quality", ""));
Parameters.Add(new ValueLookupParameter("Results", ""));
Parameters.Add(new ScopeTreeLookupParameter("ParameterConfigurationTree", ""));
Parameters.Add(new LookupParameter("ReferenceQualityAverages", ""));
Parameters.Add(new LookupParameter("ReferenceQualityDeviations", ""));
Parameters.Add(new LookupParameter("ReferenceEvaluatedSolutionAverages", ""));
Parameters.Add(new LookupParameter>(MetaOptimizationProblem.ProblemsParameterName));
Parameters.Add(new LookupParameter("Maximization", "Set to false if the problem should be minimized."));
Parameters.Add(new LookupParameter(MetaOptimizationProblem.QualityWeightParameterName));
Parameters.Add(new LookupParameter(MetaOptimizationProblem.StandardDeviationWeightParameterName));
Parameters.Add(new LookupParameter(MetaOptimizationProblem.EvaluatedSolutionsWeightParameterName));
}
[StorableConstructor]
private ReferenceQualityAnalyzer(bool deserializing) : base(deserializing) { }
private ReferenceQualityAnalyzer(ReferenceQualityAnalyzer original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new ReferenceQualityAnalyzer(this, cloner);
}
#endregion
public override IOperation Apply() {
ResultCollection results = ResultsParameter.ActualValue;
ItemArray solutions = ParameterConfigurationParameter.ActualValue;
ItemArray qualities = QualityParameter.ActualValue;
bool maximization = MaximizationParameter.ActualValue.Value;
double qualityWeight = QualityWeightParameter.ActualValue.Value;
double standardDeviationWeight = StandardDeviationWeightParameter.ActualValue.Value;
double evaluatedSolutionsWeight = EvaluatedSolutionsWeightParameter.ActualValue.Value;
if (ReferenceQualityAveragesParameter.ActualValue == null) {
// this is generation zero. calculate the reference values and apply them on population. in future generations `AlgorithmRunsAnalyzer` will do the nomalization
DoubleArray referenceQualityAverages = CalculateReferenceQualityAverages(solutions, maximization);
DoubleArray referenceQualityDeviations = CalculateReferenceQualityDeviations(solutions, maximization);
DoubleArray referenceEvaluatedSolutionAverages = CalculateReferenceEvaluatedSolutionAverages(solutions, maximization);
ReferenceQualityAveragesParameter.ActualValue = referenceQualityAverages;
ReferenceQualityDeviationsParameter.ActualValue = referenceQualityDeviations;
ReferenceEvaluatedSolutionAveragesParameter.ActualValue = referenceEvaluatedSolutionAverages;
NormalizePopulation(solutions, qualities, referenceQualityAverages, referenceQualityDeviations, referenceEvaluatedSolutionAverages, qualityWeight, standardDeviationWeight, evaluatedSolutionsWeight, maximization);
results.Add(new Result("ReferenceQualities", referenceQualityAverages));
results.Add(new Result("ReferenceQualityDeviations", referenceQualityDeviations));
results.Add(new Result("ReferenceEvaluatedSolutionAverages", referenceEvaluatedSolutionAverages));
}
return base.Apply();
}
private DoubleArray CalculateReferenceQualityAverages(ItemArray solutions, bool maximization) {
DoubleArray references = new DoubleArray(ProblemsParameter.ActualValue.Count);
for (int pi = 0; pi < ProblemsParameter.ActualValue.Count; pi++) {
if (maximization)
references[pi] = solutions.Where(x => x.AverageQualities != null).Select(x => x.AverageQualities[pi]).Max();
else
references[pi] = solutions.Where(x => x.AverageQualities != null).Select(x => x.AverageQualities[pi]).Min();
}
return references;
}
private DoubleArray CalculateReferenceQualityDeviations(ItemArray solutions, bool maximization) {
DoubleArray references = new DoubleArray(ProblemsParameter.ActualValue.Count);
for (int pi = 0; pi < ProblemsParameter.ActualValue.Count; pi++) {
if (maximization)
references[pi] = solutions.Where(x => x.QualityStandardDeviations != null).Select(x => x.QualityStandardDeviations[pi]).Max();
else
references[pi] = solutions.Where(x => x.QualityStandardDeviations != null).Select(x => x.QualityStandardDeviations[pi]).Min();
}
return references;
}
private DoubleArray CalculateReferenceEvaluatedSolutionAverages(ItemArray solutions, bool maximization) {
DoubleArray references = new DoubleArray(ProblemsParameter.ActualValue.Count);
for (int pi = 0; pi < ProblemsParameter.ActualValue.Count; pi++) {
if (maximization)
references[pi] = solutions.Where(x => x.AverageEvaluatedSolutions != null).Select(x => x.AverageEvaluatedSolutions[pi]).Max();
else
references[pi] = solutions.Where(x => x.AverageEvaluatedSolutions != null).Select(x => x.AverageEvaluatedSolutions[pi]).Min();
}
return references;
}
private void NormalizePopulation(ItemArray solutions, ItemArray qualities,
DoubleArray referenceQualityAverages,
DoubleArray referenceQualityDeviations,
DoubleArray referenceEvaluatedSolutionAverages,
double qualityAveragesWeight,
double qualityDeviationsWeight,
double evaluatedSolutionsWeight,
bool maximization) {
for (int i = 0; i < solutions.Length; i++) {
if (solutions[i].AverageQualities == null || solutions[i].QualityStandardDeviations == null || solutions[i].AverageEvaluatedSolutions == null) {
// this parameterConfigurationTree has not been evaluated correctly (due to a faulty configuration, which led to an exception)
// since we are in generation zero, there is no WorstQuality available for a penalty value
double penaltyValue = maximization ? double.MinValue : double.MaxValue;
qualities[i].Value = penaltyValue;
} else {
qualities[i].Value = MetaOptimizationUtil.Normalize(solutions[i],
referenceQualityAverages.ToArray(),
referenceQualityDeviations.ToArray(),
referenceEvaluatedSolutionAverages.ToArray(),
qualityAveragesWeight,
qualityDeviationsWeight,
evaluatedSolutionsWeight,
maximization);
}
}
}
}
}