public sealed override IOperation InstrumentedApply()
        {
            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.InstrumentedApply());
        }
        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);
        }
Пример #3
0
    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;
    }