public int NonPermanentRandomization()
        {
            RNGCrypto rand = new RNGCrypto();

            int edgeToRemove = rand.Next(0, existingEdges.Count);

            Debug.Assert(edgeToRemove < existingEdges.Count);
            int rvertex1 = existingEdges[edgeToRemove].Key;
            int rvertex2 = existingEdges[edgeToRemove].Value;

            int edgeToAdd = rand.Next(0, nonExistingEdges.Count);

            Debug.Assert(edgeToAdd < nonExistingEdges.Count);
            int avertex1 = nonExistingEdges[edgeToAdd].Key;
            int avertex2 = nonExistingEdges[edgeToAdd].Value;

            RemoveConnection(rvertex1, rvertex2);
            // Calculate removed cycles count
            int removedCyclesCount = Cycles3ByVertices(rvertex1, rvertex2);

            AddConnection(avertex1, avertex2);
            // Calculate removed cycles count
            int addedCyclesCount = Cycles3ByVertices(avertex1, avertex2);

            return(addedCyclesCount - removedCyclesCount);
        }
        protected override SortedDictionary <Double, Double> CalculateActivePartModelB()
        {
            // Retrieving research parameters from network. Research MUST be Activation. //
            Debug.Assert(network.ResearchParameterValues != null);
            Debug.Assert(network.ResearchParameterValues.ContainsKey(ResearchParameter.ActivationStepCount));
            Debug.Assert(network.ResearchParameterValues.ContainsKey(ResearchParameter.ActiveMu));
            Debug.Assert(network.ResearchParameterValues.ContainsKey(ResearchParameter.Lambda));
            Debug.Assert(network.ResearchParameterValues.ContainsKey(ResearchParameter.TracingStepIncrement));

            SortedDictionary <Double, Double> ActiveParts = new SortedDictionary <Double, Double>();
            UInt32 Time   = Convert.ToUInt32(network.ResearchParameterValues[ResearchParameter.ActivationStepCount]);
            Double Mu     = Convert.ToDouble(network.ResearchParameterValues[ResearchParameter.ActiveMu]);
            Double Lambda = Convert.ToDouble(network.ResearchParameterValues[ResearchParameter.Lambda]);
            object v      = network.ResearchParameterValues[ResearchParameter.TracingStepIncrement];
            UInt32 tracingStepIncrement = ((v != null) ? Convert.ToUInt32(v) : 0);

            Double    P1   = Mu / (Lambda + Mu);
            Int32     t    = 0;
            RNGCrypto Rand = new RNGCrypto();
            uint      currentTracingStep = tracingStepIncrement;
            int       curActiveNodeCount = container.GetActiveNodesCount();

            while (t <= Time && curActiveNodeCount != 0)
            {
                Int32 RandomActiveNode = GetRandomActiveNodeIndex(Rand);
                if (Rand.NextDouble() <= P1)
                {
                    container.SetActiveStatus(RandomActiveNode, false);
                }
                else
                {
                    int x = 0;
                    //foreach (int x in container.Neighbourship[RandomActiveNode])
                    //{
                    if (Rand.NextDouble() < Lambda && container.GetActiveStatus(x) == true)
                    {
                        container.SetActiveStatus(x, true);
                    }
                    //}
                }

                /*if (currentTracingStep == t)
                 * {
                 *  container.Trace(network.ResearchName + "_ActivePartB",
                 *      "Realization_" + network.NetworkID.ToString(),
                 *      "Matrix_" + currentTracingStep.ToString());
                 *  currentTracingStep += tracingStepIncrement;
                 *
                 *  network.UpdateStatus(NetworkStatus.StepCompleted);
                 * }*/

                ++t;
                curActiveNodeCount = container.GetActiveNodesCount();
                Double ActivePartA = (Double)curActiveNodeCount / (Double)container.Size;
                ActiveParts.Add(t - 1, ActivePartA);
            }
            Debug.Assert(t > Time || !container.DoesActiveNodeExist());

            return(ActiveParts);
        }
        internal Double NonPermanentRandomization(ref List <EdgesAddedOrRemoved> edges)
        {
            Debug.Assert(evolutionInformation != null, "Evolution Information is not initialized.");
            RNGCrypto rand         = new RNGCrypto();
            int       edgeToRemove = rand.Next(0, EdgesCount);

            Debug.Assert(edgeToRemove < evolutionInformation.existingEdges.Count);
            int rvertex1 = evolutionInformation.existingEdges[edgeToRemove].Key;
            int rvertex2 = evolutionInformation.existingEdges[edgeToRemove].Value;

            int edgeToAdd = rand.Next(0, evolutionInformation.nonExistingEdges.Count);
            int avertex1  = evolutionInformation.nonExistingEdges[edgeToAdd].Key;
            int avertex2  = evolutionInformation.nonExistingEdges[edgeToAdd].Value;

            RemoveConnection(rvertex1, rvertex2);
            if (edges != null)
            {
                edges.Add(new EdgesAddedOrRemoved(rvertex1, rvertex2, false));
            }
            // Calculate removed cycles count
            int removedCyclesCount = Cycles3ByVertices(rvertex1, rvertex2);

            AddConnection(avertex1, avertex2);
            if (edges != null)
            {
                edges.Add(new EdgesAddedOrRemoved(avertex1, avertex2, true));
            }
            // Calculate removed cycles count
            int addedCyclesCount = Cycles3ByVertices(avertex1, avertex2);

            return(addedCyclesCount - removedCyclesCount);
        }
        /// <summary>
        /// Sets random active statuses with given probability to all nodes of the network.
        /// </summary>
        /// <param name="p">Activation probability for each node.</param>
        public void RandomActivating(Double p)
        {
            RNGCrypto rand = new RNGCrypto();

            activeNodes = new BitArray(Size, false);
            for (Int32 i = 0; i < Size; ++i)
            {
                if (rand.NextDouble() <= p)
                {
                    activeNodes[i] = true;
                }
            }
        }
        internal Double PermanentRandomization(ref List <EdgesAddedOrRemoved> edges)
        {
            Debug.Assert(evolutionInformation != null, "Evolution Information is not initialized.");
            RNGCrypto rand = new RNGCrypto();
            int       e1   = rand.Next(0, evolutionInformation.existingEdges.Count);
            int       e2   = rand.Next(0, evolutionInformation.existingEdges.Count);

            //Debug.Assert(e1 < evolutionInformation.existingEdges.Count && e2 < evolutionInformation.existingEdges.Count);

            while (e1 == e2 ||
                   evolutionInformation.existingEdges[e1].Key == evolutionInformation.existingEdges[e2].Key ||
                   evolutionInformation.existingEdges[e1].Value == evolutionInformation.existingEdges[e2].Value ||
                   evolutionInformation.existingEdges[e1].Key == evolutionInformation.existingEdges[e2].Value ||
                   evolutionInformation.existingEdges[e1].Value == evolutionInformation.existingEdges[e2].Key ||
                   AreConnected(evolutionInformation.existingEdges[e1].Key, evolutionInformation.existingEdges[e2].Key) ||
                   AreConnected(evolutionInformation.existingEdges[e1].Value, evolutionInformation.existingEdges[e2].Value))
            {
                e1 = rand.Next(0, evolutionInformation.existingEdges.Count);
                e2 = rand.Next(0, evolutionInformation.existingEdges.Count);
                //Debug.Assert(e1 < evolutionInformation.existingEdges.Count && e2 < evolutionInformation.existingEdges.Count);
            }

            int vertex1 = evolutionInformation.existingEdges[e1].Key, vertex2 = evolutionInformation.existingEdges[e1].Value;
            int vertex3 = evolutionInformation.existingEdges[e2].Key, vertex4 = evolutionInformation.existingEdges[e2].Value;

            RemoveConnection(vertex1, vertex2);
            RemoveConnection(vertex3, vertex4);
            if (edges != null)
            {
                edges.Add(new EdgesAddedOrRemoved(vertex1, vertex2, false));
                edges.Add(new EdgesAddedOrRemoved(vertex3, vertex4, false));
            }
            // Calculate removed cycles count
            int removedCyclesCount = Cycles3ByVertices(vertex1, vertex2) +
                                     Cycles3ByVertices(vertex3, vertex4);

            AddConnection(vertex1, vertex3);
            AddConnection(vertex2, vertex4);
            if (edges != null)
            {
                edges.Add(new EdgesAddedOrRemoved(vertex1, vertex3, true));
                edges.Add(new EdgesAddedOrRemoved(vertex2, vertex4, true));
            }
            // Calculate removed cycles count
            int addedCyclesCount = Cycles3ByVertices(vertex1, vertex3) +
                                   Cycles3ByVertices(vertex2, vertex4);

            return(addedCyclesCount - removedCyclesCount);
        }
        private Int32 GetRandomActiveNodeIndex(RNGCrypto Rand)
        {
            List <Int32> ActiveIndexes = new List <Int32>();

            for (int i = 0; i < container.Size; ++i)
            {
                if (container.GetActiveStatus(i))
                {
                    ActiveIndexes.Add(i);
                }
            }

            if (ActiveIndexes.Count == 0)
            {
                return(-1);
            }

            return(GetRandomIndex(ActiveIndexes, Rand));
        }
        public int PermanentRandomization()
        {
            RNGCrypto rand = new RNGCrypto();

            int e1 = rand.Next(0, existingEdges.Count);

            Debug.Assert(e1 < existingEdges.Count);
            int e2 = rand.Next(0, existingEdges.Count);

            Debug.Assert(e2 < existingEdges.Count);

            while (e1 == e2 ||
                   existingEdges[e1].Key == existingEdges[e2].Key ||
                   existingEdges[e1].Value == existingEdges[e2].Value ||
                   existingEdges[e1].Key == existingEdges[e2].Value ||
                   existingEdges[e1].Value == existingEdges[e2].Key ||
                   AreConnected(existingEdges[e1].Key, existingEdges[e2].Key) ||
                   AreConnected(existingEdges[e1].Value, existingEdges[e2].Value))
            {
                e1 = rand.Next(0, existingEdges.Count);
                Debug.Assert(e1 < existingEdges.Count);
                e2 = rand.Next(0, existingEdges.Count);
                Debug.Assert(e2 < existingEdges.Count);
            }

            int vertex1 = existingEdges[e1].Key, vertex2 = existingEdges[e1].Value;
            int vertex3 = existingEdges[e2].Key, vertex4 = existingEdges[e2].Value;

            RemoveConnection(vertex1, vertex2);
            RemoveConnection(vertex3, vertex4);
            // Calculate removed cycles count
            int removedCyclesCount = Cycles3ByVertices(vertex1, vertex2) +
                                     Cycles3ByVertices(vertex3, vertex4);

            AddConnection(vertex1, vertex3);
            AddConnection(vertex2, vertex4);
            // Calculate removed cycles count
            int addedCyclesCount = Cycles3ByVertices(vertex1, vertex3) +
                                   Cycles3ByVertices(vertex2, vertex4);

            return(addedCyclesCount - removedCyclesCount);
        }
예제 #8
0
 public HMNetworkGenerator(ContainerMode containerMode) : base(containerMode)
 {
     container = new NonHierarchicContainer(containerMode);
     rand      = new RNGCrypto();
     node      = 0;
 }
예제 #9
0
        public void Calculate()
        {
            NonHierarchicContainer containerToChange = (NonHierarchicContainer)container.Clone();

            containerToChange.InitializeEvolutionInformation();

            RNGCrypto rand = new RNGCrypto();
            int       currentStep = 0, currentTracingStep = tracingStepIncrement;

            Cycles3Trajectory.Add(currentStep, currentCycle3Count);

            while (currentStep != stepCount)
            {
                NonHierarchicContainer savedContainer = (NonHierarchicContainer)containerToChange.Clone();
                ++currentStep;

                List <EdgesAddedOrRemoved> edges = visualMode ? new List <EdgesAddedOrRemoved>() : null;
                double deltaCount = permanentDistribution ?
                                    containerToChange.PermanentRandomization(ref edges) :
                                    containerToChange.NonPermanentRandomization(ref edges);
                double newCycle3Count = currentCycle3Count + deltaCount;

                int delta = (int)(newCycle3Count - currentCycle3Count);
                if (delta > 0) // accept case
                {
                    Cycles3Trajectory.Add(currentStep, newCycle3Count);
                    if (visualMode)
                    {
                        EvolutionInformation.Add(edges);
                    }
                    currentCycle3Count = newCycle3Count;
                }
                else
                {
                    double probability = Math.Exp((-omega * Math.Abs(delta)));
                    if (rand.NextDouble() < probability) // accept case
                    {
                        Cycles3Trajectory.Add(currentStep, newCycle3Count);
                        if (visualMode)
                        {
                            EvolutionInformation.Add(edges);
                        }
                        currentCycle3Count = newCycle3Count;
                    }
                    else // reject case
                    {
                        Cycles3Trajectory.Add(currentStep, currentCycle3Count);
                        containerToChange = savedContainer;
                    }
                }

                network.UpdateStatus(NetworkStatus.StepCompleted);

                // TODO closed Trace

                /*if (currentTracingStep == currentStep)
                 * {
                 *  container.Trace(network.ResearchName,
                 *      "Realization_" + network.NetworkID.ToString(),
                 *      "Matrix_" + currentTracingStep.ToString());
                 *  currentTracingStep += tracingStepIncrement;
                 *
                 *  network.UpdateStatus(NetworkStatus.StepCompleted);
                 * }*/
            }
        }
예제 #10
0
 public DistributedRandom(string function_str)
 {
     function_ = new SimpleFunction(function_str);
     random_   = new RNGCrypto();
 }
 private Int32 GetRandomIndex(List <Int32> list, RNGCrypto Rand) => list.OrderBy(x => Rand.Next()).FirstOrDefault();