Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- 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);
- }
- 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));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement