#region License Information
/* HeuristicLab
* Copyright (C) 2002-2017 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.Collections.Generic;
using System.Linq;
using HEAL.Attic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.IntegerVectorEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Operators {
[Item("GQAPQualitySimilarityReducer", "Reduces two populations two one by using quality and similarity information to obtain a population of high quality, but also diverse solutions.")]
[StorableType("3E9B9095-C0FE-4B16-B2FD-9EC349A37CD2")]
public class GQAPQualitySimilarityReducer : SingleSuccessorOperator, IQualityAwareGQAPOperator, IAssignmentAwareGQAPOperator, IReducer {
public ILookupParameter AssignmentParameter {
get { return (ILookupParameter)Parameters["Assignment"]; }
}
public ILookupParameter QualityParameter {
get { return (ILookupParameter)Parameters["Quality"]; }
}
public ILookupParameter EvaluationParameter {
get { return (ILookupParameter)Parameters["Evaluation"]; }
}
public ILookupParameter MinimumPopulationSizeParameter {
get { return (ILookupParameter)Parameters["MinimumPopulationSize"]; }
}
public ILookupParameter MaximumPopulationSizeParameter {
get { return (ILookupParameter)Parameters["MaximumPopulationSize"]; }
}
[StorableConstructor]
protected GQAPQualitySimilarityReducer(StorableConstructorFlag _) : base(_) { }
protected GQAPQualitySimilarityReducer(GQAPQualitySimilarityReducer original, Cloner cloner) : base(original, cloner) { }
public GQAPQualitySimilarityReducer()
: base() {
Parameters.Add(new LookupParameter("Assignment", GQAPSolutionCreator.AssignmentDescription));
Parameters.Add(new LookupParameter("Quality", ""));
Parameters.Add(new LookupParameter("Evaluation", GQAP.EvaluationDescription));
Parameters.Add(new LookupParameter("MinimumPopulationSize", "The size of the population that should not be undershot."));
Parameters.Add(new LookupParameter("MaximumPopulationSize", "The size of the population that should not be surpassed."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new GQAPQualitySimilarityReducer(this, cloner);
}
public override IOperation Apply() {
string vectorName = AssignmentParameter.TranslatedName;
string qualityName = QualityParameter.TranslatedName;
int populationSize = MaximumPopulationSizeParameter.ActualValue.Value;
IScope remaining = ExecutionContext.Scope.SubScopes[0];
IScope selected = ExecutionContext.Scope.SubScopes[1];
List population =
remaining.SubScopes.Select(x => new Individual((IntegerVector)x.Variables[vectorName].Value,
((DoubleValue)x.Variables[qualityName].Value).Value,
x)).ToList();
HashSet candidates = new HashSet(
selected.SubScopes.Select(x => new Individual((IntegerVector)x.Variables[vectorName].Value,
((DoubleValue)x.Variables[qualityName].Value).Value,
x)));
foreach (var candidate in candidates) {
double[] similarities = population.Select(x => HammingSimilarityCalculator.CalculateSimilarity(x.Solution, candidate.Solution)).ToArray();
if (!similarities.Any()) {
population.Add(candidate);
break;
}
if (similarities.Max() == 1.0) break;
if (population.Count < populationSize) {
population.Add(candidate);
} else {
var replacement = population.Select((v, i) => new { V = v, Index = i })
.Where(x => x.V.Quality > candidate.Quality);
if (replacement.Any()) {
replacement.OrderBy(x => similarities[x.Index]).ToArray();
population.Remove(replacement.Last().V);
population.Add(candidate);
}
}
}
ExecutionContext.Scope.SubScopes.Clear();
ExecutionContext.Scope.SubScopes.AddRange(population.Select(x => x.Scope));
return base.Apply();
}
private class Individual {
internal IntegerVector Solution { get; set; }
internal double Quality { get; set; }
internal IScope Scope { get; set; }
internal Individual(IntegerVector vector, double quality, IScope scope) {
Solution = vector;
Quality = quality;
Scope = scope;
}
}
}
}