Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs @ 5593

Last change on this file since 5593 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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