Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/22/12 11:11:38 (12 years ago)
Author:
jkarder
Message:

#1331:

  • synced branch with trunk
  • added custom interface (ISimilarityBasedOperator) to mark operators that conduct similarity calculation
  • similarity calculators are now parameterized by the algorithm
  • deleted SolutionPool2TierUpdateMethod
  • deleted KnapsackMultipleGuidesPathRelinker
  • moved IImprovementOperator, IPathRelinker and ISimilarityCalculator to HeuristicLab.Optimization
  • added parameter descriptions
  • fixed plugin references
  • fixed count of EvaluatedSolutions
  • fixed check for duplicate solutions
  • minor code improvements
Location:
branches/ScatterSearch (trunk integration)
Files:
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • branches/ScatterSearch (trunk integration)

  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.Knapsack/3.3/HeuristicLab.Problems.Knapsack-3.3.csproj

    r7789 r8086  
    116116    <Compile Include="MoveEvaluators\KnapsackMoveEvaluator.cs" />
    117117    <Compile Include="MoveEvaluators\KnapsackOneBitflipMoveEvaluator.cs" />
    118     <Compile Include="PathRelinkers\KnapsackMultipleGuidesPathRelinker.cs" />
    119118    <Compile Include="PathRelinkers\KnapsackPathRelinker.cs" />
    120119    <Compile Include="PathRelinkers\KnapsackSimultaneousPathRelinker.cs" />
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.Knapsack/3.3/Improvers/KnapsackImprovementOperator.cs

    r7789 r8086  
    2020#endregion
    2121
     22using System;
    2223using System.Linq;
    2324using HeuristicLab.Common;
     
    2627using HeuristicLab.Encodings.BinaryVectorEncoding;
    2728using HeuristicLab.Operators;
    28 using HeuristicLab.Optimization.Operators;
     29using HeuristicLab.Optimization;
    2930using HeuristicLab.Parameters;
    3031using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    7071      set { PenaltyParameter.ActualValue = value; }
    7172    }
    72     private IItem Target {
    73       get { return TargetParameter.ActualValue; }
    74     }
    7573    private IntArray Values {
    7674      get { return ValuesParameter.ActualValue; }
     
    8987      : base() {
    9088      #region Create parameters
    91       Parameters.Add(new ScopeParameter("CurrentScope"));
    92       Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity"));
    93       Parameters.Add(new LookupParameter<DoubleValue>("Penalty"));
    94       Parameters.Add(new ValueLookupParameter<IItem>("Target"));
    95       Parameters.Add(new LookupParameter<IntArray>("Values"));
    96       Parameters.Add(new LookupParameter<IntArray>("Weights"));
     89      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved."));
     90      Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack."));
     91      Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight."));
     92      Parameters.Add(new ValueLookupParameter<IItem>("Target", "This parameter is used for name translation only."));
     93      Parameters.Add(new LookupParameter<IntArray>("Values", "The values of the items."));
     94      Parameters.Add(new LookupParameter<IntArray>("Weights", "The weights of the items."));
    9795      #endregion
    9896    }
     
    104102    public override IOperation Apply() {
    105103      BinaryVector sol = CurrentScope.Variables[TargetParameter.ActualName].Value as BinaryVector;
     104      if (sol == null)
     105        throw new ArgumentException("Cannot improve solution because it has the wrong type.");
    106106
    107107      // calculate value-to-weight ratio
     
    113113      // calculate order for ratio
    114114      int[] order = new int[ratio.Length];
    115       ratio.Select((x, index) => new { Value = x, ValueIndex = index })
    116            .OrderBy(x => x.Value)
    117            .Select((x, index) => new { ValueIndex = x.ValueIndex, ItemIndex = index }).ToList()
    118            .ForEach(x => order[x.ItemIndex] = x.ValueIndex);
     115      foreach (var x in ratio.Select((x, index) => new { Value = x, ValueIndex = index })
     116                             .OrderBy(x => x.Value)
     117                             .Select((x, index) => new { ValueIndex = x.ValueIndex, ItemIndex = index })) {
     118        order[x.ItemIndex] = x.ValueIndex;
     119      }
    119120
     121      int evaluatedSolutions = 0;
    120122      int j = sol.Length - 1;
    121123      while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, Penalty, Weights, Values)
    122                               .SumWeights.Value > KnapsackCapacity.Value && j >= 0)
     124                              .SumWeights.Value > KnapsackCapacity.Value && j >= 0) {
    123125        sol[order[j--]] = false;
     126        evaluatedSolutions++;
     127      }
    124128
    125129      // calculate weight
     
    136140          weight += Weights[order[j]];
    137141        } else feasible = false;
     142        evaluatedSolutions++;
    138143      }
    139144
    140145      CurrentScope.Variables[TargetParameter.ActualName].Value = sol;
     146      CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", new IntValue(evaluatedSolutions)));
    141147
    142148      return base.Apply();
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs

    r7789 r8086  
    2828using HeuristicLab.Encodings.BinaryVectorEncoding;
    2929using HeuristicLab.Optimization;
    30 using HeuristicLab.Optimization.Operators;
    3130using HeuristicLab.Parameters;
    3231using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    246245    private void InitializeOperators() {
    247246      Operators.Add(new KnapsackImprovementOperator());
    248       Operators.Add(new KnapsackMultipleGuidesPathRelinker());
    249247      Operators.Add(new KnapsackPathRelinker());
    250248      Operators.Add(new KnapsackSimultaneousPathRelinker());
     
    306304      }
    307305      foreach (ISimilarityCalculator op in Operators.OfType<ISimilarityCalculator>()) {
    308         op.TargetParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    309         op.TargetParameter.Hidden = true;
     306        op.Target = SolutionCreator.BinaryVectorParameter.ActualName;
    310307      }
    311308    }
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.Knapsack/3.3/Plugin.cs.frame

    r7259 r8086  
    3636  [PluginDependency("HeuristicLab.Operators", "3.3")]
    3737  [PluginDependency("HeuristicLab.Optimization", "3.3")]
     38  [PluginDependency("HeuristicLab.Optimization.Operators", "3.3")]
    3839  [PluginDependency("HeuristicLab.Parameters", "3.3")]
    3940  [PluginDependency("HeuristicLab.Persistence", "3.3")]
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.Knapsack/3.3/SimilarityCalculators/KnapsackSimilarityCalculator.cs

    r7793 r8086  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Encodings.BinaryVectorEncoding;
    2526using HeuristicLab.Optimization.Operators;
    26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    2828namespace HeuristicLab.Problems.Knapsack {
     
    3131  /// </summary>
    3232  [Item("KnapsackSimilarityCalculator", "An operator that performs similarity calculation between two knapsack solutions.")]
    33   [StorableClass]
    3433  public sealed class KnapsackSimilarityCalculator : SimilarityCalculator {
    35     [StorableConstructor]
    3634    private KnapsackSimilarityCalculator(bool deserializing) : base(deserializing) { }
    3735    private KnapsackSimilarityCalculator(KnapsackSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
    3836    public KnapsackSimilarityCalculator() : base() { }
    3937
    40     public override IDeepCloneable Clone(Cloner cloner) {
    41       return new KnapsackSimilarityCalculator(this, cloner);
     38    public static double CalculateSimilarity(BinaryVector left, BinaryVector right) {
     39      if (left == null || right == null)
     40        throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null.");
     41
     42      double similarity = 0.0;
     43      for (int i = 0; i < left.Length; i++)
     44        if (left[i] == right[i]) similarity++;
     45      return similarity / left.Length;
    4246    }
    4347
    4448    protected override double CalculateSimilarity(IScope left, IScope right) {
    45       BinaryVector sol1 = left.Variables[TargetParameter.ActualName].Value as BinaryVector;
    46       BinaryVector sol2 = right.Variables[TargetParameter.ActualName].Value as BinaryVector;
     49      BinaryVector sol1 = left.Variables[Target].Value as BinaryVector;
     50      BinaryVector sol2 = right.Variables[Target].Value as BinaryVector;
    4751
    48       double similarity = 0.0;
    49       for (int i = 0; i < sol1.Length; i++)
    50         if (sol1[i] == sol2[i]) similarity++;
    51       return similarity / sol1.Length;
     52      return CalculateSimilarity(sol1, sol2);
    5253    }
    5354  }
Note: See TracChangeset for help on using the changeset viewer.