src/GenFx.Components/SelectionOperators/RouletteWheelSampler.cs
/// <summary>
/// Returns a <see cref="GeneticEntity"/> that was randomly selected from the <see cref="GeneticEntity"/>
/// objects in <paramref name="wheelSlices"/> according to the range of values associated with it.
/// </summary>
/// <param name="wheelSlices"><see cref="WheelSlice"/> objects defining the
/// structure of the "roulette wheel".</param>
/// <returns>
/// <see cref="GeneticEntity"/> that was randomly selected from the <see cref="GeneticEntity"/>
/// objects in <paramref name="wheelSlices"/> according to the range of values associated with it.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="wheelSlices"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="wheelSlices"/> is empty.</exception>
/// <exception cref="ArgumentException"><paramref name="wheelSlices"/> contains a null item.</exception>
Returns a  that was randomly selected from the
objects in  according to the range of values associated with it.
Returns a that was randomly selected from the objects in according to the range of values associated with it .
public static GeneticEntity GetEntity(IList<WheelSlice> wheelSlices)
        {
            if (wheelSlices == null)
            {
                throw new ArgumentNullException(nameof(wheelSlices));
            }
            if (wheelSlices.Count == 0)
            {
                throw new ArgumentException(
                  StringUtil.GetFormattedString(Resources.ErrorMsg_EmptyList), nameof(wheelSlices));
            }
            double totalSize = 0;
            foreach (WheelSlice wheelSlice in wheelSlices)
            {
                if (wheelSlice == null)
                {
                    throw new ArgumentException(Resources.ErrorMsg_NullItemInList, nameof(wheelSlices));
                }
                totalSize += wheelSlice.Size;
            }
            if (totalSize == 0)
            {
                return wheelSlices[RandomNumberService.Instance.GetRandomValue(wheelSlices.Count)].Entity;
            }
            List<EntityPercentageRange> percentageRanges = new List<EntityPercentageRange>();
            double lastMaxValue = 0;
            for (int i = 0; i < wheelSlices.Count; i++)
            {
                double percent = (wheelSlices[i].Size / totalSize) * 100;
                percentageRanges.Add(new EntityPercentageRange(wheelSlices[i].Entity, lastMaxValue, lastMaxValue + percent));
                lastMaxValue += percent;
            }
            return SpinWheel(percentageRanges);
        }
public static GeneticEntity GetEntity ( IList < WheelSlice > wheelSlices ) { if ( wheelSlices == null ) { throw new ArgumentNullException ( nameof ( wheelSlices ) ) ; } if ( wheelSlices . Count == 0 ) { throw new ArgumentException ( StringUtil . GetFormattedString ( Resources . ErrorMsg_EmptyList ) , nameof ( wheelSlices ) ) ; } double totalSize = 0 ; foreach ( WheelSlice wheelSlice in wheelSlices ) { if ( wheelSlice == null ) { throw new ArgumentException ( Resources . ErrorMsg_NullItemInList , nameof ( wheelSlices ) ) ; } totalSize += wheelSlice . Size ; } if ( totalSize == 0 ) { return wheelSlices [ RandomNumberService . Instance . GetRandomValue ( wheelSlices . Count ) ] . Entity ; } List < EntityPercentageRange > percentageRanges = new List < EntityPercentageRange > ( ) ; double lastMaxValue = 0 ; for ( int i = 0 ; i < wheelSlices . Count ; i ++ ) { double percent = ( wheelSlices [ i ] . Size / totalSize ) * 100 ; percentageRanges . Add ( new EntityPercentageRange ( wheelSlices [ i ] . Entity , lastMaxValue , lastMaxValue + percent ) ) ; lastMaxValue += percent ; } return SpinWheel ( percentageRanges ) ; }
Returns a  that was randomly selected from the
objects in  according to the range of values associated with it.
Returns a that was randomly selected from the objects in according to the range of values associated with it .
// none of the wheel slices have a size.  default to selecting a random entity
that was randomly selected from the
objects in  according to the range of values associated with it.
that was randomly selected from the objects in according to the range of values associated with it .
None
None
contains a null item.
contains a null item .
wheelSlices
IList<WheelSlice>
objects defining the
structure of the "roulette wheel".
objects defining the structure of the " roulette wheel " .