Changeset 13005 for trunk/sources/HeuristicLab.Optimization.Operators
- Timestamp:
- 10/15/15 12:18:42 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization.Operators/3.3/SolutionSimilarityCalculator.cs
r12129 r13005 22 22 using System; 23 23 using System.Linq; 24 using System.Threading.Tasks; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 41 42 [Storable] 42 43 public string QualityVariableName { get; set; } 44 [Storable] 45 public bool ExecuteInParallel { get; set; } 46 [Storable] 47 public int MaxDegreeOfParallelism { get; set; } 43 48 #endregion 44 49 … … 48 53 protected SolutionSimilarityCalculator(SolutionSimilarityCalculator original, Cloner cloner) 49 54 : base(original, cloner) { 50 this.SolutionVariableName = original.SolutionVariableName; 51 this.QualityVariableName = original.QualityVariableName; 52 } 53 protected SolutionSimilarityCalculator() : base() { } 55 SolutionVariableName = original.SolutionVariableName; 56 QualityVariableName = original.QualityVariableName; 57 ExecuteInParallel = original.ExecuteInParallel; 58 MaxDegreeOfParallelism = original.MaxDegreeOfParallelism; 59 } 60 61 protected SolutionSimilarityCalculator() : base() { 62 ExecuteInParallel = false; 63 MaxDegreeOfParallelism = -1; 64 } 65 66 [StorableHook(HookType.AfterDeserialization)] 67 private void AfterDeserialization() { 68 if (MaxDegreeOfParallelism == 0) { 69 ExecuteInParallel = false; 70 MaxDegreeOfParallelism = -1; 71 } 72 } 54 73 55 74 public double[][] CalculateSolutionCrowdSimilarity(IScope leftSolutionCrowd, IScope rightSolutionCrowd) { … … 75 94 76 95 public double[][] CalculateSolutionCrowdSimilarity(IScope solutionCrowd) { 77 if (solutionCrowd == null) 96 if (solutionCrowd == null) { 78 97 throw new ArgumentException("Cannot calculate similarity because the provided crowd is null."); 79 98 } 80 99 var individuals = solutionCrowd.SubScopes; 81 100 82 if (!individuals.Any()) 101 if (!individuals.Any()) { 83 102 throw new ArgumentException("Cannot calculate similarity because the provided crowd is empty."); 103 } 84 104 85 105 var similarityMatrix = new double[individuals.Count][]; 86 for (int i = 0; i < individuals.Count; i++) similarityMatrix[i] = new double[individuals.Count]; 87 88 if (IsCommutative) { 89 for (int i = 0; i < individuals.Count; i++) { 90 for (int j = i; j < individuals.Count; j++) { 91 similarityMatrix[i][j] = similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]); 106 for (int i = 0; i < individuals.Count; i++) { 107 similarityMatrix[i] = new double[individuals.Count]; 108 } 109 110 if (ExecuteInParallel) { 111 var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = MaxDegreeOfParallelism }; 112 if (IsCommutative) { 113 Parallel.For(0, individuals.Count, parallelOptions, i => { 114 for (int j = i; j < individuals.Count; j++) { 115 similarityMatrix[i][j] = 116 similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]); 117 } 118 }); 119 } else { 120 Parallel.For(0, individuals.Count, parallelOptions, i => { 121 for (int j = i; j < individuals.Count; j++) { 122 similarityMatrix[i][j] = CalculateSolutionSimilarity(individuals[i], individuals[j]); 123 if (i == j) continue; 124 similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[j], individuals[i]); 125 } 126 }); 127 } 128 } else { 129 if (IsCommutative) { 130 for (int i = 0; i < individuals.Count; i++) { 131 for (int j = i; j < individuals.Count; j++) { 132 similarityMatrix[i][j] = 133 similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]); 134 } 92 135 } 93 } 94 } else{95 for (int i = 0; i < individuals.Count; i++) {96 for (int j = i; j < individuals.Count; j++) {97 similarityMatrix[i][j] = CalculateSolutionSimilarity(individuals[i], individuals[j]);98 if (i == j) continue;99 similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[j], individuals[i]);136 } else { 137 for (int i = 0; i < individuals.Count; i++) { 138 for (int j = i; j < individuals.Count; j++) { 139 similarityMatrix[i][j] = CalculateSolutionSimilarity(individuals[i], individuals[j]); 140 if (i == j) continue; 141 similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[j], individuals[i]); 142 } 100 143 } 101 144 }
Note: See TracChangeset
for help on using the changeset viewer.