#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.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common; 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.")] [StorableClass] public class GQAPQualitySimilarityReducer : SingleSuccessorOperator, IAssignmentAwareGQAPOperator, IQualityAwareGQAPOperator, IPopulationReducer { public ILookupParameter AssignmentParameter { get { return (ILookupParameter)Parameters["Assignment"]; } } public ILookupParameter MaximizationParameter { get { return (ILookupParameter)Parameters["Maximization"]; } } public ILookupParameter QualityParameter { get { return (ILookupParameter)Parameters["Quality"]; } } public ILookupParameter FlowDistanceQualityParameter { get { return (ILookupParameter)Parameters["FlowDistanceQuality"]; } } public ILookupParameter InstallationQualityParameter { get { return (ILookupParameter)Parameters["InstallationQuality"]; } } public ILookupParameter OverbookedCapacityParameter { get { return (ILookupParameter)Parameters["OverbookedCapacity"]; } } public ILookupParameter MinimumPopulationSizeParameter { get { return (ILookupParameter)Parameters["MinimumPopulationSize"]; } } public ILookupParameter MaximumPopulationSizeParameter { get { return (ILookupParameter)Parameters["MaximumPopulationSize"]; } } [StorableConstructor] protected GQAPQualitySimilarityReducer(bool deserializing) : base(deserializing) { } protected GQAPQualitySimilarityReducer(GQAPQualitySimilarityReducer original, Cloner cloner) : base(original, cloner) { } public GQAPQualitySimilarityReducer() : base() { Parameters.Add(new LookupParameter("Assignment", GQAPSolutionCreator.AssignmentDescription)); Parameters.Add(new LookupParameter("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription)); Parameters.Add(new LookupParameter("Quality", GQAPEvaluator.QualityDescription)); Parameters.Add(new LookupParameter("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription)); Parameters.Add(new LookupParameter("InstallationQuality", GQAPEvaluator.InstallationQualityDescription)); Parameters.Add(new LookupParameter("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription)); 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; bool maximization = MaximizationParameter.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 => IntegerVectorEqualityComparer.GetSimilarity(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 => (maximization && x.V.Quality < candidate.Quality) || (!maximization && 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; } } } }