//变异 public void Mutation() { //double temp = V / 5; //int mut = (int)Math.Ceiling(temp); for (int i = 0; i < V; i++) { var p = ran.NextDouble(); if (p < pm) { encoded[ran.Next(0, V - 1)] = MyMaths.NextDouble(0, 10); } } }
public static void Crossover1(List <Chromosome> waitCrossover) { //cBest 不参与交叉 //waitCrossover.Sort(); //var cBest = waitCrossover.First(); //waitCrossover.RemoveAt(0); //需要交叉的染色体序号 List <int> needChromosomeIndexs = new List <int>(); for (int i = 0; i < waitCrossover.Count - 1; i++) { double p = MyMaths.NextDouble(0, 1); if (p < pc1) { needChromosomeIndexs.Add(i); } } //交叉数量不为偶数 if (needChromosomeIndexs.Count % 2 != 0) { needChromosomeIndexs.Add(waitCrossover.Count - 1); } //进行交叉,直至全部交叉完成 while (needChromosomeIndexs.Count != 0) { var index1 = needChromosomeIndexs[ran.Next(needChromosomeIndexs.Count)]; var index2 = needChromosomeIndexs[ran.Next(needChromosomeIndexs.Count)]; while (index1 == index2) { index2 = needChromosomeIndexs[ran.Next(needChromosomeIndexs.Count)]; } Chromosome chromosome = waitCrossover[index1]; chromosome.Cross(waitCrossover[index2]); needChromosomeIndexs.Remove(index1); needChromosomeIndexs.Remove(index2); } //加入cBest //waitCrossover.Add(cBest); }
public double GetFitness() { return(MyMaths.Fitness(GetDecoded())); }
public static void Crossover(List <Chromosome> waitCrossover) { //cBest 不参与交叉 waitCrossover.Sort(); var cBest = waitCrossover.First(); waitCrossover.RemoveAt(0); //Console.WriteLine("cbest:" + string.Join(",", cBest.GetDecoded())); // int[] seq = new int[V]; // seq = cBest.GetDecoded(); // for (int mm = 0; mm < V; mm++) // { // Console.WriteLine("基因" + mm + ":" + seq[mm]); // } //需要交叉的染色体序号 List <int> needChromosomeIndexs = new List <int>(); for (int i = 0; i < waitCrossover.Count - 1; i++) { double p = MyMaths.NextDouble(0, 1); if (p < pc) { needChromosomeIndexs.Add(i); } } //交叉数量不为偶数 if (needChromosomeIndexs.Count % 2 != 0) { needChromosomeIndexs.Add(waitCrossover.Count - 1); } Console.WriteLine("待交叉染色体:" + waitCrossover.Count); //进行交叉,直至全部交叉完成 while (needChromosomeIndexs.Count != 0) { var index1 = needChromosomeIndexs[ran.Next(needChromosomeIndexs.Count)]; var index2 = needChromosomeIndexs[ran.Next(needChromosomeIndexs.Count)]; while (index1 == index2) { index2 = needChromosomeIndexs[ran.Next(needChromosomeIndexs.Count)]; } Console.WriteLine("编号:" + index1 + "," + index2); // // Console.WriteLine("第一条"+ string.Join(",", waitCrossover[index1].encoded)); // Console.WriteLine("第二条"+ string.Join(",", waitCrossover[index2].encoded)); Console.WriteLine("第一条" + string.Join(",", waitCrossover[index1].GetDecoded())); Console.WriteLine("第二条" + string.Join(",", waitCrossover[index2].GetDecoded())); Console.WriteLine("Cross"); Chromosome chromosome = waitCrossover[index1]; chromosome.Cross(waitCrossover[index2]); // Console.WriteLine("交叉后第一条"+ string.Join(",", waitCrossover[index1].encoded)); // Console.WriteLine("交叉后第二条"+ string.Join(",", waitCrossover[index2].encoded)); Console.WriteLine("交叉后第一条" + string.Join(",", waitCrossover[index1].GetDecoded())); Console.WriteLine("交叉后第二条" + string.Join(",", waitCrossover[index2].GetDecoded())); needChromosomeIndexs.Remove(index1); needChromosomeIndexs.Remove(index2); } //加入cBest //Console.WriteLine("交叉后染色体:"+waitCrossover.Count); waitCrossover.Add(cBest); //Console.WriteLine("cbest:" + string.Join(",", waitCrossover.Last().GetDecoded())); //Console.WriteLine("总染色体:"+waitCrossover.Count); }