Exemple #1
0
        private static void AssertConnections(
            ConnectionGenes <T> connGenes,
            DirectedGraph digraph,
            INodeIdMap nodeIndexByIdMap,
            int[]?connectionIndexMap)
        {
            // Connection counts.
            Debug.Assert(connGenes._connArr.Length == digraph.ConnectionIdArrays.Length);

            // Connection order.
            Debug.Assert(SortUtils.IsSortedAscending(connGenes._connArr));
            Debug.Assert(IsSortedAscending(digraph.ConnectionIdArrays));

            // Connection node ID mappings.
            DirectedConnection[] connArr = connGenes._connArr;
            int[] srcIdArr = digraph.ConnectionIdArrays._sourceIdArr;
            int[] tgtIdArr = digraph.ConnectionIdArrays._targetIdArr;

            for (int i = 0; i < connGenes._connArr.Length; i++)
            {
                // Determine the index of the equivalent connection in the digraph.
                int genomeConnIdx = (connectionIndexMap is null) ? i : connectionIndexMap[i];

                Debug.Assert(nodeIndexByIdMap.Map(connArr[genomeConnIdx].SourceId) == srcIdArr[i]);
                Debug.Assert(nodeIndexByIdMap.Map(connArr[genomeConnIdx].TargetId) == tgtIdArr[i]);
            }
        }
        private void InitGraphTraversal(int startNodeId, int connIdx)
        {
            // Push connIdx onto the stack.
            _traversalStack.Push(connIdx);

            // Add the current node to the set of visited nodes; this prevents the traversal algorithm from re-entering this node
            // (it's on the stack thus it is in the process of being traversed).
            _visitedNodes[_nodeIdxByIdMap.Map(startNodeId)] = true;
        }
Exemple #3
0
        public void CreateInverse(int fixedNodeCount)
        {
            var nodeIdxById = new Dictionary <int, int>
            {
                { 100, fixedNodeCount },
                { 50, fixedNodeCount + 1 },
                { 200, fixedNodeCount + 2 },
                { 70, fixedNodeCount + 3 },
                { 300, fixedNodeCount + 4 },
                { 30, fixedNodeCount + 5 }
            };

            var nodeIdMap = new DictionaryNodeIdMap(fixedNodeCount, nodeIdxById);

            INodeIdMap inverseMap = nodeIdMap.CreateInverseMap();

            Assert.Equal(nodeIdMap.Count, inverseMap.Count);

            Assert.Equal(0, inverseMap.Map(0));
            Assert.Equal(fixedNodeCount - 1, inverseMap.Map(fixedNodeCount - 1));

            Assert.Equal(100, inverseMap.Map(fixedNodeCount));
            Assert.Equal(50, inverseMap.Map(fixedNodeCount + 1));
            Assert.Equal(200, inverseMap.Map(fixedNodeCount + 2));
            Assert.Equal(70, inverseMap.Map(fixedNodeCount + 3));
            Assert.Equal(300, inverseMap.Map(fixedNodeCount + 4));
            Assert.Equal(30, inverseMap.Map(fixedNodeCount + 5));
        }
    private static ConnectionIds CopyAndMapIds(
        Span <DirectedConnection> connSpan,
        INodeIdMap nodeIdMap)
    {
        int count   = connSpan.Length;
        var connIds = new ConnectionIds(count);
        var srcIds  = connIds.GetSourceIdSpan();
        var tgtIds  = connIds.GetTargetIdSpan();

        for (int i = 0; i < count; i++)
        {
            srcIds[i] = nodeIdMap.Map(connSpan[i].SourceId);
            tgtIds[i] = nodeIdMap.Map(connSpan[i].TargetId);
        }

        return(connIds);
    }
Exemple #5
0
        private static ConnectionIdArrays CopyAndMapIds(
            IList <DirectedConnection> connectionList,
            INodeIdMap nodeIdMap)
        {
            int count = connectionList.Count;

            int [] srcIdArr = new int[count];
            int [] tgtIdArr = new int[count];

            for (int i = 0; i < connectionList.Count; i++)
            {
                srcIdArr[i] = nodeIdMap.Map(connectionList[i].SourceId);
                tgtIdArr[i] = nodeIdMap.Map(connectionList[i].TargetId);
            }

            return(new ConnectionIdArrays(srcIdArr, tgtIdArr));
        }
Exemple #6
0
    private static void CopyAndMapIds(
        DirectedConnection[] connArr,
        INodeIdMap nodeIdMap,
        out ConnectionIds connIds)
    {
        int count = connArr.Length;

        connIds = new ConnectionIds(count);
        var srcIds = connIds.GetSourceIdSpan();
        var tgtIds = connIds.GetTargetIdSpan();

        for (int i = 0; i < count; i++)
        {
            srcIds[i] = nodeIdMap.Map(connArr[i].SourceId);
            tgtIds[i] = nodeIdMap.Map(connArr[i].TargetId);
        }
    }
Exemple #7
0
        private static void CopyAndMapIds(
            DirectedConnection[] connArr,
            INodeIdMap nodeIdMap,
            out ConnectionIdArrays connIdArrays)
        {
            int count = connArr.Length;

            int[] srcIdArr = new int[count];
            int[] tgtIdArr = new int[count];

            for (int i = 0; i < count; i++)
            {
                srcIdArr[i] = nodeIdMap.Map(connArr[i].SourceId);
                tgtIdArr[i] = nodeIdMap.Map(connArr[i].TargetId);
            }

            connIdArrays = new ConnectionIdArrays(srcIdArr, tgtIdArr);
        }
Exemple #8
0
    /// <summary>
    /// Split each IWeightedDirectedConnection in a list into an array of DirectedConnections(s), and an array of weights.
    /// Map the node IDs to indexes as we go.
    /// </summary>
    private static void CopyAndMapIds(
        Span <WeightedDirectedConnection <T> > connSpan,
        INodeIdMap nodeIdMap,
        out ConnectionIds connIds,
        out T[] weightArr)
    {
        int count = connSpan.Length;

        connIds = new ConnectionIds(count);
        var srcIds = connIds.GetSourceIdSpan();
        var tgtIds = connIds.GetTargetIdSpan();

        weightArr = new T[count];

        for (int i = 0; i < count; i++)
        {
            srcIds[i]    = nodeIdMap.Map(connSpan[i].SourceId);
            tgtIds[i]    = nodeIdMap.Map(connSpan[i].TargetId);
            weightArr[i] = connSpan[i].Weight;
        }
    }
Exemple #9
0
        /// <summary>
        /// Split each IWeightedDirectedConnection in a list into an array of DirectedConnections(s), and an array of weights.
        /// Map the node IDs to indexes as we go.
        /// </summary>
        private static void CopyAndMapIds(
            IList <WeightedDirectedConnection <T> > connectionList,
            INodeIdMap nodeIdMap,
            out ConnectionIdArrays connIdArrays,
            out T[] weightArr)
        {
            int count = connectionList.Count;

            int[] srcIdArr = new int[count];
            int[] tgtIdArr = new int[count];
            weightArr = new T[count];

            for (int i = 0; i < count; i++)
            {
                srcIdArr[i]  = nodeIdMap.Map(connectionList[i].SourceId);
                tgtIdArr[i]  = nodeIdMap.Map(connectionList[i].TargetId);
                weightArr[i] = connectionList[i].Weight;
            }

            connIdArrays = new ConnectionIdArrays(srcIdArr, tgtIdArr);
        }
        private static bool ValidateConnections(
            ConnectionGenes <T> connGenes,
            DirectedGraph digraph,
            INodeIdMap nodeIndexByIdMap,
            int[] connectionIndexMap)
        {
            // Connection counts.
            if (connGenes._connArr.Length != digraph.ConnectionIdArrays.Length)
            {
                return(false);
            }

            // Connection order.
            if (!SortUtils.IsSortedAscending(connGenes._connArr) ||
                !IsSortedAscending(digraph.ConnectionIdArrays))
            {
                return(false);
            }

            // Connection node ID mappings.
            DirectedConnection[] connArr = connGenes._connArr;
            int[] srcIdArr = digraph.ConnectionIdArrays._sourceIdArr;
            int[] tgtIdArr = digraph.ConnectionIdArrays._targetIdArr;

            for (int i = 0; i < connGenes._connArr.Length; i++)
            {
                DirectedConnection connGene = connArr[i];

                // Determine the index of he equivalent connection in the digraph.
                int genomeConnIdx = (null == connectionIndexMap) ? i : connectionIndexMap[i];

                if (nodeIndexByIdMap.Map(connArr[genomeConnIdx].SourceId) != srcIdArr[i] ||
                    nodeIndexByIdMap.Map(connArr[genomeConnIdx].TargetId) != tgtIdArr[i])
                {
                    return(false);
                }
            }

            return(true);
        }
 /// <summary>
 /// Create a clone of the provided <see cref="DirectedConnection"/>, but with IDs mapped into a different ID space.
 /// </summary>
 /// <param name="conn">The connection to clone.</param>
 /// <param name="nodeIdxById">The node ID mapping.</param>
 /// <returns>A new <see cref="DirectedConnection"/>.</returns>
 public static DirectedConnection CloneAndMap(DirectedConnection conn, INodeIdMap nodeIdxById)
 {
     return(new DirectedConnection(nodeIdxById.Map(conn.SourceId), nodeIdxById.Map(conn.TargetId)));
 }