Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3/Analyzers/BestLAPSolutionAnalyzer.cs @ 8022

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

#1855:

  • Transformed LAP into a SingleObjectiveHeuristicOptimizationProblem
  • Added HungarianAlgorithm as separate algorithm for solving the problem
File size: 7.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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.LinearAssignment {
33  [Item("BestLAPSolutionAnalyzer", "Analyzes the best solution found.")]
34  [StorableClass]
35  public class BestLAPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
36    public bool EnabledByDefault { get { return true; } }
37
38    public ILookupParameter<BoolValue> MaximizationParameter {
39      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41    public ILookupParameter<DoubleMatrix> CostsParameter {
42      get { return (ILookupParameter<DoubleMatrix>)Parameters["Costs"]; }
43    }
44    public IScopeTreeLookupParameter<Permutation> AssignmentParameter {
45      get { return (IScopeTreeLookupParameter<Permutation>)Parameters["Assignment"]; }
46    }
47    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
48      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
49    }
50    public ILookupParameter<LAPAssignment> BestSolutionParameter {
51      get { return (ILookupParameter<LAPAssignment>)Parameters["BestSolution"]; }
52    }
53    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
54      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
55    }
56    public ILookupParameter<ItemSet<Permutation>> BestKnownSolutionsParameter {
57      get { return (ILookupParameter<ItemSet<Permutation>>)Parameters["BestKnownSolutions"]; }
58    }
59    public ILookupParameter<Permutation> BestKnownSolutionParameter {
60      get { return (ILookupParameter<Permutation>)Parameters["BestKnownSolution"]; }
61    }
62    public IValueLookupParameter<ResultCollection> ResultsParameter {
63      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
64    }
65
66    [StorableConstructor]
67    protected BestLAPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
68    protected BestLAPSolutionAnalyzer(BestLAPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
69    public BestLAPSolutionAnalyzer()
70      : base() {
71      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
72      Parameters.Add(new LookupParameter<DoubleMatrix>("Costs", LinearAssignmentProblem.CostsDescription));
73      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Assignment", "The LAP solutions from which the best solution should be analyzed."));
74      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the LAP solutions which should be analyzed."));
75      Parameters.Add(new LookupParameter<LAPAssignment>("BestSolution", "The best LAP solution."));
76      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best LAP solution should be stored."));
77      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this LAP instance."));
78      Parameters.Add(new LookupParameter<ItemSet<Permutation>>("BestKnownSolutions", "The best known solutions (there may be multiple) of this LAP instance."));
79      Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The best known solution of this LAP instance."));
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new BestLAPSolutionAnalyzer(this, cloner);
84    }
85
86    public override IOperation Apply() {
87      var costs = CostsParameter.ActualValue;
88      var permutations = AssignmentParameter.ActualValue;
89      var qualities = QualityParameter.ActualValue;
90      var results = ResultsParameter.ActualValue;
91      bool max = MaximizationParameter.ActualValue.Value;
92      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
93     
94      var sorted = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToArray();
95      if (max) sorted = sorted.Reverse().ToArray();
96      int i = sorted.First().index;
97
98      if (bestKnownQuality == null
99          || max && qualities[i].Value > bestKnownQuality.Value
100          || !max && qualities[i].Value < bestKnownQuality.Value) {
101        // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known
102        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
103        BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone();
104        BestKnownSolutionsParameter.ActualValue = new ItemSet<Permutation>(new PermutationEqualityComparer());
105        BestKnownSolutionsParameter.ActualValue.Add((Permutation)permutations[i].Clone());
106      } else if (bestKnownQuality.Value == qualities[i].Value) {
107        // if we matched the best-known quality we'll try to set the best-known solution if it isn't null
108        // and try to add it to the pool of best solutions if it is different
109        if (BestKnownSolutionParameter.ActualValue == null)
110          BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone();
111        if (BestKnownSolutionsParameter.ActualValue == null)
112          BestKnownSolutionsParameter.ActualValue = new ItemSet<Permutation>(new PermutationEqualityComparer());
113        foreach (var k in sorted) { // for each solution that we found check if it is in the pool of best-knowns
114          if (!max && k.Value > qualities[i].Value
115            || max && k.Value < qualities[i].Value) break; // stop when we reached a solution worse than the best-known quality
116          Permutation p = permutations[k.index];
117          if (!BestKnownSolutionsParameter.ActualValue.Contains(p))
118            BestKnownSolutionsParameter.ActualValue.Add((Permutation)permutations[k.index].Clone());
119        }
120      }
121
122      LAPAssignment assignment = BestSolutionParameter.ActualValue;
123      if (assignment == null) {
124        assignment = new LAPAssignment(costs, (Permutation)permutations[i].Clone(), new DoubleValue(qualities[i].Value));
125        BestSolutionParameter.ActualValue = assignment;
126        results.Add(new Result("Best LAP Solution", assignment));
127      } else {
128        if (max && assignment.Quality.Value < qualities[i].Value ||
129          !max && assignment.Quality.Value > qualities[i].Value) {
130          assignment.Costs = costs;
131          assignment.Assignment = (Permutation)permutations[i].Clone();
132          assignment.Quality.Value = qualities[i].Value;
133        }
134      }
135
136      return base.Apply();
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.