Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs @ 13398

Last change on this file since 13398 was 3797, checked in by mkommend, 14 years ago

corrected namespaces and removed resources files in HL 3.3 solution (ticket #893)

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