Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on refactoring of algorithm analysis and tracing (#999)

  • removed specific analyzer interfaces
  • adapted MultiAnalyzer
File size: 4.8 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.Analyzers {
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, IBestKnapsackSolutionAnalyzer, IAnalyzer {
41
42    public ILookupParameter<BinaryVector> BinaryVectorParameter {
43      get { return (ILookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
44    }
45    ILookupParameter IBestKnapsackSolutionAnalyzer.BinaryVectorParameter {
46      get { return BinaryVectorParameter; }
47    }
48    public ILookupParameter<IntValue> KnapsackCapacityParameter {
49      get { return (ILookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
50    }
51    public ILookupParameter<IntArray> WeightsParameter {
52      get { return (ILookupParameter<IntArray>)Parameters["Weights"]; }
53    }
54    public ILookupParameter<IntArray> ValuesParameter {
55      get { return (ILookupParameter<IntArray>)Parameters["Values"]; }
56    }
57    public ILookupParameter<DoubleValue> QualityParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
59    }
60    ILookupParameter IBestKnapsackSolutionAnalyzer.QualityParameter {
61      get { return QualityParameter; }
62    }
63    public ILookupParameter<KnapsackSolution> BestSolutionParameter {
64      get { return (ILookupParameter<KnapsackSolution>)Parameters["BestSolution"]; }
65    }
66    public IValueLookupParameter<ResultCollection> ResultsParameter {
67      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
68    }
69
70    public BestKnapsackSolutionAnalyzer()
71      : base() {
72      Parameters.Add(new LookupParameter<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 LookupParameter<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    }
81
82    public override IOperation Apply() {
83      BinaryVector binaryVector = BinaryVectorParameter.ActualValue;
84      DoubleValue quality = QualityParameter.ActualValue;
85      ResultCollection results = ResultsParameter.ActualValue;
86
87      KnapsackSolution solution = BestSolutionParameter.ActualValue;
88      if (solution == null) {
89        solution = new KnapsackSolution(binaryVector, QualityParameter.ActualValue,
90          KnapsackCapacityParameter.ActualValue, WeightsParameter.ActualValue, ValuesParameter.ActualValue);
91        BestSolutionParameter.ActualValue = solution;
92        results.Add(new Result("Best Knapsack Solution", solution));
93      }  else {
94        solution.BinaryVector = binaryVector;
95        solution.Quality = QualityParameter.ActualValue;
96        solution.Capacity = KnapsackCapacityParameter.ActualValue;
97        solution.Weights = WeightsParameter.ActualValue;
98        solution.Values = ValuesParameter.ActualValue;
99
100        results["Best Knapsack Solution"].Value = solution;
101      }
102
103      return base.Apply();
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.