0

I'm writing an genetic algorithm for finding coefficients having given X, Y points. The principle of operation is described on this page - https://towardsdatascience.com/introduction-to-genetic-algorithms-including-example-code-e396e98d8bf3

I have problem, because sometimes after mutation or crossover my double value is NaN.

I've tried do this using byte[] and BitArray, but in both approaches I have the same result.

Converting double <-> BitArray:

public void ConvertByteArrayToCoefficients(BitArray array)
{
    Coefficients.Clear(); //Coefficients are stored in List<double>

    for (int i = 0; i < _degree + 1; i++)
    {
        var arr = array.ToByteArray();
        double value = BitConverter.ToDouble(array.ToByteArray(), i * sizeof(double));

        Coefficients.Add(value);
    }
}

public BitArray GetAllCoefficientsInBytes()
{
    BitArray bytes = new BitArray(0);

    for (int i = 0; i < Coefficients.Count; i++) //append is extension method
        bytes = bytes.Append(new BitArray(BitConverter.GetBytes(Coefficients[i])));

    return bytes;
}

Mutation:

public void Mutate(int percentageChance)
{
    BitArray bytes = GetAllCoefficientsInBytes();

    for (int i = 0; i < bytes.Length; i++)
    {
        if (_randomProvider.Next(0, 100) < percentageChance)
        {
            if (bytes.Get(i))
                bytes[i] = false;
            else
                bytes[i] = true;
         }
    }

    ConvertByteArrayToCoefficients(bytes);
}

Crossover - method called for every two polynomial:

private void CrossoverSingle(Polynomial poly1, Polynomial poly2)
{
    int cutPosition = _randomProvider.Next(1, (_degreeOfPolynomial + 1) * sizeof(double) * 8);

    BitArray bytesOne = poly1.GetAllCoefficientsInBytes();
    BitArray bytesTwo = poly2.GetAllCoefficientsInBytes();

    for (int i = bytesOne.Length-1; i >= cutPosition; i--)
    {
        bool bitOne = bytesOne[i];
        bool bitTwo = bytesTwo[i];

        if (bitOne != bitTwo)
        {
            bytesOne[i] = bitTwo;
            bytesTwo[i] = bitOne;
        }
    }

    _crossoveredChildren.Add(new Polynomial(_randomProvider, _degreeOfPolynomial, bytesOne));
    _crossoveredChildren.Add(new Polynomial(_randomProvider, _degreeOfPolynomial, bytesTwo));
}

All code is on github: https://github.com/Makulak/CoefficientsFinder Maybe You have any idea why it is happening?

5
  • Because you’re meddling with the bits and producing a bit pattern that means NaN. If you don’t want to get them you’ll have to make sure you don’t produce such patterns. Jun 16, 2019 at 18:13
  • So, the quickest option is just simple do { .. }while(double.IsNan(value) when I change bits?
    – JoeDoe
    Jun 16, 2019 at 18:15
  • @JoeDoe No, that's a horrible way.
    – Dai
    Jun 16, 2019 at 18:16
  • Are these integer, rational, or real coefficients? What does the BitArray value actually represent?
    – Dai
    Jun 16, 2019 at 18:17
  • coeffitients are real. In BitArray I have all coefficients in binary (method GetAllCoefficientsInBytes())
    – JoeDoe
    Jun 16, 2019 at 18:32

1 Answer 1

5

It's because you're using random bytes to generate IEEE-754 numbers. You should not do that because IEEE-754 defines the structure for these numbers and using random byte input won't give you random numbers because some bits represent things like the is Not-a-Number field, and NaN values are "viral" and invalidate other calculations.

To generate random Double numbers you should use System.Random.NextDouble().

2
  • But in genetic algorithm I have to swap single bytes between two values, or mutate them (change randomly 0 <--> 1)
    – JoeDoe
    Jun 16, 2019 at 18:34
  • @JoeDoe But you can't do that though, this is because floating-point numbers don't have a trivial binary representation (the way that integers do). C# is not a language that's meant for editing raw bytes either. You need to rethink how your mutation algorithm will work (e.g. using values from parents to represent bounds on subsequent random numbers).
    – Dai
    Jun 16, 2019 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.