Advertisement
Guest User

Untitled

a guest
May 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7. import java.util.Stack;
  8.  
  9. public final class ASAGA {
  10.  
  11.     static Random random = new Random();
  12.    
  13.     static class Solution {
  14.         int[] values;
  15.        
  16.         public Solution(int v[]) {
  17.             values = v.clone();
  18.         }
  19.        
  20.         public float fitness() {
  21.             int err = 0;
  22.             for(int i=0; i<values.length; i++)
  23.                 err += Math.pow(values[i] - i, 2);
  24.            
  25.             if(err == 0)
  26.                 return 1.1f;
  27.        
  28.             return 1.0f / err;
  29.         }
  30.        
  31.         public Solution clone() {
  32.             return new Solution(this.values);
  33.         }
  34.        
  35.         public boolean compare(Solution x) {
  36.             for(int i=0; i<values.length; i++) {
  37.                 if(values[i] != x.values[i]) return false;
  38.             }
  39.             return true;
  40.         }
  41.     }
  42.  
  43.     public static int[] randomDistribuction(int n) {
  44.         Stack<Integer> numbers = new Stack<>();
  45.         for(int i=0; i<n; i++)
  46.             numbers.add(i);
  47.         int[] distribuction = new int[n];
  48.         Random r = new Random();
  49.         for(int i=0; i<n; i++) {
  50.             int valueIdx = r.nextInt(numbers.size());
  51.             distribuction[i] = numbers.get(valueIdx);
  52.             numbers.remove(valueIdx);
  53.         }
  54.         return distribuction;
  55.     }
  56.    
  57.     public static Solution mutation(Solution a) {
  58.         Solution mutated = a.clone();
  59.         int rate = random.nextInt(1) + 1;
  60.         for(int i=0; i<rate; i++) {
  61.             int posA = random.nextInt(mutated.values.length);
  62.             int posB = random.nextInt(mutated.values.length);
  63.             int tmp = mutated.values[posA];
  64.             mutated.values[posA] = mutated.values[posB];
  65.             mutated.values[posB] = tmp;
  66.         }
  67.         return mutated;
  68.     }
  69.    
  70.     public static Solution selection(List<Solution> population) {
  71.         Stack<Float> roulet = new Stack<>();
  72.         float fitnessAll = 0;
  73.         for(Solution x : population) {
  74.             fitnessAll += x.fitness();
  75.         }
  76.         float influence = 0;
  77.         for(int i=0; i<population.size(); i++) {
  78.        
  79.             influence += (population.get(i).fitness() / (fitnessAll * 1.0));
  80.             roulet.push(influence);
  81.         }
  82.         float s = random.nextFloat();
  83.         for(int i=0; i<roulet.size(); i++) {
  84.             if(s <= roulet.get(i)) {
  85.                 return population.get(i);
  86.             }
  87.         }
  88.         //
  89.         return population.get(population.size() - 1);
  90.     }
  91.    
  92.     public static Solution bestSolution(List<Solution> solutions) {
  93.         Solution best = null;
  94.         for(Solution x : solutions) {
  95.             if(best == null || x.fitness() > best.fitness())
  96.                 best = x;
  97.         }
  98.         return best;
  99.     }
  100.    
  101.     public static List<Solution> filter(List<Solution> population) {
  102.         float factor = random.nextFloat() * 0.2f + 0.1f;
  103.         int newSize = (int) Math.ceil(population.size() * factor);
  104.        
  105.         Solution bestSolution = bestSolution(population);
  106.        
  107.         List<Solution> newPopulation = new ArrayList<>();
  108.         newPopulation.add(bestSolution);
  109.        
  110.         population.remove(bestSolution);
  111.        
  112.         for(int i=0; i<newSize && population.size() > 0; i++) {
  113.             Solution s = selection(population);
  114.             newPopulation.add(s);
  115.             population.remove(s);
  116.         }
  117.    
  118.         return newPopulation;
  119.     }
  120.    
  121.    
  122.     public static Solution cross(List<Solution> population) {
  123.         if(population.size() < 2) return null;
  124.        
  125.         Solution a = selection(population);
  126.         population.remove(a);
  127.         Solution b = selection(population);
  128.         population.add(a);
  129.        
  130.         Solution c = a.clone();
  131.        
  132.         //
  133.         int x = random.nextInt(a.values.length);
  134.         int e = x;
  135.         while(true) {
  136.             c.values[x] = b.values[x];
  137.             for(int i=0; i<b.values.length; i++) {
  138.                 if(b.values[i] == a.values[x]) {
  139.                     x = i;
  140.                     break;
  141.                 }
  142.             }
  143.             if(x == e)
  144.                 break;
  145.         }
  146.        
  147.         if(c.compare(a) || c.compare(b)) return null;
  148.        
  149.         return c;
  150.     }
  151.    
  152.     public static void main(String[] args) throws IOException, InterruptedException {
  153.         List<Solution> population = new ArrayList<>();
  154.        
  155.         population.add(new Solution(randomDistribuction(100)));
  156.        
  157.         Scanner s = new Scanner(System.in);
  158.        
  159.         int generation = 1;
  160.        
  161.         while(true) {
  162.             System.out.println("Geração: " + generation);
  163.             System.out.println("População: " + population.size());
  164.             System.out.println("Best Fitness: " + bestSolution(population).fitness());
  165.             System.out.println("Best Solution: " + Arrays.toString(bestSolution(population).values));
  166.             System.out.println();
  167.            
  168.             // Population
  169.             if(population.size() > 10)
  170.                 population = filter(population);
  171.            
  172.             // Mutation
  173.             int mutations = random.nextInt(10) + 1;
  174.             for(int i=0; i<mutations; i++)
  175.                 population.add(mutation(selection(population)));
  176.            
  177.             // Crosss
  178.             int cross = random.nextInt(10) + 1;
  179.             for(int i=0; i<cross; i++) {
  180.                 Solution newMember = cross(population);
  181.                 if(newMember == null) continue;
  182.                 population.add(newMember);
  183.             }
  184.             //
  185.             if(bestSolution(population).fitness() > 1) break;
  186.            
  187.            
  188.            
  189.             generation += 1;
  190.         }
  191.         //
  192.         System.out.println("Best Solution: " + Arrays.toString(bestSolution(population).values));
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement