#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 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;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
///
/// An operator that analyzes the validation best symbolic data analysis solution for multi objective symbolic data analysis problems.
///
[Item("SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for multi objective symbolic data analysis problems.")]
[StorableClass]
public abstract class SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer : SymbolicDataAnalysisMultiObjectiveValidationAnalyzer,
ISymbolicDataAnalysisMultiObjectiveAnalyzer
where S : class, ISymbolicDataAnalysisSolution
where T : class, ISymbolicDataAnalysisMultiObjectiveEvaluator
where U : class, IDataAnalysisProblemData {
private const string ValidationBestSolutionsParameterName = "Best validation solutions";
private const string ValidationBestSolutionQualitiesParameterName = "Best validation solution qualities";
private const string UpdateAlwaysParameterName = "Always update best solutions";
#region parameter properties
public ILookupParameter> ValidationBestSolutionsParameter {
get { return (ILookupParameter>)Parameters[ValidationBestSolutionsParameterName]; }
}
public ILookupParameter> ValidationBestSolutionQualitiesParameter {
get { return (ILookupParameter>)Parameters[ValidationBestSolutionQualitiesParameterName]; }
}
public IFixedValueParameter UpdateAlwaysParameter {
get { return (IFixedValueParameter)Parameters[UpdateAlwaysParameterName]; }
}
#endregion
#region properties
public ItemList ValidationBestSolutions {
get { return ValidationBestSolutionsParameter.ActualValue; }
set { ValidationBestSolutionsParameter.ActualValue = value; }
}
public ItemList ValidationBestSolutionQualities {
get { return ValidationBestSolutionQualitiesParameter.ActualValue; }
set { ValidationBestSolutionQualitiesParameter.ActualValue = value; }
}
public BoolValue UpdateAlways {
get { return UpdateAlwaysParameter.Value; }
}
#endregion
[StorableConstructor]
protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
public SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer()
: base() {
Parameters.Add(new LookupParameter>(ValidationBestSolutionsParameterName, "The validation best (Pareto-optimal) symbolic data analysis solutions."));
Parameters.Add(new LookupParameter>(ValidationBestSolutionQualitiesParameterName, "The qualities of the validation best (Pareto-optimal) solutions."));
Parameters.Add(new FixedValueParameter(UpdateAlwaysParameterName, "Determines if the best validation solutions should always be updated regardless of its quality.", new BoolValue(false)));
UpdateAlwaysParameter.Hidden = true;
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
Parameters.Add(new FixedValueParameter(UpdateAlwaysParameterName, "Determines if the best training solutions should always be updated regardless of its quality.", new BoolValue(false)));
UpdateAlwaysParameter.Hidden = true;
}
}
public override IOperation Apply() {
IEnumerable rows = GenerateRowsToEvaluate();
if (!rows.Any()) return base.Apply();
var results = ResultCollection;
// create empty parameter and result values
if (ValidationBestSolutions == null) {
ValidationBestSolutions = new ItemList();
ValidationBestSolutionQualities = new ItemList();
results.Add(new Result(ValidationBestSolutionQualitiesParameter.Name, ValidationBestSolutionQualitiesParameter.Description, ValidationBestSolutionQualities));
results.Add(new Result(ValidationBestSolutionsParameter.Name, ValidationBestSolutionsParameter.Description, ValidationBestSolutions));
}
//if the pareto front of best solutions shall be updated regardless of the quality, the list initialized empty to discard old solutions
IList trainingBestQualities;
if (UpdateAlways.Value) {
trainingBestQualities = new List();
} else {
trainingBestQualities = ValidationBestSolutionQualities.Select(x => x.ToArray()).ToList();
}
#region find best trees
IList nonDominatedIndexes = new List();
ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
bool[] maximization = Maximization.ToArray();
List newNonDominatedQualities = new List();
var evaluator = EvaluatorParameter.ActualValue;
var problemData = ProblemDataParameter.ActualValue;
IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
var qualities = tree
.Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
.ToArray();
for (int i = 0; i < tree.Length; i++) {
if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
IsNonDominated(qualities[i], qualities, maximization)) {
if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer())) {
newNonDominatedQualities.Add(qualities[i]);
nonDominatedIndexes.Add(i);
}
}
}
#endregion
#region update Pareto-optimal solution archive
if (nonDominatedIndexes.Count > 0) {
ItemList nonDominatedQualities = new ItemList();
ItemList nonDominatedSolutions = new ItemList();
// add all new non-dominated solutions to the archive
foreach (var index in nonDominatedIndexes) {
S solution = CreateSolution(tree[index], qualities[index]);
nonDominatedSolutions.Add(solution);
nonDominatedQualities.Add(new DoubleArray(qualities[index]));
}
// add old non-dominated solutions only if they are not dominated by one of the new solutions
for (int i = 0; i < trainingBestQualities.Count; i++) {
if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer())) {
nonDominatedSolutions.Add(ValidationBestSolutions[i]);
nonDominatedQualities.Add(ValidationBestSolutionQualities[i]);
}
}
}
results[ValidationBestSolutionsParameter.Name].Value = nonDominatedSolutions;
results[ValidationBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities;
}
#endregion
return base.Apply();
}
protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
private bool IsNonDominated(double[] point, IList points, bool[] maximization) {
foreach (var refPoint in points) {
bool refPointDominatesPoint = true;
for (int i = 0; i < point.Length; i++) {
refPointDominatesPoint &= IsBetter(refPoint[i], point[i], maximization[i]);
}
if (refPointDominatesPoint) return false;
}
return true;
}
private bool IsBetter(double lhs, double rhs, bool maximization) {
if (maximization) return lhs > rhs;
else return lhs < rhs;
}
private class DoubleArrayComparer : IEqualityComparer {
public bool Equals(double[] x, double[] y) {
if (y.Length != x.Length) throw new ArgumentException();
for (int i = 0; i < x.Length; i++) {
if (!x[i].IsAlmost(y[i])) return false;
}
return true;
}
public int GetHashCode(double[] obj) {
int c = obj.Length;
for (int i = 0; i < obj.Length; i++)
c ^= obj[i].GetHashCode();
return c;
}
}
}
}