Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Evolution {
- private int n;
- private int numberOfFFE = 0;
- private Data data;
- private Utils ut;
- private int bestScore = 10000;
- private int popsize;
- private float crossprob, mutationprob;
- private HashMap<int[], Integer> bufferedScores = new HashMap<>(10000);
- private ArrayList<int[]> generation = new ArrayList<>();
- private int crossIndex;
- private PrintWriter pw;
- private int number;
- public Evolution(Data data, int pop_size, float cross_prob, float mutation_prob, int crossIndex){
- this.data = data;
- n = data.getN();
- popsize = pop_size;
- crossprob = cross_prob;
- mutationprob = mutation_prob;
- ut = new Utils();
- this.crossIndex = crossIndex;
- }
- void getFirstGeneration(){
- generation = new ArrayList<>();
- int count = popsize;
- while (count > 0) {
- int [] perm = ut.randomPerm(n);
- generation.add(perm);
- int fitness = data.scoreFunction(perm);
- if(fitness < bestScore) bestScore = fitness;
- bufferedScores.put(perm, fitness);
- numberOfFFE++;
- count--;
- }
- printGeneration();
- System.out.println("First generation created");
- System.out.println("FFE calls: "+numberOfFFE+" , best score: "+bestScore);
- }
- private void evolve(){
- bestScore = Collections.min(bufferedScores.values());
- numberOfFFE = 0;
- for(int i=0; i<popsize/2; i++){
- cross(generation.get(i), generation.get(popsize/2+i));
- }
- pw.print(++number +","+numberOfFFE+","+bestScore+"\n");
- System.out.println("Liczba FFE: "+numberOfFFE+", cache size: " + bufferedScores.size()+", best fitness: "+bestScore);
- printGeneration();
- }
- private void cross(int[] parent1, int[] parent2) {
- HashMap<Integer, int[]> candidates = new HashMap<>();
- candidates.put(data.scoreFunction(parent1), parent1);
- candidates.put(data.scoreFunction(parent2), parent2);
- if(Main.n.random(1) < crossprob) {
- StringBuilder sb = new StringBuilder();
- crossIndex = new Random().nextInt(n);
- int[] child1 = new int[parent1.length];
- System.arraycopy(parent1, 0, child1, 0, crossIndex);
- System.arraycopy(parent2, crossIndex, child1, crossIndex, child1.length - crossIndex);
- validate(child1);
- mutate(child1);
- candidates.put(data.scoreFunction(child1), child1);
- sb.append(data.scoreFunction(parent1)).append(" ").append(data.scoreFunction(parent2)).append(" ").append(data.scoreFunction(child1)).append(" ");
- if (!bufferedScores.containsKey(child1)) {
- int fitness = data.scoreFunction(child1);
- if(fitness < bestScore) bestScore = fitness;
- bufferedScores.put(child1, fitness);
- numberOfFFE++;
- }
- int[] child2 = new int[parent1.length];
- System.arraycopy(parent2, 0, child2, 0, crossIndex);
- System.arraycopy(parent1, crossIndex, child2, crossIndex, child2.length - crossIndex);
- validate(child2);
- mutate(child2);
- sb.append(data.scoreFunction(child2));
- if (!bufferedScores.containsKey(child2)) {
- int fitness = data.scoreFunction(child2);
- if(fitness < bestScore) bestScore = fitness;
- bufferedScores.put(child2, data.scoreFunction(child2));
- numberOfFFE++;
- }
- candidates.put(data.scoreFunction(child2), child2);
- int bestOfFour = Collections.min(candidates.keySet());
- generation.set(generation.indexOf(parent1), candidates.get(bestOfFour));
- candidates.remove(bestOfFour);
- bestOfFour = Collections.min(candidates.keySet());
- generation.set(generation.indexOf(parent2), candidates.get(bestOfFour));
- System.out.println(sb);
- }
- }
- private void printGeneration(){
- for(int[] x : generation){
- for(int z : x) {
- System.out.print(z+" ");
- }
- System.out.println(" : "+data.scoreFunction(x));
- }
- }
- private void mutate(int[] perm) {
- if (Main.n.random(1) < mutationprob) {
- int index1 = Main.n.random(perm.length);
- int index2 = Main.n.random(perm.length);
- int temp = perm[index1];
- perm[index1] = perm[index2];
- perm[index2] = temp;
- }
- }
- private void validate(int[] perm){
- ArrayList<Integer> list = new ArrayList<>(ut.getSet(n));
- for (int i=0; i<perm.length; i++) {
- if(list.contains(perm[i])) list.remove(new Integer(perm[i]));
- else {
- perm[i] = list.get(0);
- list.remove(0);
- }
- }
- }
- void startEvolution(int iterations) {
- try {
- pw = new PrintWriter(new FileWriter("res.txt"));
- pw.print("Generation,FFE calls,Generation best\n");
- while (iterations>0) {
- evolve();
- iterations--;
- }
- pw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement