Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/QualitySimilarityMerger.cs @ 7419

Last change on this file since 7419 was 7419, checked in by abeham, 12 years ago

#1614

  • reworked parameterization (one interface for every parameter resp. parameter group)
  • unified parameter descriptions
File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
32
33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Operators {
34  [Item("QualitySimilarityMerger", "Merges two populations by using quality and similarity information to maintain a population of high quality, but also diverse solutions.")]
35  [StorableClass]
36  public class QualitySimilarityMerger : SingleSuccessorOperator, IAssignmentAwareGQAPOperator, IQualityAwareGQAPOperator, IMerger {
37    public ILookupParameter<IntegerVector> AssignmentParameter {
38      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
39    }
40    public ILookupParameter<BoolValue> MaximizationParameter {
41      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
42    }
43    public ILookupParameter<DoubleValue> QualityParameter {
44      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
45    }
46    public ILookupParameter<DoubleValue> FlowDistanceQualityParameter {
47      get { return (ILookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
48    }
49    public ILookupParameter<DoubleValue> InstallationQualityParameter {
50      get { return (ILookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
51    }
52    public ILookupParameter<DoubleValue> OverbookedCapacityParameter {
53      get { return (ILookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
54    }
55    public ILookupParameter<IntValue> PopulationSizeParameter {
56      get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
57    }
58
59    [StorableConstructor]
60    protected QualitySimilarityMerger(bool deserializing) : base(deserializing) { }
61    protected QualitySimilarityMerger(QualitySimilarityMerger original, Cloner cloner) : base(original, cloner) { }
62    public QualitySimilarityMerger()
63      : base() {
64      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
65      Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
66      Parameters.Add(new LookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
67      Parameters.Add(new LookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
68      Parameters.Add(new LookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
69      Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
70      Parameters.Add(new LookupParameter<IntValue>("PopulationSize", "The size of the population that should not be surpassed."));
71    }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new QualitySimilarityMerger(this, cloner);
75    }
76
77    public override IOperation Apply() {
78      string vectorName = AssignmentParameter.TranslatedName;
79      string qualityName = QualityParameter.TranslatedName;
80      int populationSize = PopulationSizeParameter.ActualValue.Value;
81      bool maximization = MaximizationParameter.ActualValue.Value;
82
83      IScope remaining = ExecutionContext.Scope.SubScopes[0];
84      IScope selected = ExecutionContext.Scope.SubScopes[1];
85
86      List<Individual> population =
87        remaining.SubScopes.Select(x => new Individual((IntegerVector)x.Variables[vectorName].Value,
88                                                      ((DoubleValue)x.Variables[qualityName].Value).Value,
89                                                      x)).ToList();
90      HashSet<Individual> candidates = new HashSet<Individual>(
91        selected.SubScopes.Select(x => new Individual((IntegerVector)x.Variables[vectorName].Value,
92                                                     ((DoubleValue)x.Variables[qualityName].Value).Value,
93                                                     x)));
94
95      foreach (var candidate in candidates) {
96        double[] similarities = population.Select(x => GQAPIntegerVectorProximityCalculator.CalculateGenotypeSimilarity(x.Solution, candidate.Solution)).ToArray();
97        if (!similarities.Any()) {
98          population.Add(candidate);
99        } else if (population.Count < populationSize) {
100          double maxSimilarity = similarities.Max();
101          if (maxSimilarity < 1.0)
102            population.Add(candidate);
103        } else {
104          var replacement = population.Select((v, i) => new { V = v, Index = i })
105                                      .Where(x => (maximization && x.V.Quality < candidate.Quality)
106                                               || (!maximization && x.V.Quality > candidate.Quality));
107          if (replacement.Any()) {
108            replacement.OrderBy(x => similarities[x.Index]).ToArray();
109            population.Remove(replacement.Last().V);
110            population.Add(candidate);
111          }
112        }
113      }
114
115      ExecutionContext.Scope.SubScopes.Clear();
116      ExecutionContext.Scope.SubScopes.AddRange(population.Select(x => x.Scope));
117
118      return base.Apply();
119    }
120
121    private class Individual {
122      internal IntegerVector Solution { get; set; }
123      internal double Quality { get; set; }
124      internal IScope Scope { get; set; }
125
126      internal Individual(IntegerVector vector, double quality, IScope scope) {
127        Solution = vector;
128        Quality = quality;
129        Scope = scope;
130      }
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.