private void getTwoParent(double[] fitlst) { //确定两个父代精英 //找出fitlst的最大值对应的index int laIndex = 0; double lav = fitlst[0]; for (int i = 0; i < fitlst.Length; i++) { if (lav < fitlst[i]) { lav = fitlst[i]; laIndex = i; } } parent1 = new chrom(Chroms[laIndex].args, _ub, _lb, _gn); //找出次大值 int laIndex2 = 0; double lav2 = fitlst[0]; if (laIndex == 0) { laIndex2 = 1; lav2 = fitlst[1]; } for (int i = 0; i < fitlst.Length; i++) { if (lav2 < fitlst[i] && laIndex != i) { lav2 = fitlst[i]; laIndex2 = i; } } parent2 = new chrom(Chroms[laIndex2].args, _ub, _lb, _gn); //Console.WriteLine(lav +";"+lav2 ); }
//生成两个子代 private void geTwoChild(chrom p1, chrom p2) { //根据parent1和parent2进行交叉,然后各自变异,生成两个子代 double[] cdata1 = new double[_n]; double[] cdata2 = new double[_n]; Random rnd = new Random(); //以一定的概率进行交叉 if (rnd.NextDouble() <= _pr) { double r; for (int i = 0; i < _n; i++) { r = rnd.NextDouble(); cdata1[i] = r * p2.args[i] + (1 - r) * p1.args[i]; cdata2[i] = r * p1.args[i] + (1 - r) * p2.args[i]; } } else { cdata1 = p1.args; cdata2 = p2.args; } child1.args = cdata1; child2.args = cdata2; //以一定概率进行变异 if (rnd.NextDouble() <= _pm) { child1.toMut(k); child2.toMut(k); } }