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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.Knapsack.Analyzers {
|
---|
35 | /// <summary>
|
---|
36 | /// An operator for analyzing the best solution for a knapsack problem.
|
---|
37 | /// </summary>
|
---|
38 | [Item("MultiPopulationBestKnapsackSolutionAnalyzer", "An operator for analyzing the best solution for a knapsack problem.")]
|
---|
39 | [StorableClass]
|
---|
40 | class MultiPopulationBestKnapsackSolutionAnalyzer : SingleSuccessorOperator, IBestKnapsackSolutionAnalyzer, IAnalyzer {
|
---|
41 |
|
---|
42 | public ILookupParameter<ItemArray<ItemArray<BinaryVector>>> BinaryVectorParameter {
|
---|
43 | get { return (ILookupParameter<ItemArray<ItemArray<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<ItemArray<ItemArray<DoubleValue>>> QualityParameter {
|
---|
58 | get { return (ILookupParameter<ItemArray<ItemArray<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 MultiPopulationBestKnapsackSolutionAnalyzer()
|
---|
71 | : base() {
|
---|
72 | Parameters.Add(new ScopeTreeLookupParameter<ItemArray<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<ItemArray<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 | ItemArray<ItemArray<BinaryVector>> binaryVectors = BinaryVectorParameter.ActualValue;
|
---|
84 | ItemArray<ItemArray<DoubleValue>> qualities = QualityParameter.ActualValue;
|
---|
85 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
86 |
|
---|
87 | DoubleValue bestQuality = new DoubleValue(double.MaxValue);
|
---|
88 | BinaryVector bestBinaryVector = null;
|
---|
89 |
|
---|
90 | for (int i = 0; i < qualities.Length; i++) {
|
---|
91 | for (int j = 0; j < qualities[i].Length; j++) {
|
---|
92 | if (qualities[i][j].Value < bestQuality.Value) {
|
---|
93 | bestQuality = qualities[i][j];
|
---|
94 | bestBinaryVector = binaryVectors[i][j];
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | KnapsackSolution solution = BestSolutionParameter.ActualValue;
|
---|
100 | if (solution == null) {
|
---|
101 | solution = new KnapsackSolution(bestBinaryVector, bestQuality,
|
---|
102 | KnapsackCapacityParameter.ActualValue, WeightsParameter.ActualValue, ValuesParameter.ActualValue);
|
---|
103 | BestSolutionParameter.ActualValue = solution;
|
---|
104 |
|
---|
105 | results.Add(new Result("Best Knapsack Solution", solution));
|
---|
106 | } else {
|
---|
107 | if (solution.Quality.Value > bestQuality.Value) {
|
---|
108 | solution.BinaryVector = bestBinaryVector;
|
---|
109 | solution.Quality = bestQuality;
|
---|
110 | solution.Capacity = KnapsackCapacityParameter.ActualValue;
|
---|
111 | solution.Weights = WeightsParameter.ActualValue;
|
---|
112 | solution.Values = ValuesParameter.ActualValue;
|
---|
113 |
|
---|
114 | results["Best Knapsack Solution"].Value = solution;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | return base.Apply();
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|