- Timestamp:
- 06/15/11 23:11:41 (14 years ago)
- Location:
- branches/QAPAlgorithms/HeuristicLab.Problems.QuadraticAssignment/3.3
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/QAPAlgorithms/HeuristicLab.Problems.QuadraticAssignment/3.3/Analyzers/QAPAlleleFrequencyAnalyzer.cs
r6342 r6416 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using HeuristicLab.Analysis; 23 24 using HeuristicLab.Common; … … 56 57 57 58 protected override Allele[] CalculateAlleles(Permutation solution) { 58 Allele[] alleles = new Allele[solution.Length]; 59 Allele[] alleles = new Allele[solution.Length * solution.Length]; 60 Dictionary<string, int> allelesDict = new Dictionary<string, int>(); 59 61 DoubleMatrix weights = WeightsParameter.ActualValue; 60 62 DoubleMatrix distances = DistancesParameter.ActualValue; 61 int source, target;62 63 double impact; 63 64 64 for (int i = 0; i < solution.Length; i++) { 65 source = i; 66 target = solution[i]; 67 impact = 0; 68 for (int j = 0; j < solution.Length; j++) 69 impact += weights[source, j] * distances[target, solution[j]]; 70 alleles[i] = new Allele(source.ToString() + "->" + target.ToString(), impact); 65 for (int x = 0; x < solution.Length; x++) { 66 for (int y = 0; y < solution.Length; y++) { 67 string allele = weights[x, y].ToString() + ">" + distances[solution[x], solution[y]].ToString(); 68 int repetition = 1; 69 if (allelesDict.ContainsKey(allele)) repetition += allelesDict[allele]; 70 allelesDict[allele] = repetition; 71 impact = weights[x, y] * distances[solution[x], solution[y]]; 72 alleles[x * solution.Length + y] = new Allele(allele + "/" + repetition, impact); 73 } 71 74 } 72 75 -
branches/QAPAlgorithms/HeuristicLab.Problems.QuadraticAssignment/3.3/Analyzers/QAPPopulationDiversityAnalyzer.cs
r6342 r6416 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 25 26 using HeuristicLab.Encodings.PermutationEncoding; 27 using HeuristicLab.Parameters; 26 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 29 … … 33 35 [StorableClass] 34 36 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 35 47 [StorableConstructor] 36 48 private QAPPopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { } 37 49 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 } 39 56 40 57 public override IDeepCloneable Clone(Cloner cloner) { … … 42 59 } 43 60 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 44 74 protected override double[,] CalculateSimilarities(Permutation[] solutions) { 75 DoubleMatrix weights = WeightsParameter.ActualValue, distances = DistancesParameter.ActualValue; 76 bool phenotypeSimilarity = UsePhenotypeSimilarityParameter.Value.Value; 45 77 int count = solutions.Length; 46 78 double[,] similarities = new double[count, count]; … … 49 81 similarities[i, i] = 1; 50 82 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]); 52 86 similarities[j, i] = similarities[i, j]; 53 87 } … … 55 89 return similarities; 56 90 } 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 }66 91 } 67 92 } -
branches/QAPAlgorithms/HeuristicLab.Problems.QuadraticAssignment/3.3/HeuristicLab.Problems.QuadraticAssignment-3.3.csproj
r6342 r6416 124 124 <Compile Include="Parsers\QAPLIBParser.cs" /> 125 125 <Compile Include="QAPAssignment.cs" /> 126 <Compile Include="QAPPermutationProximityCalculator.cs" /> 126 127 <Compile Include="QuadraticAssignmentProblem.cs" /> 127 128 <EmbeddedResource Include="Data\bur26a.dat" /> -
branches/QAPAlgorithms/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs
r6342 r6416 390 390 Distances = new DoubleMatrix(parser.Distances); 391 391 Weights = new DoubleMatrix(parser.Weights); 392 Name = "Quadratic Assignment Problem (loaded instance " + instance + ")";393 Description = "Loaded embedded problem data of instance " + instance + ".";392 Name = instance; 393 Description = "Loaded embedded QAPLIB problem data of instance " + instance + "."; 394 394 OnReset(); 395 395 }
Note: See TracChangeset
for help on using the changeset viewer.