Beispiel #1
0
        /// <summary>
        /// Tells if an algorithm that is currently in <paramref name="state"/> state
        /// should stop (and don't make further steps) or not. Takes step count from
        /// the <paramref name="state"/>.
        /// </summary>
        /// <param name="state">The current algorithm state.</param>
        /// <returns>
        /// <c>true</c> if an algorithm that is currently in <paramref name="state"/>
        /// state should stop (and don't make further steps). Otherwise <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"> if <paramref name="state"/>
        /// is <c>null</c>.</exception>
        public override bool ShouldStop(IAlgorithmState state)
        {
            bool shouldStop = base.ShouldStop(state);

            if (!shouldStop)
            {
                if (state == null)
                {
                    throw new ArgumentNullException(nameof(state));
                }

                shouldStop = state.StepNumber >= this.Threshold;
            }

            return(shouldStop);
        }
Beispiel #2
0
        /// <summary>
        /// Tells if an algorithm that is currently in <paramref name="state"/> state
        /// should stop (and don't make further steps) or not. Stops if absolute value
        /// of the difference between the quality of <paramref name="state"/>'s Best
        /// Solution and <see cref="QualityProximityStopCondition"/>.Expectation is less
        /// than or equal to <see cref="QualityProximityStopCondition.QualityThreshold"/>.
        /// </summary>
        /// <param name="state">The current algorithm state.</param>
        /// <returns>
        /// <c>true</c> if an algorithm that is currently in <paramref name="state"/>
        /// state should stop (and don't make further steps). Otherwise <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"> if <paramref name="state"/>
        /// is <c>null</c>.</exception>
        public override bool ShouldStop(IAlgorithmState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            Debug.Assert(state.BestSolution != null, "State best solution is null");
            Debug.Assert(this.Expectation != null, "Expectation is null");
            Debug.Assert(!double.IsNaN(this.QualityThreshold), "Quality threshold is NaN");
            Debug.Assert(!double.IsInfinity(this.QualityThreshold), "Quality threshold is Infinity");

            double qualityProximity = Math.Abs(state.BestSolution.Quality - Expectation.Quality);

            Debug.Assert(!double.IsNaN(qualityProximity), "Quality proximity is NaN");
            Debug.Assert(!double.IsInfinity(qualityProximity), "Quality proximity is Infinity");

            return(qualityProximity.IsLessOrEqual(this.QualityThreshold));
        }
Beispiel #3
0
        /// <summary>
        /// Tells if an algorithm that is currently in <paramref name="state"/> state
        /// should stop (and don't make further steps) or not. Stops if the distance
        /// between the <paramref name="state"/>'s Best Solution and the
        /// <see cref="CoordinateProximityStopCondition"/>.Expectation is less than
        /// or equal to <see cref="CoordinateProximityStopCondition"/>.DistanceThreshold.
        /// </summary>
        /// <param name="state">The current algorithm state.</param>
        /// <returns>
        /// <c>true</c> if an algorithm that is currently in <paramref name="state"/>
        /// state should stop (and don't make further steps). Otherwise <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"> if <paramref name="state"/>
        /// is <c>null</c>.</exception>
        public override bool ShouldStop(IAlgorithmState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            Debug.Assert(this.DistanceCalculator != null, "Distance calculator is null");
            Debug.Assert(state.BestSolution != null, "State best solution is null");
            Debug.Assert(this.Expectation != null, "Expectation is null");
            Debug.Assert(!double.IsNaN(this.DistanceThreshold), "Distance threshold is NaN");
            Debug.Assert(!double.IsInfinity(this.DistanceThreshold), "Distance threshold is Infinity");

            double distance = this.DistanceCalculator.Calculate(state.BestSolution, this.Expectation);

            Debug.Assert(!double.IsNaN(distance), "Distance is NaN");
            Debug.Assert(!double.IsInfinity(distance), "Distance is Infinity");

            return(distance.IsLessOrEqual(this.DistanceThreshold));
        }
Beispiel #4
0
        /// <summary>
        /// Tells if an algorithm that is currently in <paramref name="state"/> state
        /// should stop (and don't make further steps) or not.
        /// </summary>
        /// <param name="state">The current algorithm state.</param>
        /// <returns>
        /// <c>true</c> if an algorithm that is currently in <paramref name="state"/>
        /// state should stop (and don't make further steps). Otherwise <c>false</c>.
        /// </returns>
        /// <exception cref="System.InvalidOperationException"> if unsupported
        /// <see cref="StopConditionChain.AggregationOperator"/> is used to
        /// chain stop conditions.</exception>
        public bool ShouldStop(IAlgorithmState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            switch (this.aggregationMode)
            {
            case StopConditionChain.AggregationOperator.And:
                foreach (IStopCondition stopCondition in this.stopConditions)
                {
                    Debug.Assert(stopCondition != null, "Stop condition is null");

                    if (!stopCondition.ShouldStop(state))
                    {
                        return(false);
                    }
                }

                return(true);

            case StopConditionChain.AggregationOperator.Or:
                foreach (IStopCondition stopCondition in this.stopConditions)
                {
                    Debug.Assert(stopCondition != null, "Stop condition is null");

                    if (stopCondition.ShouldStop(state))
                    {
                        return(true);
                    }
                }

                return(false);

            default:
                throw new InvalidOperationException();
            }
        }
 /// <summary>
 /// Tells if an algorithm that is currently in <paramref name="state"/> state
 /// should stop (and don't make further steps) or not.
 /// </summary>
 /// <param name="state">The current algorithm state.</param>
 /// <returns>
 /// <c>true</c> if an algorithm that is currently in <paramref name="state"/>
 /// state should stop (and don't make further steps). Otherwise <c>false</c>.
 /// </returns>
 public virtual bool ShouldStop(IAlgorithmState state)
 {
     return(this.count >= this.Threshold);
 }
Beispiel #6
0
 /// <summary>
 /// Tells if an algorithm that is currently in <paramref name="state"/> state
 /// should stop (and don't make further steps) or not.
 /// </summary>
 /// <param name="state">The current algorithm state.</param>
 /// <returns>
 /// <c>true</c> if an algorithm that is currently in <paramref name="state"/>
 /// state should stop (and don't make further steps). Otherwise <c>false</c>.
 /// </returns>
 public abstract bool ShouldStop(IAlgorithmState state);
 /// <summary>
 /// Invokes the <see cref="ProgressChanged"/> event.
 /// </summary>
 /// <param name="state">The state of an algorithm from an abstract view.</param>
 protected virtual void OnReport(IAlgorithmState <TStep> state)
 {
     ProgressChanged?.Invoke(this, state);
 }
 void IProgress <IAlgorithmState <TStep> > .Report(IAlgorithmState <TStep> value)
 {
     OnReport(value);
 }