Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AlternatingPositionCrossover throw IndexOutOfRangeException #61

Closed
dxball opened this issue Jun 12, 2019 · 1 comment
Closed

AlternatingPositionCrossover throw IndexOutOfRangeException #61

dxball opened this issue Jun 12, 2019 · 1 comment
Assignees
Labels

Comments

@dxball
Copy link

dxball commented Jun 12, 2019

Describe the bug
Using the AlternatingPositionCrossover cause System.IndexOutOfRangeException

To Reproduce
I've made a sample project, please give a look
GeneticSharpTest.zip

Expected behavior
Should not throw an exception

Version:
2.5.1

@giacomelli giacomelli self-assigned this Jun 12, 2019
@giacomelli giacomelli added the bug label Jun 12, 2019
@giacomelli
Copy link
Owner

giacomelli commented Jun 12, 2019

The error occurs inside the CreateChild method:

for (int i = 0; i < firstParent.Length && childGenesIndex < firstParent.Length; i++)
{
	AddChildGene(childGenes, ref childGenesIndex, firstParent.GetGene(i));
	AddChildGene(childGenes, ref childGenesIndex, secondParent.GetGene(i));
}

If childGenesIndex is in the last position and the first AddChildGene increment it, the IndexOutOfRangeException will happen in the second AddChildGene call.

I added a second check to fix the problem:

for (int i = 0; i < firstParent.Length && childGenesIndex < firstParent.Length; i++)
{
	AddChildGene(childGenes, ref childGenesIndex, firstParent.GetGene(i));

	// The childGenesIndes could be incremented by the previous AddChildGene call
	if (childGenesIndex < secondParent.Length)
		AddChildGene(childGenes, ref childGenesIndex, secondParent.GetGene(i));
}

A hotfix is on the way on to the AppVeyor, in a few minutes the 2.5.2 version will be available on NuGet.

Thanks for the bug report!

giacomelli added a commit that referenced this issue Jun 12, 2019
Fix #61 - AlternatingPositionCrossover throw IndexOutOfRangeException
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants