[8496] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Analysis.AlgorithmBehavior {
|
---|
| 29 | public static class AlgorithmBehaviorHelpers {
|
---|
| 30 | public static bool IsSubtour(IntArray tour, Permutation permutation) {
|
---|
| 31 | // determine starting position for subtour match
|
---|
| 32 | int idx = permutation.Select((x, index) => new { Value = x, Index = index }).Single(x => x.Value == tour[0]).Index;
|
---|
| 33 | bool isSubtour = true;
|
---|
| 34 | for (int i = 1; i < tour.Length; i++) {
|
---|
| 35 | if (tour[i] == permutation[(idx + 1) % permutation.Length]) { // check right side
|
---|
| 36 | idx = (idx + 1) % permutation.Length;
|
---|
| 37 | } else if (tour[i] == permutation[(idx - 1 + permutation.Length) % permutation.Length]) { // check left side
|
---|
| 38 | idx = (idx - 1 + permutation.Length) % permutation.Length;
|
---|
| 39 | } else {
|
---|
| 40 | isSubtour = false;
|
---|
| 41 | break;
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | return isSubtour;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public static List<IntArray> ExtractSubtours(Permutation permutation) {
|
---|
| 48 | var subtours = new List<IntArray>();
|
---|
| 49 | for (int i = 2; i <= permutation.Count() / 2; i++) { // increase schema length from 2 to n/2
|
---|
| 50 | for (int j = 0; j < permutation.Count(); j++) { // visit all positions in the permutation
|
---|
| 51 | var schema = new IntArray(i);
|
---|
| 52 | for (int k = 0; k < i; k++) schema[k] = permutation[(j + k) % permutation.Length]; // copy edge to schema
|
---|
| 53 | subtours.Add(schema);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | return subtours;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public static List<IntArray> ExtractSubtours(Permutation permutation, int length) {
|
---|
| 60 | var subtours = new List<IntArray>();
|
---|
| 61 | int i = 2;
|
---|
| 62 | for (int j = 0; j < permutation.Count(); j++) { // visit all positions in the permutation
|
---|
| 63 | var schema = new IntArray(i);
|
---|
| 64 | for (int k = 0; k < i; k++) schema[k] = permutation[(j + k) % permutation.Length]; // copy edge to schema
|
---|
| 65 | subtours.Add(schema);
|
---|
| 66 | }
|
---|
| 67 | return subtours;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public static IList<IntArray> GenerateWildcardSchemata(IntArray schema) {
|
---|
| 71 | string formatString = "{0:d" + schema.Length + "}";
|
---|
| 72 | var combs = new List<bool[]>();
|
---|
| 73 | string comb = string.Empty;
|
---|
| 74 |
|
---|
| 75 | // create all desired wildcard combinations
|
---|
| 76 | long i = 0;
|
---|
| 77 | int wildcardCount = 0; // first wildcard combination contains no wildcards
|
---|
| 78 | while (wildcardCount < schema.Length) {
|
---|
| 79 | wildcardCount = (comb = Convert.ToString(i++, 2)).PadLeft(schema.Length, '0').Count(x => x == '1');
|
---|
| 80 | if (wildcardCount > (schema.Length / 8) && wildcardCount < (schema.Length / 2)) // only include wildcards within certain limits
|
---|
| 81 | combs.Add(comb.Select(x => x == '1').ToArray()); // '1' becomes true which indicates a wildcard
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | // create all desired wildcard instances
|
---|
| 85 | var wildcardSchemata = new List<IntArray>();
|
---|
| 86 | foreach (var c in combs) {
|
---|
| 87 | wildcardSchemata.Add(new IntArray(schema.Select((x, index) => c[index] ? -1 : x).ToArray())); // -1 is a wildcard in the schema
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | return wildcardSchemata;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | // attention: brute force matching
|
---|
| 94 | // should be replaced with KMP, BM or RK
|
---|
| 95 | public static bool IsMatch(IntArray pattern, int[] tour) {
|
---|
| 96 | bool match = false;
|
---|
| 97 | for (int i = 0; i <= tour.Length - pattern.Length && !match; i++) {
|
---|
| 98 | for (int j = 0; j < pattern.Length && (match = pattern[j] == tour[i + j] || pattern[j] == -1); j++) ;
|
---|
| 99 | }
|
---|
| 100 | return match;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|