Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3124 was 3124, checked in by svonolfe, 14 years ago

Added MoveEvaluator to the Knapsack problem (#917)

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