Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/24/12 15:04:37 (12 years ago)
Author:
jkarder
Message:

#1331:

  • applied some of the changes suggested by ascheibe in comment:32:ticket:1331
  • restructured path relinking and improvement operators and similarity calculators
  • fixed bug in TSPMultipleGuidesPathRelinker
Location:
branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3
Files:
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/HeuristicLab.Algorithms.ScatterSearch-3.3.csproj

    r8306 r8319  
    8585  </ItemGroup>
    8686  <ItemGroup>
    87     <Compile Include="IScatterSearchOperator.cs" />
    8887    <Compile Include="OffspringProcessor.cs" />
    8988    <None Include="HeuristicLab.snk" />
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/OffspringProcessor.cs

    r8086 r8319  
    3232  [Item("OffspringProcessor", "An operator that creates a subscope with subscopes for every variable in the current scope.")]
    3333  [StorableClass]
    34   public sealed class OffspringProcessor : SingleSuccessorOperator, IScatterSearchOperator {
     34  public sealed class OffspringProcessor : SingleSuccessorOperator {
    3535    #region Parameter properties
    3636    public ScopeParameter CurrentScopeParameter {
    3737      get { return (ScopeParameter)Parameters["CurrentScope"]; }
    38     }
    39     public IValueLookupParameter<IItem> TargetParameter {
    40       get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
    4138    }
    4239    #endregion
     
    4542    private IScope CurrentScope {
    4643      get { return CurrentScopeParameter.ActualValue; }
    47     }
    48     private IItem Target {
    49       get { return TargetParameter.ActualValue; }
    5044    }
    5145    #endregion
     
    5852      #region Create parameters
    5953      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the offspring as variables."));
    60       Parameters.Add(new ValueLookupParameter<IItem>("Target", "This parameter is used for name translation only."));
    6154      #endregion
    6255    }
     
    6760
    6861    public override IOperation Apply() {
    69       VariableCollection offspringSolutions = CurrentScope.Variables;
    70       IScope offspringScope = new Scope("Offspring");
    71       foreach (var solution in offspringSolutions) {
    72         IScope scope = new Scope();
    73         scope.Variables.Add(new Variable(TargetParameter.ActualName, solution.Value));
    74         offspringScope.SubScopes.Add(scope);
    75       }
     62      var child = new Scope();
     63      child.Variables.AddRange(CurrentScope.Variables);
     64      var offspringScope = new Scope();
     65      offspringScope.SubScopes.Add(child);
     66      var parents = CurrentScope.SubScopes.ToArray();
     67      CurrentScope.Variables.Clear();
     68      CurrentScope.SubScopes.Clear();
     69      CurrentScope.SubScopes.AddRange(parents);
    7670      CurrentScope.SubScopes.Add(offspringScope);
    77       CurrentScope.Variables.Clear();
    7871      return base.Apply();
    7972    }
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/PopulationRebuildMethod.cs

    r8086 r8319  
    3434  [Item("PopulationRebuildMethod", "An operator that updates the reference set and rebuilds the population.")]
    3535  [StorableClass]
    36   public sealed class PopulationRebuildMethod : SingleSuccessorOperator, IScatterSearchOperator {
     36  public sealed class PopulationRebuildMethod : SingleSuccessorOperator {
    3737    #region Parameter properties
    3838    public ScopeParameter CurrentScopeParameter {
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/ReferenceSetUpdateMethod.cs

    r8304 r8319  
    3939  public sealed class ReferenceSetUpdateMethod : SingleSuccessorOperator, ISimilarityBasedOperator {
    4040    #region ISimilarityBasedOperator Members
    41     public ISimilarityCalculator SimilarityCalculator { get; set; }
     41    public ISolutionSimilarityCalculator SimilarityCalculator { get; set; }
    4242    #endregion
    4343
     
    7676
    7777    public override IOperation Apply() {
    78       IDictionary<IScope, double> population = new Dictionary<IScope, double>();
    79       foreach (var pScope in CurrentScope.SubScopes[0].SubScopes) {
    80         double similarity = 0;
    81         foreach (var rScope in CurrentScope.SubScopes[1].SubScopes) {
    82           similarity += SimilarityCalculator.CalculateIndividualSimilarity(pScope, rScope);
    83         }
    84         population[pScope] = similarity;
     78      var populationSimilarity = new Dictionary<IScope, double>();
     79      var populationScope = CurrentScope.SubScopes[0];
     80      var refSetScope = CurrentScope.SubScopes[1];
     81      var similarityMatrix = SimilarityCalculator.CalculateSolutionCrowdSimilarity(populationScope, refSetScope);
     82      for (int i = 0; i < populationScope.SubScopes.Count; i++) {
     83        populationSimilarity[populationScope.SubScopes[i]] = similarityMatrix[i].Sum();
    8584      }
    8685      int numberOfHighQualitySolutions = CurrentScope.SubScopes[1].SubScopes.Count;
    87       foreach (var entry in population.OrderBy(x => x.Value).Take(ReferenceSetSize.Value - numberOfHighQualitySolutions)) {
     86      foreach (var entry in populationSimilarity.OrderBy(x => x.Value).Take(ReferenceSetSize.Value - numberOfHighQualitySolutions)) {
    8887        CurrentScope.SubScopes[1].SubScopes.Add(entry.Key);
    8988        CurrentScope.SubScopes[0].SubScopes.Remove(entry.Key);
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearch.cs

    r8299 r8319  
    2222using System;
    2323using System.Linq;
    24 using System.Reflection;
    2524using HeuristicLab.Analysis;
    2625using HeuristicLab.Common;
     
    8988      get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
    9089    }
    91     public ConstrainedValueParameter<ISimilarityCalculator> SimilarityCalculatorParameter {
    92       get { return (ConstrainedValueParameter<ISimilarityCalculator>)Parameters["SimilarityCalculator"]; }
     90    public ConstrainedValueParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
     91      get { return (ConstrainedValueParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
    9392    }
    9493    #endregion
     
    139138      set { SetSeedRandomlyParameter.Value = value; }
    140139    }
    141     private ISimilarityCalculator SimilarityCalculator {
     140    private ISolutionSimilarityCalculator SimilarityCalculator {
    142141      get { return SimilarityCalculatorParameter.Value; }
    143142      set { SimilarityCalculatorParameter.Value = value; }
     
    185184      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
    186185      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
    187       Parameters.Add(new ConstrainedValueParameter<ISimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
     186      Parameters.Add(new ConstrainedValueParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
    188187      #endregion
    189188
     
    382381    }
    383382    private void UpdateSimilarityCalculators() {
    384       ISimilarityCalculator oldDiversityCalculator = SimilarityCalculatorParameter.Value;
     383      ISolutionSimilarityCalculator oldSimilarityCalculator = SimilarityCalculatorParameter.Value;
    385384      SimilarityCalculatorParameter.ValidValues.Clear();
    386       ISimilarityCalculator defaultDiversityCalculator = Problem.Operators.OfType<ISimilarityCalculator>().FirstOrDefault();
    387 
    388       foreach (ISimilarityCalculator diversityCalculator in Problem.Operators.OfType<ISimilarityCalculator>())
    389         SimilarityCalculatorParameter.ValidValues.Add(diversityCalculator);
    390 
    391       if (oldDiversityCalculator != null) {
    392         ISimilarityCalculator diversityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldDiversityCalculator.GetType());
    393         if (diversityCalculator != null) SimilarityCalculatorParameter.Value = diversityCalculator;
    394         else oldDiversityCalculator = null;
    395       }
    396       if (oldDiversityCalculator == null && defaultDiversityCalculator != null)
    397         SimilarityCalculatorParameter.Value = defaultDiversityCalculator;
     385      ISolutionSimilarityCalculator defaultSimilarityCalculator = Problem.Operators.OfType<ISolutionSimilarityCalculator>().FirstOrDefault();
     386
     387      foreach (ISolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISolutionSimilarityCalculator>())
     388        SimilarityCalculatorParameter.ValidValues.Add(similarityCalculator);
     389
     390      if (oldSimilarityCalculator != null) {
     391        ISolutionSimilarityCalculator similarityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSimilarityCalculator.GetType());
     392        if (similarityCalculator != null) SimilarityCalculatorParameter.Value = similarityCalculator;
     393        else oldSimilarityCalculator = null;
     394      }
     395      if (oldSimilarityCalculator == null && defaultSimilarityCalculator != null)
     396        SimilarityCalculatorParameter.Value = defaultSimilarityCalculator;
    398397    }
    399398    private void ParameterizeBestSelector() {
     
    410409        MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
    411410        MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    412         MainLoop.TargetParameter.ActualName = Improver.TargetParameter.ActualName;
    413         foreach (var op in MainLoop.OperatorGraph.Operators.OfType<IScatterSearchOperator>()) {
    414           // parameter should be accessed direclty (using an interface definition)
    415           PropertyInfo propInfo = op.GetType().GetProperty(MainLoop.TargetParameter.Name + "Parameter");
    416           if (propInfo != null && propInfo.CanRead)
    417             (propInfo.GetValue(op, null) as IValueLookupParameter<IItem>).ActualName = MainLoop.TargetParameter.ActualName;
    418           propInfo = op.GetType().GetProperty(MainLoop.QualityParameter.Name + "Parameter");
    419           if (propInfo != null && propInfo.CanRead)
    420             (propInfo.GetValue(op, null) as IValueLookupParameter<IItem>).ActualName = MainLoop.QualityParameter.ActualName;
    421         }
     411        MainLoop.OperatorGraph.Operators.OfType<PopulationRebuildMethod>().Single().QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     412        MainLoop.OperatorGraph.Operators.OfType<SolutionPoolUpdateMethod>().Single().QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    422413        foreach (ISimilarityBasedOperator op in MainLoop.OperatorGraph.Operators.OfType<ISimilarityBasedOperator>())
    423414          op.SimilarityCalculator = SimilarityCalculator;
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearchMainLoop.cs

    r8086 r8319  
    8989      get { return (IValueLookupParameter<VariableCollection>)Parameters["Results"]; }
    9090    }
    91     public IValueLookupParameter<ISimilarityCalculator> SimilarityCalculatorParameter {
    92       get { return (IValueLookupParameter<ISimilarityCalculator>)Parameters["SimilarityCalculator"]; }
    93     }
    94     public IValueLookupParameter<IItem> TargetParameter {
    95       get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
     91    public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
     92      get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
    9693    }
    9794    #endregion
     
    166163      set { ResultsParameter.ActualValue = value; }
    167164    }
    168     private ISimilarityCalculator SimilarityCalculator {
     165    private ISolutionSimilarityCalculator SimilarityCalculator {
    169166      get { return SimilarityCalculatorParameter.ActualValue; }
    170167      set { SimilarityCalculatorParameter.ActualValue = value; }
    171     }
    172     private IItem Target {
    173       get { return TargetParameter.ActualValue; }
    174       set { TargetParameter.ActualValue = value; }
    175168    }
    176169    #endregion
     
    204197      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
    205198      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
    206       Parameters.Add(new ValueLookupParameter<ISimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
    207       Parameters.Add(new ValueLookupParameter<IItem>("Target", "This parameter is used for name translation only."));
     199      Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
    208200      #endregion
    209201
     
    298290      pathRelinker.Name = "PathRelinker";
    299291      pathRelinker.OperatorParameter.ActualName = PathRelinkerParameter.Name;
    300       pathRelinker.Successor = offspringProcessor;
     292      pathRelinker.Successor = rightSelector;
    301293
    302294      crossover.Name = "Crossover";
     
    304296      crossover.Successor = offspringProcessor;
    305297
    306       offspringProcessor.TargetParameter.ActualName = TargetParameter.ActualName;
    307298      offspringProcessor.Successor = rightSelector;
    308299
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs

    r8304 r8319  
    2828using HeuristicLab.Operators;
    2929using HeuristicLab.Optimization;
    30 using HeuristicLab.Optimization.Operators;
    3130using HeuristicLab.Parameters;
    3231using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3837  [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")]
    3938  [StorableClass]
    40   public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, IScatterSearchOperator, ISimilarityBasedOperator {
     39  public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, ISimilarityBasedOperator {
    4140    #region ISimilarityBasedOperator Members
    42     public ISimilarityCalculator SimilarityCalculator { get; set; }
     41    public ISolutionSimilarityCalculator SimilarityCalculator { get; set; }
    4342    #endregion
    4443
     
    130129      if (orderedOffspring.Any(hasBetterQuality)) {
    131130        // produce the set union
    132         var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), new SolutionEqualityComparer<IScope>(SimilarityCalculator.CalculateIndividualSimilarity));
     131        var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), new SolutionEqualityComparer<IScope>(SimilarityCalculator.CalculateSolutionSimilarity));
    133132        if (union.Count() > orderedParents.Count()) {
    134133          var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
     
    142141    }
    143142
     143    // derive SingleObjectiveSolutionSimilarityCalculator from EqualityComparer
     144    // delete this ...
    144145    public class SolutionEqualityComparer<T> : EqualityComparer<T> {
    145146      private readonly Func<T, T, double> similarityCalculator;
Note: See TracChangeset for help on using the changeset viewer.