Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3402 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 4.2 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Operators;
29using System;
30
31namespace HeuristicLab.Problems.Knapsack {
32  /// <summary>
33  /// A base class for operators which evaluates Knapsack solutions given in BinaryVector encoding.
34  /// </summary>
35  [Item("KnapsackEvaluator", "Evaluates solutions for the Knapsack problem.")]
36  [StorableClass]
37  public class KnapsackEvaluator : SingleSuccessorOperator, IKnapsackEvaluator {
38    public ILookupParameter<DoubleValue> QualityParameter {
39      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
40    }
41   
42    public ILookupParameter<BinaryVector> BinaryVectorParameter {
43      get { return (ILookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
44    }
45
46    public ILookupParameter<IntValue> KnapsackCapacityParameter {
47      get { return (ILookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
48    }
49    public ILookupParameter<DoubleValue> PenaltyParameter {
50      get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }
51    }
52    public ILookupParameter<IntArray> WeightsParameter {
53      get { return (ILookupParameter<IntArray>)Parameters["Weights"]; }
54    }
55    public ILookupParameter<IntArray> ValuesParameter {
56      get { return (ILookupParameter<IntArray>)Parameters["Values"]; }
57    }
58
59    public KnapsackEvaluator()
60      : base() {
61      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the OneMax solution."));
62      Parameters.Add(new LookupParameter<BinaryVector>("BinaryVector", "The OneMax solution given in path representation which should be evaluated."));
63      Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack."));
64      Parameters.Add(new LookupParameter<IntArray>("Weights", "The weights of the items."));
65      Parameters.Add(new LookupParameter<IntArray>("Values", "The values of the items."));
66      Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight."));
67    }
68
69    public static DoubleValue Apply(BinaryVector v, IntValue capacity, DoubleValue penalty, IntArray weights, IntArray values) {
70      if (weights.Length != values.Length)
71        throw new InvalidOperationException("The weights and values parameters of the Knapsack problem have different sizes");
72     
73      double quality = 0;
74
75      int weight = 0;
76      int value = 0;
77
78      for (int i = 0; i < v.Length; i++) {
79        if (v[i]) {
80          weight += weights[i];
81          value += values[i];
82        }
83      }
84
85      if (weight > capacity.Value) {
86        quality =
87          value - penalty.Value * (weight - capacity.Value);
88      } else {
89        quality = value;
90      }
91
92      return new DoubleValue(quality);
93    }
94
95    public sealed override IOperation Apply() {
96      BinaryVector v = BinaryVectorParameter.ActualValue;
97
98      DoubleValue quality = Apply(BinaryVectorParameter.ActualValue,
99        KnapsackCapacityParameter.ActualValue,
100        PenaltyParameter.ActualValue,
101        WeightsParameter.ActualValue,
102        ValuesParameter.ActualValue);
103
104      QualityParameter.ActualValue = quality;
105
106      return base.Apply();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.