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)

Location:
trunk/sources/HeuristicLab.Problems.Knapsack/3.3
Files:
3 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();
  • trunk/sources/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs

    r3528 r3537  
    143143
    144144      int itemCount = rand.Next(10, 100);
    145       int capacity = itemCount * 5;
    146 
    147       KnapsackCapacity = new IntValue(capacity);
    148145      Weights = new IntArray(itemCount);
    149146      Values = new IntArray(itemCount);
     147
     148      double totalWeight = 0;
    150149
    151150      for (int i = 0; i < itemCount; i++ ) {
     
    155154        Values[i] = value;
    156155        Weights[i] = weight;
    157       }
     156        totalWeight += weight;
     157      }
     158
     159      int capacity = (int)Math.Round(0.7 * totalWeight);
     160      KnapsackCapacity = new IntValue(capacity);
    158161    }
    159162
     
    175178
    176179      creator.BinaryVectorParameter.ActualName = "KnapsackSolution";
    177       evaluator.QualityParameter.ActualName = "NumberOfOnes";
    178180
    179181      InitializeRandomKnapsackInstance();
  • trunk/sources/HeuristicLab.Problems.Knapsack/3.3/MoveEvaluators/KnapsackOneBitflipMoveEvaluator.cs

    r3376 r3537  
    5656        PenaltyParameter.ActualValue,
    5757        WeightsParameter.ActualValue,
    58         ValuesParameter.ActualValue);
     58        ValuesParameter.ActualValue).Quality;
    5959
    6060      double moveQuality = quality.Value;
Note: See TracChangeset for help on using the changeset viewer.