Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/WalkExporter/Util.cs @ 16955

Last change on this file since 16955 was 16955, checked in by abeham, 5 years ago

#2457: worked on thesis

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using System.Text.RegularExpressions;
24using HeuristicLab.Encodings.PermutationEncoding;
25using HeuristicLab.Problems.Instances;
26
27namespace WalkExporter {
28  static class Util {
29
30    public static double Evaluate(Permutation sol, QAPData qap) {
31      var weights = qap.Weights;
32      var distances = qap.Distances;
33      var q = 0.0;
34      for (var i = 0; i < sol.Length; i++)
35        for (var j = 0; j < sol.Length; j++)
36          q += weights[i, j] * distances[sol[i], sol[j]];
37      return q;
38    }
39
40    public static double EvaluateSwap2Diff(Permutation sol, int a, int b, QAPData qap) {
41      if (a == b) return 0;
42      double moveQuality = 0;
43      int fac1 = a, fac2 = b;
44      int loc1 = sol[fac1], loc2 = sol[fac2];
45      var weights = qap.Weights;
46      var distances = qap.Distances;
47
48      for (int j = 0; j < sol.Length; j++) {
49        if (j == fac1) {
50          moveQuality += weights[fac1, fac1] * (distances[loc2, loc2] - distances[loc1, loc1]);
51          moveQuality += weights[fac1, fac2] * (distances[loc2, loc1] - distances[loc1, loc2]);
52        } else if (j == fac2) {
53          moveQuality += weights[fac2, fac2] * (distances[loc1, loc1] - distances[loc2, loc2]);
54          moveQuality += weights[fac2, fac1] * (distances[loc1, loc2] - distances[loc2, loc1]);
55        } else {
56          int locJ = sol[j];
57          moveQuality += weights[fac1, j] * (distances[loc2, locJ] - distances[loc1, locJ]);
58          moveQuality += weights[j, fac1] * (distances[locJ, loc2] - distances[locJ, loc1]);
59          moveQuality += weights[fac2, j] * (distances[loc1, locJ] - distances[loc2, locJ]);
60          moveQuality += weights[j, fac2] * (distances[locJ, loc1] - distances[locJ, loc2]);
61        }
62      }
63      return moveQuality;
64    }
65
66    public static int Dist(Permutation a, Permutation b) {
67      var dist = 0;
68      for (var i = 0; i < a.Length; i++)
69        if (a[i] != b[i]) dist++;
70      return dist;
71    }
72
73    public static string GetGeneratorClass(string name) {
74      var match = Regex.Match(name, @"(?<n>.*)-\d+$");
75      if (match.Success) {
76        name = match.Groups["n"].Value;
77      }
78      var subCls = name.Last();
79      var cls = name.Substring(0, 3);
80      switch (cls) {
81        case "lip":
82          cls = name.Substring(0, 4) + "-" + subCls;
83          break;
84        case "RAN":
85          cls = name.Substring(0, 4) + "-" + name[name.Length - 2] + name[name.Length - 1];
86          break;
87        case "tai":
88          if (Regex.IsMatch(name, "tai\\d+e\\d+")) cls += "-e";
89          else if (char.IsLetter(subCls)) cls += "-" + subCls;
90          break;
91      }
92      return cls;
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.