Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17658


Ignore:
Timestamp:
07/07/20 21:52:47 (4 years ago)
Author:
dleko
Message:

#2825 Add better error handling for MinArgMin and MaxArgMax methods. Add comments describing the methods.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/Utility.cs

    r17616 r17658  
    9494        internal static TOut Min<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
    9595        {
    96             //var it = inputs.GetEnumerator();
    97             //it.MoveNext();
    98             //var minValue = func(it.Current);
    99             //while (it.MoveNext())
    100             //{
    101             //    var currentValue = func(it.Current);
    102             //    if (minValue.CompareTo(currentValue) > 0) minValue = currentValue;
    103             //}
    104 
    105             //return minValue;
    10696            return MinArgMin(func, inputs).Item2;
    10797        }
     
    112102        }
    113103
     104        /// <summary>
     105        /// Finds the value amongst <paramref name="inputs" /> such that the output value returned
     106        /// by <paramref name="func" /> is minimal.
     107        /// </summary>
     108        /// <typeparam name="TIn">The input type for the function.</typeparam>
     109        /// <typeparam name="TOut">The comparable output type of the function.</typeparam>
     110        /// <param name="func">The function for which to find the minimal value.</param>
     111        /// <param name="inputs">
     112        /// The function arguments for which to find the one with the minimum output value when
     113        /// given to <paramref name="func" />.
     114        /// </param>
     115        /// <returns></returns>
    114116        internal static Tuple<TIn, TOut> MinArgMin<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
    115117        {
     118            if (inputs == null) throw new ArgumentNullException(nameof(inputs));
    116119            var it = inputs.GetEnumerator();
    117             it.MoveNext();
     120            var hasItems = it.MoveNext();
     121            if (!hasItems) throw new InvalidOperationException("No items given to find the minimum of");
     122
     123            // find minimum argument and minimum value
    118124            var minArg = it.Current;
    119125            var minValue = func(it.Current);
     
    134140        internal static TOut Max<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
    135141        {
    136             //var it = inputs.GetEnumerator();
    137             //it.MoveNext();
    138             //var maxValue = func(it.Current);
    139             //while (it.MoveNext())
    140             //{
    141             //    var currentValue = func(it.Current);
    142             //    if (maxValue.CompareTo(currentValue) < 0) maxValue = currentValue;
    143             //}
    144 
    145             //return maxValue;
    146142            return MaxArgMax(func, inputs).Item2;
    147143        }
     
    152148        }
    153149
     150        /// <summary>
     151        /// Finds the value amongst <paramref name="inputs" /> such that the output value returned
     152        /// by <paramref name="func" /> is as big as possible.
     153        /// </summary>
     154        /// <typeparam name="TIn">The input type for the function.</typeparam>
     155        /// <typeparam name="TOut">The comparable output type of the function.</typeparam>
     156        /// <param name="func">The function for which to find the biggest value.</param>
     157        /// <param name="inputs">
     158        /// The function arguments for which to find the one with the biggest output value when
     159        /// given to <paramref name="func" />.
     160        /// </param>
     161        /// <returns></returns>
    154162        internal static Tuple<TIn, TOut> MaxArgMax<TIn, TOut>(Func<TIn, TOut> func, IEnumerable<TIn> inputs) where TOut : IComparable
    155163        {
     164            if (inputs == null) throw new ArgumentNullException(nameof(inputs));
    156165            var it = inputs.GetEnumerator();
    157             it.MoveNext();
     166            var hasItems = it.MoveNext();
     167            if (!hasItems) throw new InvalidOperationException("No items given to find the maximum of");
     168
     169            // find maximum argument and maximum value
    158170            var maxArg = it.Current;
    159171            var maxValue = func(it.Current);
Note: See TracChangeset for help on using the changeset viewer.