1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022010 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 


22  using HeuristicLab.Core;


23  using HeuristicLab.Data;


24  using HeuristicLab.Parameters;


25  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


26  using HeuristicLab.Encodings.BinaryVectorEncoding;


27  using HeuristicLab.Operators;


28  using System;


29 


30  namespace 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 sealed override IOperation Apply() {


69  BinaryVector v = BinaryVectorParameter.ActualValue;


70  int capacity = KnapsackCapacityParameter.ActualValue.Value;


71  double penalty = PenaltyParameter.ActualValue.Value;


72 


73  if (WeightsParameter.ActualValue.Length != ValuesParameter.ActualValue.Length)


74  throw new InvalidOperationException("The weights and values parameters of the Knapsack problem have different sizes");


75 


76  int weight = 0;


77  int value = 0;


78  double quality = 0;


79 


80  for (int i = 0; i < v.Length; i++) {


81  if (v[i]) {


82  weight += WeightsParameter.ActualValue[i];


83  value += ValuesParameter.ActualValue[i];


84  }


85  }


86 


87  if (weight > capacity) {


88  quality =


89  value  penalty * (weight  capacity);


90  } else {


91  quality = value;


92  }


93 


94  QualityParameter.ActualValue = new DoubleValue(quality);


95 


96  return base.Apply();


97  }


98  }


99  }

