Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3124


Ignore:
Timestamp:
03/19/10 16:05:27 (15 years ago)
Author:
svonolfe
Message:

Added MoveEvaluator to the Knapsack problem (#917)

Location:
trunk/sources/HeuristicLab.Problems.Knapsack/3.3
Files:
4 added
3 edited

Legend:

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

    r3070 r3124  
    6666    }
    6767
    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)
     68    public static DoubleValue Apply(BinaryVector v, IntValue capacity, DoubleValue penalty, IntArray weights, IntArray values) {
     69      if (weights.Length != values.Length)
    7470        throw new InvalidOperationException("The weights and values parameters of the Knapsack problem have different sizes");
     71     
     72      double quality = 0;
    7573
    7674      int weight = 0;
    7775      int value = 0;
    78       double quality = 0;
    7976
    8077      for (int i = 0; i < v.Length; i++) {
    8178        if (v[i]) {
    82           weight += WeightsParameter.ActualValue[i];
    83           value += ValuesParameter.ActualValue[i];
     79          weight += weights[i];
     80          value += values[i];
    8481        }
    8582      }
    8683
    87       if (weight > capacity) {
     84      if (weight > capacity.Value) {
    8885        quality =
    89           value - penalty * (weight - capacity);
     86          value - penalty.Value * (weight - capacity.Value);
    9087      } else {
    9188        quality = value;
    9289      }
    9390
    94       QualityParameter.ActualValue = new DoubleValue(quality);
     91      return new DoubleValue(quality);
     92    }
     93
     94    public sealed override IOperation Apply() {
     95      BinaryVector v = BinaryVectorParameter.ActualValue;
     96
     97      DoubleValue quality = Apply(BinaryVectorParameter.ActualValue,
     98        KnapsackCapacityParameter.ActualValue,
     99        PenaltyParameter.ActualValue,
     100        WeightsParameter.ActualValue,
     101        ValuesParameter.ActualValue);
     102
     103      QualityParameter.ActualValue = quality;
    95104
    96105      return base.Apply();
  • trunk/sources/HeuristicLab.Problems.Knapsack/3.3/HeuristicLab.Problems.Knapsack-3.3.csproj

    r3116 r3124  
    8585    <Compile Include="Interfaces\IKnapsackEvaluator.cs" />
    8686    <Compile Include="Interfaces\IKnapsackSolutionsVisualizer.cs" />
     87    <Compile Include="Interfaces\IKnapsackMoveEvaluator.cs" />
    8788    <Compile Include="Knapsack.cs" />
     89    <Compile Include="MoveEvaluators\KnapsackMoveEvaluator.cs" />
     90    <Compile Include="MoveEvaluators\KnapsackOneBitflipMoveEvaluator.cs" />
    8891    <Compile Include="Properties\AssemblyInfo.cs" />
    8992  </ItemGroup>
  • trunk/sources/HeuristicLab.Problems.Knapsack/3.3/Knapsack.cs

    r3116 r3124  
    235235      OnVisualizerChanged();
    236236    }
     237    void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
     238      string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
     239      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
     240        op.OneBitflipMoveParameter.ActualName = name;
     241      }
     242    }
    237243    #endregion
    238244
     
    274280        ParameterizeOperators();
    275281      }
     282
     283      InitializeMoveGenerators();
     284    }
     285    private void InitializeMoveGenerators() {
     286      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
     287        if (op is IMoveGenerator) {
     288          op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
     289        }
     290      }
    276291    }
    277292    private void ParameterizeOperators() {
     
    283298        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    284299      }
     300      foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
     301        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
     302      }
     303      foreach (IKnapsackMoveEvaluator op in Operators.OfType<IKnapsackMoveEvaluator>()) {
     304        op.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
     305        op.PenaltyParameter.ActualName = PenaltyParameter.Name;
     306        op.WeightsParameter.ActualName = WeightsParameter.Name;
     307        op.ValuesParameter.ActualName = ValuesParameter.Name;
     308      }
    285309    }
    286310    #endregion
Note: See TracChangeset for help on using the changeset viewer.