public static Result GreedMutate(Result item) { Result result = new Result() { SequenceIndexes = item.SequenceIndexes.ToArray() }; if (StaticRandom.Rand() % 2 == 0) { return(MutationLogic.Mutate(item)); } var nucleotidIndexes = EvaluationLogic.GetWeakConnectedNucleotidIndexes(item).OrderByDescending(n => n.Item2).ToArray(); if (nucleotidIndexes.Length >= 10) { int firstIndex = StaticRandom.Rand(10); int secondIndex = StaticRandom.Rand(Global.Nucleotids.Count); int tmp = result.SequenceIndexes[firstIndex]; result.SequenceIndexes[firstIndex] = result.SequenceIndexes[secondIndex]; result.SequenceIndexes[secondIndex] = tmp; } else { return(MutationLogic.Mutate(item)); } return(result); }
public static Result Cross(Result item1, Result item2) { Result result = new Result() { SequenceIndexes = new int[item1.SequenceIndexes.Length] }; int crossingPoint = StaticRandom.Rand((int)Math.Floor(item1.SequenceIndexes.Length * 0.1), (int)Math.Floor(item1.SequenceIndexes.Length * 0.9)); if (StaticRandom.Rand() % 2 == 0) { FillBeginning(ref result, item1.SequenceIndexes, crossingPoint); FillEnding(ref result, item2.SequenceIndexes, crossingPoint); return(result); } else { FillBeginning(ref result, item1.SequenceIndexes.Reverse().ToArray(), crossingPoint); FillEnding(ref result, item2.SequenceIndexes.Reverse().ToArray(), crossingPoint); result.SequenceIndexes = result.SequenceIndexes.Reverse().ToArray(); return(result); } }
public static Result GenerateRandomSolution() { int[] result = new int[Global.Nucleotids.Count]; for (int i = 0; i < result.Length; i++) { result[i] = i + 1; } return(new Result() { SequenceIndexes = result.OrderBy(i => StaticRandom.Rand()).ToArray() }); }
public static Result Mutate(Result item) { Result result = new Result() { SequenceIndexes = item.SequenceIndexes.ToArray() }; int index1 = StaticRandom.Rand(result.SequenceIndexes.Length - 1); int index2 = StaticRandom.Rand(result.SequenceIndexes.Length - 1); int tmp = result.SequenceIndexes[index1]; result.SequenceIndexes[index1] = result.SequenceIndexes[index2]; result.SequenceIndexes[index2] = tmp; return(result); }
public static Result Cross2Points(Result item1, Result item2) { Result result = new Result() { SequenceIndexes = new int[item1.SequenceIndexes.Length] }; int crossingPoint1 = StaticRandom.Rand((int)(item1.SequenceIndexes.Length * 0.6)); int crossingPoint2 = StaticRandom.Rand(crossingPoint1, (int)(item1.SequenceIndexes.Length)); FillSequenceBetweenPoints(result, item1.SequenceIndexes, crossingPoint1, crossingPoint2); List <int> order = GetFillOrderFromSequence(result, item2.SequenceIndexes, crossingPoint2); FillResultFromOrder(result, crossingPoint1, crossingPoint2, order); return(result); }
public static Result GenerateGreedySolution() { IList <int> sequence = new List <int>(); IList <int> notUsed = Enumerable.Range(0, Global.Nucleotids.Count).ToList(); int starting = StaticRandom.Rand(Global.Nucleotids.Count - 1); sequence.Add(starting); notUsed.Remove(starting); while (sequence.Count != Global.Nucleotids.Count) { var current = sequence.Last(); var best = notUsed.Select(i => new Tuple <int, int>(i, EvaluationLogic.GetSinglePartialSum(Global.Nucleotids[current], Global.Nucleotids[i]))).OrderBy(i => i.Item2).First().Item1; sequence.Add(best); notUsed.Remove(best); } return(new Result() { SequenceIndexes = sequence.Select(i => i + 1).ToArray() }); }
public static void Execute(ref Result[] input, int tournamentSize, int singleTournamentSize = 4) { var slicedInput = input.Take(tournamentSize).ToArray(); Result[] randomOrder = slicedInput.OrderBy(s => StaticRandom.Rand()).ToArray(); Result[] results = new Result[slicedInput.Length / singleTournamentSize]; for (int i = 0; i < slicedInput.Length - 1; i += singleTournamentSize) { var bestResult = randomOrder[i]; for (int j = 1; j < singleTournamentSize; j++) { if (randomOrder[i + j].EvaluationPoints > bestResult.EvaluationPoints || (randomOrder[i + j].EvaluationPoints == bestResult.EvaluationPoints && randomOrder[i + j].TotalLength < bestResult.TotalLength) || (randomOrder[i + j].EvaluationPoints == bestResult.EvaluationPoints && EvaluationLogic.GetWeakConnectedNucleotidIndexes(bestResult).Count > EvaluationLogic.GetWeakConnectedNucleotidIndexes(randomOrder[i + j]).Count)) { bestResult = randomOrder[i + j]; } } results[i / singleTournamentSize] = bestResult; } for (int i = 0; i < slicedInput.Length / singleTournamentSize; i++) { input[i] = results[i]; } }