JAVA代码改成C#代码
packagesimpleGa;publicclassAlgorithm{/*GA算法的参数*/privatestaticfinaldoubleuniformRate=0...
package simpleGa;
public class Algorithm {
/* GA 算法的参数 */
private static final double uniformRate = 0.5; //交叉概率
private static final double mutationRate = 0.015; //突变概率
private static final int tournamentSize = 5; //淘汰数组的大小
private static final boolean elitism = true; //精英主义
// 进化一个种群
public static Population evolvePopulation(Population pop) {
// 存放新一代的种群
Population newPopulation = new Population(pop.size(), false);
// 把最优秀的那个 放在第一个位置.
if (elitism) {
newPopulation.saveIndividual(0, pop.getFittest());
}
// Crossover population
int elitismOffset;
if (elitism) {
elitismOffset = 1;
} else {
elitismOffset = 0;
}
// Loop over the population size and create new individuals with
// crossover
for (int i = elitismOffset; i < pop.size(); i++) {
//随机选择两个 优秀的个体
Individual indiv1 = tournamentSelection(pop);
Individual indiv2 = tournamentSelection(pop);
//进行交叉
Individual newIndiv = crossover(indiv1, indiv2);
newPopulation.saveIndividual(i, newIndiv);
}
// Mutate population 突变
for (int i = elitismOffset; i < newPopulation.size(); i++) {
mutate(newPopulation.getIndividual(i));
}
return newPopulation;
} 展开
public class Algorithm {
/* GA 算法的参数 */
private static final double uniformRate = 0.5; //交叉概率
private static final double mutationRate = 0.015; //突变概率
private static final int tournamentSize = 5; //淘汰数组的大小
private static final boolean elitism = true; //精英主义
// 进化一个种群
public static Population evolvePopulation(Population pop) {
// 存放新一代的种群
Population newPopulation = new Population(pop.size(), false);
// 把最优秀的那个 放在第一个位置.
if (elitism) {
newPopulation.saveIndividual(0, pop.getFittest());
}
// Crossover population
int elitismOffset;
if (elitism) {
elitismOffset = 1;
} else {
elitismOffset = 0;
}
// Loop over the population size and create new individuals with
// crossover
for (int i = elitismOffset; i < pop.size(); i++) {
//随机选择两个 优秀的个体
Individual indiv1 = tournamentSelection(pop);
Individual indiv2 = tournamentSelection(pop);
//进行交叉
Individual newIndiv = crossover(indiv1, indiv2);
newPopulation.saveIndividual(i, newIndiv);
}
// Mutate population 突变
for (int i = elitismOffset; i < newPopulation.size(); i++) {
mutate(newPopulation.getIndividual(i));
}
return newPopulation;
} 展开
1个回答
2015-08-11
展开全部
楼主,这是片段,,,,,,从语法上,主要是String、数组、等的方法转换一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询