Example #1
0
        private GammaInfo CrossEntropy2(GammaInfo gammaInfo)
        {
            IBoolean tempRareEventExpression = new EqualTo(_reExpression, new ConstantValue(gammaInfo.IntermediateRareEvent));
            var      gammaInfoOut            = new GammaInfo(_reactions.NumReactions)
            {
                IntermediateRareEvent = gammaInfo.IntermediateRareEvent,
                IntermediateGamma     = gammaInfo.IntermediateGamma
            };

            for (int i = 0; i < _crossEntropyRuns; i++)
            {
                StartRealization();
                var    n      = new int[_reactions.NumReactions];
                var    lambda = new double[_reactions.NumReactions];
                double weight = 1.0;

                while (CurrentTime < duration)
                {
                    if (tempRareEventExpression.Value)
                    {
                        gammaInfoOut.UpdateGamma(weight, n, lambda);
                        break;
                    }

                    StepOnce(ref weight, gammaInfoOut.IntermediateGamma, ref n, ref lambda);
                }
            }

            gammaInfoOut.SetIntermediateGamma();

            return(gammaInfoOut);
        }
Example #2
0
        private void RunCrossEntropy()
        {
            int   iter = 1;
            float intermediateRareEvent = _rareEventType == 1 ? 0.0f : _reExpression.Value;
            var   gammaInfo             = new GammaInfo(_reactions.NumReactions)
            {
                IntermediateRareEvent = intermediateRareEvent
            };

            using (StreamWriter output = File.AppendText(modelInfo.Name + "_dwSSA_CEinfo.txt"))
            {
                while (Math.Abs(intermediateRareEvent - _rareEventValue) > float.Epsilon)
                {
                    Console.WriteLine("\nCross Entropy iteration " + iter + ":");
                    output.WriteLine("\nCross Entropy iteration " + iter + ":");
                    output.Flush();

                    gammaInfo = CrossEntropy1(gammaInfo);
                    bool rareEventFlag = (Math.Abs(gammaInfo.IntermediateRareEvent - _rareEventValue) <= float.Epsilon);
                    intermediateRareEvent = gammaInfo.IntermediateRareEvent;

                    output.WriteLine("   Intermediate rare event: " + intermediateRareEvent);
                    Console.WriteLine("   Intermediate rare event: " + intermediateRareEvent);
                    output.Flush();

                    if (rareEventFlag)
                    {
                        output.WriteLine("\nReached the rare event. Exiting multilevel cross entropy simulation...");
                        Console.WriteLine("\nReached the rare event. Exiting multilevel cross entropy simulation...");
                        output.Flush();
                        break;
                    }

                    gammaInfo.UpdateStructure();
                    gammaInfo = CrossEntropy2(gammaInfo);

                    for (int i = 0; i < _reactions.NumReactions; i++)
                    {
                        output.WriteLine("   Intermediate gamma " + (i + 1) + ": " + gammaInfo.IntermediateGamma[i]);
                        Console.WriteLine("   Intermediate gamma " + (i + 1) + ": " + gammaInfo.IntermediateGamma[i]);
                    }

                    output.Flush();
                    gammaInfo.UpdateStructure();
                    iter++;

                    if (iter == 15)
                    {
                        throw new ApplicationException("multilevel cross entropy did not converged in 15 iterations.");
                    }
                }
            }

            for (int i = 0; i < _reactions.NumReactions; i++)
            {
                _gamma[i] = gammaInfo.IntermediateGamma[i];
            }
        }
Example #3
0
        private GammaInfo CrossEntropy1(GammaInfo gammaInfo)
        {
            var maxRareEventValue = new float[_crossEntropyRuns];
            int counter           = 0;

            for (int i = 0; i < _crossEntropyRuns; i++)
            {
                StartRealization();
                float  currentMin = _rareEventType * (_rareEventValue - _reExpression.Value);
                var    n          = new int[_reactions.NumReactions];
                var    lambda     = new double[_reactions.NumReactions];
                double weight     = 1.0;

                while (CurrentTime < duration)
                {
                    if (_rareEventTest.Value)
                    {
                        counter++;
                        gammaInfo.UpdateGamma(weight, n, lambda);
                        break;
                    }

                    StepOnce(ref weight, gammaInfo.IntermediateGamma, ref n, ref lambda);
                    float tempMin = _rareEventType * (_rareEventValue - _reExpression.Value);
                    currentMin = Math.Min(currentMin, tempMin);
                }

                maxRareEventValue[i] = currentMin;
            }

            Array.Sort(maxRareEventValue);
            float ireComp = maxRareEventValue[(int)Math.Ceiling(_crossEntropyRuns * _crossEntropyThreshold)];
            float pastIntermediateRareEvent = gammaInfo.IntermediateRareEvent;

            gammaInfo.IntermediateRareEvent = ireComp < 0 ? _rareEventValue : _rareEventType * (_rareEventValue - ireComp);

            if (gammaInfo.IntermediateRareEvent >= _rareEventValue && counter >= (int)(_crossEntropyRuns * _crossEntropyThreshold))
            {
                gammaInfo.SetIntermediateGamma();
                var gammaInfoOut = new GammaInfo(_reactions.NumReactions)
                {
                    IntermediateRareEvent = _rareEventValue
                };

                for (int i = 0; i < _reactions.NumReactions; i++)
                {
                    gammaInfoOut.IntermediateGamma[i] = gammaInfo.IntermediateGamma[i];
                }

                return(gammaInfoOut);
            }

            var cetCriteria = (pastIntermediateRareEvent - gammaInfo.IntermediateRareEvent) * _rareEventType;

            if (cetCriteria > 0)
            {
                _crossEntropyThreshold *= 0.8f;
                Console.WriteLine("Cross entropy threshold changed to : " + _crossEntropyThreshold);
                Console.WriteLine("CETC: {0}   pire: {1}   ire:{2} ", cetCriteria, pastIntermediateRareEvent, gammaInfo.IntermediateRareEvent);

                if (_crossEntropyThreshold * _crossEntropyRuns * 0.8 < _crossEntropyMinDataSize)
                {
                    _crossEntropyRuns = (int)Math.Ceiling(_crossEntropyMinDataSize / _crossEntropyThreshold);
                    Console.WriteLine("Number of cross entropy simulations changed to : " + _crossEntropyRuns);
                }
            }

            return(gammaInfo);
        }