static void Main(string[] args) { //真实数据集 //string[] filepath = { "../Data/RealData/S1_500.txt", "../Data/RealData/S2_500.txt", "../Data/RealData/S3_500.txt", "../Data/RealData/S4_500.txt", "../Data/RealData/S5_500.txt" };//数据集规模500 string[] filepath = { "../Data/RealData/S1_400.txt", "../Data/RealData/S2_400.txt", "../Data/RealData/S3_400.txt", "../Data/RealData/S4_400.txt", "../Data/RealData/S5_400.txt" };//数据集规模400 //string[] filepath = { "../Data/RealData/S1_300.txt", "../Data/RealData/S2_300.txt", "../Data/RealData/S3_300.txt", "../Data/RealData/S4_300.txt", "../Data/RealData/S5_300.txt" };//数据集规模300 //string[] filepath = { "../Data/RealData/S1_200.txt", "../Data/RealData/S2_200.txt", "../Data/RealData/S3_200.txt", "../Data/RealData/S4_200.txt", "../Data/RealData/S5_200.txt" };//数据集规模200 //string[] filepath = { "../Data/RealData/S1_100.txt", "../Data/RealData/S2_100.txt", "../Data/RealData/S3_100.txt", "../Data/RealData/S4_100.txt", "../Data/RealData/S5_100.txt" };//数据集规模100 //随机数据集 //string[] filepath = { "../Data/Random/S1_500.txt", "../Data/Random/S2_500.txt", "../Data/Random/S3_500.txt", "../Data/Random/S4_500.txt", "../Data/Random/S5_500.txt" };//数据集规模500 //string[] filepath = { "../Data/Random/S1_400.txt", "../Data/Random/S2_400.txt", "../Data/Random/S3_400.txt", "../Data/Random/S4_400.txt", "../Data/Random/S5_400.txt" };//数据集规模400 //string[] filepath = { "../Data/Random/S1_300.txt", "../Data/Random/S2_300.txt", "../Data/Random/S3_300.txt", "../Data/Random/S4_300.txt", "../Data/Random/S5_300.txt" };//数据集规模300 //string[] filepath = { "../Data/Random/S1_200.txt", "../Data/Random/S2_200.txt", "../Data/Random/S3_200.txt", "../Data/Random/S4_200.txt", "../Data/Random/S5_200.txt" };//数据集规模200 //string[] filepath = { "../Data/Random/S1_100.txt", "../Data/Random/S2_100.txt", "../Data/Random/S3_100.txt", "../Data/Random/S4_100.txt", "../Data/Random/S5_100.txt" };//数据集规模100 GA_Server bestserver = new GA_Server(); List <GA_Server> scrlist = new List <GA_Server>(); List <Server>[] wlist = new List <Server> [ConstNum.PARTICE_DIM]; double fit = 0, totaltime = 0; wlist = GetData.splitedatafromfile(filepath, ConstNum.PARTICE_DIM); Timing dobj = new Timing(); int sum = 0; double totalfit = 0; double RMSE = 0; double[] ttfit = new double[101]; for (int i = 0; i < 100; i++) { scrlist = GA_Server.GetinitGA_server(wlist); dobj.startTime(); bestserver = GA.GetBest(scrlist, wlist, ref fit); dobj.StopTime(); totaltime += dobj.Result().Milliseconds; totaltime += dobj.Result().Seconds * 1000; totalfit += fit; ttfit[i] = fit; if (Math.Abs(fit - 0.5608) <= 0.0005)//QWS--500,400 { sum++; } //if (Math.Abs(fit - 0.6835) <= 0.0005)//QWS-300 //{ // sum++; //} //if (Math.Abs(fit - 0.7802) <= 0.0005)//QWS-200 //{ // sum++; //} //if (Math.Abs(fit - 0.8733) <= 0.0005)//QWS-100 //{ // sum++; //} //if (Math.Abs(fit - 1.4482) <= 0.0005)//RWS--500 //{ // sum++; //} //if (Math.Abs(fit - 1.4569) <= 0.0005)//RWS--400 //{ // sum++; //} //if (Math.Abs(fit - 1.4742) <= 0.0005)//RWS--300 //{ // sum++; //} //if (Math.Abs(fit - 1.5292) <= 0.0005)//RWS--200 //{ // sum++; //} //if (Math.Abs(fit - 1.5711) <= 0.0005)//RWS--100 //{ // sum++; //} Console.WriteLine("Best fit={0}", fit); Console.WriteLine("The Best combination is {0},{1},{2},{3},{4}", bestserver.getIndextask(0), bestserver.getIndextask(1), bestserver.getIndextask(2), bestserver.getIndextask(3), bestserver.getIndextask(4)); Console.WriteLine("The time cost is {0}ms,{1}s,********{2}", dobj.Result().Milliseconds, dobj.Result().Seconds, sum); } //求均方根误差 for (int i = 0; i < 100; i++) { RMSE += (ttfit[i] - totalfit / 100) * (ttfit[i] - totalfit / 100); } RMSE = RMSE / 100; RMSE = Math.Sqrt(RMSE); //Console.WriteLine("Average is {0},time is {1},RMSE is {2},*******{3}", totalfit/ 100, totaltime / 100,RMSE,(double)sum/100); Console.WriteLine("Average is {0}", totalfit / 100); Console.WriteLine("time is {0}", totaltime / 100); Console.WriteLine("RMSE is {0}", RMSE); Console.WriteLine("*******{0}", (double)sum / 100); Console.ReadKey(); }
public static GA_Server GetBest(List <GA_Server> scrlist, List <Server>[] wlist, ref double fit)//scrlist表示初始种群,wlist表示子服务集 { GA_Server globalbest = new GA_Server(); double bestfit = 100; Random rad = new Random(); List <GA_Server> dlist = new List <GA_Server>(); for (int i = 0; i < scrlist.Count; i++) { dlist.Add(new GA_Server()); } for (int t = 0; t < ConstNum.T; t++) { for (int i = 0; i < scrlist.Count; i++)//求每个粒子的适应度 { scrlist[i].fitness(wlist); } for (int i = 0; i < scrlist.Count; i++)//保留每次进化产生的最优粒子 { if (bestfit > scrlist[i].Fit) { bestfit = scrlist[i].Fit; int[] a = new int[ConstNum.PARTICE_DIM]; for (int k = 0; k < ConstNum.PARTICE_DIM; k++) { a[k] = scrlist[i].getIndextask(k); } globalbest.setservers(a); } } //选择 List <GA_Server> temp = new List <GA_Server>(); for (int i = 0; i < ConstNum.NP; i++) { temp.Add(new GA_Server()); } double tfit = 0; //scrlist=GA.GA_Sort_fitness(scrlist); for (int i = 0; i < ConstNum.NP; i++) { int[] a = new int[ConstNum.PARTICE_DIM]; for (int k = 0; k < ConstNum.PARTICE_DIM; k++) { a[k] = scrlist[i].getIndextask(k); } temp[i].setservers(a); temp[i].Fit = 1 / scrlist[i].Fit; tfit += temp[i].Fit; } double tt = 0; for (int i = 0; i < ConstNum.NP; i++) //两个for循环,轮盘赌选择 { tt += temp[i].Fit; temp[i].Fit = tt / tfit; } for (int i = 0; i < ConstNum.NP; i++) { double po; int pos = 0, j; po = rad.NextDouble(); for (j = 0; j < ConstNum.NP - 1; j++) { if (po < temp[j].Fit && j == 0) { pos = j; } else { if (po >= temp[j].Fit && po < temp[j + 1].Fit) { pos = j + 1; break; } } } int[] a = new int[ConstNum.PARTICE_DIM]; for (int k = 0; k < ConstNum.PARTICE_DIM; k++) { a[k] = temp[pos].getIndextask(k); } dlist[i].setservers(a); dlist[i].Fit = scrlist[pos].Fit; } //交叉:随机选择父体,进行交叉 //GA_Sort_fitness(scrlist); int[] CrossoverNum = new int[(int)(ConstNum.crossover * ConstNum.NP)]; for (int i = 0; i < CrossoverNum.Length; i++) { int rad1, rad2, rad3;//rad1表示父本1;rad2表示父本2;rad3交叉位置 while (true) { rad1 = rad.Next(0, dlist.Count); rad2 = rad.Next(0, dlist.Count); if (rad1 != rad2) { break; } } rad3 = rad.Next(0, ConstNum.PARTICE_DIM); int a = 0; for (int j = rad3; j < ConstNum.PARTICE_DIM; j++) { a = dlist[rad1].getIndextask(j); dlist[rad1].setIndextask(j, dlist[rad2].getIndextask(j)); dlist[rad2].setIndextask(j, a); } } //变异 for (int i = 0; i < (int)(ConstNum.Mutation * scrlist.Count); i++) { int rd1, rd2, rd3;//rd1表示待变异的算子编号;rd2表示变异的位置;rd3表示变异结果 rd1 = rad.Next(0, scrlist.Count); rd2 = rad.Next(0, ConstNum.PARTICE_DIM); rd3 = rad.Next(0, wlist[rd2].Count); dlist[rd1].setIndextask(rd2, rd3); } //新种群产生 for (int i = 0; i < scrlist.Count; i++) { int[] a = new int[ConstNum.PARTICE_DIM]; for (int k = 0; k < ConstNum.PARTICE_DIM; k++) { a[k] = dlist[i].getIndextask(k); } scrlist[i].setservers(a); } } fit = bestfit; return(globalbest); }