Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614: worked on GQAP and GRASP+PR

File size: 5.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;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Operators {
33  [Item("QualitySimilarityMerger", "Merges two populations by using quality and similarity information to maintain a population of high quality, but also diverse solutions.")]
34  [StorableClass]
35  public class QualitySimilarityMerger : SingleSuccessorOperator, IGQAPMerger {
36    public ILookupParameter<IntegerVector> AssignmentParameter {
37      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
38    }
39    public ILookupParameter<DoubleValue> QualityParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
41    }
42    public ILookupParameter<IntValue> PopulationSizeParameter {
43      get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
44    }
45    public IValueLookupParameter<BoolValue> MaximizationParameter {
46      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
47    }
48
49    [StorableConstructor]
50    protected QualitySimilarityMerger(bool deserializing) : base(deserializing) { }
51    protected QualitySimilarityMerger(QualitySimilarityMerger original, Cloner cloner) : base(original, cloner) { }
52    public QualitySimilarityMerger()
53      : base() {
54      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", "The equipment-location assignment."));
55      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a solution."));
56      Parameters.Add(new LookupParameter<IntValue>("PopulationSize", "The size of the population that should not be surpassed."));
57      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is to be maximized, false otherwise."));
58    }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new QualitySimilarityMerger(this, cloner);
62    }
63
64    public override IOperation Apply() {
65      string vectorName = AssignmentParameter.TranslatedName;
66      string qualityName = QualityParameter.TranslatedName;
67      int populationSize = PopulationSizeParameter.ActualValue.Value;
68      bool maximization = MaximizationParameter.ActualValue.Value;
69
70      IScope remaining = ExecutionContext.Scope.SubScopes[0];
71      IScope selected = ExecutionContext.Scope.SubScopes[1];
72
73      List<Individual> population =
74        remaining.SubScopes.Select(x => new Individual((IntegerVector)x.Variables[vectorName].Value,
75                                                      ((DoubleValue)x.Variables[qualityName].Value).Value,
76                                                      x)).ToList();
77      HashSet<Individual> candidates = new HashSet<Individual>(
78        selected.SubScopes.Select(x => new Individual((IntegerVector)x.Variables[vectorName].Value,
79                                                     ((DoubleValue)x.Variables[qualityName].Value).Value,
80                                                     x)));
81
82      foreach (var candidate in candidates) {
83        double[] similarities = population.Select(x => GQAPIntegerVectorProximityCalculator.CalculateGenotypeSimilarity(x.Solution, candidate.Solution)).ToArray();
84        if (!similarities.Any()) {
85          population.Add(candidate);
86        } else if (population.Count < populationSize) {
87          double maxSimilarity = similarities.Max();
88          if (maxSimilarity < 1.0)
89            population.Add(candidate);
90        } else {
91          var replacement = population.Select((v, i) => new { V = v, Index = i })
92                                      .Where(x => (maximization && x.V.Quality < candidate.Quality)
93                                               || (!maximization && x.V.Quality > candidate.Quality));
94          if (replacement.Any()) {
95            replacement.OrderBy(x => similarities[x.Index]).ToArray();
96            population.Remove(replacement.Last().V);
97            population.Add(candidate);
98          }
99        }
100      }
101
102      ExecutionContext.Scope.SubScopes.Clear();
103      ExecutionContext.Scope.SubScopes.AddRange(population.Select(x => x.Scope));
104
105      return base.Apply();
106    }
107
108    private class Individual {
109      internal IntegerVector Solution { get; set; }
110      internal double Quality { get; set; }
111      internal IScope Scope { get; set; }
112
113      internal Individual(IntegerVector vector, double quality, IScope scope) {
114        Solution = vector;
115        Quality = quality;
116        Scope = scope;
117      }
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.