Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs @ 15631

Last change on this file since 15631 was 4513, checked in by swinkler, 14 years ago

Corrected comments and descriptions (#1182); formatted code.

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