Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs @ 13404

Last change on this file since 13404 was 13404, checked in by abeham, 8 years ago

#2521:

  • Adapted Knapsack problem to new problem infrastructure
  • Introduced additional interfaces in binary vector encoding
  • Improved KnapsackImprovementOperator which requires less evaluated solutions in case of an infeasible start solution

Loosely related change:

  • All LookupParameters are now shown by default
  • Wiring code should make sure that wired parameters are hidden
File size: 6.7 KB
RevLine 
[3641]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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]
[13404]38  public class BestKnapsackSolutionAnalyzer : SingleSuccessorOperator, IBinaryVectorSolutionsOperator, IAnalyzer, ISingleObjectiveOperator {
[7172]39    public virtual bool EnabledByDefault {
40      get { return true; }
41    }
42
[13404]43    public ILookupParameter<BoolValue> MaximizationParameter {
44      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
[3787]45    }
[13404]46    public IScopeTreeLookupParameter<BinaryVector> BinaryVectorsParameter {
47      get { return (IScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVectors"]; }
[3641]48    }
[13404]49    public ILookupParameter<IntValue> KnapsackCapacityParameter {
50      get { return (ILookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
[3641]51    }
[13404]52    public ILookupParameter<IntArray> WeightsParameter {
53      get { return (ILookupParameter<IntArray>)Parameters["Weights"]; }
[3641]54    }
[13404]55    public ILookupParameter<IntArray> ValuesParameter {
56      get { return (ILookupParameter<IntArray>)Parameters["Values"]; }
[3641]57    }
[13404]58    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
59      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[3641]60    }
[13404]61    public ILookupParameter<KnapsackSolution> BestSolutionParameter {
62      get { return (ILookupParameter<KnapsackSolution>)Parameters["BestSolution"]; }
[3641]63    }
[13404]64    public IValueLookupParameter<ResultCollection> ResultsParameter {
65      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
[3641]66    }
[13404]67    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
68      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
[3787]69    }
[13404]70    public ILookupParameter<BinaryVector> BestKnownSolutionParameter {
71      get { return (ILookupParameter<BinaryVector>)Parameters["BestKnownSolution"]; }
[3787]72    }
[3641]73
[4722]74    [StorableConstructor]
75    protected BestKnapsackSolutionAnalyzer(bool deserializing) : base(deserializing) { }
76    protected BestKnapsackSolutionAnalyzer(BestKnapsackSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
[3641]77    public BestKnapsackSolutionAnalyzer()
78      : base() {
[3787]79      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
[13404]80      Parameters.Add(new ScopeTreeLookupParameter<BinaryVector>("BinaryVectors", "The Knapsack solutions from which the best solution should be visualized."));
[3641]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
[4513]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."));
[3641]87      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the knapsack solution should be stored."));
[3787]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."));
[3641]90    }
91
[4722]92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new BestKnapsackSolutionAnalyzer(this, cloner);
94    }
95
[3641]96    public override IOperation Apply() {
[13404]97      var binaryVectors = BinaryVectorsParameter.ActualValue;
98      var qualities = QualityParameter.ActualValue;
99      var results = ResultsParameter.ActualValue;
100      var max = MaximizationParameter.ActualValue.Value;
101      var bestKnownQuality = BestKnownQualityParameter.ActualValue;
[3641]102
[3787]103      int i = -1;
[13404]104      i = !max ? qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index
105               : qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
[3667]106
[3787]107      if (bestKnownQuality == null ||
108          max && qualities[i].Value > bestKnownQuality.Value ||
109          !max && qualities[i].Value < bestKnownQuality.Value) {
110        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
111        BestKnownSolutionParameter.ActualValue = (BinaryVector)binaryVectors[i].Clone();
112      }
113
[13404]114      var solution = BestSolutionParameter.ActualValue;
[3641]115      if (solution == null) {
[3787]116        solution = new KnapsackSolution((BinaryVector)binaryVectors[i].Clone(), new DoubleValue(qualities[i].Value),
[3641]117          KnapsackCapacityParameter.ActualValue, WeightsParameter.ActualValue, ValuesParameter.ActualValue);
118        BestSolutionParameter.ActualValue = solution;
119        results.Add(new Result("Best Knapsack Solution", solution));
[4068]120      } else {
[3787]121        if (max && qualities[i].Value > solution.Quality.Value ||
122          !max && qualities[i].Value < solution.Quality.Value) {
123          solution.BinaryVector = (BinaryVector)binaryVectors[i].Clone();
124          solution.Quality = new DoubleValue(qualities[i].Value);
125          solution.Capacity = KnapsackCapacityParameter.ActualValue;
126          solution.Weights = WeightsParameter.ActualValue;
127          solution.Values = ValuesParameter.ActualValue;
128        }
[3641]129      }
130
131      return base.Apply();
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.