Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/26/10 18:58:32 (15 years ago)
Author:
svonolfe
Message:

Implemented reviewers comments for the KnapsackProblem (#917)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.Knapsack/3.3/Evaluators/KnapsackEvaluator.cs

    r3376 r3537  
    3939      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
    4040    }
     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    }
    4153   
    4254    public ILookupParameter<BinaryVector> BinaryVectorParameter {
     
    6072      : base() {
    6173      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."));
    6277      Parameters.Add(new LookupParameter<BinaryVector>("BinaryVector", "The OneMax solution given in path representation which should be evaluated."));
    6378      Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack."));
     
    6782    }
    6883
    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) {
    7092      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
    7397      double quality = 0;
    7498
    7599      int weight = 0;
    76100      int value = 0;
     101      double appliedPenalty = 0;
    77102
    78103      for (int i = 0; i < v.Length; i++) {
     
    84109
    85110      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      }
    91113
    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;
    93122    }
    94123
     
    96125      BinaryVector v = BinaryVectorParameter.ActualValue;
    97126
    98       DoubleValue quality = Apply(BinaryVectorParameter.ActualValue,
     127      KnapsackEvaluation evaluation = Apply(BinaryVectorParameter.ActualValue,
    99128        KnapsackCapacityParameter.ActualValue,
    100129        PenaltyParameter.ActualValue,
     
    102131        ValuesParameter.ActualValue);
    103132
    104       QualityParameter.ActualValue = quality;
     133      QualityParameter.ActualValue = evaluation.Quality;
     134      SumWeightsParameter.ActualValue = evaluation.SumWeights;
     135      SumValuesParameter.ActualValue = evaluation.SumValues;
     136      AppliedPenaltyParameter.ActualValue = evaluation.AppliedPenalty;
    105137
    106138      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.