private static ulong CalculateActiveBitConnectionsHalfSum(IAdjacencyMatrix adjacencyMatrix, IEnumerable <uint> activeBitIndicesOrdered) { ulong activeBitConnectionsHalfSum = 0; var usedIndices = new List <uint>(); foreach (var activeBitIndex in activeBitIndicesOrdered) { for (var i = 0U; i < activeBitIndex; i++) { activeBitConnectionsHalfSum += adjacencyMatrix[(int)i, (int)activeBitIndex]; } foreach (var i in usedIndices) { activeBitConnectionsHalfSum -= adjacencyMatrix[(int)i, (int)activeBitIndex]; } usedIndices.Add(activeBitIndex); for (var j = activeBitIndex; j < adjacencyMatrix.Size; j++) { activeBitConnectionsHalfSum += adjacencyMatrix[(int)activeBitIndex, (int)j]; } } return(activeBitConnectionsHalfSum); }
public static bool ContainsEdge <T>(this IAdjacencyMatrix <T> graph, T startValue, T endValue) where T : IEquatable <T> { if (graph is null) { throw new ArgumentNullException(nameof(graph)); } return(graph[startValue, endValue] != default); }
private static long CalculateAdjacencyMatrixScore(IAdjacencyMatrix adjacencyMatrix, IReadOnlyList <uint> activeBitIndices) { var activeBitConnectionsHalfSum = adjacencyMatrix.CalculateActiveBitConnectionsHalfSum(activeBitIndices); return // Active connections ((long)activeBitConnectionsHalfSum - // Inactive connections ((long)adjacencyMatrix.HalfSum - (long)activeBitConnectionsHalfSum)); }
/// <summary> /// Create a new vector bassed on the neighbors in the adjacencyMatrix. /// </summary> public static T[] Crossover <T>(this IAdjacencyMatrix <T> adjacencyMatrix, T firstElement, int childLength) { var childArray = new T[childLength]; childArray[0] = firstElement; for (int i = 1; i < childLength; i++) { childArray[i] = adjacencyMatrix.GetNeighbor(childArray[i - 1]); } return(childArray); }
/// <summary> /// A* algorithm that traverses the graph to find shortest path between set vertices /// </summary> /// <param name="graph"> <see cref="Graph"/> instance. </param> /// <param name="startVertex"> The start <see cref="Vertex"/> of path to calculate minimum distance for. </param> /// <param name="endVertex"> The end <see cref="Vertex"/> of path to calculate minimum distance for. </param> /// <returns> The shortest (minimum cost) path from starting point to ending point. </returns> public Pathway <T> FindPath(IAdjacencyMatrix <T> graph, Vertex <T> start, Vertex <T> target) { /// System.Collections.Generic.SortedList by default does not allow duplicate items. /// Since items are keyed by TotalCost there can be duplicate entries per key. var priorityComparer = Comparer <int> .Create((x, y) => (x <= y)? -1 : 1); var opened = new PriorityQueue <int, Vertex <T> >(priorityComparer); var visited = new PriorityQueue <int, Vertex <T> >(priorityComparer); var path = new Dictionary <Vertex <T>, PathStep <T> >(); // Resets the AStar algorithm with the newly specified start node and goal node. var current = start; int estimatedDistance = _distanceMesureable.MesureDistance(start, target); path[start] = new PathStep <T>(start, null, 0, estimatedDistance); opened.Enqueue(path[start].TotalCost, start); // Continue searching until either failure or the goal node has been found. AlgorithmState state = AlgorithmState.Searching; while (state == AlgorithmState.Searching) { current = GetNext(opened, visited); if (current == null) { state = AlgorithmState.PathDoesNotExist; break; } // Remove from the open list and place on the closed list // since this node is now being searched. visited.Enqueue(path[current].TotalCost, current); // Found the goal, stop searching. if (current.Equals(target)) { state = AlgorithmState.PathFound; break; } ExtendOpened(opened, visited, current, target, path); } ICollection <Vertex <T> > vertices = ReconstructPath(current, path); int totalDistance = current != null && path.ContainsKey(current) ? path[current].MovementCost : 0; return(new Pathway <T>(vertices, totalDistance, state)); }
private static void ProcessTheSpecificLabel( IDictionary <IBitVector, IDictionary <string, double> > matchingScoresPerLabelPerPunchedCard, string punchedCardKey, IBitVector key, int samplesCount, IAdjacencyMatrix adjacencyMatrix, IBitVector punchedInput) { var matchingScorePerLabel = CalculateMatchingScore(punchedInput, adjacencyMatrix, samplesCount); if (!matchingScoresPerLabelPerPunchedCard.TryGetValue(key, out var dictionary)) { dictionary = new Dictionary <string, double>(); matchingScoresPerLabelPerPunchedCard[key] = dictionary; } dictionary.Add(punchedCardKey, matchingScorePerLabel); }
private static long CalculateAdjacencyMatrixScore(IAdjacencyMatrix adjacencyMatrix) { return((long)adjacencyMatrix.HalfSum); }
internal static double CalculateMatchingScore(IBitVector punchedInput, IAdjacencyMatrix adjacencyMatrix, int samplesCount) { return((double)CalculateAdjacencyMatrixScore( adjacencyMatrix, punchedInput.ActiveBitIndices) / samplesCount); }
public Graph(IAdjacencyMatrix <T> adjacencyMatrix) { _adjacencyMatrix = adjacencyMatrix; }