Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Analyzers/BestGQAPSolutionAnalyzer.cs @ 6956

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

#1614

  • readded new simpler branch
File size: 6.5 KB
RevLine 
[6956]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
33  /// <summary>
34  /// An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.
35  /// </summary>
36  [Item("Best GeneralizedQAP Solution Analyzer", "An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.")]
37  [StorableClass]
38  public sealed class BestGQAPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
39    public LookupParameter<BoolValue> MaximizationParameter {
40      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
41    }
42    public LookupParameter<DoubleMatrix> DistancesParameter {
43      get { return (LookupParameter<DoubleMatrix>)Parameters["Distances"]; }
44    }
45    public LookupParameter<DoubleMatrix> WeightsParameter {
46      get { return (LookupParameter<DoubleMatrix>)Parameters["Weights"]; }
47    }
48    public ScopeTreeLookupParameter<IntegerVector> AssignmentParameter {
49      get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; }
50    }
51    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
52      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
53    }
54    public LookupParameter<GQAPAssignment> BestSolutionParameter {
55      get { return (LookupParameter<GQAPAssignment>)Parameters["BestSolution"]; }
56    }
57    public ValueLookupParameter<ResultCollection> ResultsParameter {
58      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
59    }
60    public LookupParameter<DoubleValue> BestKnownQualityParameter {
61      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
62    }
63    public LookupParameter<IntegerVector> BestKnownSolutionParameter {
64      get { return (LookupParameter<IntegerVector>)Parameters["BestKnownSolution"]; }
65    }
66
67    [StorableConstructor]
68    private BestGQAPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
69    private BestGQAPSolutionAnalyzer(BestGQAPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new BestGQAPSolutionAnalyzer(this, cloner);
72    }
73    public BestGQAPSolutionAnalyzer()
74      : base() {
75      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
76      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances between the locations."));
77      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the facilities."));
78      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", "The GQAP solutions from which the best solution should be analyzed."));
79      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the GQAP solutions which should be analyzed."));
80      Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution."));
81      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored."));
82      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this GQAP instance."));
83      Parameters.Add(new LookupParameter<IntegerVector>("BestKnownSolution", "The best known solution of this GQAP instance."));
84    }
85
86    public override IOperation Apply() {
87      DoubleMatrix distances = DistancesParameter.ActualValue;
88      DoubleMatrix weights = WeightsParameter.ActualValue;
89      ItemArray<IntegerVector> assignments = AssignmentParameter.ActualValue;
90      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
91      ResultCollection results = ResultsParameter.ActualValue;
92      bool max = MaximizationParameter.ActualValue.Value;
93      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
94
95      var sorted = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToArray();
96      if (max) sorted = sorted.Reverse().ToArray();
97      int i = sorted.First().index;
98
99      if (bestKnownQuality == null
100          || max && qualities[i].Value > bestKnownQuality.Value
101          || !max && qualities[i].Value < bestKnownQuality.Value) {
102        // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known
103        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
104        BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[i].Clone();
105      }
106
107      GQAPAssignment assignment = BestSolutionParameter.ActualValue;
108      if (assignment == null) {
109        assignment = new GQAPAssignment(weights, (IntegerVector)assignments[i].Clone(), new DoubleValue(qualities[i].Value));
110        assignment.Distances = distances;
111        BestSolutionParameter.ActualValue = assignment;
112        results.Add(new Result("Best GQAP Solution", assignment));
113      } else {
114        if (max && assignment.Quality.Value < qualities[i].Value ||
115          !max && assignment.Quality.Value > qualities[i].Value) {
116          assignment.Distances = distances;
117          assignment.Weights = weights;
118          assignment.Assignment = (IntegerVector)assignments[i].Clone();
119          assignment.Quality.Value = qualities[i].Value;
120        }
121      }
122
123      return base.Apply();
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.