Changeset 14471 for branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/LinearLinkageMemPR.cs
- Timestamp:
- 12/09/16 15:56:22 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/LinearLinkageMemPR.cs
r14466 r14471 58 58 59 59 protected override bool Eq(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b) { 60 return a.Solution.SequenceEqual(b.Solution); 60 var s1 = a.Solution; 61 var s2 = b.Solution; 62 if (s1.Length != s2.Length) return false; 63 for (var i = 0; i < s1.Length; i++) 64 if (s1[i] != s2[i]) return false; 65 return true; 61 66 } 62 67 63 68 protected override double Dist(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b) { 64 if (a.Solution.Length != b.Solution.Length) throw new ArgumentException("Comparing encodings of unequal length"); 65 var dist = 0; 66 for (var i = 0; i < a.Solution.Length; i++) { 67 if (a.Solution[i] != b.Solution[i]) dist++; 68 } 69 return dist / (double)a.Solution.Length; 69 return HammingSimilarityCalculator.CalculateSimilarity(a.Solution, b.Solution); 70 70 } 71 71 … … 94 94 protected override int TabuWalk(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> scope, int maxEvals, CancellationToken token, ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> subspace = null) { 95 95 return 0; 96 /*Func<Encodings.LinearLinkageEncoding.LinearLinkage, IRandom, double> eval = new EvaluationWrapper<Encodings.LinearLinkageEncoding.LinearLinkage>(Context.Problem, scope).Evaluate; 97 var quality = scope.Fitness; 98 var lle = scope.Solution; 99 var random = Context.Random; 100 var evaluations = 0; 101 var maximization = Context.Problem.Maximization; 102 if (double.IsNaN(quality)) { 103 quality = eval(lle, random); 104 evaluations++; 105 } 106 Encodings.LinearLinkageEncoding.LinearLinkage bestOfTheWalk = null; 107 double bestOfTheWalkF = double.NaN; 108 var current = (Encodings.LinearLinkageEncoding.LinearLinkage)lle.Clone(); 109 var currentF = quality; 110 var overallImprovement = false; 111 var tabu = new double[current.Length, current.Length]; 112 for (var i = 0; i < current.Length; i++) { 113 for (var j = i; j < current.Length; j++) { 114 tabu[i, j] = tabu[j, i] = double.MaxValue; 115 } 116 tabu[i, current[i]] = currentF; 117 } 118 119 var steps = 0; 120 var stepsUntilBestOfWalk = 0; 121 for (var iter = 0; iter < int.MaxValue; iter++) { 122 var allTabu = true; 123 var bestOfTheRestF = double.NaN; 124 object bestOfTheRest = null; 125 var improved = false; 126 foreach () { 127 if (subspace != null && !(subspace[swap.Index1, 0] && subspace[swap.Index2, 0])) 128 continue; 129 130 131 var q = eval(current, random); 132 evaluations++; 133 if (FitnessComparer.IsBetter(maximization, q, quality)) { 134 overallImprovement = true; 135 quality = q; 136 for (var i = 0; i < current.Length; i++) lle[i] = current[i]; 137 } 138 // check if it would not be an improvement to swap these into their positions 139 var isTabu = !FitnessComparer.IsBetter(maximization, q, tabu[swap.Index1, current[swap.Index1]]) 140 && !FitnessComparer.IsBetter(maximization, q, tabu[swap.Index2, current[swap.Index2]]); 141 if (!isTabu) allTabu = false; 142 if (FitnessComparer.IsBetter(maximization, q, currentF) && !isTabu) { 143 if (FitnessComparer.IsBetter(maximization, q, bestOfTheWalkF)) { 144 bestOfTheWalk = (Encodings.LinearLinkageEncoding.LinearLinkage)current.Clone(); 145 bestOfTheWalkF = q; 146 stepsUntilBestOfWalk = steps; 147 } 148 steps++; 149 improved = true; 150 // perform the move 151 currentF = q; 152 // mark that to move them to their previous position requires to make an improvement 153 tabu[swap.Index1, current[swap.Index2]] = maximization ? Math.Max(q, tabu[swap.Index1, current[swap.Index2]]) 154 : Math.Min(q, tabu[swap.Index1, current[swap.Index2]]); 155 tabu[swap.Index2, current[swap.Index1]] = maximization ? Math.Max(q, tabu[swap.Index2, current[swap.Index1]]) 156 : Math.Min(q, tabu[swap.Index2, current[swap.Index1]]); 157 } else { // undo the move 158 if (!isTabu && FitnessComparer.IsBetter(maximization, q, bestOfTheRestF)) { 159 bestOfTheRest = swap; 160 bestOfTheRestF = q; 161 } 162 current[swap.Index2] = current[swap.Index1]; 163 current[swap.Index1] = h; 164 } 165 if (evaluations >= maxEvals) break; 166 } 167 if (!allTabu && !improved && bestOfTheRest != null) { 168 tabu[bestOfTheRest.Index1, current[bestOfTheRest.Index1]] = maximization ? Math.Max(currentF, tabu[bestOfTheRest.Index1, current[bestOfTheRest.Index1]]) 169 : Math.Min(currentF, tabu[bestOfTheRest.Index1, current[bestOfTheRest.Index1]]); 170 tabu[bestOfTheRest.Index2, current[bestOfTheRest.Index2]] = maximization ? Math.Max(currentF, tabu[bestOfTheRest.Index2, current[bestOfTheRest.Index2]]) 171 : Math.Min(currentF, tabu[bestOfTheRest.Index2, current[bestOfTheRest.Index2]]); 172 173 var h = current[bestOfTheRest.Index1]; 174 current[bestOfTheRest.Index1] = current[bestOfTheRest.Index2]; 175 current[bestOfTheRest.Index2] = h; 176 177 currentF = bestOfTheRestF; 178 steps++; 179 } else if (allTabu) break; 180 if (evaluations >= maxEvals) break; 181 } 182 Context.IncrementEvaluatedSolutions(evaluations); 183 if (!overallImprovement && bestOfTheWalk != null) { 184 quality = bestOfTheWalkF; 185 for (var i = 0; i < current.Length; i++) lle[i] = bestOfTheWalk[i]; 186 } 187 return stepsUntilBestOfWalk;*/ 96 188 } 97 189
Note: See TracChangeset
for help on using the changeset viewer.