Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/09/11 11:01:08 (13 years ago)
Author:
mkommend
Message:

#1479: Updated grammar editor branch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Problems.QuadraticAssignment/3.3/Analyzers/QAPPopulationDiversityAnalyzer.cs

    r6377 r6647  
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Core;
     25using HeuristicLab.Data;
    2526using HeuristicLab.Encodings.PermutationEncoding;
     27using HeuristicLab.Parameters;
    2628using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2729
     
    3335  [StorableClass]
    3436  public sealed class QAPPopulationDiversityAnalyzer : PopulationDiversityAnalyzer<Permutation> {
     37    public IValueParameter<BoolValue> UsePhenotypeSimilarityParameter {
     38      get { return (IValueParameter<BoolValue>)Parameters["UsePhenotypeSimilarity"]; }
     39    }
     40    public ILookupParameter<DoubleMatrix> WeightsParameter {
     41      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
     42    }
     43    public ILookupParameter<DoubleMatrix> DistancesParameter {
     44      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
     45    }
     46
    3547    [StorableConstructor]
    3648    private QAPPopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { }
    3749    private QAPPopulationDiversityAnalyzer(QAPPopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { }
    38     public QAPPopulationDiversityAnalyzer() : base() { }
     50    public QAPPopulationDiversityAnalyzer()
     51      : base() {
     52      Parameters.Add(new ValueParameter<BoolValue>("UsePhenotypeSimilarity", "True if the similarity should be measured a level closer to the phenotype (the number of similar assignments of individual weights to distances). Set to false if the number of equal assignments (facility to location) should be counted.", new BoolValue(false)));
     53      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
     54      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
     55    }
    3956
    4057    public override IDeepCloneable Clone(Cloner cloner) {
     
    4259    }
    4360
     61    [StorableHook(HookType.AfterDeserialization)]
     62    private void AfterDeserialization() {
     63      // BackwardsCompatibility3.3
     64      #region Backwards compatible code, remove with 3.4
     65      if (!Parameters.ContainsKey("UsePhenotypeSimilarity"))
     66        Parameters.Add(new ValueParameter<BoolValue>("UsePhenotypeSimilarity", "True if the similarity should be measured a level closer to the phenotype (the number of similar assignments of individual weights to distances). Set to false if the number of equal assignments (facility to location) should be counted.", new BoolValue(false)));
     67      if (!Parameters.ContainsKey("Weights"))
     68        Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
     69      if (!Parameters.ContainsKey("Distances"))
     70        Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
     71      #endregion
     72    }
     73
    4474    protected override double[,] CalculateSimilarities(Permutation[] solutions) {
     75      DoubleMatrix weights = WeightsParameter.ActualValue, distances = DistancesParameter.ActualValue;
     76      bool phenotypeSimilarity = UsePhenotypeSimilarityParameter.Value.Value;
    4577      int count = solutions.Length;
    4678      double[,] similarities = new double[count, count];
     
    4981        similarities[i, i] = 1;
    5082        for (int j = i + 1; j < count; j++) {
    51           similarities[i, j] = CalculateSimilarity(solutions[i], solutions[j]);
     83          if (phenotypeSimilarity)
     84            similarities[i, j] = QAPPermutationProximityCalculator.CalculatePhenotypeSimilarity(solutions[i], solutions[j], weights, distances);
     85          else similarities[i, j] = QAPPermutationProximityCalculator.CalculateGenotypeSimilarity(solutions[i], solutions[j]);
    5286          similarities[j, i] = similarities[i, j];
    5387        }
     
    5589      return similarities;
    5690    }
    57 
    58     private double CalculateSimilarity(Permutation assignment1, Permutation assignment2) {
    59       int identicalAssignments = 0;
    60       for (int i = 0; i < assignment1.Length; i++) {
    61         if (assignment1[i] == assignment2[i])
    62           identicalAssignments++;
    63       }
    64       return ((double)identicalAssignments) / assignment1.Length;
    65     }
    6691  }
    6792}
Note: See TracChangeset for help on using the changeset viewer.