| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using Itinero.Network.Attributes; |
| | 4 | |
|
| | 5 | | namespace Itinero.Profiles.EdgeTypesMap; |
| | 6 | |
|
| | 7 | | internal class ProfileEdgeTypeSetMinimizer |
| | 8 | | { |
| 0 | 9 | | private readonly LruCache<long, (IEnumerable<(string key, string value)> attributes, long edgeFactorHash)> |
| 0 | 10 | | _cache = new(100000); |
| 0 | 11 | | private readonly Dictionary<string, int> _probablyImportant = new(); |
| | 12 | | private readonly IReadOnlyCollection<Profile> _profiles; |
| | 13 | |
|
| 0 | 14 | | public ProfileEdgeTypeSetMinimizer(IReadOnlyCollection<Profile> profiles, |
| 0 | 15 | | params string[] defaultKeys) |
| 0 | 16 | | { |
| 0 | 17 | | _profiles = profiles; |
| 0 | 18 | | for (var i = 0; i < defaultKeys.Length; i++) |
| 0 | 19 | | { |
| 0 | 20 | | var key = defaultKeys[i]; |
| 0 | 21 | | _probablyImportant[key] = 1 + defaultKeys.Length - i; |
| 0 | 22 | | } |
| 0 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Calculates the smallest attribute set which gives equivalent routing properties for the given profile. |
| | 27 | | /// It'll try to remove as much attributes as possible, while keeping the same speeds and factors. |
| | 28 | | /// Thus: |
| | 29 | | /// <code> |
| | 30 | | /// var attributes = ... |
| | 31 | | /// var edgeFactors = profiles.Select(profile => profile.Factor(attributes)); |
| | 32 | | /// var pruned = new AttributeSetMinimizer(profiles).MinimizeAttributes(attributes); |
| | 33 | | /// var edgeFactorsPruned = profiles.Select(profile => profile.Factor(pruned)); |
| | 34 | | /// Assert.Equals(edgeFactors, edgeFactorsPruned); |
| | 35 | | /// </code> |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="attributes">The attributes to pruned</param> |
| | 38 | | /// <returns>A pruned set of attributes</returns> |
| | 39 | | public IEnumerable<(string key, string value)> MinimizeAttributes( |
| | 40 | | IEnumerable<(string key, string value)> attributes) |
| 0 | 41 | | { |
| 0 | 42 | | if (attributes.Count() <= 1) |
| 0 | 43 | | { |
| | 44 | | // Nothing to cull here |
| 0 | 45 | | return attributes; |
| | 46 | | } |
| | 47 | |
|
| 0 | 48 | | var attributesHash = attributes.GetHash(); |
| 0 | 49 | | if (_cache.TryGet(attributesHash, out var prunedSet)) |
| 0 | 50 | | { |
| | 51 | | // The current attributes are already known! |
| | 52 | | // We simply return the cached set |
| 0 | 53 | | return prunedSet.attributes; |
| | 54 | | } |
| | 55 | |
|
| | 56 | | // The hash of the factors for the profiles over the full attributes |
| | 57 | | // The hash of the factors for the _pruned_ attributes must be the same in the end! |
| 0 | 58 | | var targetHash = this.GetEdgeFactorHash(attributes); |
| | 59 | |
|
| | 60 | | // We make a ranking of what keys are probably important |
| 0 | 61 | | var importantKeys = |
| 0 | 62 | | attributes.Select(attr => (attr.key, this.ImportanceOf(attr.key))) |
| 0 | 63 | | .OrderByDescending(attr => attr.Item2); |
| | 64 | |
|
| | 65 | | // The pruned attributes |
| 0 | 66 | | HashSet<(string key, string value)> pruned = new(); |
| 0 | 67 | | long prunedAttributesHash = 0; |
| 0 | 68 | | long lastFactorHash = 0; |
| | 69 | |
|
| | 70 | | // We add the important keys one by one, until we have the same hash |
| 0 | 71 | | foreach (var (importantKey, _) in importantKeys) |
| 0 | 72 | | { |
| 0 | 73 | | attributes.TryGetValue(importantKey, out var value); |
| | 74 | |
|
| 0 | 75 | | pruned.Add((importantKey, value)); |
| 0 | 76 | | prunedAttributesHash = (importantKey, value).GetDiffHash(prunedAttributesHash); |
| | 77 | |
|
| 0 | 78 | | if (_cache.TryGet(prunedAttributesHash, out var fromCache)) |
| 0 | 79 | | { |
| 0 | 80 | | lastFactorHash = fromCache.edgeFactorHash; |
| | 81 | | // We've already seen this hash! Lets inspect the previously calculated values... |
| 0 | 82 | | if (fromCache.edgeFactorHash == targetHash) |
| 0 | 83 | | { |
| | 84 | | // Hooray! We have found the correct value without doing a thing |
| 0 | 85 | | this.AddImportance(fromCache.attributes); |
| 0 | 86 | | return fromCache.attributes; |
| | 87 | | } |
| 0 | 88 | | } |
| | 89 | | else |
| 0 | 90 | | { |
| 0 | 91 | | long currentFactorHash = this.GetEdgeFactorHash(pruned); |
| 0 | 92 | | _cache.Add(prunedAttributesHash, |
| 0 | 93 | | (new HashSet<(string key, string value)>(pruned), currentFactorHash)); |
| 0 | 94 | | if (currentFactorHash == targetHash) |
| 0 | 95 | | { |
| | 96 | | // We have found our smaller configuration! |
| 0 | 97 | | this.AddImportance(pruned); |
| 0 | 98 | | return pruned; |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | if (lastFactorHash == currentFactorHash) |
| 0 | 102 | | { |
| | 103 | | // Hmm, this last key didn't change anything... We negatively affect this value in order to prevent |
| 0 | 104 | | this.AddNotImportant(importantKey); |
| 0 | 105 | | } |
| | 106 | |
|
| 0 | 107 | | lastFactorHash = currentFactorHash; |
| 0 | 108 | | } |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | // Normally, we don't reach this point |
| 0 | 112 | | return pruned; |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | private void AddImportance(IEnumerable<(string key, string value)> attributes) |
| 0 | 116 | | { |
| 0 | 117 | | var usedKeys = new HashSet<string>(); |
| 0 | 118 | | foreach (var (key, _) in attributes) |
| 0 | 119 | | { |
| 0 | 120 | | usedKeys.Add(key); |
| 0 | 121 | | if (!_probablyImportant.ContainsKey(key)) |
| 0 | 122 | | { |
| 0 | 123 | | _probablyImportant[key] = 1; |
| 0 | 124 | | } |
| | 125 | | else |
| 0 | 126 | | { |
| 0 | 127 | | _probablyImportant[key]++; |
| 0 | 128 | | } |
| 0 | 129 | | } |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | private void AddNotImportant(string key) |
| 0 | 133 | | { |
| 0 | 134 | | if (!_probablyImportant.ContainsKey(key)) |
| 0 | 135 | | { |
| 0 | 136 | | _probablyImportant[key] = -1; |
| 0 | 137 | | } |
| | 138 | | else |
| 0 | 139 | | { |
| 0 | 140 | | _probablyImportant[key]--; |
| 0 | 141 | | } |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | private int ImportanceOf(string key) |
| 0 | 145 | | { |
| 0 | 146 | | if (_probablyImportant.TryGetValue(key, out var v)) |
| 0 | 147 | | { |
| 0 | 148 | | return v; |
| | 149 | | } |
| | 150 | |
|
| 0 | 151 | | return 0; |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | /// <summary> |
| | 155 | | /// Calculates, for every profile, the respective factor and hashes them together |
| | 156 | | /// </summary> |
| | 157 | | private int GetEdgeFactorHash(IEnumerable<(string key, string value)> attributes) |
| 0 | 158 | | { |
| 0 | 159 | | var hash = 13.GetHashCode(); |
| | 160 | |
|
| 0 | 161 | | foreach (var profile in _profiles) |
| 0 | 162 | | { |
| 0 | 163 | | var factor = profile.Factor(attributes); |
| 0 | 164 | | hash ^= factor.GetHashCode(); |
| 0 | 165 | | } |
| | 166 | |
|
| 0 | 167 | | return hash; |
| 0 | 168 | | } |
| | 169 | | } |