#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.BinaryVectorEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.Knapsack {
///
/// A base class for operators which evaluate Knapsack solutions given in BinaryVector encoding.
///
[Item("KnapsackEvaluator", "Evaluates solutions for the Knapsack problem.")]
[StorableClass]
public class KnapsackEvaluator : SingleSuccessorOperator, IKnapsackEvaluator {
public ILookupParameter QualityParameter {
get { return (ILookupParameter)Parameters["Quality"]; }
}
public ILookupParameter SumWeightsParameter {
get { return (ILookupParameter)Parameters["SumWeights"]; }
}
public ILookupParameter SumValuesParameter {
get { return (ILookupParameter)Parameters["SumValues"]; }
}
public ILookupParameter AppliedPenaltyParameter {
get { return (ILookupParameter)Parameters["AppliedPenalty"]; }
}
public ILookupParameter BinaryVectorParameter {
get { return (ILookupParameter)Parameters["BinaryVector"]; }
}
public ILookupParameter KnapsackCapacityParameter {
get { return (ILookupParameter)Parameters["KnapsackCapacity"]; }
}
public ILookupParameter PenaltyParameter {
get { return (ILookupParameter)Parameters["Penalty"]; }
}
public ILookupParameter WeightsParameter {
get { return (ILookupParameter)Parameters["Weights"]; }
}
public ILookupParameter ValuesParameter {
get { return (ILookupParameter)Parameters["Values"]; }
}
[StorableConstructor]
protected KnapsackEvaluator(bool deserializing) : base(deserializing) { }
protected KnapsackEvaluator(KnapsackEvaluator original, Cloner cloner) : base(original, cloner) { }
public KnapsackEvaluator()
: base() {
Parameters.Add(new LookupParameter("Quality", "The evaluated quality of the OneMax solution."));
Parameters.Add(new LookupParameter("SumWeights", "The evaluated quality of the OneMax solution."));
Parameters.Add(new LookupParameter("SumValues", "The evaluated quality of the OneMax solution."));
Parameters.Add(new LookupParameter("AppliedPenalty", "The evaluated quality of the OneMax solution."));
Parameters.Add(new LookupParameter("BinaryVector", "The OneMax solution given in path representation which should be evaluated."));
Parameters.Add(new LookupParameter("KnapsackCapacity", "Capacity of the Knapsack."));
Parameters.Add(new LookupParameter("Weights", "The weights of the items."));
Parameters.Add(new LookupParameter("Values", "The values of the items."));
Parameters.Add(new LookupParameter("Penalty", "The penalty value for each unit of overweight."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new KnapsackEvaluator(this, cloner);
}
public struct KnapsackEvaluation {
public DoubleValue Quality;
public DoubleValue SumWeights;
public DoubleValue SumValues;
public DoubleValue AppliedPenalty;
}
public static KnapsackEvaluation Apply(BinaryVector v, IntValue capacity, DoubleValue penalty, IntArray weights, IntArray values) {
if (weights.Length != values.Length)
throw new InvalidOperationException("The weights and values parameters of the Knapsack problem have different sizes");
KnapsackEvaluation result = new KnapsackEvaluation();
double quality = 0;
int weight = 0;
int value = 0;
double appliedPenalty = 0;
for (int i = 0; i < v.Length; i++) {
if (v[i]) {
weight += weights[i];
value += values[i];
}
}
if (weight > capacity.Value) {
appliedPenalty = penalty.Value * (weight - capacity.Value);
}
quality = value - appliedPenalty;
result.AppliedPenalty = new DoubleValue(appliedPenalty);
result.SumWeights = new DoubleValue(weight);
result.SumValues = new DoubleValue(value);
result.Quality = new DoubleValue(quality);
return result;
}
public sealed override IOperation Apply() {
BinaryVector v = BinaryVectorParameter.ActualValue;
KnapsackEvaluation evaluation = Apply(BinaryVectorParameter.ActualValue,
KnapsackCapacityParameter.ActualValue,
PenaltyParameter.ActualValue,
WeightsParameter.ActualValue,
ValuesParameter.ActualValue);
QualityParameter.ActualValue = evaluation.Quality;
SumWeightsParameter.ActualValue = evaluation.SumWeights;
SumValuesParameter.ActualValue = evaluation.SumValues;
AppliedPenaltyParameter.ActualValue = evaluation.AppliedPenalty;
return base.Apply();
}
}
}