Example #1
0
        private Event CreateEvent()
        {
            Event evt;

            switch (Type)
            {
            case EventType.Output:
                evt = new OutputEvent();
                break;

            case EventType.Abort:
                evt = new AbortCondition
                {
                    ThresholdMin = ThresholdMin,
                    ThresholdMax = ThresholdMax
                };
                break;

            default:
                throw new InvalidOperationException("Invalid event type");
            }
            evt.StartTime   = StartTime;
            evt.EndTime     = EndTime;
            evt.ChannelName = Channel;
            return(evt);
        }
        public void SettingNotificationWithEventLoadsEditorWithEventState_Abort()
        {
            var configService = A.Fake <IConfigService>();

            A.CallTo(() => configService.Config).Returns(new Config());

            var abortCondition = new AbortCondition
            {
                ChannelName  = "Channel",
                StartTime    = TimeSpan.FromSeconds(1),
                EndTime      = TimeSpan.FromSeconds(2),
                ThresholdMin = 100,
                ThresholdMax = 200
            };

            var viewModel = new EventEditorViewModel(configService)
            {
                Notification = new Confirmation
                {
                    Content = abortCondition
                }
            };

            Assert.Equal(abortCondition.ChannelName, viewModel.Channel);
            Assert.Equal(abortCondition.StartTime, viewModel.StartTime);
            Assert.Equal(abortCondition.EndTime, viewModel.EndTime);
            Assert.Equal(abortCondition.ThresholdMin, viewModel.ThresholdMin);
            Assert.Equal(abortCondition.ThresholdMax, viewModel.ThresholdMax);
            Assert.Equal(EventType.Abort, viewModel.Type);
        }
Example #3
0
        static void Main(string[] args)
        {
            ExclusiveLocking.RunTest();
            NonExclusiveLocking.RunTest();
            Signaling.RunTest();
            SignalingWithReset.RunTest();
            AbortCondition.RunTest();

            Console.ReadLine();
        }
 public bool AbortConditionMet()
 {
     return(BO.Count == 0 || AbortCondition.IsTrue());
 }
            /// <summary>Finds the argmin of <see cref="IOneDimOptimizerAlgorithm.Function"/>.
            /// </summary>
            /// <param name="x">An initial guess of the algorithm (if applicable); on exit this argument contains the argmin.</param>
            /// <param name="argMin">The estimated argmin of the objective function (output).</param>
            /// <param name="minimum">The minimum, i.e. the function value with respect to <paramref name="argMin"/> which represents the argmin (output).</param>
            /// <returns>The state of the algorithm, i.e. an indicating whether <paramref name="argMin"/> and <paramref name="minimum"/> contains valid data.</returns>
            public OneDimOptimizer.State FindMinimum(double x, out double argMin, out double minimum)
            {
                var lowerBound         = m_Constraint.IntervalRepresentation.Infimum;
                var upperBound         = m_Constraint.IntervalRepresentation.Supremum;
                var constraintDiameter = upperBound - lowerBound;

                if (m_Constraint.IntervalRepresentation.GetPointPosition(x) != PointRegionRelation.InsideOrBoundaryPoint)
                {
                    x = lowerBound + constraintDiameter * SingleRandomNumberStream.GetNextDouble();
                }
                var y = m_ObjectiveFunction.GetValue(x);
                int evaluationsNeeded = 1;

                minimum = Double.MaxValue;
                argMin  = x;
                var xLastAccepted = x;
                var yLastAccepted = y;

                var yStar = Enumerable.Repeat(21.0, AbortCondition.RequiredNumberOfAcceptedPoints).ToArray(); // todo: initialized with 21.0 ??

                yStar[0] = yLastAccepted;
                int yStarIndex = 0;

                int numberOfAcceptedPoint = 0;
                var stepLength            = Configuration.InitialStepLength;
                var temperature           = Configuration.InitialTemperature;

                for (int iteration = 1; iteration <= AbortCondition.MaxIterations; iteration++)
                {
                    for (int k = 1; k <= AbortCondition.NumberOfIterationsBeforeTemperatureReduction; k++)
                    {
                        for (int j = 1; j <= AbortCondition.NumberOfCyles; j++)
                        {
                            /* 1.) generate a new point and ensure that the point is inside the constraint: */
                            if (Configuration.PointGenerationRule == OneDimSAOptimizerConfiguration.GenerationRule.UseWholeDomain)
                            {
                                x = xLastAccepted + stepLength * (2.0 * SingleRandomNumberStream.GetNextDouble() - 1.0);
                            }
                            else
                            {
                                var sign = SingleRandomNumberStream.GetNextDouble() >= 0.5 ? 1 : -1;
                                if (sign < 0)
                                {
                                    var leftPoint = DoMath.Max(lowerBound, xLastAccepted - stepLength);
                                    x = leftPoint + (xLastAccepted - leftPoint) * SingleRandomNumberStream.GetNextDouble();
                                }
                                else
                                {
                                    var rightPoint = DoMath.Min(upperBound, xLastAccepted + stepLength);
                                    x = xLastAccepted + (rightPoint - xLastAccepted) * SingleRandomNumberStream.GetNextDouble();
                                }
                            }
                            if ((x < lowerBound) || (x > upperBound))
                            {
                                x = lowerBound + constraintDiameter * SingleRandomNumberStream.GetNextDouble();
                            }
                            y = m_ObjectiveFunction.GetValue(x);
                            evaluationsNeeded++;

                            if (evaluationsNeeded > AbortCondition.MaxEvaluations)
                            {
                                return(State.Create(OptimizerErrorClassification.EvaluationLimitExceeded, argMin, minimum, evaluationsNeeded, 1));
                            }

                            /* 2.) check if this point is 'better' than the point before (accept point) */
                            if (y < yLastAccepted)
                            {
                                xLastAccepted = x;
                                yLastAccepted = y;
                                numberOfAcceptedPoint++;

                                yStarIndex++;
                                if (yStarIndex >= AbortCondition.RequiredNumberOfAcceptedPoints)
                                {
                                    yStarIndex = 0;
                                }
                                yStar[yStarIndex] = y;

                                if (y < minimum)  /* maybe it is also better than the minimum founded before */
                                {
                                    argMin  = x;
                                    minimum = y;
                                }
                            }
                            else  /* use metropolis criterion */
                            {
                                var p = Math.Exp(-(y - yLastAccepted) / temperature);
                                if (p > SingleRandomNumberStream.GetNextDouble()) // accept this value as new point
                                {
                                    xLastAccepted = x;
                                    yLastAccepted = y;
                                    numberOfAcceptedPoint++;

                                    yStarIndex++;
                                    if (yStarIndex >= AbortCondition.RequiredNumberOfAcceptedPoints)
                                    {
                                        yStarIndex = 0;
                                    }
                                    yStar[yStarIndex] = y;
                                }
                            }
                        }

                        /* 3.) adjust step length so that approximately half of all evaluations are accepted */
                        var acceptanceRate = numberOfAcceptedPoint / ((double)AbortCondition.NumberOfCyles);
                        if (acceptanceRate > 0.6)
                        {
                            stepLength = stepLength * (1.0 + Configuration.StepLengthControlFactor * (acceptanceRate - 0.6) / 0.4);
                        }
                        else if (acceptanceRate < 0.4)
                        {
                            stepLength = stepLength / (1.0 + Configuration.StepLengthControlFactor * ((0.4 - acceptanceRate) / 0.4));
                        }
                        if (stepLength > constraintDiameter)
                        {
                            stepLength = constraintDiameter;
                        }
                        numberOfAcceptedPoint = 0;
                    }
                    if (AbortCondition.IsSatisfied(minimum, y, yLastAccepted, yStar, yStarIndex) == true)
                    {
                        return(State.Create(OptimizerErrorClassification.ProperResult, argMin, minimum, evaluationsNeeded, iteration));
                    }

                    /* prepare for another loop */
                    temperature  *= Configuration.CoolDownFactor;
                    yLastAccepted = minimum;
                    xLastAccepted = argMin;
                }
                return(State.Create(OptimizerErrorClassification.IterationLimitExceeded, argMin, minimum, evaluationsNeeded, AbortCondition.MaxIterations));
            }