public void SortedSubsetOperatorBase_CleanOutSections_DeleteSectionsWith0Length(int sectionIndex1, int sectionIndex2) { var originalChromosome = SortedSubsetTestData.CreateChromosome(); var sections = new int[originalChromosome.Sections.Length + 2][]; var sourceIndex = 0; for (int targetIndex = 0; targetIndex < sections.Length; targetIndex++) { if (targetIndex != sectionIndex1 && targetIndex != sectionIndex2) { sections[targetIndex] = originalChromosome.Sections[sourceIndex]; sourceIndex++; } else { sections[targetIndex] = new int[0]; } } var testChromosome = new SortedSubsetChromosome(sections); var random = Substitute.For <IRandom>(); var parameterSet = new ParameterSet(); var conflictDetectors = new List <INeighborhoodConflictDetector>() { AllRightConflictDetector.Instance }; var operatorBase = new SortedSubsetOperatorBase(random, parameterSet, conflictDetectors); operatorBase.CleanOutSections(testChromosome); testChromosome.Sections.Should().BeEquivalentTo(originalChromosome.Sections); }
public void SortedSubsetOperatorBase_ConflictDetectedWithLeftNeighbor_ShouldReturn(int position, int?geneValue, bool expected) { var targetSection = new int[] { 2, 5, 7, 8, 10 }; var random = new FastRandom(DateTime.Now.Millisecond); var parameterSet = new ParameterSet(); var conflictDetectors = new List <INeighborhoodConflictDetector>() { new DifferentParityConflictDetector() }; var operatorBase = new SortedSubsetOperatorBase(random, parameterSet, conflictDetectors); var result = operatorBase.ConflictDetectedWithLeftNeighbor(targetSection, position, geneValue); result.Should().Be(expected); }
public void SubsetOperatorBase_FindNewPosition_ReturnPosition(int sectionIndex, int geneValue, int expected) { var chromosome = SortedSubsetTestData.CreateChromosome(); var random = Substitute.For <IRandom>(); var parameterSet = new ParameterSet(); var conflictDetectors = new List <INeighborhoodConflictDetector>() { AllRightConflictDetector.Instance }; var operatorBase = new SortedSubsetOperatorBase(random, parameterSet, conflictDetectors); var section = chromosome.Sections[sectionIndex]; var result = operatorBase.FindNewGenePosition(section, geneValue); result.Should().Be(expected); }
public void SortedSubsetOperatorBase_GetGenes_Return(int sectionIndex, int position, int count, int[] expected) { var chromosome = SortedSubsetTestData.CreateChromosome(); var random = Substitute.For <IRandom>(); var parameterSet = new ParameterSet(); var conflictDetectors = new List <INeighborhoodConflictDetector>() { AllRightConflictDetector.Instance }; var operatorBase = new SortedSubsetOperatorBase(random, parameterSet, conflictDetectors); var result = operatorBase.GetGenes(chromosome, sectionIndex, position, count); result.Should().BeEquivalentTo(expected); }
public void SortedSubsetOperatorBase_CountInsertableGenes_Return(int sectionIndex, int insertPosition, int firstGeneIndex, int expected) { var chromosome = SortedSubsetTestData.CreateChromosome(); var genesToInsert = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; var random = new FastRandom(DateTime.Now.Millisecond); var parameterSet = new ParameterSet(); var conflictDetectors = new List <INeighborhoodConflictDetector>() { AllRightConflictDetector.Instance }; var operatorBase = new SortedSubsetOperatorBase(random, parameterSet, conflictDetectors); var result = operatorBase.CountInsertableGenes(chromosome, sectionIndex, insertPosition, genesToInsert, firstGeneIndex); result.Should().Be(expected); }
public void SortedSubsetOperatorBase_InsertGenesAtPositionWithConflict_ShouldReturns(bool leftConflict, bool rightConflict, bool expectedSuccess) { var geneValuesToInsert = new int[] { 10, 11, 12, 13 }; var chromosome = SortedSubsetTestData.CreateChromosome(); var random = Substitute.For <IRandom>(); var parameterSet = new ParameterSet(); var conflictDetectors = new List <INeighborhoodConflictDetector>() { new PredeterminedConflictDetector(leftConflict, rightConflict) }; var operatorBase = new SortedSubsetOperatorBase(random, parameterSet, conflictDetectors); var success = operatorBase.InsertGenes(chromosome, 0, 1, geneValuesToInsert, 0, 1); success.Should().Be(expectedSuccess); }
public void SortedSubsetOperatorBase_InsertGenesAtPosition_ShouldInsert(int sectionIndex, int insertPosition, int firstGeneIndex, int count) { var geneValuesToInsert = new int[] { 10, 11, 12, 13 }; var chromosome = SortedSubsetTestData.CreateChromosome(); var clone = chromosome.DeepClone(); var random = Substitute.For <IRandom>(); var parameterSet = new ParameterSet(); var conflictDetectors = new List <INeighborhoodConflictDetector>() { AllRightConflictDetector.Instance }; var operatorBase = new SortedSubsetOperatorBase(random, parameterSet, conflictDetectors); operatorBase.InsertGenes(chromosome, sectionIndex, insertPosition, geneValuesToInsert, firstGeneIndex, count); chromosome.Sections[sectionIndex].Length.Should().Be(clone.Sections[sectionIndex].Length + count); chromosome.Sections[sectionIndex][insertPosition].Should().Be(geneValuesToInsert[firstGeneIndex]); chromosome.Sections[sectionIndex][insertPosition + count - 1].Should().Be(geneValuesToInsert[firstGeneIndex + count - 1]); }
public void GivenSortedSubsetOperatorBase_WhenDeleteSection_ThenShouldDelete(int sectionIndex) { var chromosome = SortedSubsetTestData.CreateChromosome(); var clone = chromosome.DeepClone(); var random = Substitute.For <IRandom>(); var parameterSet = new ParameterSet(); var conflictDetectors = new List <INeighborhoodConflictDetector>() { AllRightConflictDetector.Instance }; var operatorBase = new SortedSubsetOperatorBase(random, parameterSet, conflictDetectors); operatorBase.DeleteSection(chromosome, sectionIndex); chromosome.Sections.GetLength(0).Should().Be(2); if (sectionIndex < clone.Sections.GetLength(0) - 1) { chromosome.Sections[sectionIndex].Should().BeEquivalentTo(clone.Sections[sectionIndex + 1]); } }
public void GivenSortedSubsetOperatorBase_WhenDeleteGenes_ThenShouldDelete(int sectionIndex, int position, int count) { var chromosome = SortedSubsetTestData.CreateChromosome(); var clone = chromosome.DeepClone(); var random = Substitute.For <IRandom>(); var parameterSet = new ParameterSet(); var conflictDetectors = new List <INeighborhoodConflictDetector>() { AllRightConflictDetector.Instance }; var operatorBase = new SortedSubsetOperatorBase(random, parameterSet, conflictDetectors); operatorBase.DeleteGenesFromSection(chromosome, sectionIndex, position, count); chromosome.Sections[sectionIndex].Length.Should().Be(clone.Sections[sectionIndex].Length - count); if (position + count < clone.Sections[sectionIndex].Length) { chromosome.Sections[sectionIndex][position].Should().Be(clone.Sections[sectionIndex][position + count]); } }