Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Knapsack/3.3/Evaluators/KnapsackEvaluator.cs @ 4208

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

Sorted usings and removed unused usings in entire solution (#1094)

File size: 5.7 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.BinaryVectorEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.Knapsack {
31  /// <summary>
32  /// A base class for operators which evaluates Knapsack solutions given in BinaryVector encoding.
33  /// </summary>
34  [Item("KnapsackEvaluator", "Evaluates solutions for the Knapsack problem.")]
35  [StorableClass]
36  public class KnapsackEvaluator : SingleSuccessorOperator, IKnapsackEvaluator {
37    public ILookupParameter<DoubleValue> QualityParameter {
38      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
39    }
40
41    public ILookupParameter<DoubleValue> SumWeightsParameter {
42      get { return (ILookupParameter<DoubleValue>)Parameters["SumWeights"]; }
43    }
44
45    public ILookupParameter<DoubleValue> SumValuesParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters["SumValues"]; }
47    }
48
49    public ILookupParameter<DoubleValue> AppliedPenaltyParameter {
50      get { return (ILookupParameter<DoubleValue>)Parameters["AppliedPenalty"]; }
51    }
52
53    public ILookupParameter<BinaryVector> BinaryVectorParameter {
54      get { return (ILookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
55    }
56
57    public ILookupParameter<IntValue> KnapsackCapacityParameter {
58      get { return (ILookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
59    }
60    public ILookupParameter<DoubleValue> PenaltyParameter {
61      get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }
62    }
63    public ILookupParameter<IntArray> WeightsParameter {
64      get { return (ILookupParameter<IntArray>)Parameters["Weights"]; }
65    }
66    public ILookupParameter<IntArray> ValuesParameter {
67      get { return (ILookupParameter<IntArray>)Parameters["Values"]; }
68    }
69
70    public KnapsackEvaluator()
71      : base() {
72      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the OneMax solution."));
73      Parameters.Add(new LookupParameter<DoubleValue>("SumWeights", "The evaluated quality of the OneMax solution."));
74      Parameters.Add(new LookupParameter<DoubleValue>("SumValues", "The evaluated quality of the OneMax solution."));
75      Parameters.Add(new LookupParameter<DoubleValue>("AppliedPenalty", "The evaluated quality of the OneMax solution."));
76      Parameters.Add(new LookupParameter<BinaryVector>("BinaryVector", "The OneMax solution given in path representation which should be evaluated."));
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      Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight."));
81    }
82
83    public struct KnapsackEvaluation {
84      public DoubleValue Quality;
85      public DoubleValue SumWeights;
86      public DoubleValue SumValues;
87      public DoubleValue AppliedPenalty;
88    }
89
90    public static KnapsackEvaluation Apply(BinaryVector v, IntValue capacity, DoubleValue penalty, IntArray weights, IntArray values) {
91      if (weights.Length != values.Length)
92        throw new InvalidOperationException("The weights and values parameters of the Knapsack problem have different sizes");
93
94      KnapsackEvaluation result = new KnapsackEvaluation();
95
96      double quality = 0;
97
98      int weight = 0;
99      int value = 0;
100      double appliedPenalty = 0;
101
102      for (int i = 0; i < v.Length; i++) {
103        if (v[i]) {
104          weight += weights[i];
105          value += values[i];
106        }
107      }
108
109      if (weight > capacity.Value) {
110        appliedPenalty = penalty.Value * (weight - capacity.Value);
111      }
112
113      quality = value - appliedPenalty;
114
115      result.AppliedPenalty = new DoubleValue(appliedPenalty);
116      result.SumWeights = new DoubleValue(weight);
117      result.SumValues = new DoubleValue(value);
118      result.Quality = new DoubleValue(quality);
119
120      return result;
121    }
122
123    public sealed override IOperation Apply() {
124      BinaryVector v = BinaryVectorParameter.ActualValue;
125
126      KnapsackEvaluation evaluation = Apply(BinaryVectorParameter.ActualValue,
127        KnapsackCapacityParameter.ActualValue,
128        PenaltyParameter.ActualValue,
129        WeightsParameter.ActualValue,
130        ValuesParameter.ActualValue);
131
132      QualityParameter.ActualValue = evaluation.Quality;
133      SumWeightsParameter.ActualValue = evaluation.SumWeights;
134      SumValuesParameter.ActualValue = evaluation.SumValues;
135      AppliedPenaltyParameter.ActualValue = evaluation.AppliedPenalty;
136
137      return base.Apply();
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.