[3070] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3070] | 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 |
|
---|
[4068] | 22 | using System;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[3070] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[4068] | 26 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
[3070] | 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.Knapsack {
|
---|
| 32 | /// <summary>
|
---|
[4513] | 33 | /// A base class for operators which evaluate Knapsack solutions given in BinaryVector encoding.
|
---|
[3070] | 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 | }
|
---|
[3537] | 41 |
|
---|
| 42 | public ILookupParameter<DoubleValue> SumWeightsParameter {
|
---|
| 43 | get { return (ILookupParameter<DoubleValue>)Parameters["SumWeights"]; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public ILookupParameter<DoubleValue> SumValuesParameter {
|
---|
| 47 | get { return (ILookupParameter<DoubleValue>)Parameters["SumValues"]; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public ILookupParameter<DoubleValue> AppliedPenaltyParameter {
|
---|
| 51 | get { return (ILookupParameter<DoubleValue>)Parameters["AppliedPenalty"]; }
|
---|
| 52 | }
|
---|
[4068] | 53 |
|
---|
[3070] | 54 | public ILookupParameter<BinaryVector> BinaryVectorParameter {
|
---|
| 55 | get { return (ILookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public ILookupParameter<IntValue> KnapsackCapacityParameter {
|
---|
| 59 | get { return (ILookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
|
---|
| 60 | }
|
---|
| 61 | public ILookupParameter<DoubleValue> PenaltyParameter {
|
---|
| 62 | get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }
|
---|
| 63 | }
|
---|
| 64 | public ILookupParameter<IntArray> WeightsParameter {
|
---|
| 65 | get { return (ILookupParameter<IntArray>)Parameters["Weights"]; }
|
---|
| 66 | }
|
---|
| 67 | public ILookupParameter<IntArray> ValuesParameter {
|
---|
| 68 | get { return (ILookupParameter<IntArray>)Parameters["Values"]; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[4722] | 71 | [StorableConstructor]
|
---|
| 72 | protected KnapsackEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 73 | protected KnapsackEvaluator(KnapsackEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
[3070] | 74 | public KnapsackEvaluator()
|
---|
| 75 | : base() {
|
---|
| 76 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the OneMax solution."));
|
---|
[3537] | 77 | Parameters.Add(new LookupParameter<DoubleValue>("SumWeights", "The evaluated quality of the OneMax solution."));
|
---|
| 78 | Parameters.Add(new LookupParameter<DoubleValue>("SumValues", "The evaluated quality of the OneMax solution."));
|
---|
| 79 | Parameters.Add(new LookupParameter<DoubleValue>("AppliedPenalty", "The evaluated quality of the OneMax solution."));
|
---|
[3070] | 80 | Parameters.Add(new LookupParameter<BinaryVector>("BinaryVector", "The OneMax solution given in path representation which should be evaluated."));
|
---|
| 81 | Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack."));
|
---|
| 82 | Parameters.Add(new LookupParameter<IntArray>("Weights", "The weights of the items."));
|
---|
| 83 | Parameters.Add(new LookupParameter<IntArray>("Values", "The values of the items."));
|
---|
| 84 | Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight."));
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[4722] | 87 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 88 | return new KnapsackEvaluator(this, cloner);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[3537] | 91 | public struct KnapsackEvaluation {
|
---|
| 92 | public DoubleValue Quality;
|
---|
| 93 | public DoubleValue SumWeights;
|
---|
| 94 | public DoubleValue SumValues;
|
---|
| 95 | public DoubleValue AppliedPenalty;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public static KnapsackEvaluation Apply(BinaryVector v, IntValue capacity, DoubleValue penalty, IntArray weights, IntArray values) {
|
---|
[3124] | 99 | if (weights.Length != values.Length)
|
---|
[3537] | 100 | throw new InvalidOperationException("The weights and values parameters of the Knapsack problem have different sizes");
|
---|
| 101 |
|
---|
| 102 | KnapsackEvaluation result = new KnapsackEvaluation();
|
---|
| 103 |
|
---|
[3124] | 104 | double quality = 0;
|
---|
[3070] | 105 |
|
---|
| 106 | int weight = 0;
|
---|
| 107 | int value = 0;
|
---|
[3537] | 108 | double appliedPenalty = 0;
|
---|
[3070] | 109 |
|
---|
| 110 | for (int i = 0; i < v.Length; i++) {
|
---|
| 111 | if (v[i]) {
|
---|
[3124] | 112 | weight += weights[i];
|
---|
| 113 | value += values[i];
|
---|
[3070] | 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[3124] | 117 | if (weight > capacity.Value) {
|
---|
[3537] | 118 | appliedPenalty = penalty.Value * (weight - capacity.Value);
|
---|
[4068] | 119 | }
|
---|
[3070] | 120 |
|
---|
[4068] | 121 | quality = value - appliedPenalty;
|
---|
[3537] | 122 |
|
---|
| 123 | result.AppliedPenalty = new DoubleValue(appliedPenalty);
|
---|
| 124 | result.SumWeights = new DoubleValue(weight);
|
---|
| 125 | result.SumValues = new DoubleValue(value);
|
---|
| 126 | result.Quality = new DoubleValue(quality);
|
---|
| 127 |
|
---|
| 128 | return result;
|
---|
[3124] | 129 | }
|
---|
[3070] | 130 |
|
---|
[3124] | 131 | public sealed override IOperation Apply() {
|
---|
| 132 | BinaryVector v = BinaryVectorParameter.ActualValue;
|
---|
| 133 |
|
---|
[3537] | 134 | KnapsackEvaluation evaluation = Apply(BinaryVectorParameter.ActualValue,
|
---|
[4068] | 135 | KnapsackCapacityParameter.ActualValue,
|
---|
| 136 | PenaltyParameter.ActualValue,
|
---|
| 137 | WeightsParameter.ActualValue,
|
---|
[3124] | 138 | ValuesParameter.ActualValue);
|
---|
| 139 |
|
---|
[3537] | 140 | QualityParameter.ActualValue = evaluation.Quality;
|
---|
| 141 | SumWeightsParameter.ActualValue = evaluation.SumWeights;
|
---|
| 142 | SumValuesParameter.ActualValue = evaluation.SumValues;
|
---|
| 143 | AppliedPenaltyParameter.ActualValue = evaluation.AppliedPenalty;
|
---|
[3124] | 144 |
|
---|
[3070] | 145 | return base.Apply();
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|