public int partitionOnetarget(List <EA.Entity> population, int low, int high) { EA.Entity key = population[low]; int i = low; int j = high; while (i < j) { while (i < j) {// if (!(compareSingle(population[j], key) == 1 || compareSingle(population[j], key) == 0)) { break; } --j; } population[i] = population[j]; while (i < j) { if (!(compareSingle(population[i], key) == -1 || compareSingle(population[i], key) == 0)) { break; } ++i; } population[j] = population[i]; } population[i] = key; return(i); }
/* * 通过适应度值,计算A与B间支配关系 * 1:A占支配地位 * -1:B占支配地位 * 0:A=B * 11: 互相非支配 */ public int domain(EA.Entity A, EA.Entity B) { double epison = 0.000001; int i; HashSet <int> set = new HashSet <int>(); for (i = 0; i < objNum; i++) { if (Math.Abs(A.fitnessVec[i] - B.fitnessVec[i]) < epison) { set.Add(0); } else if (A.fitnessVec[i] < B.fitnessVec[i]) { set.Add(1); } else { set.Add(-1); } } ////判断支配关系 //if (A.EqualsEntity(B)) {// 两个个体相同 // return 0; //} //if (set.Contains(1) && !set.Contains(-1)) {// A支配B // return 1; //} //if (set.Contains(-1) & !set.Contains(1)) {// A被B支配 // return -1; //} //return 11;// A和B互相非支配。特别的,若A和B的各个指标相同(因为取浮点数后六位比较,概率极小),但校正系数不同,也认为互相非支配。 if (set.Contains(0) && set.Count() == 1) { return(0); } else if (!set.Contains(-1)) { return(1); } else if (!set.Contains(1)) { return(-1); } else { return(11); } }
//把S复制到D void copy(ref EA.Entity D, ref EA.Entity S) { for (int j = 0; j < objNum; j++) { D.fitnessVec[j] = S.fitnessVec[j]; } for (int i = 0; i < scen; i++) { for (int k = 0; k < coeNum; k++) { D.gen[i, k] = S.gen[i, k]; } } }
//x>y 1; x<y -1; x==y 0 public int compareSingle(EA.Entity x, EA.Entity y) { double epison = 0.000001; if (Math.Abs(x.fitnessVec[0] - y.fitnessVec[0]) < epison) { return(0); } if (x.fitnessVec[0] > y.fitnessVec[0]) { return(1); } return(-1); }
//对population[start..end]进行一趟快速排序, //将支配population[start]的个体或与population[start]不相关的个体存放到population[start..i-1]中, //将被population[start]支配的个体存放到population[i+1..end]中 public int quickPass(List <EA.Entity> population, int start, int end) { int i = start; int j = end; EA.Entity x = population[start]; bool non_domminated_sign = true; int flag; EA.Entity E; while (i < j) { flag = domain(x, E = population[j]); while ((i < j) && (flag == 1 || flag == 0)) { j--; if ((flag = domain(E = population[j], x)) == 1) { non_domminated_sign = false; } flag = domain(x, E = population[j]); } population[i] = population[j]; while (i < j && (flag = domain(E = population[i], x)) != -1) { i++; if ((flag = domain(E = population[i], x)) == 1) { non_domminated_sign = false; } } population[j] = population[i]; } population[i] = x; if (non_domminated_sign) { EA.Entity EE = new EA.Entity(); copy(ref EE, ref x); NDSet.Add(EE); } return(i); }
//层次分析法,从优胜集中选出一个最好个体 public EA.Entity AHP(int POPSIZE, double[] wight) { double best = Math.Pow(POPSIZE, objNum); int index = 0; double[] max = new double[objNum]; double[] min = new double[objNum]; for (int i = 0; i < NDSet.Count; i++) { for (int j = 0; j < objNum; j++) { if (max[j] < NDSet[i].fitnessVec[j]) { max[j] = NDSet[i].fitnessVec[j]; } if (min[j] > NDSet[i].fitnessVec[j]) { min[j] = NDSet[i].fitnessVec[j]; } } } double[] avg = new double[objNum]; for (int i = 0; i < objNum; i++) { avg[i] = (max[i] + min[i]) / 2; } for (int i = 0; i < NDSet.Count; i++) { NDSet[i].fitness = 0; for (int j = 0; j < objNum; j++) { NDSet[i].fitness += (NDSet[i].fitnessVec[j] / avg[j]) * wight[j]; } if (NDSet[i].fitness < best) { best = NDSet[i].fitness; index = i; } } EA.Entity Best = NDSet[index]; return(Best); }
//对population[start..end]进行一趟快速排序, //将支配population[start]的个体或与population[start]不相关的个体存放到population[start..i-1]中, //将被population[start]支配的个体存放到population[i+1..end]中 public int quickPass1(List <EA.Entity> population, int start, int end) { int i = start; int j = end; EA.Entity x = population[start]; while (i < j) { while (i < j && (domain(x, population[j]) == 1 || domain(x, population[j]) == 0)) { j--; } population[i] = population[j]; while (i < j && domain(population[i], x) != -1) { i++; } population[j] = population[i]; } population[i] = x; return(i); }
/* * 通过适应度值,计算A与B间支配关系 * 1:A占支配地位 * -1:B占支配地位 * 0:A=B * 11: 互相非支配 */ public int domain(EA.Entity A, EA.Entity B) { int i; HashSet <int> set = new HashSet <int>(); for (i = 0; i < objNum; i++) { if (A.fitnessVec[i] == B.fitnessVec[i]) { set.Add(0); } else if (A.fitnessVec[i] < B.fitnessVec[i]) { set.Add(1); } else { set.Add(-1); } } if (set.Contains(0) && set.Count() == 1) { return(0); } else if (!set.Contains(-1)) { return(1); } else if (!set.Contains(1)) { return(-1); } else { return(11); } }