JAVA代码改成C#代码
//进行两个个体的交叉(暂且想象为makelove的过程吧)。交叉的概率为uniformRateprivatestaticIndividualcrossover(Indi...
// 进行两个个体的交叉 (暂且想象为make love的过程吧)。 交叉的概率为uniformRate
private static Individual crossover(Individual indiv1, Individual indiv2) {
Individual newSol = new Individual();
// 随机的从 两个个体中选择
for (int i = 0; i < indiv1.size(); i++) {
if (Math.random() <= uniformRate) {
newSol.setGene(i, indiv1.getGene(i));
} else {
newSol.setGene(i, indiv2.getGene(i));
}
}
return newSol;
}
// 突变个体。 突变的概率为 mutationRate
private static void mutate(Individual indiv) {
for (int i = 0; i < indiv.size(); i++) {
if (Math.random() <= mutationRate) {
// 生成随机的 0 或 1
byte gene = (byte) Math.round(Math.random());
indiv.setGene(i, gene);
}
}
}
// 随机选择一个较优秀的个体,用了进行交叉
private static Individual tournamentSelection(Population pop) {
// Create a tournament population
Population tournamentPop = new Population(tournamentSize, false);
//随机选择 tournamentSize 个放入 tournamentPop 中
for (int i = 0; i < tournamentSize; i++) {
int randomId = (int) (Math.random() * pop.size());
tournamentPop.saveIndividual(i, pop.getIndividual(randomId));
}
// 找到淘汰数组中最优秀的
Individual fittest = tournamentPop.getFittest();
return fittest;
}
} 展开
private static Individual crossover(Individual indiv1, Individual indiv2) {
Individual newSol = new Individual();
// 随机的从 两个个体中选择
for (int i = 0; i < indiv1.size(); i++) {
if (Math.random() <= uniformRate) {
newSol.setGene(i, indiv1.getGene(i));
} else {
newSol.setGene(i, indiv2.getGene(i));
}
}
return newSol;
}
// 突变个体。 突变的概率为 mutationRate
private static void mutate(Individual indiv) {
for (int i = 0; i < indiv.size(); i++) {
if (Math.random() <= mutationRate) {
// 生成随机的 0 或 1
byte gene = (byte) Math.round(Math.random());
indiv.setGene(i, gene);
}
}
}
// 随机选择一个较优秀的个体,用了进行交叉
private static Individual tournamentSelection(Population pop) {
// Create a tournament population
Population tournamentPop = new Population(tournamentSize, false);
//随机选择 tournamentSize 个放入 tournamentPop 中
for (int i = 0; i < tournamentSize; i++) {
int randomId = (int) (Math.random() * pop.size());
tournamentPop.saveIndividual(i, pop.getIndividual(randomId));
}
// 找到淘汰数组中最优秀的
Individual fittest = tournamentPop.getFittest();
return fittest;
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询