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