Exemple #1
0
        async Task TestDirection(Logger logger, double[] vectorFromThisToDestination, int iteration = 0)
        {
            // are all vectors along directionVector?
            bool neighbor_along_destinationVector_exists = false;

            foreach (var neighbor in ConnectedNeighborsCanBeUsedForNewRequests)
            {
                var thisRegIdVector               = RegistrationIdDistance.GetVectorValues(CryptoLibrary, this.Configuration.LocalPeerRegistrationId, vectorFromThisToDestination.Length);
                var neighborRegIdVector           = RegistrationIdDistance.GetVectorValues(CryptoLibrary, neighbor.RemoteRegistrationId, vectorFromThisToDestination.Length);
                var vectorFromLocalPeerToNeighbor = new double[thisRegIdVector.Length];
                for (int i = 0; i < vectorFromLocalPeerToNeighbor.Length; i++)
                {
                    vectorFromLocalPeerToNeighbor[i] = RegistrationIdDistance.GetDifferenceInLoopedRegistrationIdSpace(thisRegIdVector[i], neighborRegIdVector[i]);
                }

                double multProduct = 0;
                for (int dimensionI = 0; dimensionI < vectorFromThisToDestination.Length; dimensionI++)
                {
                    multProduct += vectorFromLocalPeerToNeighbor[dimensionI] * vectorFromThisToDestination[dimensionI];
                }
                if (multProduct > 0)
                {
                    neighbor_along_destinationVector_exists = true;
                    break;
                }
            }
            if (neighbor_along_destinationVector_exists == false)
            {
                if (iteration < 8)
                {
                    if (ConnectedNeighbors.Count < _configuration.AbsoluteMaxNumberOfNeighbors)
                    {
                        logger.WriteToLog_higherLevelDetail_EmitListOfPeers($"no neighbors to destination {MiscProcedures.VectorToString(vectorFromThisToDestination)}, sending REGISTER request... iteration={iteration}", this);

                        // try to fix the pain: connect to neighbors at empty direction
                        await ConnectToNewNeighborAsync(Engine.DateTimeNowUtc, true, vectorFromThisToDestination);

                        if (iteration >= 2)
                        {
                            await Engine.EngineThreadQueue.WaitAsync(TimeSpan.FromSeconds(10), "fixing empty direction 1237");
                        }

                        await TestDirection(logger, vectorFromThisToDestination, iteration + 1);
                    }
                    else
                    {
                        logger.WriteToLog_lightPain_EmitListOfPeers($"no neighbors to destination {MiscProcedures.VectorToString(vectorFromThisToDestination)} after {iteration} iterations. {ConnectedNeighbors.Count} connected neighbors already", this);
                    }
                }
                else
                { // pain is not fixed
                    logger.WriteToLog_lightPain_EmitListOfPeers($"no neighbors to destination {MiscProcedures.VectorToString(vectorFromThisToDestination)} after {iteration} iterations. {ConnectedNeighbors.Count} connected neighbors", this);
                }
            }
        }
Exemple #2
0
        public static double[] GetDifferenceVector(RegistrationId from, RegistrationId to, ICryptoLibrary cryptoLibrary, int numberOfDimensions)
        {
            var fromRegIdVector        = RegistrationIdDistance.GetVectorValues(cryptoLibrary, from, numberOfDimensions);
            var destinationRegIdVector = RegistrationIdDistance.GetVectorValues(cryptoLibrary, to, numberOfDimensions);
            var diff = new double[fromRegIdVector.Length];

            for (int i = 0; i < diff.Length; i++)
            {
                diff[i] = RegistrationIdDistance.GetDifferenceInLoopedRegistrationIdSpace(fromRegIdVector[i], destinationRegIdVector[i]);
            }
            return(diff);
        }
Exemple #3
0
        ///<param name="considerValueOfUniqueSectors">false when registering via EP</param>
        /// <returns>component of mutual value</returns>
        public float GetValue(float[] neighborVector, bool neighborIsAlreadyConnected, bool considerValueOfUniqueSectors)
        {
            float distanceFromLocalPeerToNeighbor = 0;
            var   vectorFromLocalPeerToNeighbor   = new float[NumberOfDimensions];

            for (int i = 0; i < neighborVector.Length; i++)
            {
                var vectorFromLocalPeerToNeighbor_i = RegistrationIdDistance.GetDifferenceInLoopedRegistrationIdSpace(_localPeerVector[i], neighborVector[i]);
                vectorFromLocalPeerToNeighbor[i] = vectorFromLocalPeerToNeighbor_i;
                distanceFromLocalPeerToNeighbor += vectorFromLocalPeerToNeighbor_i * vectorFromLocalPeerToNeighbor_i;
            }
            distanceFromLocalPeerToNeighbor = (float)Math.Sqrt(distanceFromLocalPeerToNeighbor);
            float r = -distanceFromLocalPeerToNeighbor;

            if (considerValueOfUniqueSectors)
            {
                var sectorIndex            = _vsic.GetSectorIndex(vectorFromLocalPeerToNeighbor);
                var neighborsCountInSector = _currentNeighborsCountPerSectors[sectorIndex];
                if (neighborIsAlreadyConnected)
                {
                    if (neighborsCountInSector == 1)
                    {
                        r += EmptySectorOccupationValue;                              // this neighbor is the only one in the sector
                    }
                    //   else if (neighborsCountInSector == 2) r += 1.0f; // TODO questionable heuristics really 1 or another value?
                }
                else
                {
                    if (neighborsCountInSector == 0)
                    {
                        r += EmptySectorOccupationValue;
                    }
                    //  else if (neighborsCountInSector == 1) r += 1.0f; // TODO questionable heuristics really 1 or another value?
                }
            }
            return(r);
        }
Exemple #4
0
        public P2pConnectionValueCalculator(float[] localPeerVector, IEnumerable <float[]> currentNeighborsVectors, Action <string> wtl)
        {
            _localPeerVector = localPeerVector;
            if (!_vsics.TryGetValue(NumberOfDimensions, out _vsic))
            {
                _vsic = new VectorSectorIndexCalculator(NumberOfDimensions);
                _vsics.Add(NumberOfDimensions, _vsic);
            }
            _currentNeighborsCountPerSectors = new int[_vsic.IndexesCount];

            int neighborIndex = 0;
            var vectorsFromLocalPeerToNeighbors = new List <float[]>();

            foreach (var neighborVector in currentNeighborsVectors)
            {
                var   vectorFromLocalPeerToNeighbor        = new float[NumberOfDimensions];
                float vectorFromLocalPeerToNeighbor_length = 0;
                for (int i = 0; i < NumberOfDimensions; i++)
                {
                    var vectorFromLocalPeerToNeighbor_i = RegistrationIdDistance.GetDifferenceInLoopedRegistrationIdSpace(_localPeerVector[i], neighborVector[i]);
                    vectorFromLocalPeerToNeighbor[i]      = vectorFromLocalPeerToNeighbor_i;
                    vectorFromLocalPeerToNeighbor_length += vectorFromLocalPeerToNeighbor_i * vectorFromLocalPeerToNeighbor_i;
                }
                vectorFromLocalPeerToNeighbor_length = (float)Math.Sqrt(vectorFromLocalPeerToNeighbor_length);
                var sectorIndex = _vsic.GetSectorIndex(vectorFromLocalPeerToNeighbor);
                _currentNeighborsCountPerSectors[sectorIndex]++;


                for (int i = 0; i < NumberOfDimensions; i++)
                {
                    vectorFromLocalPeerToNeighbor[i] /= vectorFromLocalPeerToNeighbor_length;
                }
                wtl?.Invoke($"neighbor#{neighborIndex} localToNeighbor=[{String.Join(";", vectorFromLocalPeerToNeighbor.Select(x => x.ToString()))}] sectorIndex={sectorIndex}");

                vectorsFromLocalPeerToNeighbors.Add(vectorFromLocalPeerToNeighbor);

                neighborIndex++;
            }

            _emptyDirectionVector = null;

            //_emptyDirectionVector = FindEmptyDirection(NumberOfDimensions, _vsic, vectorsFromLocalPeerToNeighbors);
            //if (_emptyDirectionVector != null)
            //{
            //    _thereIsNeighborAlongEmptyDirectionVector = false;
            //    neighborIndex = 0;
            //    foreach (var vectorFromLocalPeerToNeighbor in vectorsFromLocalPeerToNeighbors)
            //    {
            //        float mulResult = 0;
            //        for (int i = 0; i < NumberOfDimensions; i++)
            //            mulResult += vectorFromLocalPeerToNeighbor[i] * _emptyDirectionVector[i];
            //        wtl?.Invoke($"neighbor#{neighborIndex}=[{String.Join(";", vectorFromLocalPeerToNeighbor.Select(x => x.ToString()))}]:mulResult={mulResult};");
            //        if (mulResult > 0)
            //        {
            //            _thereIsNeighborAlongEmptyDirectionVector = true;
            //            break;
            //        }
            //        neighborIndex++;
            //    }
            //}

            if (wtl != null)
            {
                for (int sectorIndex = 0; sectorIndex < _vsic.IndexesCount; sectorIndex++)
                {
                    wtl.Invoke($"sector{sectorIndex} simplexVector=[{String.Join(";", _vsic.GetSimplexVector(sectorIndex).Select(x => x.ToString()))}]");
                }
                if (_emptyDirectionVector != null)
                {
                    wtl.Invoke($"emptyDirectionVector=[{String.Join(";", _emptyDirectionVector.Select(x => x.ToString()))}]");
                }
                else
                {
                    wtl.Invoke($"emptyDirectionVector=null");
                }
            }
        }