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("BestKnapsackSolutionAnalyzer", "An operator for analyzing the best solution for a knapsack problem.")]
|
---|
39 | [StorableClass]
|
---|
40 | class BestKnapsackSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
41 |
|
---|
42 | public ScopeTreeLookupParameter<BinaryVector> BinaryVectorParameter {
|
---|
43 | get { return (ScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
|
---|
44 | }
|
---|
45 | public LookupParameter<IntValue> KnapsackCapacityParameter {
|
---|
46 | get { return (LookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
|
---|
47 | }
|
---|
48 | public LookupParameter<IntArray> WeightsParameter {
|
---|
49 | get { return (LookupParameter<IntArray>)Parameters["Weights"]; }
|
---|
50 | }
|
---|
51 | public LookupParameter<IntArray> ValuesParameter {
|
---|
52 | get { return (LookupParameter<IntArray>)Parameters["Values"]; }
|
---|
53 | }
|
---|
54 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
55 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
56 | }
|
---|
57 | public LookupParameter<KnapsackSolution> BestSolutionParameter {
|
---|
58 | get { return (LookupParameter<KnapsackSolution>)Parameters["BestSolution"]; }
|
---|
59 | }
|
---|
60 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
61 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public BestKnapsackSolutionAnalyzer()
|
---|
65 | : base() {
|
---|
66 | Parameters.Add(new ScopeTreeLookupParameter<BinaryVector>("BinaryVector", "The knapsack solutions from which the best solution should be visualized."));
|
---|
67 | Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack."));
|
---|
68 | Parameters.Add(new LookupParameter<IntArray>("Weights", "The weights of the items."));
|
---|
69 | Parameters.Add(new LookupParameter<IntArray>("Values", "The values of the items."));
|
---|
70 |
|
---|
71 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the knapsack solutions which should be visualized."));
|
---|
72 | Parameters.Add(new LookupParameter<KnapsackSolution>("BestSolution", "The best knapsack solution."));
|
---|
73 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the knapsack solution should be stored."));
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override IOperation Apply() {
|
---|
77 | ItemArray<BinaryVector> binaryVectors = BinaryVectorParameter.ActualValue;
|
---|
78 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
79 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
80 |
|
---|
81 | int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
82 |
|
---|
83 | KnapsackSolution solution = BestSolutionParameter.ActualValue;
|
---|
84 | if (solution == null) {
|
---|
85 | solution = new KnapsackSolution(binaryVectors[i], qualities[i],
|
---|
86 | KnapsackCapacityParameter.ActualValue, WeightsParameter.ActualValue, ValuesParameter.ActualValue);
|
---|
87 | BestSolutionParameter.ActualValue = solution;
|
---|
88 | results.Add(new Result("Best Knapsack Solution", solution));
|
---|
89 | } else {
|
---|
90 | solution.BinaryVector = binaryVectors[i];
|
---|
91 | solution.Quality = qualities[i];
|
---|
92 | solution.Capacity = KnapsackCapacityParameter.ActualValue;
|
---|
93 | solution.Weights = WeightsParameter.ActualValue;
|
---|
94 | solution.Values = ValuesParameter.ActualValue;
|
---|
95 |
|
---|
96 | results["Best Knapsack Solution"].Value = solution;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return base.Apply();
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|