#region License Information /* HeuristicLab * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Linq; using System.Text.RegularExpressions; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Problems.Instances; namespace WalkExporter { static class Util { public static double Evaluate(Permutation sol, QAPData qap) { var weights = qap.Weights; var distances = qap.Distances; var q = 0.0; for (var i = 0; i < sol.Length; i++) for (var j = 0; j < sol.Length; j++) q += weights[i, j] * distances[sol[i], sol[j]]; return q; } public static double EvaluateSwap2Diff(Permutation sol, int a, int b, QAPData qap) { if (a == b) return 0; double moveQuality = 0; int fac1 = a, fac2 = b; int loc1 = sol[fac1], loc2 = sol[fac2]; var weights = qap.Weights; var distances = qap.Distances; for (int j = 0; j < sol.Length; j++) { if (j == fac1) { moveQuality += weights[fac1, fac1] * (distances[loc2, loc2] - distances[loc1, loc1]); moveQuality += weights[fac1, fac2] * (distances[loc2, loc1] - distances[loc1, loc2]); } else if (j == fac2) { moveQuality += weights[fac2, fac2] * (distances[loc1, loc1] - distances[loc2, loc2]); moveQuality += weights[fac2, fac1] * (distances[loc1, loc2] - distances[loc2, loc1]); } else { int locJ = sol[j]; moveQuality += weights[fac1, j] * (distances[loc2, locJ] - distances[loc1, locJ]); moveQuality += weights[j, fac1] * (distances[locJ, loc2] - distances[locJ, loc1]); moveQuality += weights[fac2, j] * (distances[loc1, locJ] - distances[loc2, locJ]); moveQuality += weights[j, fac2] * (distances[locJ, loc1] - distances[locJ, loc2]); } } return moveQuality; } public static int Dist(Permutation a, Permutation b) { var dist = 0; for (var i = 0; i < a.Length; i++) if (a[i] != b[i]) dist++; return dist; } public static string GetGeneratorClass(string name) { var match = Regex.Match(name, @"(?.*)-\d+$"); if (match.Success) { name = match.Groups["n"].Value; } var subCls = name.Last(); var cls = name.Substring(0, 3); switch (cls) { case "lip": cls = name.Substring(0, 4) + "-" + subCls; break; case "RAN": cls = name.Substring(0, 4) + "-" + name[name.Length - 2] + name[name.Length - 1]; break; case "tai": if (Regex.IsMatch(name, "tai\\d+e\\d+")) cls += "-e"; else if (char.IsLetter(subCls)) cls += "-" + subCls; break; } return cls; } } }