- Timestamp:
- 04/26/10 18:58:32 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.Knapsack/3.3/Evaluators/KnapsackEvaluator.cs
r3376 r3537 39 39 get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; } 40 40 } 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 } 41 53 42 54 public ILookupParameter<BinaryVector> BinaryVectorParameter { … … 60 72 : base() { 61 73 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the OneMax solution.")); 74 Parameters.Add(new LookupParameter<DoubleValue>("SumWeights", "The evaluated quality of the OneMax solution.")); 75 Parameters.Add(new LookupParameter<DoubleValue>("SumValues", "The evaluated quality of the OneMax solution.")); 76 Parameters.Add(new LookupParameter<DoubleValue>("AppliedPenalty", "The evaluated quality of the OneMax solution.")); 62 77 Parameters.Add(new LookupParameter<BinaryVector>("BinaryVector", "The OneMax solution given in path representation which should be evaluated.")); 63 78 Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.")); … … 67 82 } 68 83 69 public static DoubleValue Apply(BinaryVector v, IntValue capacity, DoubleValue penalty, IntArray weights, IntArray values) { 84 public struct KnapsackEvaluation { 85 public DoubleValue Quality; 86 public DoubleValue SumWeights; 87 public DoubleValue SumValues; 88 public DoubleValue AppliedPenalty; 89 } 90 91 public static KnapsackEvaluation Apply(BinaryVector v, IntValue capacity, DoubleValue penalty, IntArray weights, IntArray values) { 70 92 if (weights.Length != values.Length) 71 throw new InvalidOperationException("The weights and values parameters of the Knapsack problem have different sizes"); 72 93 throw new InvalidOperationException("The weights and values parameters of the Knapsack problem have different sizes"); 94 95 KnapsackEvaluation result = new KnapsackEvaluation(); 96 73 97 double quality = 0; 74 98 75 99 int weight = 0; 76 100 int value = 0; 101 double appliedPenalty = 0; 77 102 78 103 for (int i = 0; i < v.Length; i++) { … … 84 109 85 110 if (weight > capacity.Value) { 86 quality = 87 value - penalty.Value * (weight - capacity.Value); 88 } else { 89 quality = value; 90 } 111 appliedPenalty = penalty.Value * (weight - capacity.Value); 112 } 91 113 92 return new DoubleValue(quality); 114 quality = value - appliedPenalty; 115 116 result.AppliedPenalty = new DoubleValue(appliedPenalty); 117 result.SumWeights = new DoubleValue(weight); 118 result.SumValues = new DoubleValue(value); 119 result.Quality = new DoubleValue(quality); 120 121 return result; 93 122 } 94 123 … … 96 125 BinaryVector v = BinaryVectorParameter.ActualValue; 97 126 98 DoubleValue quality= Apply(BinaryVectorParameter.ActualValue,127 KnapsackEvaluation evaluation = Apply(BinaryVectorParameter.ActualValue, 99 128 KnapsackCapacityParameter.ActualValue, 100 129 PenaltyParameter.ActualValue, … … 102 131 ValuesParameter.ActualValue); 103 132 104 QualityParameter.ActualValue = quality; 133 QualityParameter.ActualValue = evaluation.Quality; 134 SumWeightsParameter.ActualValue = evaluation.SumWeights; 135 SumValuesParameter.ActualValue = evaluation.SumValues; 136 AppliedPenaltyParameter.ActualValue = evaluation.AppliedPenalty; 105 137 106 138 return base.Apply();
Note: See TracChangeset
for help on using the changeset viewer.