例如,我的初始输入任务设置为{1,2,3,4,5},然后我想创建三个不同的顺序,例如Gene1 = 1,3,5,42gene2 = 1,5,4,2,3和gene3 = 5,2,3,4,1这是三个不同顺序的任务,我想随机整理所有任务以产生三个不同顺序,不同顺序的任务对应不同的遗传密码,java中是否有此问题的示例或任何语言
您可以将初始输入任务集洗牌
以下代码显示了基因1,2,3,4的随机排列
List<Integer> all_genes = new ArrayList<Integer>();
all_genes.add(1);
all_genes.add(2);
all_genes.add(3);
all_genes.add(4);
for (int i = 0; i < 10; i++) {
List<Integer> temp_gene = new ArrayList<Integer>(all_genes);
java.util.Collections.shuffle(all_genes);
System.out.println(all_genes);
}
简单的排列GA将遵循以下简单过程:
虽然不满足停止条件。
如果对于解决方案中的所有位置x,置换问题的质量估计为x*gene[x]
。
我定义了两个类,第一个是GA本身,如下所示:
import java.util.ArrayList;
import java.util.List;
public class SimplePermuationGA {
public static void main(String[] args) {
// TODO code application logic here
int number_of_generations = 10;
int problem_size = 4;
int population_size = 30;
float crossover_probability = 0.7f;
float mutation_probability = 0.01f;
GAOperators operators = new GAOperators(population_size, problem_size, crossover_probability, mutation_probability);
ArrayList<ArrayList<Integer>> population = operators.getInitialSolutions();
for (int i = 0; i < number_of_generations; i++) {
ArrayList<ArrayList<Integer>> new_population = operators.performSelection(population);
population = (ArrayList<ArrayList<Integer>>) new_population.clone();
}
ArrayList<Integer> best_solution = population.get(0);
int best_fitness = operators.evaluationSolution(best_solution);
for (int i = 0; i < population_size; i++) {
ArrayList<Integer> solution = population.get(i);
int fitness = operators.evaluationSolution(solution);
if (best_fitness < fitness) {
best_fitness = fitness;
best_solution = (ArrayList<Integer>) solution.clone();
}
}
System.out.println(best_solution);
System.out.println(best_fitness);
}
}
第二类包含所有遗传算子(即选择,交叉和突变),并且还包含评估和初始化函数以简化操作。见下文:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class GAOperators {
int problem_size;
int population_size;
float crossover_probability;
float mutation_probability;
Random r;
public GAOperators(int population_size, int problem_size, float crossover_probability, float mutation_probability) {
this.problem_size = problem_size;
this.population_size = population_size;
this.crossover_probability = crossover_probability;
this.mutation_probability = mutation_probability;
r = new Random();
}
public ArrayList<ArrayList<Integer>> getInitialSolutions() {
ArrayList<Integer> all_genes = new ArrayList<>();
for (int i = 0; i < problem_size; i++) {
all_genes.add(i);
}
ArrayList<ArrayList<Integer>> population = new ArrayList<>();
for (int i = 0; i < population_size; i++) {
List<Integer> temp_gene = new ArrayList<>(all_genes);
java.util.Collections.shuffle(all_genes);
population.add(all_genes);
}
return population;
}
public ArrayList<ArrayList<Integer>> performSelection(ArrayList<ArrayList<Integer>> population)
{
ArrayList<ArrayList<Integer>> new_population = new ArrayList<>();
for (int i = 0; i < population_size; i++) {
ArrayList<ArrayList<Integer>> parents = new ArrayList<>();
for (int j = 0; j < 2; j++) {
ArrayList<Integer> parent_temp1 = population.get(r.nextInt(population_size));
ArrayList<Integer> parent_temp2 = population.get(r.nextInt(population_size));
int fitness_1 = this.evaluationSolution(parent_temp1);
int fitness_2 = this.evaluationSolution(parent_temp2);
ArrayList<Integer> parent = parent_temp1;
if (fitness_1 < fitness_2) {
parent = parent_temp2;
}
parents.add(parent);
}
ArrayList<Integer> offspring = new ArrayList<>();
if(r.nextFloat() < crossover_probability)
offspring = this.performCrossover(parents.get(0), parents.get(1));
else
offspring = parents.get(0);
if(r.nextFloat() < mutation_probability)
offspring = this.performMutation(offspring);
new_population.add((ArrayList<Integer>) offspring.clone());
}
return new_population;
}
public int evaluationSolution(ArrayList<Integer> Solution){
int fitness = 0;
for (int i = 0; i < Solution.size(); i++) {
fitness+=(i*Solution.get(i));
}
return fitness;
}
public ArrayList<Integer> performCrossover(ArrayList<Integer> Parent1, ArrayList<Integer> Parent2){
ArrayList<Integer> crossedList = new ArrayList<Integer>();
int crossover_point = 0;
while (crossover_point == 0) {
crossover_point = r.nextInt(Parent1.size());
}
for (int i = 0; i < crossover_point; i++) {
crossedList.add(Parent1.get(i));
}
for (int i = 0; i < Parent2.size(); i++) {
if (!crossedList.contains(Parent2.get(i))) {
crossedList.add(Parent2.get(i));
}
}
return crossedList;
}
public ArrayList<Integer> performMutation(ArrayList<Integer> Solution){
ArrayList<Integer> mutatedList = new ArrayList<>();
int mutation_point = 0;
int insertion_point = 0;
while (mutation_point == 0) {
mutation_point = r.nextInt(Solution.size());
}
while (insertion_point == 0|| insertion_point == mutation_point) {
insertion_point = r.nextInt(Solution.size());
}
mutatedList.addAll(Solution);
int value_to_displace = Solution.get(mutation_point);
mutatedList.remove(mutation_point);
mutatedList.add(insertion_point, value_to_displace);
return mutatedList;
}
}
我希望这会有所帮助