public static GAParameters CreateRandom() { Random r = RandomNumberGeneratorProvider.Instance; GAParameters g = new GAParameters(); g.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length); g.PopulationSizeIndex = r.Next(0, PopulationSize.Length); g.CrossoverRateIndex = r.Next(0, CrossoverRate.Length); g.MutationRateIndex = r.Next(0, MutationRate.Length); g.ElitismRateIndex = r.Next(0, ElitismRate.Length); g.GenerationGapIndex = r.Next(0, GenerationGap.Length); g.GAVariantIndex = r.Next(0, GAVariant.Length); g.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length); g.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length); g.MutationOperatorIndex = r.Next(0, MutationOperator.Length); g.TournamentSizeIndex = r.Next(0, TournamentSize.Length); g.SelectivePressureIndex = r.Next(0, SelectivePressure.Length); while (!g.IsValid()) { g.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length); g.PopulationSizeIndex = r.Next(0, PopulationSize.Length); g.CrossoverRateIndex = r.Next(0, CrossoverRate.Length); g.MutationRateIndex = r.Next(0, MutationRate.Length); g.ElitismRateIndex = r.Next(0, ElitismRate.Length); g.GenerationGapIndex = r.Next(0, GenerationGap.Length); g.GAVariantIndex = r.Next(0, GAVariant.Length); g.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length); g.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length); g.MutationOperatorIndex = r.Next(0, MutationOperator.Length); g.TournamentSizeIndex = r.Next(0, TournamentSize.Length); g.SelectivePressureIndex = r.Next(0, SelectivePressure.Length); } return(g); }
public static Tuple <GAParameters, GAParameters> Cross(GAParameters g1, GAParameters g2) { var r = RandomNumberGeneratorProvider.Instance; var prob = r.NextDouble(); if (prob < 0.05) { if (r.NextDouble() < 0.5) { g1.PopulationSizeIndex = r.Next(0, PopulationSize.Length); } else { g2.PopulationSizeIndex = r.Next(0, PopulationSize.Length); } } else if (prob < 0.1) { if (r.NextDouble() < 0.5) { g1.CrossoverRateIndex = r.Next(0, CrossoverRate.Length); } else { g2.CrossoverRateIndex = r.Next(0, CrossoverRate.Length); } } else if (prob < 0.15) { if (r.NextDouble() < 0.5) { g1.MutationRateIndex = r.Next(0, MutationRate.Length); } else { g2.MutationRateIndex = r.Next(0, MutationRate.Length); } } else if (prob < 0.20) { if (r.NextDouble() < 0.5) { g1.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length); } else { g2.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length); } } else if (prob < 0.25) { if (r.NextDouble() < 0.5) { g1.GAVariantIndex = r.Next(0, GAVariant.Length); } else { g2.GAVariantIndex = r.Next(0, GAVariant.Length); } } else if (prob < 0.30) { if (r.NextDouble() < 0.5) { g1.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length); } else { g2.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length); } } else if (prob < 0.35) { if (r.NextDouble() < 0.5) { g1.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length); } else { g2.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length); } } else if (prob < 0.40) { if (r.NextDouble() < 0.5) { g1.MutationOperatorIndex = r.Next(0, MutationOperator.Length); } else { g2.MutationOperatorIndex = r.Next(0, MutationOperator.Length); } } else if (prob < 0.45) { if (r.NextDouble() < 0.5) { g1.TournamentSizeIndex = r.Next(0, TournamentSize.Length); } else { g2.TournamentSizeIndex = r.Next(0, TournamentSize.Length); } } else if (prob < 0.50) { if (r.NextDouble() < 0.5) { g1.ElitismRateIndex = r.Next(0, ElitismRate.Length); } else { g2.ElitismRateIndex = r.Next(0, ElitismRate.Length); } } else if (prob < 0.50) { if (r.NextDouble() < 0.5) { g1.GenerationGapIndex = r.Next(0, GenerationGap.Length); } else { g2.GenerationGapIndex = r.Next(0, GenerationGap.Length); } } else if (prob < 0.50) { if (r.NextDouble() < 0.5) { g1.SelectivePressureIndex = r.Next(0, SelectivePressure.Length); } else { g2.SelectivePressureIndex = r.Next(0, SelectivePressure.Length); } } while (!g1.IsValid() && !g2.IsValid()) { if (prob < 0.05) { if (r.NextDouble() < 0.5) { g1.PopulationSizeIndex = r.Next(0, PopulationSize.Length); } else { g2.PopulationSizeIndex = r.Next(0, PopulationSize.Length); } } else if (prob < 0.1) { if (r.NextDouble() < 0.5) { g1.CrossoverRateIndex = r.Next(0, CrossoverRate.Length); } else { g2.CrossoverRateIndex = r.Next(0, CrossoverRate.Length); } } else if (prob < 0.15) { if (r.NextDouble() < 0.5) { g1.MutationRateIndex = r.Next(0, MutationRate.Length); } else { g2.MutationRateIndex = r.Next(0, MutationRate.Length); } } else if (prob < 0.20) { if (r.NextDouble() < 0.5) { g1.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length); } else { g2.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length); } } else if (prob < 0.25) { if (r.NextDouble() < 0.5) { g1.GAVariantIndex = r.Next(0, GAVariant.Length); } else { g2.GAVariantIndex = r.Next(0, GAVariant.Length); } } else if (prob < 0.30) { if (r.NextDouble() < 0.5) { g1.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length); } else { g2.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length); } } else if (prob < 0.35) { if (r.NextDouble() < 0.5) { g1.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length); } else { g2.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length); } } else if (prob < 0.40) { if (r.NextDouble() < 0.5) { g1.MutationOperatorIndex = r.Next(0, MutationOperator.Length); } else { g2.MutationOperatorIndex = r.Next(0, MutationOperator.Length); } } else if (prob < 0.45) { if (r.NextDouble() < 0.5) { g1.TournamentSizeIndex = r.Next(0, TournamentSize.Length); } else { g2.TournamentSizeIndex = r.Next(0, TournamentSize.Length); } } else if (prob < 0.50) { if (r.NextDouble() < 0.5) { g1.ElitismRateIndex = r.Next(0, ElitismRate.Length); } else { g2.ElitismRateIndex = r.Next(0, ElitismRate.Length); } } else if (prob < 0.50) { if (r.NextDouble() < 0.5) { g1.GenerationGapIndex = r.Next(0, GenerationGap.Length); } else { g2.GenerationGapIndex = r.Next(0, GenerationGap.Length); } } else if (prob < 0.50) { if (r.NextDouble() < 0.5) { g1.SelectivePressureIndex = r.Next(0, SelectivePressure.Length); } else { g2.SelectivePressureIndex = r.Next(0, SelectivePressure.Length); } } } return(new Tuple <GAParameters, GAParameters>(g1, g2)); }