Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2701_MemPRAlgorithm/HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs @ 16811

Last change on this file since 16811 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.BinaryVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.Knapsack {
33  /// <summary>
34  /// An operator for analyzing the best solution for a Knapsack problem.
35  /// </summary>
36  [Item("BestKnapsackSolutionAnalyzer", "An operator for analyzing the best solution for a Knapsack problem.")]
37  [StorableClass]
38  public class BestKnapsackSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
39    public virtual bool EnabledByDefault {
40      get { return true; }
41    }
42
43    public LookupParameter<BoolValue> MaximizationParameter {
44      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46    public ScopeTreeLookupParameter<BinaryVector> BinaryVectorParameter {
47      get { return (ScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
48    }
49    public LookupParameter<IntValue> KnapsackCapacityParameter {
50      get { return (LookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
51    }
52    public LookupParameter<IntArray> WeightsParameter {
53      get { return (LookupParameter<IntArray>)Parameters["Weights"]; }
54    }
55    public LookupParameter<IntArray> ValuesParameter {
56      get { return (LookupParameter<IntArray>)Parameters["Values"]; }
57    }
58    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
59      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
60    }
61    public LookupParameter<KnapsackSolution> BestSolutionParameter {
62      get { return (LookupParameter<KnapsackSolution>)Parameters["BestSolution"]; }
63    }
64    public ValueLookupParameter<ResultCollection> ResultsParameter {
65      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
66    }
67    public LookupParameter<DoubleValue> BestKnownQualityParameter {
68      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
69    }
70    public LookupParameter<BinaryVector> BestKnownSolutionParameter {
71      get { return (LookupParameter<BinaryVector>)Parameters["BestKnownSolution"]; }
72    }
73
74    [StorableConstructor]
75    protected BestKnapsackSolutionAnalyzer(bool deserializing) : base(deserializing) { }
76    protected BestKnapsackSolutionAnalyzer(BestKnapsackSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
77    public BestKnapsackSolutionAnalyzer()
78      : base() {
79      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
80      Parameters.Add(new ScopeTreeLookupParameter<BinaryVector>("BinaryVector", "The Knapsack solutions from which the best solution should be visualized."));
81      Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack."));
82      Parameters.Add(new LookupParameter<IntArray>("Weights", "The weights of the items."));
83      Parameters.Add(new LookupParameter<IntArray>("Values", "The values of the items."));
84
85      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the Knapsack solutions which should be visualized."));
86      Parameters.Add(new LookupParameter<KnapsackSolution>("BestSolution", "The best Knapsack solution."));
87      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the knapsack solution should be stored."));
88      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution."));
89      Parameters.Add(new LookupParameter<BinaryVector>("BestKnownSolution", "The best known solution."));
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new BestKnapsackSolutionAnalyzer(this, cloner);
94    }
95
96    public override IOperation Apply() {
97      ItemArray<BinaryVector> binaryVectors = BinaryVectorParameter.ActualValue;
98      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
99      ResultCollection results = ResultsParameter.ActualValue;
100      bool max = MaximizationParameter.ActualValue.Value;
101      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
102
103      int i = -1;
104      if (!max)
105        i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
106      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
107
108      if (bestKnownQuality == null ||
109          max && qualities[i].Value > bestKnownQuality.Value ||
110          !max && qualities[i].Value < bestKnownQuality.Value) {
111        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
112        BestKnownSolutionParameter.ActualValue = (BinaryVector)binaryVectors[i].Clone();
113      }
114
115      KnapsackSolution solution = BestSolutionParameter.ActualValue;
116      if (solution == null) {
117        solution = new KnapsackSolution((BinaryVector)binaryVectors[i].Clone(), new DoubleValue(qualities[i].Value),
118          KnapsackCapacityParameter.ActualValue, WeightsParameter.ActualValue, ValuesParameter.ActualValue);
119        BestSolutionParameter.ActualValue = solution;
120        results.Add(new Result("Best Knapsack Solution", solution));
121      } else {
122        if (max && qualities[i].Value > solution.Quality.Value ||
123          !max && qualities[i].Value < solution.Quality.Value) {
124          solution.BinaryVector = (BinaryVector)binaryVectors[i].Clone();
125          solution.Quality = new DoubleValue(qualities[i].Value);
126          solution.Capacity = KnapsackCapacityParameter.ActualValue;
127          solution.Weights = WeightsParameter.ActualValue;
128          solution.Values = ValuesParameter.ActualValue;
129        }
130      }
131
132      return base.Apply();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.