Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/PopulationReducers/GQAPQualitySimilarityReducer.cs @ 13820

Last change on this file since 13820 was 7523, checked in by abeham, 13 years ago

#1614

  • sorted operators
  • added crossover defined by Cordeau et al.
  • fixed a few bugs
File size: 6.8 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("GQAPQualitySimilarityReducer", "Reduces two populations two one by using quality and similarity information to obtain a population of high quality, but also diverse solutions.")]
35  [StorableClass]
36  public class GQAPQualitySimilarityReducer : SingleSuccessorOperator, IAssignmentAwareGQAPOperator, IQualityAwareGQAPOperator, IPopulationReducer {
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> MinimumPopulationSizeParameter {
56      get { return (ILookupParameter<IntValue>)Parameters["MinimumPopulationSize"]; }
57    }
58    public ILookupParameter<IntValue> MaximumPopulationSizeParameter {
59      get { return (ILookupParameter<IntValue>)Parameters["MaximumPopulationSize"]; }
60    }
61
62    [StorableConstructor]
63    protected GQAPQualitySimilarityReducer(bool deserializing) : base(deserializing) { }
64    protected GQAPQualitySimilarityReducer(GQAPQualitySimilarityReducer original, Cloner cloner) : base(original, cloner) { }
65    public GQAPQualitySimilarityReducer()
66      : base() {
67      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
68      Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
69      Parameters.Add(new LookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
70      Parameters.Add(new LookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
71      Parameters.Add(new LookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
72      Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
73      Parameters.Add(new LookupParameter<IntValue>("MinimumPopulationSize", "The size of the population that should not be undershot."));
74      Parameters.Add(new LookupParameter<IntValue>("MaximumPopulationSize", "The size of the population that should not be surpassed."));
75    }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new GQAPQualitySimilarityReducer(this, cloner);
79    }
80
81    public override IOperation Apply() {
82      string vectorName = AssignmentParameter.TranslatedName;
83      string qualityName = QualityParameter.TranslatedName;
84      int populationSize = MaximumPopulationSizeParameter.ActualValue.Value;
85      bool maximization = MaximizationParameter.ActualValue.Value;
86
87      IScope remaining = ExecutionContext.Scope.SubScopes[0];
88      IScope selected = ExecutionContext.Scope.SubScopes[1];
89
90      List<Individual> population =
91        remaining.SubScopes.Select(x => new Individual((IntegerVector)x.Variables[vectorName].Value,
92                                                      ((DoubleValue)x.Variables[qualityName].Value).Value,
93                                                      x)).ToList();
94      HashSet<Individual> candidates = new HashSet<Individual>(
95        selected.SubScopes.Select(x => new Individual((IntegerVector)x.Variables[vectorName].Value,
96                                                     ((DoubleValue)x.Variables[qualityName].Value).Value,
97                                                     x)));
98
99      foreach (var candidate in candidates) {
100        double[] similarities = population.Select(x => IntegerVectorEqualityComparer.GetSimilarity(x.Solution, candidate.Solution)).ToArray();
101        if (!similarities.Any()) {
102          population.Add(candidate);
103          break;
104        }
105        if (similarities.Max() == 1.0) break;
106        if (population.Count < populationSize) {
107          population.Add(candidate);
108        } else {
109          var replacement = population.Select((v, i) => new { V = v, Index = i })
110                                      .Where(x => (maximization && x.V.Quality < candidate.Quality)
111                                               || (!maximization && x.V.Quality > candidate.Quality));
112          if (replacement.Any()) {
113            replacement.OrderBy(x => similarities[x.Index]).ToArray();
114            population.Remove(replacement.Last().V);
115            population.Add(candidate);
116          }
117        }
118      }
119
120      ExecutionContext.Scope.SubScopes.Clear();
121      ExecutionContext.Scope.SubScopes.AddRange(population.Select(x => x.Scope));
122
123      return base.Apply();
124    }
125
126    private class Individual {
127      internal IntegerVector Solution { get; set; }
128      internal double Quality { get; set; }
129      internal IScope Scope { get; set; }
130
131      internal Individual(IntegerVector vector, double quality, IScope scope) {
132        Solution = vector;
133        Quality = quality;
134        Scope = scope;
135      }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.