src/GenFx.Components/Lists/IntegerListEntity.cs
/// <summary>
/// Initializes the component to ensure its readiness for algorithm execution.
/// </summary>
/// <param name="algorithm">The algorithm that is to use this component.</param>
Initializes the component to ensure its readiness for algorithm execution.
Initializes the component to ensure its readiness for algorithm execution .
public override void Initialize(GeneticAlgorithm algorithm)
        {
            base.Initialize(algorithm);
            if (this.RequiresUniqueElementValues)
            {
                List<int> availableInts = new List<int>();
                for (int i = this.MinElementValue; i <= this.MaxElementValue; i++)
                {
                    availableInts.Add(i);
                }
                int n = availableInts.Count;
                while (n > 1)
                {
                    n--;
                    int k = RandomNumberService.Instance.GetRandomValue(n);
                    int value = availableInts[k];
                    availableInts[k] = availableInts[n];
                    availableInts[n] = value;
                }
                availableInts.RemoveRange(this.Length, availableInts.Count - this.Length);
                for (int i = 0; i < availableInts.Count; i++)
                {
                    this[i] = availableInts[i];
                }
            }
            else
            {
                for (int i = 0; i < this.Length; i++)
                {
                    this[i] = RandomNumberService.Instance.GetRandomValue(this.MinElementValue - 1, this.MaxElementValue) + 1;
                }
            }
            this.UpdateStringRepresentation();
        }
public override void Initialize ( GeneticAlgorithm algorithm ) { base . Initialize ( algorithm ) ; if ( this . RequiresUniqueElementValues ) { List < int > availableInts = new List < int > ( ) ; for ( int i = this . MinElementValue ; i <= this . MaxElementValue ; i ++ ) { availableInts . Add ( i ) ; } int n = availableInts . Count ; while ( n > 1 ) { n -- ; int k = RandomNumberService . Instance . GetRandomValue ( n ) ; int value = availableInts [ k ] ; availableInts [ k ] = availableInts [ n ] ; availableInts [ n ] = value ; } availableInts . RemoveRange ( this . Length , availableInts . Count - this . Length ) ; for ( int i = 0 ; i < availableInts . Count ; i ++ ) { this [ i ] = availableInts [ i ] ; } } else { for ( int i = 0 ; i < this . Length ; i ++ ) { this [ i ] = RandomNumberService . Instance . GetRandomValue ( this . MinElementValue - 1 , this . MaxElementValue ) + 1 ; } } this . UpdateStringRepresentation ( ) ; }
Initializes the component to ensure its readiness for algorithm execution.
Initializes the component to ensure its readiness for algorithm execution .
// randomize the ints // Normally, we would get a random value like this GetRandomValue(MinElementValue, MaxElementValue + 1), // but if MaxElementValue is equal to Int32.Max, then adding 1 to it would cause an overlflow, so // we shift the range down by 1 to get the random value and then add 1 to it.
algorithm
GeneticAlgorithm
The algorithm that is to use this component.
The algorithm that is to use this component .