Changeset 14691 for branches/PerformanceComparison/HeuristicLab.Analysis.FitnessLandscape/3.3/ProblemCharacteristicAnalysis/QAP
- Timestamp:
- 02/23/17 10:42:58 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.Analysis.FitnessLandscape/3.3/ProblemCharacteristicAnalysis/QAP/QAPDirectedWalk.cs
r14690 r14691 79 79 private QAPDirectedWalk(QAPDirectedWalk original, Cloner cloner) : base(original, cloner) { } 80 80 public QAPDirectedWalk() { 81 characteristics.AddRange(new[] { "Swap2.Sharpness", "Swap2.Bumpiness", "Swap2.Flatness", "Swap2.Steadiness"}81 characteristics.AddRange(new[] { "Swap2.Sharpness", "Swap2.Bumpiness", "Swap2.Flatness", /*"Swap2.Steadiness"*/ } 82 82 .Select(x => new StringValue(x)).ToList()); 83 83 Parameters.Add(new FixedValueParameter<IntValue>("Paths", "The number of paths to explore (a path is a set of solutions that connect two randomly chosen solutions).", new IntValue(50))); … … 118 118 119 119 var trajectories = Run(random, (QuadraticAssignmentProblem)Problem, permutations, BestImprovement).ToList(); 120 var firstDerivatives = trajectories.Select(path => ApproximateDerivative(path).ToList()).ToList(); 121 var secondDerivatives = firstDerivatives.Select(d1 => ApproximateDerivative(d1).ToList()).ToList(); 120 var result = PermutationPathAnalysis.GetCharacteristics(trajectories); 122 121 123 var props = GetCharacteristics(trajectories, firstDerivatives, secondDerivatives).ToDictionary(x => x.Item1, x => x.Item2);124 122 foreach (var chara in characteristics.CheckedItems.Select(x => x.Value.Value)) { 125 if (chara == "Swap2.Sharpness") yield return new Result("Swap2.Sharpness", new DoubleValue(props["Sharpness"])); 126 if (chara == "Swap2.Bumpiness") yield return new Result("Swap2.Bumpiness", new DoubleValue(props["Bumpiness"])); 127 if (chara == "Swap2.Flatness") yield return new Result("Swap2.Flatness", new DoubleValue(props["Flatness"])); 128 if (chara == "Swap2.Steadiness") yield return new Result("Swap2.Steadiness", new DoubleValue(props["Steadiness"])); 129 } 130 } 131 132 public static IEnumerable<IResult> Calculate(List<List<Tuple<Permutation, double>>> trajectories) { 133 var firstDerivatives = trajectories.Select(path => ApproximateDerivative(path).ToList()).ToList(); 134 var secondDerivatives = firstDerivatives.Select(d1 => ApproximateDerivative(d1).ToList()).ToList(); 135 136 var props = GetCharacteristics(trajectories, firstDerivatives, secondDerivatives).ToDictionary(x => x.Item1, x => x.Item2); 137 yield return new Result("Swap2.Sharpness", new DoubleValue(props["Sharpness"])); 138 yield return new Result("Swap2.Bumpiness", new DoubleValue(props["Bumpiness"])); 139 yield return new Result("Swap2.Flatness", new DoubleValue(props["Flatness"])); 140 yield return new Result("Swap2.Steadiness", new DoubleValue(props["Steadiness"])); 123 if (chara == "Swap2.Sharpness") yield return new Result("Swap2.Sharpness", new DoubleValue(result.Sharpness)); 124 if (chara == "Swap2.Bumpiness") yield return new Result("Swap2.Bumpiness", new DoubleValue(result.Bumpiness)); 125 if (chara == "Swap2.Flatness") yield return new Result("Swap2.Flatness", new DoubleValue(result.Flatness)); 126 } 141 127 } 142 128 … … 158 144 } 159 145 160 private static IEnumerable<Tuple<string, double>> GetCharacteristics(List<List<Tuple<Permutation, double>>> f, List<List<Tuple<Permutation, double>>> f1, List<List<Tuple<Permutation, double>>> f2) { 161 var sharpness = f2.Average(x => Area(x)); 162 var bumpiness = 0.0; 163 var flatness = 0.0; 164 var downPointing = f1.Where(x => x.Min(y => y.Item2) < 0).ToList(); 165 166 var steadiness = 0.0; 167 foreach (var path in downPointing) { 168 steadiness += ComBelowZero(path); 169 } 170 if (downPointing.Count > 0) steadiness /= downPointing.Count; 171 172 for (var p = 0; p < f2.Count; p++) { 173 if (f2[p].Count <= 2) continue; 174 var bump = 0; 175 var flat = 0; 176 for (var i = 0; i < f2[p].Count - 1; i++) { 177 if ((f2[p][i].Item2 > 0 && f2[p][i + 1].Item2 < 0) || (f2[p][i].Item2 < 0 && f2[p][i + 1].Item2 > 0)) { 178 bump++; 179 } else if (f2[p][i].Item2 == 0) { 180 flat++; 181 } 182 } 183 bumpiness += bump / (f2[p].Count - 1.0); 184 flatness += flat / (f2[p].Count - 1.0); 185 } 186 bumpiness /= f2.Count; 187 flatness /= f2.Count; 188 return new[] { 189 Tuple.Create("Sharpness", sharpness), 190 Tuple.Create("Bumpiness", bumpiness), 191 Tuple.Create("Flatness", flatness), 192 Tuple.Create("Steadiness", steadiness) 193 }; 194 } 195 196 public static IEnumerable<Tuple<Permutation, double>> BestDirectedWalk(QuadraticAssignmentProblem qap, Permutation start, Permutation end) { 146 private static IEnumerable<Tuple<Permutation, double>> BestDirectedWalk(QuadraticAssignmentProblem qap, Permutation start, Permutation end) { 197 147 var N = qap.Weights.Rows; 198 148 var sol = start; … … 225 175 } 226 176 227 p ublicstatic IEnumerable<Tuple<Permutation, double>> FirstDirectedWalk(IRandom random, QuadraticAssignmentProblem qap, Permutation start, Permutation end) {177 private static IEnumerable<Tuple<Permutation, double>> FirstDirectedWalk(IRandom random, QuadraticAssignmentProblem qap, Permutation start, Permutation end) { 228 178 var N = qap.Weights.Rows; 229 179 var sol = start; … … 260 210 } 261 211 262 private static double Area(IEnumerable<Tuple<Permutation, double>> path) {263 var iter = path.GetEnumerator();264 if (!iter.MoveNext()) return 0.0;265 var area = 0.0;266 var prev = iter.Current;267 while (iter.MoveNext()) {268 area += TrapezoidArea(prev, iter.Current);269 prev = iter.Current;270 }271 return area;272 }273 274 private static double TrapezoidArea(Tuple<Permutation, double> a, Tuple<Permutation, double> b) {275 var area = 0.0;276 var dist = Dist(a.Item1, b.Item1);277 if ((a.Item2 <= 0 && b.Item2 <= 0) || (a.Item2 >= 0 && b.Item2 >= 0))278 area += dist * (Math.Abs(a.Item2) + Math.Abs(b.Item2)) / 2.0;279 else {280 var k = (b.Item2 - a.Item2) / dist;281 var d = a.Item2;282 var x = -d / k;283 area += Math.Abs(x * a.Item2 / 2.0);284 area += Math.Abs((dist - x) * b.Item2 / 2.0);285 }286 return area;287 }288 289 // Center-of-Mass290 private static double ComBelowZero(IEnumerable<Tuple<Permutation, double>> path) {291 var area = 0.0;292 var com = 0.0;293 var nwalkDist = 0.0;294 Tuple<Permutation, double> prev = null;295 var iter = path.GetEnumerator();296 while (iter.MoveNext()) {297 var c = iter.Current;298 if (prev != null) {299 var ndist = Dist(prev.Item1, c.Item1) / (double)c.Item1.Length;300 nwalkDist += ndist;301 if (prev.Item2 < 0 || c.Item2 < 0) {302 var a = TrapezoidArea(prev, c) / (double)c.Item1.Length;303 area += a;304 com += (nwalkDist - (ndist / 2.0)) * a;305 }306 }307 prev = c;308 }309 return com / area;310 }311 312 private static IEnumerable<Tuple<Permutation, double>> ApproximateDerivative(IEnumerable<Tuple<Permutation, double>> data) {313 Tuple<Permutation, double> prev = null, prev2 = null;314 foreach (var d in data) {315 if (prev == null) {316 prev = d;317 continue;318 }319 if (prev2 == null) {320 prev2 = prev;321 prev = d;322 continue;323 }324 var dist = Dist(prev2.Item1, d.Item1);325 yield return Tuple.Create(prev.Item1, (d.Item2 - prev2.Item2) / (double)dist);326 prev2 = prev;327 prev = d;328 }329 }330 331 private static double Dist(Permutation a, Permutation b) {332 var dist = 0;333 for (var i = 0; i < a.Length; i++)334 if (a[i] != b[i]) dist++;335 return dist;336 }337 338 212 private static int[] GetInverse(Permutation p) { 339 213 var inv = new int[p.Length];
Note: See TracChangeset
for help on using the changeset viewer.