private void CreateNewPairIndividuals(int indexTo, Individual firstIndividual, Individual secondIndividual) { for (int i = 0; i < indexTo; i++) { int secondIndividualPermutationElementAtCurrentIndexI = secondIndividual.Permutation.ElementAt(i); int[] hybridizationTmpArray = new int[indexTo]; Array.Copy(firstIndividual.Permutation, hybridizationTmpArray, indexTo); if (WasHere(hybridizationTmpArray, secondIndividualPermutationElementAtCurrentIndexI)) { Permutator.Swap(firstIndividual.Permutation, i, FindThisNumberInArray(firstIndividual.Permutation, secondIndividualPermutationElementAtCurrentIndexI)); Permutator.SwapBeetweenArrays(firstIndividual.Permutation, secondIndividual.Permutation, i); } } }
private void CreateNewRandomPopulation() { #region Create new defualt array {0,1,2,3,4,5, ... , dimension-1} int[] defaultArray = new int[Dimension]; for (int i = 0; i < Dimension; i++) { defaultArray[i] = i; } #endregion for (int i = 0; i < POPULATION_SIZE; i++) { individuals.ElementAt(i).Permutation = Permutator.GetRandomPermutation(defaultArray); } }
private void DoMutation() { for (int i = 0; i < POPULATION_SIZE; i++) { Individual tmp = individuals.ElementAt(i); for (int j = 0; j < Dimension; j++) { int randomNumber = random.Next(MAX_PROBABILITY); if (MUTATION_PROBABILITY > randomNumber) { int randomIndex = random.Next(Dimension); while (j == randomIndex) { randomIndex = random.Next(Dimension); } //MUTATE Permutator.Swap(tmp.Permutation, j, randomIndex); tmp.Cost = CostCounter.CountCost(tmp.Permutation); } } } }
public void RandomPermutationAlgorythm() { int arraySize = dataFileReader.myObject.Dimension; int[] bestIndexesArray = new int[arraySize]; for (int i = 0; i < arraySize; i++) { bestIndexesArray[i] = i; } ExcelWorker excel = new ExcelWorker("Random alg"); int generationCounter = 1; while (generationCounter < MAX_COUNTER_VALUE) { int[] tmp = Permutator.GetRandomPermutation(bestIndexesArray); int tmpCost = CountCost(tmp); int mainCost = CountCost(bestIndexesArray); if (tmpCost < mainCost) { Array.Copy(tmp, bestIndexesArray, arraySize); } excel.AddCellToWorksheetIntoColumnsAB(generationCounter++, CountCost(bestIndexesArray)); } #region Show result string bestResult = ""; foreach (var item in bestIndexesArray) { bestResult += item.ToString() + "; "; } textBoxTotalCost.Text = bestResult; #endregion }