private void FitnessSort(GENE[] a, int st, int ed)//适应度排序 { if (st >= ed) { return; } int i = st, j = ed; GENE tmp = a[i]; while (i < j) { //while (i < j && CompareSmall(a[j].Fitness,tmp.Fitness)) while (i < j && a[j].Fitness >= tmp.Fitness) { j--; } if (i < j) { a[i] = a[j]; } //while (i < j && CompareSmall(tmp.Fitness,a[i].Fitness)) while (i < j && a[i].Fitness < tmp.Fitness) { i++; } if (i < j) { a[j] = a[i]; } } a[i] = tmp; FitnessSort(a, st, i - 1); FitnessSort(a, i + 1, ed); }
private void Replicate() { GENE[] NextPopulation = new GENE[PopulationSize]; //声明下一代种群 for (int i = 0; i < PopulationSize; i++) //对种群中每个个体进行声明 { NextPopulation[i] = new GENE(GENE.GeneLength); } double[] pi = new double[PopulationSize]; double FitnessSum = 0; //复制 FitnessSum = 0; FitnessSort(Population, 0, PopulationSize - 1); //采取“保留最佳精英策略”,将最优个体直接替代最差个体; //但最差个体仍有机会参与复制 NextPopulation[0] = Population[PopulationSize - 1]; //构造轮盘赌 for (int i = 0; i < PopulationSize; i++) { FitnessSum += Population[i].Fitness; } pi[0] = Population[0].Fitness / FitnessSum; for (int i = 1; i < PopulationSize; i++) { pi[i] = Population[i].Fitness / FitnessSum + pi[i - 1]; } Random rd = new Random(); for (int i = 1; i < PopulationSize; i++) { double tmp = rd.NextDouble(); int copy = 0; for (int j = 0; j < PopulationSize; j++) { if (tmp <= pi[j]) { copy = j; break; } } NextPopulation[i] = Population[copy]; } for (int i = 0; i < PopulationSize; i++) { Population[i] = NextPopulation[i]; } //调试 for (int i = 0; i < PopulationSize; i++) { Population[i].Fitness = FitnessFunc(Population[i]); } }
private double FitnessFunc(GENE x) //适应度函数,index为染色体编号 { var result = network.Compute(x.GeneSerial); //计算对应函数值 double tmp = 0; for (int i = 0; i < result.Length; i++) { tmp += result[i]; } return(tmp); }
public GA_12(string saveNetFile, int inputDimension, int outputDimension)//默认构造函数 { SaveNetFile = saveNetFile; InputDimension = inputDimension; OutputDimension = outputDimension; Good = 0.8; PopulationSize = 50; MutationProb = 0.001; GA_Iter_Max = 1000; Population = new GENE[PopulationSize]; BestPerson = new GENE[1000]; for (int i = 0; i < PopulationSize; i++) { Population[i] = new GENE(GENE.GeneLength); BestPerson[i] = new GENE(GENE.GeneLength); } }
public GA_12(int populationSize, double mutationProb, int ga_iter_Max, string saveNetFile, int inputDimension, int outputDimension, double good)//构造函数重载 { SaveNetFile = saveNetFile; InputDimension = inputDimension; OutputDimension = outputDimension; Good = good; PopulationSize = populationSize; MutationProb = mutationProb; GA_Iter_Max = ga_iter_Max; //CrossProb = crossProb; Population = new GENE[PopulationSize]; BestPerson = new GENE[1000]; for (int i = 0; i < PopulationSize; i++) { Population[i] = new GENE(GENE.GeneLength); BestPerson[i] = new GENE(GENE.GeneLength); } }
private double FitnessFunc(GENE x)//适应度函数 { double tmp = 0; for (int i = 0; i < GENE.GeneLength; i++) { tmp += x.GeneSerial[i]; } if (tmp > C) { return(0); } double tmp1 = 0; double alpha = 3.0; for (int i = 0; i < GENE.GeneLength; i++) { tmp1 += x.GeneSerial[i] * StockData[InveseIndex[i]][3]; } double tmp2 = C; double beta = 3.0; for (int i = 0; i < GENE.GeneLength; i++) { tmp2 -= x.GeneSerial[i]; } double tmp3 = 0; double gama = 1.0; for (int i = 0; i < GENE.GeneLength; i++) { tmp3 += x.GeneSerial[i] * abs(StockData[InveseIndex[i]][4] - EquityRatio) / EquityRatio; } return(alpha * tmp1 + beta * tmp2 - gama * tmp3); }
public GA_3(double[][] stockData, int[] inveseIndex) { InveseIndex = inveseIndex; C = 100; StockData = stockData; PopulationSize = 50; MutationProb = 0.001; GA_Iter_Max = 100; EquityRatio = 50.0; BestAnswer = new GENE[1000]; Population = new GENE[PopulationSize]; for (int i = 0; i < PopulationSize; i++) { Population[i] = new GENE(GENE.GeneLength); } for (int i = 0; i < 1000; i++) { BestAnswer[i] = new GENE(GENE.GeneLength); } }
public GA_3(double[][] stockData, int[] inveseIndex, double c, double equityRatio, int populationSize, double mutationPro, int ga_iter_max)//构造函数重载 { InveseIndex = inveseIndex; C = c; StockData = stockData; PopulationSize = populationSize; MutationProb = mutationPro; GA_Iter_Max = ga_iter_max; EquityRatio = equityRatio; Population = new GENE[PopulationSize]; BestAnswer = new GENE[1000]; for (int i = 0; i < PopulationSize; i++) { Population[i] = new GENE(GENE.GeneLength); } for (int i = 0; i < 1000; i++) { BestAnswer[i] = new GENE(GENE.GeneLength); } }